blob: 3872de5cf2bc7680a4005a0677225335c5ea58f2 [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
847
848// --- FakeInputReaderContext ---
849
850class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700851 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 sp<InputReaderPolicyInterface> mPolicy;
853 sp<InputListenerInterface> mListener;
854 int32_t mGlobalMetaState;
855 bool mUpdateGlobalMetaStateWasCalled;
856 int32_t mGeneration;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800857 int32_t mNextId;
Michael Wright17db18e2020-06-26 20:51:44 +0100858 std::weak_ptr<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700861 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
862 const sp<InputReaderPolicyInterface>& policy,
863 const sp<InputListenerInterface>& listener)
864 : mEventHub(eventHub),
865 mPolicy(policy),
866 mListener(listener),
867 mGlobalMetaState(0),
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800868 mNextId(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 virtual ~FakeInputReaderContext() { }
871
872 void assertUpdateGlobalMetaStateWasCalled() {
873 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
874 << "Expected updateGlobalMetaState() to have been called.";
875 mUpdateGlobalMetaStateWasCalled = false;
876 }
877
878 void setGlobalMetaState(int32_t state) {
879 mGlobalMetaState = state;
880 }
881
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800882 uint32_t getGeneration() {
883 return mGeneration;
884 }
885
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800886 void updatePointerDisplay() {
Michael Wright17db18e2020-06-26 20:51:44 +0100887 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800888 if (controller != nullptr) {
889 InputReaderConfiguration config;
890 mPolicy->getReaderConfiguration(&config);
891 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
892 if (viewport) {
893 controller->setDisplayViewport(*viewport);
894 }
895 }
896 }
897
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898private:
899 virtual void updateGlobalMetaState() {
900 mUpdateGlobalMetaStateWasCalled = true;
901 }
902
903 virtual int32_t getGlobalMetaState() {
904 return mGlobalMetaState;
905 }
906
907 virtual EventHubInterface* getEventHub() {
908 return mEventHub.get();
909 }
910
911 virtual InputReaderPolicyInterface* getPolicy() {
912 return mPolicy.get();
913 }
914
915 virtual InputListenerInterface* getListener() {
916 return mListener.get();
917 }
918
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100919 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 }
921
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800922 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923
Michael Wright17db18e2020-06-26 20:51:44 +0100924 virtual std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) {
925 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800926 if (controller == nullptr) {
927 controller = mPolicy->obtainPointerController(deviceId);
928 mPointerController = controller;
929 updatePointerDisplay();
930 }
931 return controller;
932 }
933
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 virtual void fadePointer() {
935 }
936
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100937 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 }
939
940 virtual int32_t bumpGeneration() {
941 return ++mGeneration;
942 }
Michael Wright842500e2015-03-13 17:32:02 -0700943
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800944 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700945
946 }
947
948 virtual void dispatchExternalStylusState(const StylusState&) {
949
950 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800951
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800952 virtual int32_t getNextId() { return mNextId++; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953};
954
955
956// --- FakeInputMapper ---
957
958class FakeInputMapper : public InputMapper {
959 uint32_t mSources;
960 int32_t mKeyboardType;
961 int32_t mMetaState;
962 KeyedVector<int32_t, int32_t> mKeyCodeStates;
963 KeyedVector<int32_t, int32_t> mScanCodeStates;
964 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800965 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700967 std::mutex mLock;
968 std::condition_variable mStateChangedCondition;
969 bool mConfigureWasCalled GUARDED_BY(mLock);
970 bool mResetWasCalled GUARDED_BY(mLock);
971 bool mProcessWasCalled GUARDED_BY(mLock);
972 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973
Arthur Hungc23540e2018-11-29 20:42:11 +0800974 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800976 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
977 : InputMapper(deviceContext),
978 mSources(sources),
979 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800981 mConfigureWasCalled(false),
982 mResetWasCalled(false),
983 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984
985 virtual ~FakeInputMapper() { }
986
987 void setKeyboardType(int32_t keyboardType) {
988 mKeyboardType = keyboardType;
989 }
990
991 void setMetaState(int32_t metaState) {
992 mMetaState = metaState;
993 }
994
995 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 std::unique_lock<std::mutex> lock(mLock);
997 base::ScopedLockAssertion assumeLocked(mLock);
998 const bool configureCalled =
999 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1000 return mConfigureWasCalled;
1001 });
1002 if (!configureCalled) {
1003 FAIL() << "Expected configure() to have been called.";
1004 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 mConfigureWasCalled = false;
1006 }
1007
1008 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001009 std::unique_lock<std::mutex> lock(mLock);
1010 base::ScopedLockAssertion assumeLocked(mLock);
1011 const bool resetCalled =
1012 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1013 return mResetWasCalled;
1014 });
1015 if (!resetCalled) {
1016 FAIL() << "Expected reset() to have been called.";
1017 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 mResetWasCalled = false;
1019 }
1020
Yi Kong9b14ac62018-07-17 13:48:38 -07001021 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001022 std::unique_lock<std::mutex> lock(mLock);
1023 base::ScopedLockAssertion assumeLocked(mLock);
1024 const bool processCalled =
1025 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1026 return mProcessWasCalled;
1027 });
1028 if (!processCalled) {
1029 FAIL() << "Expected process() to have been called.";
1030 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 if (outLastEvent) {
1032 *outLastEvent = mLastEvent;
1033 }
1034 mProcessWasCalled = false;
1035 }
1036
1037 void setKeyCodeState(int32_t keyCode, int32_t state) {
1038 mKeyCodeStates.replaceValueFor(keyCode, state);
1039 }
1040
1041 void setScanCodeState(int32_t scanCode, int32_t state) {
1042 mScanCodeStates.replaceValueFor(scanCode, state);
1043 }
1044
1045 void setSwitchState(int32_t switchCode, int32_t state) {
1046 mSwitchStates.replaceValueFor(switchCode, state);
1047 }
1048
1049 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001050 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051 }
1052
1053private:
1054 virtual uint32_t getSources() {
1055 return mSources;
1056 }
1057
1058 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1059 InputMapper::populateDeviceInfo(deviceInfo);
1060
1061 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1062 deviceInfo->setKeyboardType(mKeyboardType);
1063 }
1064 }
1065
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001067 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001069
1070 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001071 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001072 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1073 mViewport = config->getDisplayViewportByPort(*displayPort);
1074 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001075
1076 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 }
1078
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001079 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001080 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001082 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083 }
1084
1085 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001086 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087 mLastEvent = *rawEvent;
1088 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001089 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 }
1091
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001092 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1094 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1095 }
1096
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001097 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1099 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1100 }
1101
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001102 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1104 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1105 }
1106
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001107 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 const int32_t* keyCodes, uint8_t* outFlags) {
1109 bool result = false;
1110 for (size_t i = 0; i < numCodes; i++) {
1111 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1112 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1113 outFlags[i] = 1;
1114 result = true;
1115 }
1116 }
1117 }
1118 return result;
1119 }
1120
1121 virtual int32_t getMetaState() {
1122 return mMetaState;
1123 }
1124
1125 virtual void fadePointer() {
1126 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001127
1128 virtual std::optional<int32_t> getAssociatedDisplay() {
1129 if (mViewport) {
1130 return std::make_optional(mViewport->displayId);
1131 }
1132 return std::nullopt;
1133 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134};
1135
1136
1137// --- InstrumentedInputReader ---
1138
1139class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001140 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141
1142public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001143 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1144 const sp<InputReaderPolicyInterface>& policy,
1145 const sp<InputListenerInterface>& listener)
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001146 : InputReader(eventHub, policy, listener) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001148 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001150 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001152 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001153 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154 InputDeviceIdentifier identifier;
1155 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001156 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001158 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 }
1160
Prabir Pradhan28efc192019-11-05 01:10:04 +00001161 // Make the protected loopOnce method accessible to tests.
1162 using InputReader::loopOnce;
1163
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001165 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1166 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001167 if (!mNextDevices.empty()) {
1168 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1169 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170 return device;
1171 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001172 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 }
1174
1175 friend class InputReaderTest;
1176};
1177
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001178// --- InputReaderPolicyTest ---
1179class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001180protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181 sp<FakeInputReaderPolicy> mFakePolicy;
1182
Prabir Pradhan28efc192019-11-05 01:10:04 +00001183 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1184 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185};
1186
1187/**
1188 * Check that empty set of viewports is an acceptable configuration.
1189 * Also try to get internal viewport two different ways - by type and by uniqueId.
1190 *
1191 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1192 * Such configuration is not currently allowed.
1193 */
1194TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001195 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001196
1197 // We didn't add any viewports yet, so there shouldn't be any.
1198 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001199 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001200 ASSERT_FALSE(internalViewport);
1201
1202 // Add an internal viewport, then clear it
1203 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001204 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1205 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001206
1207 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001208 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001210 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001211
1212 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001213 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001215 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001216
1217 mFakePolicy->clearViewports();
1218 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001219 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001220 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001221 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222 ASSERT_FALSE(internalViewport);
1223}
1224
1225TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1226 const std::string internalUniqueId = "local:0";
1227 const std::string externalUniqueId = "local:1";
1228 const std::string virtualUniqueId1 = "virtual:2";
1229 const std::string virtualUniqueId2 = "virtual:3";
1230 constexpr int32_t virtualDisplayId1 = 2;
1231 constexpr int32_t virtualDisplayId2 = 3;
1232
1233 // Add an internal viewport
1234 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001235 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1236 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 // Add an external viewport
1238 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001239 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1240 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001241 // Add an virtual viewport
1242 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001243 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1244 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 // Add another virtual viewport
1246 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001247 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1248 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001249
1250 // Check matching by type for internal
1251 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001252 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001253 ASSERT_TRUE(internalViewport);
1254 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1255
1256 // Check matching by type for external
1257 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001258 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001259 ASSERT_TRUE(externalViewport);
1260 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1261
1262 // Check matching by uniqueId for virtual viewport #1
1263 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001264 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001265 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001266 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1268 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1269
1270 // Check matching by uniqueId for virtual viewport #2
1271 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001272 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001273 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001274 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1276 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1277}
1278
1279
1280/**
1281 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1282 * that lookup works by checking display id.
1283 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1284 */
1285TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1286 const std::string uniqueId1 = "uniqueId1";
1287 const std::string uniqueId2 = "uniqueId2";
1288 constexpr int32_t displayId1 = 2;
1289 constexpr int32_t displayId2 = 3;
1290
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001291 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1292 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001293 for (const ViewportType& type : types) {
1294 mFakePolicy->clearViewports();
1295 // Add a viewport
1296 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001297 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298 // Add another viewport
1299 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001300 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001301
1302 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001303 std::optional<DisplayViewport> viewport1 =
1304 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001305 ASSERT_TRUE(viewport1);
1306 ASSERT_EQ(displayId1, viewport1->displayId);
1307 ASSERT_EQ(type, viewport1->type);
1308
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001309 std::optional<DisplayViewport> viewport2 =
1310 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001311 ASSERT_TRUE(viewport2);
1312 ASSERT_EQ(displayId2, viewport2->displayId);
1313 ASSERT_EQ(type, viewport2->type);
1314
1315 // When there are multiple viewports of the same kind, and uniqueId is not specified
1316 // in the call to getDisplayViewport, then that situation is not supported.
1317 // The viewports can be stored in any order, so we cannot rely on the order, since that
1318 // is just implementation detail.
1319 // However, we can check that it still returns *a* viewport, we just cannot assert
1320 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001321 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001322 ASSERT_TRUE(someViewport);
1323 }
1324}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001326/**
1327 * Check getDisplayViewportByPort
1328 */
1329TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001330 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001331 const std::string uniqueId1 = "uniqueId1";
1332 const std::string uniqueId2 = "uniqueId2";
1333 constexpr int32_t displayId1 = 1;
1334 constexpr int32_t displayId2 = 2;
1335 const uint8_t hdmi1 = 0;
1336 const uint8_t hdmi2 = 1;
1337 const uint8_t hdmi3 = 2;
1338
1339 mFakePolicy->clearViewports();
1340 // Add a viewport that's associated with some display port that's not of interest.
1341 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1342 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1343 // Add another viewport, connected to HDMI1 port
1344 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1345 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1346
1347 // Check that correct display viewport was returned by comparing the display ports.
1348 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1349 ASSERT_TRUE(hdmi1Viewport);
1350 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1351 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1352
1353 // Check that we can still get the same viewport using the uniqueId
1354 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1355 ASSERT_TRUE(hdmi1Viewport);
1356 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1357 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1358 ASSERT_EQ(type, hdmi1Viewport->type);
1359
1360 // Check that we cannot find a port with "HDMI2", because we never added one
1361 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1362 ASSERT_FALSE(hdmi2Viewport);
1363}
1364
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365// --- InputReaderTest ---
1366
1367class InputReaderTest : public testing::Test {
1368protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001369 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001371 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001372 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373
Prabir Pradhan28efc192019-11-05 01:10:04 +00001374 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001375 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001376 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001377 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378
Prabir Pradhan28efc192019-11-05 01:10:04 +00001379 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1380 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 }
1382
Prabir Pradhan28efc192019-11-05 01:10:04 +00001383 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 mFakeListener.clear();
1385 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 }
1387
Chris Ye1b0c7342020-07-28 21:57:03 -07001388 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001389 const PropertyMap* configuration) {
1390 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391
1392 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001393 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394 }
1395 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001396 mReader->loopOnce();
1397 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001398 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1399 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001400 }
1401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001402 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001403 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001404 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001405 }
1406
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001407 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001408 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001409 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001410 }
1411
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001412 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001413 const std::string& name,
1414 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001415 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001416 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1417 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001418 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001419 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001420 return mapper;
1421 }
1422};
1423
1424TEST_F(InputReaderTest, GetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001425 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1426 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1427 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001428
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001429 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001430 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001431 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001432 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001433 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001434 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1435 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1436 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1437
1438 // Should also have received a notification describing the new input devices.
1439 inputDevices = mFakePolicy->getInputDevices();
1440 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001441 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001442 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1444 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1445 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1446}
1447
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001448TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001449 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001450 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001451 constexpr int32_t eventHubId = 1;
1452 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001453 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001454 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001455 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001456 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001457
Yi Kong9b14ac62018-07-17 13:48:38 -07001458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001459
1460 NotifyDeviceResetArgs resetArgs;
1461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001462 ASSERT_EQ(deviceId, resetArgs.deviceId);
1463
1464 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001465 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001466 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001467
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001469 ASSERT_EQ(deviceId, resetArgs.deviceId);
1470 ASSERT_EQ(device->isEnabled(), false);
1471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001472 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001473 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001476 ASSERT_EQ(device->isEnabled(), false);
1477
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001478 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001479 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001481 ASSERT_EQ(deviceId, resetArgs.deviceId);
1482 ASSERT_EQ(device->isEnabled(), true);
1483}
1484
Michael Wrightd02c5b62014-02-10 15:10:22 -08001485TEST_F(InputReaderTest, GetKeyCodeState_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.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001493
1494 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1495 AINPUT_SOURCE_ANY, AKEYCODE_A))
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->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
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->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1505 AKEYCODE_A))
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->getKeyCodeState(-1,
1510 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
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->getKeyCodeState(-1,
1514 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
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, GetScanCodeState_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);
1525 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526
1527 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1528 AINPUT_SOURCE_ANY, KEY_A))
1529 << "Should return unknown when the device id is >= 0 but unknown.";
1530
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001531 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1532 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1533 << "Should return unknown when the device id is valid but the sources are not "
1534 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001536 ASSERT_EQ(AKEY_STATE_DOWN,
1537 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1538 KEY_A))
1539 << "Should return value provided by mapper when device id is valid and the device "
1540 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541
1542 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1543 AINPUT_SOURCE_TRACKBALL, KEY_A))
1544 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1545
1546 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1547 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1548 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1549}
1550
1551TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001553 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001554 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001555 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001556 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001557 AINPUT_SOURCE_KEYBOARD, nullptr);
1558 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559
1560 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1561 AINPUT_SOURCE_ANY, SW_LID))
1562 << "Should return unknown when the device id is >= 0 but unknown.";
1563
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001564 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1565 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1566 << "Should return unknown when the device id is valid but the sources are not "
1567 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001568
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001569 ASSERT_EQ(AKEY_STATE_DOWN,
1570 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1571 SW_LID))
1572 << "Should return value provided by mapper when device id is valid and the device "
1573 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574
1575 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1576 AINPUT_SOURCE_TRACKBALL, SW_LID))
1577 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1578
1579 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1580 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1581 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1582}
1583
1584TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001585 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001586 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001588 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001589 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001590 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001592 mapper.addSupportedKeyCode(AKEYCODE_A);
1593 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594
1595 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1596 uint8_t flags[4] = { 0, 0, 0, 1 };
1597
1598 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1599 << "Should return false when device id is >= 0 but unknown.";
1600 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1601
1602 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001603 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1604 << "Should return false when device id is valid but the sources are not supported by "
1605 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1607
1608 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001609 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1610 keyCodes, flags))
1611 << "Should return value provided by mapper when device id is valid and the device "
1612 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1614
1615 flags[3] = 1;
1616 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1617 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1618 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1619
1620 flags[3] = 1;
1621 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1622 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1623 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1624}
1625
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001626TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001628 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629
1630 NotifyConfigurationChangedArgs args;
1631
1632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1633 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1634}
1635
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001636TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001638 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001640 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001642 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001645 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1647
1648 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001649 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001651 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652 ASSERT_EQ(EV_KEY, event.type);
1653 ASSERT_EQ(KEY_A, event.code);
1654 ASSERT_EQ(1, event.value);
1655}
1656
Garfield Tan1c7bc862020-01-28 13:24:04 -08001657TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001659 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 constexpr int32_t eventHubId = 1;
1661 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001662 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001664 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001665 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001666
1667 NotifyDeviceResetArgs resetArgs;
1668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001669 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001670
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001671 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001672 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001674 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001675 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001676
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001677 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001678 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001680 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001681 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001682
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001683 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001684 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001686 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001687 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001688}
1689
Garfield Tan1c7bc862020-01-28 13:24:04 -08001690TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1691 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001692 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001693 constexpr int32_t eventHubId = 1;
1694 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1695 // Must add at least one mapper or the device will be ignored!
1696 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001697 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001698 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1699
1700 NotifyDeviceResetArgs resetArgs;
1701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1702 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1703}
1704
Arthur Hungc23540e2018-11-29 20:42:11 +08001705TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001707 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001709 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1711 FakeInputMapper& mapper =
1712 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001713 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001714
1715 const uint8_t hdmi1 = 1;
1716
1717 // Associated touch screen with second display.
1718 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1719
1720 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001721 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001722 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001723 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1724 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001725 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001726 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1727 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001728 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001729 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001730
1731 // Add the device, and make sure all of the callbacks are triggered.
1732 // The device is added after the input port associations are processed since
1733 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001734 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001737 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001738
Arthur Hung2c9a3342019-07-23 14:18:59 +08001739 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001740 ASSERT_EQ(deviceId, device->getId());
1741 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1742 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001743
1744 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001746 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001747 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001748}
1749
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001750TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1751 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1752 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1753 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1754 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1755 // Must add at least one mapper or the device will be ignored!
1756 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1757 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1758 mReader->pushNextDevice(device);
1759 mReader->pushNextDevice(device);
1760 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1761 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1762
1763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1764
1765 NotifyDeviceResetArgs resetArgs;
1766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1767 ASSERT_EQ(deviceId, resetArgs.deviceId);
1768 ASSERT_TRUE(device->isEnabled());
1769 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1770 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1771
1772 disableDevice(deviceId);
1773 mReader->loopOnce();
1774
1775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1776 ASSERT_EQ(deviceId, resetArgs.deviceId);
1777 ASSERT_FALSE(device->isEnabled());
1778 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1779 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1780
1781 enableDevice(deviceId);
1782 mReader->loopOnce();
1783
1784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1785 ASSERT_EQ(deviceId, resetArgs.deviceId);
1786 ASSERT_TRUE(device->isEnabled());
1787 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1788 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1789}
1790
1791TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1792 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1793 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1794 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1795 // Add two subdevices to device
1796 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1797 FakeInputMapper& mapperDevice1 =
1798 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1799 FakeInputMapper& mapperDevice2 =
1800 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1801 mReader->pushNextDevice(device);
1802 mReader->pushNextDevice(device);
1803 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1804 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1805
1806 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1807 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1808
1809 ASSERT_EQ(AKEY_STATE_DOWN,
1810 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1811 ASSERT_EQ(AKEY_STATE_DOWN,
1812 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1813 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1814 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1815}
1816
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001817// --- InputReaderIntegrationTest ---
1818
1819// These tests create and interact with the InputReader only through its interface.
1820// The InputReader is started during SetUp(), which starts its processing in its own
1821// thread. The tests use linux uinput to emulate input devices.
1822// NOTE: Interacting with the physical device while these tests are running may cause
1823// the tests to fail.
1824class InputReaderIntegrationTest : public testing::Test {
1825protected:
1826 sp<TestInputListener> mTestListener;
1827 sp<FakeInputReaderPolicy> mFakePolicy;
1828 sp<InputReaderInterface> mReader;
1829
1830 virtual void SetUp() override {
1831 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001832 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1833 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001834
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001835 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001836 ASSERT_EQ(mReader->start(), OK);
1837
1838 // Since this test is run on a real device, all the input devices connected
1839 // to the test device will show up in mReader. We wait for those input devices to
1840 // show up before beginning the tests.
1841 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1842 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1843 }
1844
1845 virtual void TearDown() override {
1846 ASSERT_EQ(mReader->stop(), OK);
1847 mTestListener.clear();
1848 mFakePolicy.clear();
1849 }
1850};
1851
1852TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1853 // An invalid input device that is only used for this test.
1854 class InvalidUinputDevice : public UinputDevice {
1855 public:
1856 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1857
1858 private:
1859 void configureDevice(int fd, uinput_user_dev* device) override {}
1860 };
1861
1862 const size_t numDevices = mFakePolicy->getInputDevices().size();
1863
1864 // UinputDevice does not set any event or key bits, so InputReader should not
1865 // consider it as a valid device.
1866 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1867 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1868 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1869 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1870
1871 invalidDevice.reset();
1872 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1873 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1874 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1875}
1876
1877TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1878 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1879
1880 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1881 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1882 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1883 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1884
1885 // Find the test device by its name.
1886 std::vector<InputDeviceInfo> inputDevices;
1887 mReader->getInputDevices(inputDevices);
1888 InputDeviceInfo* keyboardInfo = nullptr;
1889 const char* keyboardName = keyboard->getName();
1890 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1891 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1892 keyboardInfo = &inputDevices[i];
1893 break;
1894 }
1895 }
1896 ASSERT_NE(keyboardInfo, nullptr);
1897 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1898 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1899 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1900
1901 keyboard.reset();
1902 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1903 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1904 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1905}
1906
1907TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1908 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1909 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1910
1911 NotifyConfigurationChangedArgs configChangedArgs;
1912 ASSERT_NO_FATAL_FAILURE(
1913 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001914 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001915 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1916
1917 NotifyKeyArgs keyArgs;
1918 keyboard->pressAndReleaseHomeKey();
1919 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1920 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001921 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001922 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001923 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1924 prevTimestamp = keyArgs.eventTime;
1925
1926 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1927 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001928 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001929 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1930}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001932/**
1933 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1934 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1935 * are passed to the listener.
1936 */
1937static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1938TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1939 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1940 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1941 NotifyKeyArgs keyArgs;
1942
1943 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1944 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1945 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1946 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1947
1948 controller->pressAndReleaseKey(BTN_GEAR_UP);
1949 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1950 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1951 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1952}
1953
Arthur Hungaab25622020-01-16 11:22:11 +08001954// --- TouchProcessTest ---
1955class TouchIntegrationTest : public InputReaderIntegrationTest {
1956protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001957 const std::string UNIQUE_ID = "local:0";
1958
1959 virtual void SetUp() override {
1960 InputReaderIntegrationTest::SetUp();
1961 // At least add an internal display.
1962 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1963 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001964 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001965
1966 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1967 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1968 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1969 }
1970
1971 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1972 int32_t orientation, const std::string& uniqueId,
1973 std::optional<uint8_t> physicalPort,
1974 ViewportType viewportType) {
1975 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1976 physicalPort, viewportType);
1977 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1978 }
1979
1980 std::unique_ptr<UinputTouchScreen> mDevice;
1981};
1982
1983TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1984 NotifyMotionArgs args;
1985 const Point centerPoint = mDevice->getCenterPoint();
1986
1987 // ACTION_DOWN
1988 mDevice->sendDown(centerPoint);
1989 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1990 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1991
1992 // ACTION_MOVE
1993 mDevice->sendMove(centerPoint + Point(1, 1));
1994 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1996
1997 // ACTION_UP
1998 mDevice->sendUp();
1999 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2000 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2001}
2002
2003TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2004 NotifyMotionArgs args;
2005 const Point centerPoint = mDevice->getCenterPoint();
2006
2007 // ACTION_DOWN
2008 mDevice->sendDown(centerPoint);
2009 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2010 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2011
2012 // ACTION_POINTER_DOWN (Second slot)
2013 const Point secondPoint = centerPoint + Point(100, 100);
2014 mDevice->sendSlot(SECOND_SLOT);
2015 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2016 mDevice->sendDown(secondPoint + Point(1, 1));
2017 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2018 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2019 args.action);
2020
2021 // ACTION_MOVE (Second slot)
2022 mDevice->sendMove(secondPoint);
2023 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2025
2026 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002027 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002028 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002029 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002030 args.action);
2031
2032 // ACTION_UP
2033 mDevice->sendSlot(FIRST_SLOT);
2034 mDevice->sendUp();
2035 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2036 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2037}
2038
2039TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2040 NotifyMotionArgs args;
2041 const Point centerPoint = mDevice->getCenterPoint();
2042
2043 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002044 mDevice->sendSlot(FIRST_SLOT);
2045 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002046 mDevice->sendDown(centerPoint);
2047 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2048 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2049
arthurhungcc7f9802020-04-30 17:55:40 +08002050 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002051 const Point secondPoint = centerPoint + Point(100, 100);
2052 mDevice->sendSlot(SECOND_SLOT);
2053 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2054 mDevice->sendDown(secondPoint);
2055 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2056 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2057 args.action);
2058
arthurhungcc7f9802020-04-30 17:55:40 +08002059 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002060 mDevice->sendMove(secondPoint + Point(1, 1));
2061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2063
arthurhungcc7f9802020-04-30 17:55:40 +08002064 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2065 // a palm event.
2066 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002067 mDevice->sendToolType(MT_TOOL_PALM);
2068 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002069 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2070 args.action);
2071 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002072
arthurhungcc7f9802020-04-30 17:55:40 +08002073 // Send up to second slot, expect first slot send moving.
2074 mDevice->sendPointerUp();
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002077
arthurhungcc7f9802020-04-30 17:55:40 +08002078 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002079 mDevice->sendSlot(FIRST_SLOT);
2080 mDevice->sendUp();
2081
arthurhungcc7f9802020-04-30 17:55:40 +08002082 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2083 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002084}
2085
Michael Wrightd02c5b62014-02-10 15:10:22 -08002086// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002087class InputDeviceTest : public testing::Test {
2088protected:
2089 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002090 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002091 static const int32_t DEVICE_ID;
2092 static const int32_t DEVICE_GENERATION;
2093 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002094 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002095 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002097 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002098 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002099 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100 FakeInputReaderContext* mFakeContext;
2101
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002102 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002103
Prabir Pradhan28efc192019-11-05 01:10:04 +00002104 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002105 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002106 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002107 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002108 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2109
Chris Ye1b0c7342020-07-28 21:57:03 -07002110 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002111 InputDeviceIdentifier identifier;
2112 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002113 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002114 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2115 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002116 }
2117
Prabir Pradhan28efc192019-11-05 01:10:04 +00002118 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002119 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002120 delete mFakeContext;
2121 mFakeListener.clear();
2122 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002123 }
2124};
2125
2126const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002127const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002128const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002129const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2130const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002131const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2132 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002133const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002134
2135TEST_F(InputDeviceTest, ImmutableProperties) {
2136 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002137 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002138 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002139}
2140
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002141TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2142 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002143}
2144
Michael Wrightd02c5b62014-02-10 15:10:22 -08002145TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2146 // Configuration.
2147 InputReaderConfiguration config;
2148 mDevice->configure(ARBITRARY_TIME, &config, 0);
2149
2150 // Reset.
2151 mDevice->reset(ARBITRARY_TIME);
2152
2153 NotifyDeviceResetArgs resetArgs;
2154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2155 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2156 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2157
2158 // Metadata.
2159 ASSERT_TRUE(mDevice->isIgnored());
2160 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2161
2162 InputDeviceInfo info;
2163 mDevice->getDeviceInfo(&info);
2164 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002165 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002166 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2167 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2168
2169 // State queries.
2170 ASSERT_EQ(0, mDevice->getMetaState());
2171
2172 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2173 << "Ignored device should return unknown key code state.";
2174 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2175 << "Ignored device should return unknown scan code state.";
2176 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2177 << "Ignored device should return unknown switch state.";
2178
2179 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2180 uint8_t flags[2] = { 0, 1 };
2181 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2182 << "Ignored device should never mark any key codes.";
2183 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2184 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2185}
2186
2187TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2188 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002189 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002190
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002191 FakeInputMapper& mapper1 =
2192 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002193 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2194 mapper1.setMetaState(AMETA_ALT_ON);
2195 mapper1.addSupportedKeyCode(AKEYCODE_A);
2196 mapper1.addSupportedKeyCode(AKEYCODE_B);
2197 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2198 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2199 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2200 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2201 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002202
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002203 FakeInputMapper& mapper2 =
2204 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002205 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002206
2207 InputReaderConfiguration config;
2208 mDevice->configure(ARBITRARY_TIME, &config, 0);
2209
2210 String8 propertyValue;
2211 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2212 << "Device should have read configuration during configuration phase.";
2213 ASSERT_STREQ("value", propertyValue.string());
2214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002215 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2216 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002217
2218 // Reset
2219 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002220 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2221 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002222
2223 NotifyDeviceResetArgs resetArgs;
2224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2225 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2226 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2227
2228 // Metadata.
2229 ASSERT_FALSE(mDevice->isIgnored());
2230 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2231
2232 InputDeviceInfo info;
2233 mDevice->getDeviceInfo(&info);
2234 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002235 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002236 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2237 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2238
2239 // State queries.
2240 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2241 << "Should query mappers and combine meta states.";
2242
2243 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2244 << "Should return unknown key code state when source not supported.";
2245 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2246 << "Should return unknown scan code state when source not supported.";
2247 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2248 << "Should return unknown switch state when source not supported.";
2249
2250 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2251 << "Should query mapper when source is supported.";
2252 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2253 << "Should query mapper when source is supported.";
2254 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2255 << "Should query mapper when source is supported.";
2256
2257 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2258 uint8_t flags[4] = { 0, 0, 0, 1 };
2259 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2260 << "Should do nothing when source is unsupported.";
2261 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2262 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2263 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2264 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2265
2266 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2267 << "Should query mapper when source is supported.";
2268 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2269 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2270 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2271 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2272
2273 // Event handling.
2274 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002275 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002276 mDevice->process(&event, 1);
2277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002278 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2279 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280}
2281
Arthur Hung2c9a3342019-07-23 14:18:59 +08002282// A single input device is associated with a specific display. Check that:
2283// 1. Device is disabled if the viewport corresponding to the associated display is not found
2284// 2. Device is disabled when setEnabled API is called
2285TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002286 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002287
2288 // First Configuration.
2289 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2290
2291 // Device should be enabled by default.
2292 ASSERT_TRUE(mDevice->isEnabled());
2293
2294 // Prepare associated info.
2295 constexpr uint8_t hdmi = 1;
2296 const std::string UNIQUE_ID = "local:1";
2297
2298 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2299 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2300 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2301 // Device should be disabled because it is associated with a specific display via
2302 // input port <-> display port association, but the corresponding display is not found
2303 ASSERT_FALSE(mDevice->isEnabled());
2304
2305 // Prepare displays.
2306 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002307 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002308 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2309 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2310 ASSERT_TRUE(mDevice->isEnabled());
2311
2312 // Device should be disabled after set disable.
2313 mFakePolicy->addDisabledDevice(mDevice->getId());
2314 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2315 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2316 ASSERT_FALSE(mDevice->isEnabled());
2317
2318 // Device should still be disabled even found the associated display.
2319 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2320 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2321 ASSERT_FALSE(mDevice->isEnabled());
2322}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323
2324// --- InputMapperTest ---
2325
2326class InputMapperTest : public testing::Test {
2327protected:
2328 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002329 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002330 static const int32_t DEVICE_ID;
2331 static const int32_t DEVICE_GENERATION;
2332 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002333 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002334 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002336 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002338 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002339 FakeInputReaderContext* mFakeContext;
2340 InputDevice* mDevice;
2341
Chris Ye1b0c7342020-07-28 21:57:03 -07002342 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002343 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002344 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002345 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2347 InputDeviceIdentifier identifier;
2348 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002349 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002350 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002351
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002352 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353 }
2354
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002355 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2356
Prabir Pradhan28efc192019-11-05 01:10:04 +00002357 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358 delete mDevice;
2359 delete mFakeContext;
2360 mFakeListener.clear();
2361 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002362 }
2363
2364 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002365 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002366 }
2367
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002368 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002369 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2370 mFakeContext->updatePointerDisplay();
2371 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002372 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2373 }
2374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002375 template <class T, typename... Args>
2376 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002377 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002378 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002380 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002381 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 }
2383
2384 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002385 int32_t orientation, const std::string& uniqueId,
2386 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002387 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002388 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002389 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2390 }
2391
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002392 void clearViewports() {
2393 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394 }
2395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002396 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2397 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398 RawEvent event;
2399 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002400 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 event.type = type;
2402 event.code = code;
2403 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002404 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 }
2406
2407 static void assertMotionRange(const InputDeviceInfo& info,
2408 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2409 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002410 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2412 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2413 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2414 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2415 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2416 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2417 }
2418
2419 static void assertPointerCoords(const PointerCoords& coords,
2420 float x, float y, float pressure, float size,
2421 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2422 float orientation, float distance) {
2423 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2424 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2425 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2426 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2427 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2428 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2429 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2430 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2431 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2432 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2433 }
2434
Michael Wright17db18e2020-06-26 20:51:44 +01002435 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002436 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002437 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438 ASSERT_NEAR(x, actualX, 1);
2439 ASSERT_NEAR(y, actualY, 1);
2440 }
2441};
2442
2443const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002444const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002445const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2447const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002448const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2449 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002450const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451
2452// --- SwitchInputMapperTest ---
2453
2454class SwitchInputMapperTest : public InputMapperTest {
2455protected:
2456};
2457
2458TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002459 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002461 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002462}
2463
2464TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002465 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002466
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002467 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002468 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002470 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002471 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472}
2473
2474TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002475 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002477 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2478 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2479 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2480 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481
2482 NotifySwitchArgs args;
2483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2484 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002485 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2486 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487 args.switchMask);
2488 ASSERT_EQ(uint32_t(0), args.policyFlags);
2489}
2490
2491
2492// --- KeyboardInputMapperTest ---
2493
2494class KeyboardInputMapperTest : public InputMapperTest {
2495protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002496 const std::string UNIQUE_ID = "local:0";
2497
2498 void prepareDisplay(int32_t orientation);
2499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002500 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002501 int32_t originalKeyCode, int32_t rotatedKeyCode,
2502 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002503};
2504
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002505/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2506 * orientation.
2507 */
2508void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002509 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2510 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002511}
2512
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002513void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002514 int32_t originalScanCode, int32_t originalKeyCode,
2515 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 NotifyKeyArgs args;
2517
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002518 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2520 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2521 ASSERT_EQ(originalScanCode, args.scanCode);
2522 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002523 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002525 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2527 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2528 ASSERT_EQ(originalScanCode, args.scanCode);
2529 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002530 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531}
2532
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002534 KeyboardInputMapper& mapper =
2535 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2536 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002538 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002539}
2540
2541TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2542 const int32_t USAGE_A = 0x070004;
2543 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2545 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002547 KeyboardInputMapper& mapper =
2548 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2549 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002550
2551 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002552 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553 NotifyKeyArgs args;
2554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2555 ASSERT_EQ(DEVICE_ID, args.deviceId);
2556 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2557 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2558 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2559 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2560 ASSERT_EQ(KEY_HOME, args.scanCode);
2561 ASSERT_EQ(AMETA_NONE, args.metaState);
2562 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2563 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2564 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2565
2566 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002567 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2569 ASSERT_EQ(DEVICE_ID, args.deviceId);
2570 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2571 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2572 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2573 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2574 ASSERT_EQ(KEY_HOME, args.scanCode);
2575 ASSERT_EQ(AMETA_NONE, args.metaState);
2576 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2577 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2578 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2579
2580 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002581 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2582 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2584 ASSERT_EQ(DEVICE_ID, args.deviceId);
2585 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2586 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2587 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2588 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2589 ASSERT_EQ(0, args.scanCode);
2590 ASSERT_EQ(AMETA_NONE, args.metaState);
2591 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2592 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2593 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2594
2595 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002596 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2597 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2599 ASSERT_EQ(DEVICE_ID, args.deviceId);
2600 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2601 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2602 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2603 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2604 ASSERT_EQ(0, args.scanCode);
2605 ASSERT_EQ(AMETA_NONE, args.metaState);
2606 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2607 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2608 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2609
2610 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002611 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2612 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2614 ASSERT_EQ(DEVICE_ID, args.deviceId);
2615 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2616 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2617 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2618 ASSERT_EQ(0, args.keyCode);
2619 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2620 ASSERT_EQ(AMETA_NONE, args.metaState);
2621 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2622 ASSERT_EQ(0U, args.policyFlags);
2623 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2624
2625 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002626 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2627 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2629 ASSERT_EQ(DEVICE_ID, args.deviceId);
2630 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2631 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2632 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2633 ASSERT_EQ(0, args.keyCode);
2634 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2635 ASSERT_EQ(AMETA_NONE, args.metaState);
2636 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2637 ASSERT_EQ(0U, args.policyFlags);
2638 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2639}
2640
2641TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002642 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2643 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002645 KeyboardInputMapper& mapper =
2646 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2647 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648
2649 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002650 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
2652 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002653 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 NotifyKeyArgs args;
2655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2656 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002657 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2659
2660 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002661 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2663 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002664 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
2666 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002667 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2669 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002670 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671
2672 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002673 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2675 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002676 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2678}
2679
2680TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002681 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2682 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2683 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2684 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002686 KeyboardInputMapper& mapper =
2687 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2688 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002690 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2692 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2693 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2694 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2695 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2696 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2697 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2698 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2699}
2700
2701TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2705 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002708 KeyboardInputMapper& mapper =
2709 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2710 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002712 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002713 ASSERT_NO_FATAL_FAILURE(
2714 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2715 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2716 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2717 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2718 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2719 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2720 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002722 clearViewports();
2723 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002724 ASSERT_NO_FATAL_FAILURE(
2725 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2726 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2727 AKEYCODE_DPAD_UP, DISPLAY_ID));
2728 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2729 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2730 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2731 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002733 clearViewports();
2734 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002735 ASSERT_NO_FATAL_FAILURE(
2736 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2737 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2738 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2739 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2740 AKEYCODE_DPAD_UP, DISPLAY_ID));
2741 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2742 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002744 clearViewports();
2745 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002746 ASSERT_NO_FATAL_FAILURE(
2747 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2748 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2749 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2750 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2751 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2752 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2753 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754
2755 // Special case: if orientation changes while key is down, we still emit the same keycode
2756 // in the key up as we did in the key down.
2757 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002758 clearViewports();
2759 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002760 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2762 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2763 ASSERT_EQ(KEY_UP, args.scanCode);
2764 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2765
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002766 clearViewports();
2767 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002768 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2770 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2771 ASSERT_EQ(KEY_UP, args.scanCode);
2772 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2773}
2774
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002775TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2776 // If the keyboard is not orientation aware,
2777 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002778 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002780 KeyboardInputMapper& mapper =
2781 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2782 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002783 NotifyKeyArgs args;
2784
2785 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002786 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002788 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2790 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2791
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002792 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002793 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002795 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2797 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2798}
2799
2800TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2801 // If the keyboard is orientation aware,
2802 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002803 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002804
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002805 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002806 KeyboardInputMapper& mapper =
2807 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2808 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002809 NotifyKeyArgs args;
2810
2811 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2812 // ^--- already checked by the previous test
2813
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002814 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002815 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002816 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002818 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2820 ASSERT_EQ(DISPLAY_ID, args.displayId);
2821
2822 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002823 clearViewports();
2824 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002825 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002826 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002828 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2830 ASSERT_EQ(newDisplayId, args.displayId);
2831}
2832
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002834 KeyboardInputMapper& mapper =
2835 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2836 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002838 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002839 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002841 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002842 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843}
2844
2845TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002846 KeyboardInputMapper& mapper =
2847 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2848 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002850 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002853 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002854 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855}
2856
2857TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002858 KeyboardInputMapper& mapper =
2859 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2860 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002862 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863
2864 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2865 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002866 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867 ASSERT_TRUE(flags[0]);
2868 ASSERT_FALSE(flags[1]);
2869}
2870
2871TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002872 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2873 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2874 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2875 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2876 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2877 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002879 KeyboardInputMapper& mapper =
2880 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2881 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882
2883 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002884 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2885 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2886 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887
2888 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002889 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2890 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002891 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2892 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2893 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002895
2896 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002897 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2898 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002899 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2900 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2901 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002902 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903
2904 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002905 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2906 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002907 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2908 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2909 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002910 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002911
2912 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002913 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2914 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002915 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2916 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2917 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002918 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919
2920 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002921 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2922 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002923 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2924 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2925 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002926 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
2928 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002929 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2930 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002931 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2932 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2933 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002934 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935}
2936
Arthur Hung2c9a3342019-07-23 14:18:59 +08002937TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2938 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002939 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2940 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2941 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2942 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002943
2944 // keyboard 2.
2945 const std::string USB2 = "USB2";
2946 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002947 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002948 InputDeviceIdentifier identifier;
2949 identifier.name = "KEYBOARD2";
2950 identifier.location = USB2;
2951 std::unique_ptr<InputDevice> device2 =
2952 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002953 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07002954 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME,
2955 Flags<InputDeviceClass>(0) /*classes*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002956 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2957 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2958 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2959 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002961 KeyboardInputMapper& mapper =
2962 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2963 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002965 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002966 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002967 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002968 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2969 device2->reset(ARBITRARY_TIME);
2970
2971 // Prepared displays and associated info.
2972 constexpr uint8_t hdmi1 = 0;
2973 constexpr uint8_t hdmi2 = 1;
2974 const std::string SECONDARY_UNIQUE_ID = "local:1";
2975
2976 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2977 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2978
2979 // No associated display viewport found, should disable the device.
2980 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2981 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2982 ASSERT_FALSE(device2->isEnabled());
2983
2984 // Prepare second display.
2985 constexpr int32_t newDisplayId = 2;
2986 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002987 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002988 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002989 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002990 // Default device will reconfigure above, need additional reconfiguration for another device.
2991 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2992 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2993
2994 // Device should be enabled after the associated display is found.
2995 ASSERT_TRUE(mDevice->isEnabled());
2996 ASSERT_TRUE(device2->isEnabled());
2997
2998 // Test pad key events
2999 ASSERT_NO_FATAL_FAILURE(
3000 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3001 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3002 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3003 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3004 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3005 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3006 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3007
3008 ASSERT_NO_FATAL_FAILURE(
3009 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3010 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3011 AKEYCODE_DPAD_RIGHT, newDisplayId));
3012 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3013 AKEYCODE_DPAD_DOWN, newDisplayId));
3014 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3015 AKEYCODE_DPAD_LEFT, newDisplayId));
3016}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003017
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003018// --- KeyboardInputMapperTest_ExternalDevice ---
3019
3020class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3021protected:
3022 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07003023 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003024 }
3025};
3026
3027TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003028 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3029 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003030
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3032 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3033 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3034 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003036 KeyboardInputMapper& mapper =
3037 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3038 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003039
3040 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3041 NotifyKeyArgs args;
3042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3043 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3044
3045 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3047 ASSERT_EQ(uint32_t(0), args.policyFlags);
3048
3049 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3051 ASSERT_EQ(uint32_t(0), args.policyFlags);
3052
3053 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3055 ASSERT_EQ(uint32_t(0), args.policyFlags);
3056
3057 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3059 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3060
3061 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3063 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3064}
3065
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003066TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003067 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003068
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003069 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3070 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3071 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003072
Powei Fengd041c5d2019-05-03 17:11:33 -07003073 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 KeyboardInputMapper& mapper =
3075 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3076 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003077
3078 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3079 NotifyKeyArgs args;
3080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3081 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3082
3083 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3085 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3086
3087 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3089 ASSERT_EQ(uint32_t(0), args.policyFlags);
3090
3091 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3093 ASSERT_EQ(uint32_t(0), args.policyFlags);
3094
3095 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3097 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3098
3099 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3101 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3102}
3103
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104// --- CursorInputMapperTest ---
3105
3106class CursorInputMapperTest : public InputMapperTest {
3107protected:
3108 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3109
Michael Wright17db18e2020-06-26 20:51:44 +01003110 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111
Prabir Pradhan28efc192019-11-05 01:10:04 +00003112 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 InputMapperTest::SetUp();
3114
Michael Wright17db18e2020-06-26 20:51:44 +01003115 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003116 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117 }
3118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003119 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3120 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003121
3122 void prepareDisplay(int32_t orientation) {
3123 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003124 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003125 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3126 orientation, uniqueId, NO_PORT, viewportType);
3127 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128};
3129
3130const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003132void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3133 int32_t originalY, int32_t rotatedX,
3134 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135 NotifyMotionArgs args;
3136
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003137 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3138 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3139 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3143 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3144 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3145 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3146}
3147
3148TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003150 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003152 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153}
3154
3155TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003157 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003159 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160}
3161
3162TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003164 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165
3166 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003167 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168
3169 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003170 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3171 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3173 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3174
3175 // When the bounds are set, then there should be a valid motion range.
3176 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3177
3178 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180
3181 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3182 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3183 1, 800 - 1, 0.0f, 0.0f));
3184 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3185 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3186 2, 480 - 1, 0.0f, 0.0f));
3187 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3188 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3189 0.0f, 1.0f, 0.0f, 0.0f));
3190}
3191
3192TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003194 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195
3196 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003197 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198
3199 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3200 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3201 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3202 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3203 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3204 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3205 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3206 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3207 0.0f, 1.0f, 0.0f, 0.0f));
3208}
3209
3210TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003212 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213
3214 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3215
3216 NotifyMotionArgs args;
3217
3218 // Button press.
3219 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003220 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3221 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3223 ASSERT_EQ(ARBITRARY_TIME, 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);
3227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3228 ASSERT_EQ(0, args.flags);
3229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3230 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, 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, 1.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
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3242 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3243 ASSERT_EQ(DEVICE_ID, args.deviceId);
3244 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3245 ASSERT_EQ(uint32_t(0), args.policyFlags);
3246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3247 ASSERT_EQ(0, args.flags);
3248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3249 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3250 ASSERT_EQ(0, args.edgeFlags);
3251 ASSERT_EQ(uint32_t(1), args.pointerCount);
3252 ASSERT_EQ(0, args.pointerProperties[0].id);
3253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3255 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3256 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3257 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3258 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3259
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003261 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3262 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3264 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3265 ASSERT_EQ(DEVICE_ID, args.deviceId);
3266 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3267 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3269 ASSERT_EQ(0, args.flags);
3270 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3271 ASSERT_EQ(0, args.buttonState);
3272 ASSERT_EQ(0, args.edgeFlags);
3273 ASSERT_EQ(uint32_t(1), args.pointerCount);
3274 ASSERT_EQ(0, args.pointerProperties[0].id);
3275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3277 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3278 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3279 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3280 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3281
3282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3283 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3284 ASSERT_EQ(DEVICE_ID, args.deviceId);
3285 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3286 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3288 ASSERT_EQ(0, args.flags);
3289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3290 ASSERT_EQ(0, args.buttonState);
3291 ASSERT_EQ(0, args.edgeFlags);
3292 ASSERT_EQ(uint32_t(1), args.pointerCount);
3293 ASSERT_EQ(0, args.pointerProperties[0].id);
3294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3296 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3297 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3298 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3299 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3300}
3301
3302TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305
3306 NotifyMotionArgs args;
3307
3308 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003309 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3310 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3314 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3315
3316 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003317 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3318 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3322 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3323}
3324
3325TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
3329 NotifyMotionArgs args;
3330
3331 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003332 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3333 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3337 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3338
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3340 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3342 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3343
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003345 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3346 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3350 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3351
3352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3355 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3356}
3357
3358TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361
3362 NotifyMotionArgs args;
3363
3364 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003365 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3366 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3367 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3368 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3370 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3372 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3373 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3374
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3378 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3379 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3380
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003382 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3383 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3384 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3388 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3389 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3390
3391 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003392 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3393 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3397 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3398
3399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3402 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3403}
3404
3405TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003407 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003409 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3411 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3412 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3413 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3414 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3415 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3416 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3417 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3418}
3419
3420TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 addConfigurationProperty("cursor.mode", "navigation");
3422 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003425 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3433 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3434
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003435 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3437 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3438 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3439 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3440 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3444
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003445 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3448 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3449 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3450 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3451 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3452 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3453 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3454
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003455 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3457 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3458 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3459 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3460 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3461 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3462 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3463 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3464}
3465
3466TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
3470 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3471 mFakePointerController->setPosition(100, 200);
3472 mFakePointerController->setButtonState(0);
3473
3474 NotifyMotionArgs motionArgs;
3475 NotifyKeyArgs keyArgs;
3476
3477 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003478 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3479 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3481 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3482 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3483 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, 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
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3489 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3490 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3492 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3493
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003494 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3495 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_EQ(0, motionArgs.buttonState);
3499 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3501 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3502
3503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003504 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505 ASSERT_EQ(0, motionArgs.buttonState);
3506 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3508 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3509
3510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003512 ASSERT_EQ(0, motionArgs.buttonState);
3513 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3515 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3516
3517 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003518 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3519 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3520 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3522 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3523 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3524 motionArgs.buttonState);
3525 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3526 mFakePointerController->getButtonState());
3527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3528 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3529
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3532 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3533 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3534 mFakePointerController->getButtonState());
3535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3536 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3537
3538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3540 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3541 motionArgs.buttonState);
3542 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3543 mFakePointerController->getButtonState());
3544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3545 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3546
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003547 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3548 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3552 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3554 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3555
3556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003558 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3559 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3561 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3562
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003563 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3564 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003566 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3567 ASSERT_EQ(0, motionArgs.buttonState);
3568 ASSERT_EQ(0, mFakePointerController->getButtonState());
3569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3570 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 -08003571 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3572 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003573
3574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575 ASSERT_EQ(0, motionArgs.buttonState);
3576 ASSERT_EQ(0, mFakePointerController->getButtonState());
3577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3579 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003580
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3582 ASSERT_EQ(0, motionArgs.buttonState);
3583 ASSERT_EQ(0, mFakePointerController->getButtonState());
3584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3586 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3587
3588 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003589 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3590 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3592 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3593 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003594
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3598 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3600 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3601
3602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3604 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3605 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3607 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3608
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003609 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3610 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 ASSERT_EQ(0, motionArgs.buttonState);
3614 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3616 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3617
3618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003620 ASSERT_EQ(0, motionArgs.buttonState);
3621 ASSERT_EQ(0, mFakePointerController->getButtonState());
3622
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3624 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3626 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3627 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3628
3629 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003630 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3631 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3633 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3634 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003635
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3639 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3641 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3642
3643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3645 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3646 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3648 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3649
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003650 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3651 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654 ASSERT_EQ(0, motionArgs.buttonState);
3655 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3657 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 -08003658
3659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3661 ASSERT_EQ(0, motionArgs.buttonState);
3662 ASSERT_EQ(0, mFakePointerController->getButtonState());
3663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3664 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3665
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3667 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3668 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3669
3670 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003671 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3672 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3674 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3675 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003676
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3680 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3682 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3683
3684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3686 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3687 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3689 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3690
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003691 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3692 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003694 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695 ASSERT_EQ(0, motionArgs.buttonState);
3696 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3698 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 -08003699
3700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3702 ASSERT_EQ(0, motionArgs.buttonState);
3703 ASSERT_EQ(0, mFakePointerController->getButtonState());
3704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3705 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3706
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3708 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3709 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3710
3711 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003712 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3713 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3715 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3716 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003717
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003719 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3721 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3723 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3724
3725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3726 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3727 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3728 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3730 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3731
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003732 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3733 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736 ASSERT_EQ(0, motionArgs.buttonState);
3737 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3739 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 -08003740
3741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3742 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3743 ASSERT_EQ(0, motionArgs.buttonState);
3744 ASSERT_EQ(0, mFakePointerController->getButtonState());
3745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3746 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3747
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3749 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3750 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3751}
3752
3753TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003755 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
3757 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3758 mFakePointerController->setPosition(100, 200);
3759 mFakePointerController->setButtonState(0);
3760
3761 NotifyMotionArgs args;
3762
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003763 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3764 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3765 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003767 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3770 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 +01003771 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003772}
3773
3774TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003775 addConfigurationProperty("cursor.mode", "pointer");
3776 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003778
3779 NotifyDeviceResetArgs resetArgs;
3780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3781 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3782 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3783
3784 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3785 mFakePointerController->setPosition(100, 200);
3786 mFakePointerController->setButtonState(0);
3787
3788 NotifyMotionArgs args;
3789
3790 // Move.
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_RELATIVE, args.source);
3796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3798 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 +01003799 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003800
3801 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003802 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3803 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3805 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3806 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3808 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3810 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3813 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3814
3815 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003816 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3817 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3819 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3822 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3824 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3825 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3827 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3828
3829 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003830 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3831 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3832 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3834 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3837 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 +01003838 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003839
3840 // Disable pointer capture and check that the device generation got bumped
3841 // and events are generated the usual way.
3842 const uint32_t generation = mFakeContext->getGeneration();
3843 mFakePolicy->setPointerCapture(false);
3844 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3845 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3846
3847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3848 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3849 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3850
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003851 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3852 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3853 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3855 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3858 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 +01003859 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860}
3861
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003862TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003864
Garfield Tan888a6a42020-01-09 11:39:16 -08003865 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003866 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003867 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3868 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003869 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003870 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3871 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3872
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003873 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3874 mFakePointerController->setPosition(100, 200);
3875 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003876
3877 NotifyMotionArgs args;
3878 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3879 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3880 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3882 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3883 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3885 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 +01003886 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003887 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3888}
3889
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890// --- TouchInputMapperTest ---
3891
3892class TouchInputMapperTest : public InputMapperTest {
3893protected:
3894 static const int32_t RAW_X_MIN;
3895 static const int32_t RAW_X_MAX;
3896 static const int32_t RAW_Y_MIN;
3897 static const int32_t RAW_Y_MAX;
3898 static const int32_t RAW_TOUCH_MIN;
3899 static const int32_t RAW_TOUCH_MAX;
3900 static const int32_t RAW_TOOL_MIN;
3901 static const int32_t RAW_TOOL_MAX;
3902 static const int32_t RAW_PRESSURE_MIN;
3903 static const int32_t RAW_PRESSURE_MAX;
3904 static const int32_t RAW_ORIENTATION_MIN;
3905 static const int32_t RAW_ORIENTATION_MAX;
3906 static const int32_t RAW_DISTANCE_MIN;
3907 static const int32_t RAW_DISTANCE_MAX;
3908 static const int32_t RAW_TILT_MIN;
3909 static const int32_t RAW_TILT_MAX;
3910 static const int32_t RAW_ID_MIN;
3911 static const int32_t RAW_ID_MAX;
3912 static const int32_t RAW_SLOT_MIN;
3913 static const int32_t RAW_SLOT_MAX;
3914 static const float X_PRECISION;
3915 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003916 static const float X_PRECISION_VIRTUAL;
3917 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003920 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
3922 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3923
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003924 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003925 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003926
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 enum Axes {
3928 POSITION = 1 << 0,
3929 TOUCH = 1 << 1,
3930 TOOL = 1 << 2,
3931 PRESSURE = 1 << 3,
3932 ORIENTATION = 1 << 4,
3933 MINOR = 1 << 5,
3934 ID = 1 << 6,
3935 DISTANCE = 1 << 7,
3936 TILT = 1 << 8,
3937 SLOT = 1 << 9,
3938 TOOL_TYPE = 1 << 10,
3939 };
3940
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003941 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3942 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003943 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003945 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946 int32_t toRawX(float displayX);
3947 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003948 float toCookedX(float rawX, float rawY);
3949 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003951 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003952 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003953 float toDisplayY(int32_t rawY, int32_t displayHeight);
3954
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955};
3956
3957const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3958const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3959const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3960const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3961const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3962const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3963const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3964const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003965const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3966const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3968const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3969const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3970const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3971const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3972const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3973const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3974const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3975const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3976const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3977const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3978const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003979const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3980 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3981const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3982 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003983const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3984 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985
3986const float TouchInputMapperTest::GEOMETRIC_SCALE =
3987 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3988 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3989
3990const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3991 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3992 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3993};
3994
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003995void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003996 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3997 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003998}
3999
4000void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4001 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4002 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003}
4004
Santos Cordonfa5cf462017-04-05 10:37:00 -07004005void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004006 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4007 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4008 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004009}
4010
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004012 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4013 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4015 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016}
4017
Jason Gerecke489fda82012-09-07 17:19:40 -07004018void TouchInputMapperTest::prepareLocationCalibration() {
4019 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4020}
4021
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022int32_t TouchInputMapperTest::toRawX(float displayX) {
4023 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4024}
4025
4026int32_t TouchInputMapperTest::toRawY(float displayY) {
4027 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4028}
4029
Jason Gerecke489fda82012-09-07 17:19:40 -07004030float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4031 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4032 return rawX;
4033}
4034
4035float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4036 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4037 return rawY;
4038}
4039
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004041 return toDisplayX(rawX, DISPLAY_WIDTH);
4042}
4043
4044float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4045 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046}
4047
4048float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004049 return toDisplayY(rawY, DISPLAY_HEIGHT);
4050}
4051
4052float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4053 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054}
4055
4056
4057// --- SingleTouchInputMapperTest ---
4058
4059class SingleTouchInputMapperTest : public TouchInputMapperTest {
4060protected:
4061 void prepareButtons();
4062 void prepareAxes(int axes);
4063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004064 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4065 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4066 void processUp(SingleTouchInputMapper& mappery);
4067 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4068 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4069 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4070 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4071 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4072 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073};
4074
4075void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004076 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077}
4078
4079void SingleTouchInputMapperTest::prepareAxes(int axes) {
4080 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4082 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 }
4084 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4086 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 }
4088 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004089 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4090 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 }
4092 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004093 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4094 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 }
4096 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 }
4100}
4101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004102void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004103 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4104 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4105 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106}
4107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004109 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4110 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111}
4112
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004113void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004114 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115}
4116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004117void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004118 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119}
4120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004121void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4122 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004123 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124}
4125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004126void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004127 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128}
4129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004130void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4131 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004132 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4133 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134}
4135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004136void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4137 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004138 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139}
4140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004141void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004142 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143}
4144
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 prepareButtons();
4147 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004148 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004150 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151}
4152
4153TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004154 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4155 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156 prepareButtons();
4157 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004158 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004160 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161}
4162
4163TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164 prepareButtons();
4165 prepareAxes(POSITION);
4166 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004167 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004169 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170}
4171
4172TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 prepareButtons();
4174 prepareAxes(POSITION);
4175 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004176 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179}
4180
4181TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182 addConfigurationProperty("touch.deviceType", "touchScreen");
4183 prepareDisplay(DISPLAY_ORIENTATION_0);
4184 prepareButtons();
4185 prepareAxes(POSITION);
4186 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004187 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188
4189 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004190 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
4192 // Virtual key is down.
4193 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4194 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4195 processDown(mapper, x, y);
4196 processSync(mapper);
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004199 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200
4201 // Virtual key is up.
4202 processUp(mapper);
4203 processSync(mapper);
4204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004206 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207}
4208
4209TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 addConfigurationProperty("touch.deviceType", "touchScreen");
4211 prepareDisplay(DISPLAY_ORIENTATION_0);
4212 prepareButtons();
4213 prepareAxes(POSITION);
4214 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004215 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216
4217 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004218 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219
4220 // Virtual key is down.
4221 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4222 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4223 processDown(mapper, x, y);
4224 processSync(mapper);
4225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004227 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228
4229 // Virtual key is up.
4230 processUp(mapper);
4231 processSync(mapper);
4232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235}
4236
4237TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 addConfigurationProperty("touch.deviceType", "touchScreen");
4239 prepareDisplay(DISPLAY_ORIENTATION_0);
4240 prepareButtons();
4241 prepareAxes(POSITION);
4242 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004243 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244
4245 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4246 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004247 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_TRUE(flags[0]);
4249 ASSERT_FALSE(flags[1]);
4250}
4251
4252TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 addConfigurationProperty("touch.deviceType", "touchScreen");
4254 prepareDisplay(DISPLAY_ORIENTATION_0);
4255 prepareButtons();
4256 prepareAxes(POSITION);
4257 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004258 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259
4260 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4261
4262 NotifyKeyArgs args;
4263
4264 // Press virtual key.
4265 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4266 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4267 processDown(mapper, x, y);
4268 processSync(mapper);
4269
4270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4271 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4272 ASSERT_EQ(DEVICE_ID, args.deviceId);
4273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4274 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4275 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4276 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4277 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4278 ASSERT_EQ(KEY_HOME, args.scanCode);
4279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4280 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4281
4282 // Release virtual key.
4283 processUp(mapper);
4284 processSync(mapper);
4285
4286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4287 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4288 ASSERT_EQ(DEVICE_ID, args.deviceId);
4289 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4290 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4291 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4292 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4293 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4294 ASSERT_EQ(KEY_HOME, args.scanCode);
4295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4296 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4297
4298 // Should not have sent any motions.
4299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4300}
4301
4302TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 addConfigurationProperty("touch.deviceType", "touchScreen");
4304 prepareDisplay(DISPLAY_ORIENTATION_0);
4305 prepareButtons();
4306 prepareAxes(POSITION);
4307 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004308 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4311
4312 NotifyKeyArgs keyArgs;
4313
4314 // Press virtual key.
4315 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4316 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4317 processDown(mapper, x, y);
4318 processSync(mapper);
4319
4320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4321 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4322 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4323 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4324 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4325 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4326 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4327 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4328 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4329 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4330 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4331
4332 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4333 // into the display area.
4334 y -= 100;
4335 processMove(mapper, x, y);
4336 processSync(mapper);
4337
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4339 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4340 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4341 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4342 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4344 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4345 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4346 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4347 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4349 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4350
4351 NotifyMotionArgs motionArgs;
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4354 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4355 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4356 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4357 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4358 ASSERT_EQ(0, motionArgs.flags);
4359 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4360 ASSERT_EQ(0, motionArgs.buttonState);
4361 ASSERT_EQ(0, motionArgs.edgeFlags);
4362 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4363 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4366 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4367 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4368 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4370
4371 // Keep moving out of bounds. Should generate a pointer move.
4372 y -= 50;
4373 processMove(mapper, x, y);
4374 processSync(mapper);
4375
4376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4379 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4380 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4382 ASSERT_EQ(0, motionArgs.flags);
4383 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4384 ASSERT_EQ(0, motionArgs.buttonState);
4385 ASSERT_EQ(0, motionArgs.edgeFlags);
4386 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4387 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4390 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4391 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4392 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4394
4395 // Release out of bounds. Should generate a pointer up.
4396 processUp(mapper);
4397 processSync(mapper);
4398
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4401 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4402 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4403 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4404 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4405 ASSERT_EQ(0, motionArgs.flags);
4406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4407 ASSERT_EQ(0, motionArgs.buttonState);
4408 ASSERT_EQ(0, motionArgs.edgeFlags);
4409 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4413 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4414 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4415 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4416 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4417
4418 // Should not have sent any more keys or motions.
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4421}
4422
4423TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 addConfigurationProperty("touch.deviceType", "touchScreen");
4425 prepareDisplay(DISPLAY_ORIENTATION_0);
4426 prepareButtons();
4427 prepareAxes(POSITION);
4428 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004429 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430
4431 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4432
4433 NotifyMotionArgs motionArgs;
4434
4435 // Initially go down out of bounds.
4436 int32_t x = -10;
4437 int32_t y = -10;
4438 processDown(mapper, x, y);
4439 processSync(mapper);
4440
4441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4442
4443 // Move into the display area. Should generate a pointer down.
4444 x = 50;
4445 y = 75;
4446 processMove(mapper, x, y);
4447 processSync(mapper);
4448
4449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4455 ASSERT_EQ(0, motionArgs.flags);
4456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4457 ASSERT_EQ(0, motionArgs.buttonState);
4458 ASSERT_EQ(0, motionArgs.edgeFlags);
4459 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4463 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4464 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4465 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4467
4468 // Release. Should generate a pointer up.
4469 processUp(mapper);
4470 processSync(mapper);
4471
4472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4474 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4475 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4476 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4477 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4478 ASSERT_EQ(0, motionArgs.flags);
4479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4480 ASSERT_EQ(0, motionArgs.buttonState);
4481 ASSERT_EQ(0, motionArgs.edgeFlags);
4482 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4486 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4487 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4488 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4489 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4490
4491 // Should not have sent any more keys or motions.
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4494}
4495
Santos Cordonfa5cf462017-04-05 10:37:00 -07004496TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004497 addConfigurationProperty("touch.deviceType", "touchScreen");
4498 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4499
4500 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4501 prepareButtons();
4502 prepareAxes(POSITION);
4503 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004504 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004505
4506 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4507
4508 NotifyMotionArgs motionArgs;
4509
4510 // Down.
4511 int32_t x = 100;
4512 int32_t y = 125;
4513 processDown(mapper, x, y);
4514 processSync(mapper);
4515
4516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4517 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4518 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4519 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4520 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4521 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4523 ASSERT_EQ(0, motionArgs.flags);
4524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4525 ASSERT_EQ(0, motionArgs.buttonState);
4526 ASSERT_EQ(0, motionArgs.edgeFlags);
4527 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4531 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4532 1, 0, 0, 0, 0, 0, 0, 0));
4533 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4534 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4535 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4536
4537 // Move.
4538 x += 50;
4539 y += 75;
4540 processMove(mapper, x, y);
4541 processSync(mapper);
4542
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4545 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4546 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4547 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4548 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4550 ASSERT_EQ(0, motionArgs.flags);
4551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4552 ASSERT_EQ(0, motionArgs.buttonState);
4553 ASSERT_EQ(0, motionArgs.edgeFlags);
4554 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4558 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4559 1, 0, 0, 0, 0, 0, 0, 0));
4560 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4561 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4563
4564 // Up.
4565 processUp(mapper);
4566 processSync(mapper);
4567
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4570 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4571 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4573 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4575 ASSERT_EQ(0, motionArgs.flags);
4576 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4577 ASSERT_EQ(0, motionArgs.buttonState);
4578 ASSERT_EQ(0, motionArgs.edgeFlags);
4579 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4580 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4583 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4584 1, 0, 0, 0, 0, 0, 0, 0));
4585 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4586 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4588
4589 // Should not have sent any more keys or motions.
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4592}
4593
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 addConfigurationProperty("touch.deviceType", "touchScreen");
4596 prepareDisplay(DISPLAY_ORIENTATION_0);
4597 prepareButtons();
4598 prepareAxes(POSITION);
4599 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004600 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601
4602 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4603
4604 NotifyMotionArgs motionArgs;
4605
4606 // Down.
4607 int32_t x = 100;
4608 int32_t y = 125;
4609 processDown(mapper, x, y);
4610 processSync(mapper);
4611
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4614 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4616 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4618 ASSERT_EQ(0, motionArgs.flags);
4619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4620 ASSERT_EQ(0, motionArgs.buttonState);
4621 ASSERT_EQ(0, motionArgs.edgeFlags);
4622 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4626 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4627 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4628 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4630
4631 // Move.
4632 x += 50;
4633 y += 75;
4634 processMove(mapper, x, y);
4635 processSync(mapper);
4636
4637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4640 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4641 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4643 ASSERT_EQ(0, motionArgs.flags);
4644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4645 ASSERT_EQ(0, motionArgs.buttonState);
4646 ASSERT_EQ(0, motionArgs.edgeFlags);
4647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4651 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4652 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4653 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4655
4656 // Up.
4657 processUp(mapper);
4658 processSync(mapper);
4659
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4661 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4662 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4663 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4664 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4665 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4666 ASSERT_EQ(0, motionArgs.flags);
4667 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4668 ASSERT_EQ(0, motionArgs.buttonState);
4669 ASSERT_EQ(0, motionArgs.edgeFlags);
4670 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4674 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4675 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4676 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4677 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4678
4679 // Should not have sent any more keys or motions.
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4682}
4683
4684TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 addConfigurationProperty("touch.deviceType", "touchScreen");
4686 prepareButtons();
4687 prepareAxes(POSITION);
4688 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004689 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690
4691 NotifyMotionArgs args;
4692
4693 // Rotation 90.
4694 prepareDisplay(DISPLAY_ORIENTATION_90);
4695 processDown(mapper, toRawX(50), toRawY(75));
4696 processSync(mapper);
4697
4698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4699 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4700 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4701
4702 processUp(mapper);
4703 processSync(mapper);
4704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4705}
4706
4707TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 addConfigurationProperty("touch.deviceType", "touchScreen");
4709 prepareButtons();
4710 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004711 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712
4713 NotifyMotionArgs args;
4714
4715 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004716 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 prepareDisplay(DISPLAY_ORIENTATION_0);
4718 processDown(mapper, toRawX(50), toRawY(75));
4719 processSync(mapper);
4720
4721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4722 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4723 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4724
4725 processUp(mapper);
4726 processSync(mapper);
4727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4728
4729 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004730 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 prepareDisplay(DISPLAY_ORIENTATION_90);
4732 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4733 processSync(mapper);
4734
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4736 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4737 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4738
4739 processUp(mapper);
4740 processSync(mapper);
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4742
4743 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004744 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 prepareDisplay(DISPLAY_ORIENTATION_180);
4746 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4747 processSync(mapper);
4748
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4750 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4751 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4752
4753 processUp(mapper);
4754 processSync(mapper);
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4756
4757 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004758 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759 prepareDisplay(DISPLAY_ORIENTATION_270);
4760 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4761 processSync(mapper);
4762
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4764 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4765 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4766
4767 processUp(mapper);
4768 processSync(mapper);
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4770}
4771
4772TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 addConfigurationProperty("touch.deviceType", "touchScreen");
4774 prepareDisplay(DISPLAY_ORIENTATION_0);
4775 prepareButtons();
4776 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004777 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778
4779 // These calculations are based on the input device calibration documentation.
4780 int32_t rawX = 100;
4781 int32_t rawY = 200;
4782 int32_t rawPressure = 10;
4783 int32_t rawToolMajor = 12;
4784 int32_t rawDistance = 2;
4785 int32_t rawTiltX = 30;
4786 int32_t rawTiltY = 110;
4787
4788 float x = toDisplayX(rawX);
4789 float y = toDisplayY(rawY);
4790 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4791 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4792 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4793 float distance = float(rawDistance);
4794
4795 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4796 float tiltScale = M_PI / 180;
4797 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4798 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4799 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4800 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4801
4802 processDown(mapper, rawX, rawY);
4803 processPressure(mapper, rawPressure);
4804 processToolMajor(mapper, rawToolMajor);
4805 processDistance(mapper, rawDistance);
4806 processTilt(mapper, rawTiltX, rawTiltY);
4807 processSync(mapper);
4808
4809 NotifyMotionArgs args;
4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4812 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4813 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4814}
4815
Jason Gerecke489fda82012-09-07 17:19:40 -07004816TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004817 addConfigurationProperty("touch.deviceType", "touchScreen");
4818 prepareDisplay(DISPLAY_ORIENTATION_0);
4819 prepareLocationCalibration();
4820 prepareButtons();
4821 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004822 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004823
4824 int32_t rawX = 100;
4825 int32_t rawY = 200;
4826
4827 float x = toDisplayX(toCookedX(rawX, rawY));
4828 float y = toDisplayY(toCookedY(rawX, rawY));
4829
4830 processDown(mapper, rawX, rawY);
4831 processSync(mapper);
4832
4833 NotifyMotionArgs args;
4834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4836 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4837}
4838
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 addConfigurationProperty("touch.deviceType", "touchScreen");
4841 prepareDisplay(DISPLAY_ORIENTATION_0);
4842 prepareButtons();
4843 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845
4846 NotifyMotionArgs motionArgs;
4847 NotifyKeyArgs keyArgs;
4848
4849 processDown(mapper, 100, 200);
4850 processSync(mapper);
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4853 ASSERT_EQ(0, motionArgs.buttonState);
4854
4855 // press BTN_LEFT, release BTN_LEFT
4856 processKey(mapper, BTN_LEFT, 1);
4857 processSync(mapper);
4858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4860 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4861
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4863 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4864 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4865
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 processKey(mapper, BTN_LEFT, 0);
4867 processSync(mapper);
4868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004869 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004874 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875
4876 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4877 processKey(mapper, BTN_RIGHT, 1);
4878 processKey(mapper, BTN_MIDDLE, 1);
4879 processSync(mapper);
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4882 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4883 motionArgs.buttonState);
4884
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4887 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4888
4889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4890 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4891 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4892 motionArgs.buttonState);
4893
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 processKey(mapper, BTN_RIGHT, 0);
4895 processSync(mapper);
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004899
4900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004902 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903
4904 processKey(mapper, BTN_MIDDLE, 0);
4905 processSync(mapper);
4906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004909
4910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004912 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913
4914 // press BTN_BACK, release BTN_BACK
4915 processKey(mapper, BTN_BACK, 1);
4916 processSync(mapper);
4917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4918 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4919 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004920
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004923 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4924
4925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4927 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928
4929 processKey(mapper, BTN_BACK, 0);
4930 processSync(mapper);
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004932 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004934
4935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004937 ASSERT_EQ(0, motionArgs.buttonState);
4938
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4940 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4941 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4942
4943 // press BTN_SIDE, release BTN_SIDE
4944 processKey(mapper, BTN_SIDE, 1);
4945 processSync(mapper);
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4947 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4948 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004949
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4953
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4956 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957
4958 processKey(mapper, BTN_SIDE, 0);
4959 processSync(mapper);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004963
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004966 ASSERT_EQ(0, motionArgs.buttonState);
4967
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4969 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4970 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4971
4972 // press BTN_FORWARD, release BTN_FORWARD
4973 processKey(mapper, BTN_FORWARD, 1);
4974 processSync(mapper);
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4976 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4977 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004978
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004981 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4982
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4985 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986
4987 processKey(mapper, BTN_FORWARD, 0);
4988 processSync(mapper);
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004992
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004995 ASSERT_EQ(0, motionArgs.buttonState);
4996
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4998 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4999 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5000
5001 // press BTN_EXTRA, release BTN_EXTRA
5002 processKey(mapper, BTN_EXTRA, 1);
5003 processSync(mapper);
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5005 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5006 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005007
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005010 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5011
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5014 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015
5016 processKey(mapper, BTN_EXTRA, 0);
5017 processSync(mapper);
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005019 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005021
5022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005024 ASSERT_EQ(0, motionArgs.buttonState);
5025
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5027 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5028 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5029
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5031
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 // press BTN_STYLUS, release BTN_STYLUS
5033 processKey(mapper, BTN_STYLUS, 1);
5034 processSync(mapper);
5035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005037 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5038
5039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5041 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042
5043 processKey(mapper, BTN_STYLUS, 0);
5044 processSync(mapper);
5045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005048
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005051 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052
5053 // press BTN_STYLUS2, release BTN_STYLUS2
5054 processKey(mapper, BTN_STYLUS2, 1);
5055 processSync(mapper);
5056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005058 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5059
5060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5062 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063
5064 processKey(mapper, BTN_STYLUS2, 0);
5065 processSync(mapper);
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005069
5070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005072 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073
5074 // release touch
5075 processUp(mapper);
5076 processSync(mapper);
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5078 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5079 ASSERT_EQ(0, motionArgs.buttonState);
5080}
5081
5082TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 addConfigurationProperty("touch.deviceType", "touchScreen");
5084 prepareDisplay(DISPLAY_ORIENTATION_0);
5085 prepareButtons();
5086 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005087 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088
5089 NotifyMotionArgs motionArgs;
5090
5091 // default tool type is finger
5092 processDown(mapper, 100, 200);
5093 processSync(mapper);
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5097
5098 // eraser
5099 processKey(mapper, BTN_TOOL_RUBBER, 1);
5100 processSync(mapper);
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5104
5105 // stylus
5106 processKey(mapper, BTN_TOOL_RUBBER, 0);
5107 processKey(mapper, BTN_TOOL_PEN, 1);
5108 processSync(mapper);
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5112
5113 // brush
5114 processKey(mapper, BTN_TOOL_PEN, 0);
5115 processKey(mapper, BTN_TOOL_BRUSH, 1);
5116 processSync(mapper);
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5120
5121 // pencil
5122 processKey(mapper, BTN_TOOL_BRUSH, 0);
5123 processKey(mapper, BTN_TOOL_PENCIL, 1);
5124 processSync(mapper);
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5128
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005129 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 processKey(mapper, BTN_TOOL_PENCIL, 0);
5131 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5132 processSync(mapper);
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5136
5137 // mouse
5138 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5139 processKey(mapper, BTN_TOOL_MOUSE, 1);
5140 processSync(mapper);
5141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5144
5145 // lens
5146 processKey(mapper, BTN_TOOL_MOUSE, 0);
5147 processKey(mapper, BTN_TOOL_LENS, 1);
5148 processSync(mapper);
5149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5152
5153 // double-tap
5154 processKey(mapper, BTN_TOOL_LENS, 0);
5155 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5156 processSync(mapper);
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5160
5161 // triple-tap
5162 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5163 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5164 processSync(mapper);
5165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5168
5169 // quad-tap
5170 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5171 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5172 processSync(mapper);
5173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5176
5177 // finger
5178 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5179 processKey(mapper, BTN_TOOL_FINGER, 1);
5180 processSync(mapper);
5181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5184
5185 // stylus trumps finger
5186 processKey(mapper, BTN_TOOL_PEN, 1);
5187 processSync(mapper);
5188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5191
5192 // eraser trumps stylus
5193 processKey(mapper, BTN_TOOL_RUBBER, 1);
5194 processSync(mapper);
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5198
5199 // mouse trumps eraser
5200 processKey(mapper, BTN_TOOL_MOUSE, 1);
5201 processSync(mapper);
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5205
5206 // back to default tool type
5207 processKey(mapper, BTN_TOOL_MOUSE, 0);
5208 processKey(mapper, BTN_TOOL_RUBBER, 0);
5209 processKey(mapper, BTN_TOOL_PEN, 0);
5210 processKey(mapper, BTN_TOOL_FINGER, 0);
5211 processSync(mapper);
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5215}
5216
5217TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218 addConfigurationProperty("touch.deviceType", "touchScreen");
5219 prepareDisplay(DISPLAY_ORIENTATION_0);
5220 prepareButtons();
5221 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005222 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005223 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224
5225 NotifyMotionArgs motionArgs;
5226
5227 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5228 processKey(mapper, BTN_TOOL_FINGER, 1);
5229 processMove(mapper, 100, 200);
5230 processSync(mapper);
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5232 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5234 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5235
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5237 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5239 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5240
5241 // move a little
5242 processMove(mapper, 150, 250);
5243 processSync(mapper);
5244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5245 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5247 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5248
5249 // down when BTN_TOUCH is pressed, pressure defaults to 1
5250 processKey(mapper, BTN_TOUCH, 1);
5251 processSync(mapper);
5252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5253 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5255 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5256
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5258 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5260 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5261
5262 // up when BTN_TOUCH is released, hover restored
5263 processKey(mapper, BTN_TOUCH, 0);
5264 processSync(mapper);
5265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5266 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5268 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5269
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5271 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5273 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5274
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5276 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5278 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5279
5280 // exit hover when pointer goes away
5281 processKey(mapper, BTN_TOOL_FINGER, 0);
5282 processSync(mapper);
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5284 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5286 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5287}
5288
5289TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290 addConfigurationProperty("touch.deviceType", "touchScreen");
5291 prepareDisplay(DISPLAY_ORIENTATION_0);
5292 prepareButtons();
5293 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005294 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295
5296 NotifyMotionArgs motionArgs;
5297
5298 // initially hovering because pressure is 0
5299 processDown(mapper, 100, 200);
5300 processPressure(mapper, 0);
5301 processSync(mapper);
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5305 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5306
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5308 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5310 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5311
5312 // move a little
5313 processMove(mapper, 150, 250);
5314 processSync(mapper);
5315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5316 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5318 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5319
5320 // down when pressure is non-zero
5321 processPressure(mapper, RAW_PRESSURE_MAX);
5322 processSync(mapper);
5323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5326 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5327
5328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5329 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5331 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5332
5333 // up when pressure becomes 0, hover restored
5334 processPressure(mapper, 0);
5335 processSync(mapper);
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5337 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5339 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5340
5341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5344 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5345
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5349 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5350
5351 // exit hover when pointer goes away
5352 processUp(mapper);
5353 processSync(mapper);
5354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5357 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5358}
5359
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360// --- MultiTouchInputMapperTest ---
5361
5362class MultiTouchInputMapperTest : public TouchInputMapperTest {
5363protected:
5364 void prepareAxes(int axes);
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5367 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5368 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5369 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5370 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5371 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5372 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5373 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5374 void processId(MultiTouchInputMapper& mapper, int32_t id);
5375 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5376 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5377 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5378 void processMTSync(MultiTouchInputMapper& mapper);
5379 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380};
5381
5382void MultiTouchInputMapperTest::prepareAxes(int axes) {
5383 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005384 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5385 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386 }
5387 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005388 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5389 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005391 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5392 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393 }
5394 }
5395 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005396 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5397 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005399 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5400 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 }
5402 }
5403 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005404 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5405 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 }
5407 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005408 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5409 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410 }
5411 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005412 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5413 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 }
5415 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005416 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5417 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 }
5419 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5421 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 }
5423 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005424 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425 }
5426}
5427
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005428void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5429 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005430 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5431 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432}
5433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5435 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005436 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437}
5438
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005439void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5440 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005441 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442}
5443
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005444void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005445 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446}
5447
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005448void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005449 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005450}
5451
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005452void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5453 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005454 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455}
5456
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005457void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005458 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459}
5460
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005461void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005462 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463}
5464
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005465void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005466 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467}
5468
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005469void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005470 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471}
5472
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005473void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005474 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475}
5476
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005477void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5478 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005479 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480}
5481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005482void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005483 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484}
5485
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005486void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005487 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488}
5489
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 addConfigurationProperty("touch.deviceType", "touchScreen");
5492 prepareDisplay(DISPLAY_ORIENTATION_0);
5493 prepareAxes(POSITION);
5494 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005495 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496
5497 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5498
5499 NotifyMotionArgs motionArgs;
5500
5501 // Two fingers down at once.
5502 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5503 processPosition(mapper, x1, y1);
5504 processMTSync(mapper);
5505 processPosition(mapper, x2, y2);
5506 processMTSync(mapper);
5507 processSync(mapper);
5508
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5510 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5511 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5513 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5514 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5515 ASSERT_EQ(0, motionArgs.flags);
5516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5517 ASSERT_EQ(0, motionArgs.buttonState);
5518 ASSERT_EQ(0, motionArgs.edgeFlags);
5519 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5523 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5524 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5525 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5526 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5527
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5529 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5530 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5531 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5532 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5533 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5534 motionArgs.action);
5535 ASSERT_EQ(0, motionArgs.flags);
5536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5537 ASSERT_EQ(0, motionArgs.buttonState);
5538 ASSERT_EQ(0, motionArgs.edgeFlags);
5539 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5542 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5545 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5547 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5548 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5549 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5551
5552 // Move.
5553 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5554 processPosition(mapper, x1, y1);
5555 processMTSync(mapper);
5556 processPosition(mapper, x2, y2);
5557 processMTSync(mapper);
5558 processSync(mapper);
5559
5560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5562 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5563 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5564 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5566 ASSERT_EQ(0, motionArgs.flags);
5567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5568 ASSERT_EQ(0, motionArgs.buttonState);
5569 ASSERT_EQ(0, motionArgs.edgeFlags);
5570 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5571 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5573 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5576 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5578 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5579 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5580 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5582
5583 // First finger up.
5584 x2 += 15; y2 -= 20;
5585 processPosition(mapper, x2, y2);
5586 processMTSync(mapper);
5587 processSync(mapper);
5588
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5590 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5591 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5592 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5593 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5594 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5595 motionArgs.action);
5596 ASSERT_EQ(0, motionArgs.flags);
5597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5598 ASSERT_EQ(0, motionArgs.buttonState);
5599 ASSERT_EQ(0, motionArgs.edgeFlags);
5600 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5603 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5606 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5608 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5609 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5610 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5612
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5615 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5616 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5617 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5619 ASSERT_EQ(0, motionArgs.flags);
5620 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5621 ASSERT_EQ(0, motionArgs.buttonState);
5622 ASSERT_EQ(0, motionArgs.edgeFlags);
5623 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5624 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5627 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5628 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5629 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5630 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5631
5632 // Move.
5633 x2 += 20; y2 -= 25;
5634 processPosition(mapper, x2, y2);
5635 processMTSync(mapper);
5636 processSync(mapper);
5637
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5639 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5640 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5641 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5642 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5644 ASSERT_EQ(0, motionArgs.flags);
5645 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5646 ASSERT_EQ(0, motionArgs.buttonState);
5647 ASSERT_EQ(0, motionArgs.edgeFlags);
5648 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5649 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5652 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5653 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5654 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5655 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5656
5657 // New finger down.
5658 int32_t x3 = 700, y3 = 300;
5659 processPosition(mapper, x2, y2);
5660 processMTSync(mapper);
5661 processPosition(mapper, x3, y3);
5662 processMTSync(mapper);
5663 processSync(mapper);
5664
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5667 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5668 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5669 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5670 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5671 motionArgs.action);
5672 ASSERT_EQ(0, motionArgs.flags);
5673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5674 ASSERT_EQ(0, motionArgs.buttonState);
5675 ASSERT_EQ(0, motionArgs.edgeFlags);
5676 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5677 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5679 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5682 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5684 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5685 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5686 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5687 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5688
5689 // Second finger up.
5690 x3 += 30; y3 -= 20;
5691 processPosition(mapper, x3, y3);
5692 processMTSync(mapper);
5693 processSync(mapper);
5694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5696 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5697 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5698 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5699 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5700 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5701 motionArgs.action);
5702 ASSERT_EQ(0, motionArgs.flags);
5703 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5704 ASSERT_EQ(0, motionArgs.buttonState);
5705 ASSERT_EQ(0, motionArgs.edgeFlags);
5706 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5709 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5712 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5714 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5715 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5716 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5718
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5720 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5721 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5722 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5723 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5725 ASSERT_EQ(0, motionArgs.flags);
5726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5727 ASSERT_EQ(0, motionArgs.buttonState);
5728 ASSERT_EQ(0, motionArgs.edgeFlags);
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(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5734 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5735 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5737
5738 // Last finger up.
5739 processMTSync(mapper);
5740 processSync(mapper);
5741
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5744 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5745 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5746 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5747 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5748 ASSERT_EQ(0, motionArgs.flags);
5749 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5750 ASSERT_EQ(0, motionArgs.buttonState);
5751 ASSERT_EQ(0, motionArgs.edgeFlags);
5752 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5756 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5757 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5758 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5759 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5760
5761 // Should not have sent any more keys or motions.
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5764}
5765
5766TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 addConfigurationProperty("touch.deviceType", "touchScreen");
5768 prepareDisplay(DISPLAY_ORIENTATION_0);
5769 prepareAxes(POSITION | ID);
5770 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005771 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5774
5775 NotifyMotionArgs motionArgs;
5776
5777 // Two fingers down at once.
5778 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5779 processPosition(mapper, x1, y1);
5780 processId(mapper, 1);
5781 processMTSync(mapper);
5782 processPosition(mapper, x2, y2);
5783 processId(mapper, 2);
5784 processMTSync(mapper);
5785 processSync(mapper);
5786
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5788 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5789 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5790 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5793 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5794
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5797 motionArgs.action);
5798 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5801 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5804 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5806 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5807
5808 // Move.
5809 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5810 processPosition(mapper, x1, y1);
5811 processId(mapper, 1);
5812 processMTSync(mapper);
5813 processPosition(mapper, x2, y2);
5814 processId(mapper, 2);
5815 processMTSync(mapper);
5816 processSync(mapper);
5817
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5820 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5821 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5823 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5826 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5828 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5829
5830 // First finger up.
5831 x2 += 15; y2 -= 20;
5832 processPosition(mapper, x2, y2);
5833 processId(mapper, 2);
5834 processMTSync(mapper);
5835 processSync(mapper);
5836
5837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5838 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5839 motionArgs.action);
5840 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5841 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5843 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5846 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5848 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5849
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5852 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5853 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5856 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5857
5858 // Move.
5859 x2 += 20; y2 -= 25;
5860 processPosition(mapper, x2, y2);
5861 processId(mapper, 2);
5862 processMTSync(mapper);
5863 processSync(mapper);
5864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5867 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5868 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5871 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5872
5873 // New finger down.
5874 int32_t x3 = 700, y3 = 300;
5875 processPosition(mapper, x2, y2);
5876 processId(mapper, 2);
5877 processMTSync(mapper);
5878 processPosition(mapper, x3, y3);
5879 processId(mapper, 3);
5880 processMTSync(mapper);
5881 processSync(mapper);
5882
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5884 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5885 motionArgs.action);
5886 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5887 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5889 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5892 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5894 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5895
5896 // Second finger up.
5897 x3 += 30; y3 -= 20;
5898 processPosition(mapper, x3, y3);
5899 processId(mapper, 3);
5900 processMTSync(mapper);
5901 processSync(mapper);
5902
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5905 motionArgs.action);
5906 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5907 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5909 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5912 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5914 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5918 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5919 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5922 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5923
5924 // Last finger up.
5925 processMTSync(mapper);
5926 processSync(mapper);
5927
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5930 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5931 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5935
5936 // Should not have sent any more keys or motions.
5937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5939}
5940
5941TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 addConfigurationProperty("touch.deviceType", "touchScreen");
5943 prepareDisplay(DISPLAY_ORIENTATION_0);
5944 prepareAxes(POSITION | ID | SLOT);
5945 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005946 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947
5948 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5949
5950 NotifyMotionArgs motionArgs;
5951
5952 // Two fingers down at once.
5953 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5954 processPosition(mapper, x1, y1);
5955 processId(mapper, 1);
5956 processSlot(mapper, 1);
5957 processPosition(mapper, x2, y2);
5958 processId(mapper, 2);
5959 processSync(mapper);
5960
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5963 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5964 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5967 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5968
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5971 motionArgs.action);
5972 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5975 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5978 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5980 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5981
5982 // Move.
5983 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5984 processSlot(mapper, 0);
5985 processPosition(mapper, x1, y1);
5986 processSlot(mapper, 1);
5987 processPosition(mapper, x2, y2);
5988 processSync(mapper);
5989
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5992 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5993 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5995 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5998 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6000 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6001
6002 // First finger up.
6003 x2 += 15; y2 -= 20;
6004 processSlot(mapper, 0);
6005 processId(mapper, -1);
6006 processSlot(mapper, 1);
6007 processPosition(mapper, x2, y2);
6008 processSync(mapper);
6009
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6012 motionArgs.action);
6013 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6014 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6016 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6019 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6021 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6022
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6025 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6026 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6029 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6030
6031 // Move.
6032 x2 += 20; y2 -= 25;
6033 processPosition(mapper, x2, y2);
6034 processSync(mapper);
6035
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6038 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6039 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6042 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6043
6044 // New finger down.
6045 int32_t x3 = 700, y3 = 300;
6046 processPosition(mapper, x2, y2);
6047 processSlot(mapper, 0);
6048 processId(mapper, 3);
6049 processPosition(mapper, x3, y3);
6050 processSync(mapper);
6051
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6053 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6054 motionArgs.action);
6055 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6056 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6058 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6061 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6063 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6064
6065 // Second finger up.
6066 x3 += 30; y3 -= 20;
6067 processSlot(mapper, 1);
6068 processId(mapper, -1);
6069 processSlot(mapper, 0);
6070 processPosition(mapper, x3, y3);
6071 processSync(mapper);
6072
6073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6074 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6075 motionArgs.action);
6076 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6079 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6082 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6084 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6085
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6088 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6089 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6092 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6093
6094 // Last finger up.
6095 processId(mapper, -1);
6096 processSync(mapper);
6097
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6100 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6101 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6104 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6105
6106 // Should not have sent any more keys or motions.
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6109}
6110
6111TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 addConfigurationProperty("touch.deviceType", "touchScreen");
6113 prepareDisplay(DISPLAY_ORIENTATION_0);
6114 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116
6117 // These calculations are based on the input device calibration documentation.
6118 int32_t rawX = 100;
6119 int32_t rawY = 200;
6120 int32_t rawTouchMajor = 7;
6121 int32_t rawTouchMinor = 6;
6122 int32_t rawToolMajor = 9;
6123 int32_t rawToolMinor = 8;
6124 int32_t rawPressure = 11;
6125 int32_t rawDistance = 0;
6126 int32_t rawOrientation = 3;
6127 int32_t id = 5;
6128
6129 float x = toDisplayX(rawX);
6130 float y = toDisplayY(rawY);
6131 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6132 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6133 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6134 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6135 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6136 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6137 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6138 float distance = float(rawDistance);
6139
6140 processPosition(mapper, rawX, rawY);
6141 processTouchMajor(mapper, rawTouchMajor);
6142 processTouchMinor(mapper, rawTouchMinor);
6143 processToolMajor(mapper, rawToolMajor);
6144 processToolMinor(mapper, rawToolMinor);
6145 processPressure(mapper, rawPressure);
6146 processOrientation(mapper, rawOrientation);
6147 processDistance(mapper, rawDistance);
6148 processId(mapper, id);
6149 processMTSync(mapper);
6150 processSync(mapper);
6151
6152 NotifyMotionArgs args;
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6154 ASSERT_EQ(0, args.pointerProperties[0].id);
6155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6156 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6157 orientation, distance));
6158}
6159
6160TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 addConfigurationProperty("touch.deviceType", "touchScreen");
6162 prepareDisplay(DISPLAY_ORIENTATION_0);
6163 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6164 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006165 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166
6167 // These calculations are based on the input device calibration documentation.
6168 int32_t rawX = 100;
6169 int32_t rawY = 200;
6170 int32_t rawTouchMajor = 140;
6171 int32_t rawTouchMinor = 120;
6172 int32_t rawToolMajor = 180;
6173 int32_t rawToolMinor = 160;
6174
6175 float x = toDisplayX(rawX);
6176 float y = toDisplayY(rawY);
6177 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6178 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6179 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6180 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6181 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6182
6183 processPosition(mapper, rawX, rawY);
6184 processTouchMajor(mapper, rawTouchMajor);
6185 processTouchMinor(mapper, rawTouchMinor);
6186 processToolMajor(mapper, rawToolMajor);
6187 processToolMinor(mapper, rawToolMinor);
6188 processMTSync(mapper);
6189 processSync(mapper);
6190
6191 NotifyMotionArgs args;
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6194 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6195}
6196
6197TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198 addConfigurationProperty("touch.deviceType", "touchScreen");
6199 prepareDisplay(DISPLAY_ORIENTATION_0);
6200 prepareAxes(POSITION | TOUCH | TOOL);
6201 addConfigurationProperty("touch.size.calibration", "diameter");
6202 addConfigurationProperty("touch.size.scale", "10");
6203 addConfigurationProperty("touch.size.bias", "160");
6204 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206
6207 // These calculations are based on the input device calibration documentation.
6208 // Note: We only provide a single common touch/tool value because the device is assumed
6209 // not to emit separate values for each pointer (isSummed = 1).
6210 int32_t rawX = 100;
6211 int32_t rawY = 200;
6212 int32_t rawX2 = 150;
6213 int32_t rawY2 = 250;
6214 int32_t rawTouchMajor = 5;
6215 int32_t rawToolMajor = 8;
6216
6217 float x = toDisplayX(rawX);
6218 float y = toDisplayY(rawY);
6219 float x2 = toDisplayX(rawX2);
6220 float y2 = toDisplayY(rawY2);
6221 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6222 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6223 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6224
6225 processPosition(mapper, rawX, rawY);
6226 processTouchMajor(mapper, rawTouchMajor);
6227 processToolMajor(mapper, rawToolMajor);
6228 processMTSync(mapper);
6229 processPosition(mapper, rawX2, rawY2);
6230 processTouchMajor(mapper, rawTouchMajor);
6231 processToolMajor(mapper, rawToolMajor);
6232 processMTSync(mapper);
6233 processSync(mapper);
6234
6235 NotifyMotionArgs args;
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6241 args.action);
6242 ASSERT_EQ(size_t(2), args.pointerCount);
6243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6244 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6246 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6247}
6248
6249TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 addConfigurationProperty("touch.deviceType", "touchScreen");
6251 prepareDisplay(DISPLAY_ORIENTATION_0);
6252 prepareAxes(POSITION | TOUCH | TOOL);
6253 addConfigurationProperty("touch.size.calibration", "area");
6254 addConfigurationProperty("touch.size.scale", "43");
6255 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006256 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257
6258 // These calculations are based on the input device calibration documentation.
6259 int32_t rawX = 100;
6260 int32_t rawY = 200;
6261 int32_t rawTouchMajor = 5;
6262 int32_t rawToolMajor = 8;
6263
6264 float x = toDisplayX(rawX);
6265 float y = toDisplayY(rawY);
6266 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6267 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6268 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6269
6270 processPosition(mapper, rawX, rawY);
6271 processTouchMajor(mapper, rawTouchMajor);
6272 processToolMajor(mapper, rawToolMajor);
6273 processMTSync(mapper);
6274 processSync(mapper);
6275
6276 NotifyMotionArgs args;
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6279 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6280}
6281
6282TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 addConfigurationProperty("touch.deviceType", "touchScreen");
6284 prepareDisplay(DISPLAY_ORIENTATION_0);
6285 prepareAxes(POSITION | PRESSURE);
6286 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6287 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006288 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289
Michael Wrightaa449c92017-12-13 21:21:43 +00006290 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006291 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006292 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6293 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6294 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6295
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 // These calculations are based on the input device calibration documentation.
6297 int32_t rawX = 100;
6298 int32_t rawY = 200;
6299 int32_t rawPressure = 60;
6300
6301 float x = toDisplayX(rawX);
6302 float y = toDisplayY(rawY);
6303 float pressure = float(rawPressure) * 0.01f;
6304
6305 processPosition(mapper, rawX, rawY);
6306 processPressure(mapper, rawPressure);
6307 processMTSync(mapper);
6308 processSync(mapper);
6309
6310 NotifyMotionArgs args;
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6313 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6314}
6315
6316TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317 addConfigurationProperty("touch.deviceType", "touchScreen");
6318 prepareDisplay(DISPLAY_ORIENTATION_0);
6319 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006320 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 NotifyMotionArgs motionArgs;
6323 NotifyKeyArgs keyArgs;
6324
6325 processId(mapper, 1);
6326 processPosition(mapper, 100, 200);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6330 ASSERT_EQ(0, motionArgs.buttonState);
6331
6332 // press BTN_LEFT, release BTN_LEFT
6333 processKey(mapper, BTN_LEFT, 1);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6337 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6338
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6340 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6341 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6342
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 processKey(mapper, BTN_LEFT, 0);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006348
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006351 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352
6353 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6354 processKey(mapper, BTN_RIGHT, 1);
6355 processKey(mapper, BTN_MIDDLE, 1);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6359 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6360 motionArgs.buttonState);
6361
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6364 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6365
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6368 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6369 motionArgs.buttonState);
6370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 processKey(mapper, BTN_RIGHT, 0);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006379 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380
6381 processKey(mapper, BTN_MIDDLE, 0);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390
6391 // press BTN_BACK, release BTN_BACK
6392 processKey(mapper, BTN_BACK, 1);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6395 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6396 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6401
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6404 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405
6406 processKey(mapper, BTN_BACK, 0);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006414 ASSERT_EQ(0, motionArgs.buttonState);
6415
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6417 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6418 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6419
6420 // press BTN_SIDE, release BTN_SIDE
6421 processKey(mapper, BTN_SIDE, 1);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6424 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6425 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006426
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6430
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6433 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434
6435 processKey(mapper, BTN_SIDE, 0);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006443 ASSERT_EQ(0, motionArgs.buttonState);
6444
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6446 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6447 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6448
6449 // press BTN_FORWARD, release BTN_FORWARD
6450 processKey(mapper, BTN_FORWARD, 1);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6453 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6454 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006455
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6459
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6462 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463
6464 processKey(mapper, BTN_FORWARD, 0);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006468 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006469
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006472 ASSERT_EQ(0, motionArgs.buttonState);
6473
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6475 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6476 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6477
6478 // press BTN_EXTRA, release BTN_EXTRA
6479 processKey(mapper, BTN_EXTRA, 1);
6480 processSync(mapper);
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6482 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6483 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006487 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6488
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6491 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492
6493 processKey(mapper, BTN_EXTRA, 0);
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006497 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006498
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006501 ASSERT_EQ(0, motionArgs.buttonState);
6502
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6504 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6505 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6506
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6508
Michael Wrightd02c5b62014-02-10 15:10:22 -08006509 // press BTN_STYLUS, release BTN_STYLUS
6510 processKey(mapper, BTN_STYLUS, 1);
6511 processSync(mapper);
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006514 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6515
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6518 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006519
6520 processKey(mapper, BTN_STYLUS, 0);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006525
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006528 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529
6530 // press BTN_STYLUS2, release BTN_STYLUS2
6531 processKey(mapper, BTN_STYLUS2, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006535 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6536
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6539 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006540
6541 processKey(mapper, BTN_STYLUS2, 0);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006545 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006546
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006549 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550
6551 // release touch
6552 processId(mapper, -1);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6556 ASSERT_EQ(0, motionArgs.buttonState);
6557}
6558
6559TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560 addConfigurationProperty("touch.deviceType", "touchScreen");
6561 prepareDisplay(DISPLAY_ORIENTATION_0);
6562 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006563 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006564
6565 NotifyMotionArgs motionArgs;
6566
6567 // default tool type is finger
6568 processId(mapper, 1);
6569 processPosition(mapper, 100, 200);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6574
6575 // eraser
6576 processKey(mapper, BTN_TOOL_RUBBER, 1);
6577 processSync(mapper);
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6581
6582 // stylus
6583 processKey(mapper, BTN_TOOL_RUBBER, 0);
6584 processKey(mapper, BTN_TOOL_PEN, 1);
6585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6589
6590 // brush
6591 processKey(mapper, BTN_TOOL_PEN, 0);
6592 processKey(mapper, BTN_TOOL_BRUSH, 1);
6593 processSync(mapper);
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6597
6598 // pencil
6599 processKey(mapper, BTN_TOOL_BRUSH, 0);
6600 processKey(mapper, BTN_TOOL_PENCIL, 1);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6605
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006606 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607 processKey(mapper, BTN_TOOL_PENCIL, 0);
6608 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6613
6614 // mouse
6615 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6616 processKey(mapper, BTN_TOOL_MOUSE, 1);
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6621
6622 // lens
6623 processKey(mapper, BTN_TOOL_MOUSE, 0);
6624 processKey(mapper, BTN_TOOL_LENS, 1);
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_MOUSE, motionArgs.pointerProperties[0].toolType);
6629
6630 // double-tap
6631 processKey(mapper, BTN_TOOL_LENS, 0);
6632 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6633 processSync(mapper);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6637
6638 // triple-tap
6639 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6640 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6645
6646 // quad-tap
6647 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6648 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6653
6654 // finger
6655 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6656 processKey(mapper, BTN_TOOL_FINGER, 1);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6661
6662 // stylus trumps finger
6663 processKey(mapper, BTN_TOOL_PEN, 1);
6664 processSync(mapper);
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6668
6669 // eraser trumps stylus
6670 processKey(mapper, BTN_TOOL_RUBBER, 1);
6671 processSync(mapper);
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6675
6676 // mouse trumps eraser
6677 processKey(mapper, BTN_TOOL_MOUSE, 1);
6678 processSync(mapper);
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6682
6683 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6684 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6685 processSync(mapper);
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6689
6690 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6691 processToolType(mapper, MT_TOOL_PEN);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6696
6697 // back to default tool type
6698 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6699 processKey(mapper, BTN_TOOL_MOUSE, 0);
6700 processKey(mapper, BTN_TOOL_RUBBER, 0);
6701 processKey(mapper, BTN_TOOL_PEN, 0);
6702 processKey(mapper, BTN_TOOL_FINGER, 0);
6703 processSync(mapper);
6704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6707}
6708
6709TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710 addConfigurationProperty("touch.deviceType", "touchScreen");
6711 prepareDisplay(DISPLAY_ORIENTATION_0);
6712 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006713 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006714 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006715
6716 NotifyMotionArgs motionArgs;
6717
6718 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6719 processId(mapper, 1);
6720 processPosition(mapper, 100, 200);
6721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6723 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6725 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6726
6727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6728 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6730 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6731
6732 // move a little
6733 processPosition(mapper, 150, 250);
6734 processSync(mapper);
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6736 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6738 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6739
6740 // down when BTN_TOUCH is pressed, pressure defaults to 1
6741 processKey(mapper, BTN_TOUCH, 1);
6742 processSync(mapper);
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6744 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6746 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6747
6748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6749 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6751 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6752
6753 // up when BTN_TOUCH is released, hover restored
6754 processKey(mapper, BTN_TOUCH, 0);
6755 processSync(mapper);
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6760
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6762 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6764 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6765
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6767 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6769 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6770
6771 // exit hover when pointer goes away
6772 processId(mapper, -1);
6773 processSync(mapper);
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6777 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6778}
6779
6780TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006781 addConfigurationProperty("touch.deviceType", "touchScreen");
6782 prepareDisplay(DISPLAY_ORIENTATION_0);
6783 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006784 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006785
6786 NotifyMotionArgs motionArgs;
6787
6788 // initially hovering because pressure is 0
6789 processId(mapper, 1);
6790 processPosition(mapper, 100, 200);
6791 processPressure(mapper, 0);
6792 processSync(mapper);
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6794 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6796 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6797
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6799 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6801 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6802
6803 // move a little
6804 processPosition(mapper, 150, 250);
6805 processSync(mapper);
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6809 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6810
6811 // down when pressure becomes non-zero
6812 processPressure(mapper, RAW_PRESSURE_MAX);
6813 processSync(mapper);
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6815 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6817 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6818
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6820 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6822 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6823
6824 // up when pressure becomes 0, hover restored
6825 processPressure(mapper, 0);
6826 processSync(mapper);
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6828 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6830 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6831
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6835 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6836
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6838 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6840 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6841
6842 // exit hover when pointer goes away
6843 processId(mapper, -1);
6844 processSync(mapper);
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6846 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6848 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6849}
6850
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006851/**
6852 * Set the input device port <--> display port associations, and check that the
6853 * events are routed to the display that matches the display port.
6854 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6855 */
6856TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006857 const std::string usb2 = "USB2";
6858 const uint8_t hdmi1 = 0;
6859 const uint8_t hdmi2 = 1;
6860 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006861 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006862
6863 addConfigurationProperty("touch.deviceType", "touchScreen");
6864 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006865 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006866
6867 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6868 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6869
6870 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6871 // for this input device is specified, and the matching viewport is not present,
6872 // the input device should be disabled (at the mapper level).
6873
6874 // Add viewport for display 2 on hdmi2
6875 prepareSecondaryDisplay(type, hdmi2);
6876 // Send a touch event
6877 processPosition(mapper, 100, 100);
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6880
6881 // Add viewport for display 1 on hdmi1
6882 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6883 // Send a touch event again
6884 processPosition(mapper, 100, 100);
6885 processSync(mapper);
6886
6887 NotifyMotionArgs args;
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6889 ASSERT_EQ(DISPLAY_ID, args.displayId);
6890}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006891
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006892TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006893 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006894 std::shared_ptr<FakePointerController> fakePointerController =
6895 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006896 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006897 fakePointerController->setPosition(100, 200);
6898 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006899 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6900
Garfield Tan888a6a42020-01-09 11:39:16 -08006901 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006902 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006903
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006904 prepareDisplay(DISPLAY_ORIENTATION_0);
6905 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006906 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006907
6908 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006909 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006910
6911 NotifyMotionArgs motionArgs;
6912 processPosition(mapper, 100, 100);
6913 processSync(mapper);
6914
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6916 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6917 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6918}
6919
Arthur Hung7c645402019-01-25 17:45:42 +08006920TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6921 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006922 prepareAxes(POSITION | ID | SLOT);
6923 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006924 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006925
6926 // Create the second touch screen device, and enable multi fingers.
6927 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006928 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006929 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006930 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006931 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006932 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006933 std::unique_ptr<InputDevice> device2 =
6934 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006935 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07006936 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME,
6937 Flags<InputDeviceClass>(0) /*classes*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006938 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6939 0 /*flat*/, 0 /*fuzz*/);
6940 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6941 0 /*flat*/, 0 /*fuzz*/);
6942 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6943 0 /*flat*/, 0 /*fuzz*/);
6944 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6945 0 /*flat*/, 0 /*fuzz*/);
6946 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6947 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6948 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006949
6950 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006951 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006952 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6953 device2->reset(ARBITRARY_TIME);
6954
6955 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006956 std::shared_ptr<FakePointerController> fakePointerController =
6957 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006958 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6959 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6960
6961 // Setup policy for associated displays and show touches.
6962 const uint8_t hdmi1 = 0;
6963 const uint8_t hdmi2 = 1;
6964 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6965 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6966 mFakePolicy->setShowTouches(true);
6967
6968 // Create displays.
6969 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006970 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006971
6972 // Default device will reconfigure above, need additional reconfiguration for another device.
6973 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006974 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006975
6976 // Two fingers down at default display.
6977 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6978 processPosition(mapper, x1, y1);
6979 processId(mapper, 1);
6980 processSlot(mapper, 1);
6981 processPosition(mapper, x2, y2);
6982 processId(mapper, 2);
6983 processSync(mapper);
6984
6985 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6986 fakePointerController->getSpots().find(DISPLAY_ID);
6987 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6988 ASSERT_EQ(size_t(2), iter->second.size());
6989
6990 // Two fingers down at second display.
6991 processPosition(mapper2, x1, y1);
6992 processId(mapper2, 1);
6993 processSlot(mapper2, 1);
6994 processPosition(mapper2, x2, y2);
6995 processId(mapper2, 2);
6996 processSync(mapper2);
6997
6998 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6999 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7000 ASSERT_EQ(size_t(2), iter->second.size());
7001}
7002
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007003TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007004 prepareAxes(POSITION);
7005 addConfigurationProperty("touch.deviceType", "touchScreen");
7006 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007008
7009 NotifyMotionArgs motionArgs;
7010 // Unrotated video frame
7011 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7012 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007013 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007014 processPosition(mapper, 100, 200);
7015 processSync(mapper);
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7017 ASSERT_EQ(frames, motionArgs.videoFrames);
7018
7019 // Subsequent touch events should not have any videoframes
7020 // This is implemented separately in FakeEventHub,
7021 // but that should match the behaviour of TouchVideoDevice.
7022 processPosition(mapper, 200, 200);
7023 processSync(mapper);
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7025 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7026}
7027
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007028TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007029 prepareAxes(POSITION);
7030 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007031 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007032 // Unrotated video frame
7033 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7034 NotifyMotionArgs motionArgs;
7035
7036 // Test all 4 orientations
7037 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7038 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7039 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7040 clearViewports();
7041 prepareDisplay(orientation);
7042 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007043 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007044 processPosition(mapper, 100, 200);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7047 frames[0].rotate(orientation);
7048 ASSERT_EQ(frames, motionArgs.videoFrames);
7049 }
7050}
7051
7052TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007053 prepareAxes(POSITION);
7054 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007055 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007056 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7057 // so mix these.
7058 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7059 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7060 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7061 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7062 NotifyMotionArgs motionArgs;
7063
7064 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007065 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007066 processPosition(mapper, 100, 200);
7067 processSync(mapper);
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7069 std::for_each(frames.begin(), frames.end(),
7070 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7071 ASSERT_EQ(frames, motionArgs.videoFrames);
7072}
7073
Arthur Hung9da14732019-09-02 16:16:58 +08007074/**
7075 * If we had defined port associations, but the viewport is not ready, the touch device would be
7076 * expected to be disabled, and it should be enabled after the viewport has found.
7077 */
7078TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007079 constexpr uint8_t hdmi2 = 1;
7080 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007081 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007082
7083 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7084
7085 addConfigurationProperty("touch.deviceType", "touchScreen");
7086 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007088
7089 ASSERT_EQ(mDevice->isEnabled(), false);
7090
7091 // Add display on hdmi2, the device should be enabled and can receive touch event.
7092 prepareSecondaryDisplay(type, hdmi2);
7093 ASSERT_EQ(mDevice->isEnabled(), true);
7094
7095 // Send a touch event.
7096 processPosition(mapper, 100, 100);
7097 processSync(mapper);
7098
7099 NotifyMotionArgs args;
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7101 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7102}
7103
Arthur Hung6cd19a42019-08-30 19:04:12 +08007104
Arthur Hung6cd19a42019-08-30 19:04:12 +08007105
Arthur Hung421eb1c2020-01-16 00:09:42 +08007106TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007107 addConfigurationProperty("touch.deviceType", "touchScreen");
7108 prepareDisplay(DISPLAY_ORIENTATION_0);
7109 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007110 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007111
7112 NotifyMotionArgs motionArgs;
7113
7114 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7115 // finger down
7116 processId(mapper, 1);
7117 processPosition(mapper, x1, y1);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7122
7123 // finger move
7124 processId(mapper, 1);
7125 processPosition(mapper, x2, y2);
7126 processSync(mapper);
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7130
7131 // finger up.
7132 processId(mapper, -1);
7133 processSync(mapper);
7134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7135 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7137
7138 // new finger down
7139 processId(mapper, 1);
7140 processPosition(mapper, x3, y3);
7141 processSync(mapper);
7142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7145}
7146
7147/**
arthurhungcc7f9802020-04-30 17:55:40 +08007148 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7149 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007150 */
arthurhungcc7f9802020-04-30 17:55:40 +08007151TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007152 addConfigurationProperty("touch.deviceType", "touchScreen");
7153 prepareDisplay(DISPLAY_ORIENTATION_0);
7154 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007155 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007156
7157 NotifyMotionArgs motionArgs;
7158
7159 // default tool type is finger
7160 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007161 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007162 processPosition(mapper, x1, y1);
7163 processSync(mapper);
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7165 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7167
7168 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7169 processToolType(mapper, MT_TOOL_PALM);
7170 processSync(mapper);
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7172 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7173
7174 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007175 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007176 processPosition(mapper, x2, y2);
7177 processSync(mapper);
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7179
7180 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007181 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007182 processSync(mapper);
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7184
7185 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007186 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007187 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007188 processPosition(mapper, x3, y3);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7193}
7194
arthurhungbf89a482020-04-17 17:37:55 +08007195/**
arthurhungcc7f9802020-04-30 17:55:40 +08007196 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7197 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007198 */
arthurhungcc7f9802020-04-30 17:55:40 +08007199TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007200 addConfigurationProperty("touch.deviceType", "touchScreen");
7201 prepareDisplay(DISPLAY_ORIENTATION_0);
7202 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7203 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7204
7205 NotifyMotionArgs motionArgs;
7206
7207 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007208 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7209 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007210 processPosition(mapper, x1, y1);
7211 processSync(mapper);
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7213 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7215
7216 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007217 processSlot(mapper, SECOND_SLOT);
7218 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007219 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007220 processSync(mapper);
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7222 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7223 motionArgs.action);
7224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7225
7226 // If the tool type of the first finger changes to MT_TOOL_PALM,
7227 // we expect to receive ACTION_POINTER_UP with cancel flag.
7228 processSlot(mapper, FIRST_SLOT);
7229 processId(mapper, FIRST_TRACKING_ID);
7230 processToolType(mapper, MT_TOOL_PALM);
7231 processSync(mapper);
7232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7233 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7234 motionArgs.action);
7235 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7236
7237 // The following MOVE events of second finger should be processed.
7238 processSlot(mapper, SECOND_SLOT);
7239 processId(mapper, SECOND_TRACKING_ID);
7240 processPosition(mapper, x2 + 1, y2 + 1);
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7244 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7245
7246 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7247 // it. Second finger receive move.
7248 processSlot(mapper, FIRST_SLOT);
7249 processId(mapper, INVALID_TRACKING_ID);
7250 processSync(mapper);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7253 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7254
7255 // Second finger keeps moving.
7256 processSlot(mapper, SECOND_SLOT);
7257 processId(mapper, SECOND_TRACKING_ID);
7258 processPosition(mapper, x2 + 2, y2 + 2);
7259 processSync(mapper);
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7262 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7263
7264 // Second finger up.
7265 processId(mapper, INVALID_TRACKING_ID);
7266 processSync(mapper);
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7268 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7269 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7270}
7271
7272/**
7273 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7274 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7275 */
7276TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7277 addConfigurationProperty("touch.deviceType", "touchScreen");
7278 prepareDisplay(DISPLAY_ORIENTATION_0);
7279 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7280 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7281
7282 NotifyMotionArgs motionArgs;
7283
7284 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7285 // First finger down.
7286 processId(mapper, FIRST_TRACKING_ID);
7287 processPosition(mapper, x1, y1);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7292
7293 // Second finger down.
7294 processSlot(mapper, SECOND_SLOT);
7295 processId(mapper, SECOND_TRACKING_ID);
7296 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007297 processSync(mapper);
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7299 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7300 motionArgs.action);
7301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7302
arthurhungcc7f9802020-04-30 17:55:40 +08007303 // If the tool type of the first finger changes to MT_TOOL_PALM,
7304 // we expect to receive ACTION_POINTER_UP with cancel flag.
7305 processSlot(mapper, FIRST_SLOT);
7306 processId(mapper, FIRST_TRACKING_ID);
7307 processToolType(mapper, MT_TOOL_PALM);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7310 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7311 motionArgs.action);
7312 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7313
7314 // Second finger keeps moving.
7315 processSlot(mapper, SECOND_SLOT);
7316 processId(mapper, SECOND_TRACKING_ID);
7317 processPosition(mapper, x2 + 1, y2 + 1);
7318 processSync(mapper);
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7321
7322 // second finger becomes palm, receive cancel due to only 1 finger is active.
7323 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007324 processToolType(mapper, MT_TOOL_PALM);
7325 processSync(mapper);
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7327 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7328
arthurhungcc7f9802020-04-30 17:55:40 +08007329 // third finger down.
7330 processSlot(mapper, THIRD_SLOT);
7331 processId(mapper, THIRD_TRACKING_ID);
7332 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007333 processPosition(mapper, x3, y3);
7334 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7336 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007338 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7339
7340 // third finger move
7341 processId(mapper, THIRD_TRACKING_ID);
7342 processPosition(mapper, x3 + 1, y3 + 1);
7343 processSync(mapper);
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7346
7347 // first finger up, third finger receive move.
7348 processSlot(mapper, FIRST_SLOT);
7349 processId(mapper, INVALID_TRACKING_ID);
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7353 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7354
7355 // second finger up, third finger receive move.
7356 processSlot(mapper, SECOND_SLOT);
7357 processId(mapper, INVALID_TRACKING_ID);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7361 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7362
7363 // third finger up.
7364 processSlot(mapper, THIRD_SLOT);
7365 processId(mapper, INVALID_TRACKING_ID);
7366 processSync(mapper);
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7368 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7369 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7370}
7371
7372/**
7373 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7374 * and the active finger could still be allowed to receive the events
7375 */
7376TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7377 addConfigurationProperty("touch.deviceType", "touchScreen");
7378 prepareDisplay(DISPLAY_ORIENTATION_0);
7379 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7380 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7381
7382 NotifyMotionArgs motionArgs;
7383
7384 // default tool type is finger
7385 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7386 processId(mapper, FIRST_TRACKING_ID);
7387 processPosition(mapper, x1, y1);
7388 processSync(mapper);
7389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7390 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7392
7393 // Second finger down.
7394 processSlot(mapper, SECOND_SLOT);
7395 processId(mapper, SECOND_TRACKING_ID);
7396 processPosition(mapper, x2, y2);
7397 processSync(mapper);
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7399 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7400 motionArgs.action);
7401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7402
7403 // If the tool type of the second finger changes to MT_TOOL_PALM,
7404 // we expect to receive ACTION_POINTER_UP with cancel flag.
7405 processId(mapper, SECOND_TRACKING_ID);
7406 processToolType(mapper, MT_TOOL_PALM);
7407 processSync(mapper);
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7409 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7410 motionArgs.action);
7411 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7412
7413 // The following MOVE event should be processed.
7414 processSlot(mapper, FIRST_SLOT);
7415 processId(mapper, FIRST_TRACKING_ID);
7416 processPosition(mapper, x1 + 1, y1 + 1);
7417 processSync(mapper);
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7420 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7421
7422 // second finger up.
7423 processSlot(mapper, SECOND_SLOT);
7424 processId(mapper, INVALID_TRACKING_ID);
7425 processSync(mapper);
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7428
7429 // first finger keep moving
7430 processSlot(mapper, FIRST_SLOT);
7431 processId(mapper, FIRST_TRACKING_ID);
7432 processPosition(mapper, x1 + 2, y1 + 2);
7433 processSync(mapper);
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7436
7437 // first finger up.
7438 processId(mapper, INVALID_TRACKING_ID);
7439 processSync(mapper);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7442 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007443}
7444
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007445// --- MultiTouchInputMapperTest_ExternalDevice ---
7446
7447class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7448protected:
7449 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07007450 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007451 }
7452};
7453
7454/**
7455 * Expect fallback to internal viewport if device is external and external viewport is not present.
7456 */
7457TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7458 prepareAxes(POSITION);
7459 addConfigurationProperty("touch.deviceType", "touchScreen");
7460 prepareDisplay(DISPLAY_ORIENTATION_0);
7461 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7462
7463 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7464
7465 NotifyMotionArgs motionArgs;
7466
7467 // Expect the event to be sent to the internal viewport,
7468 // because an external viewport is not present.
7469 processPosition(mapper, 100, 100);
7470 processSync(mapper);
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7472 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7473
7474 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007475 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007476 processPosition(mapper, 100, 100);
7477 processSync(mapper);
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7479 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7480}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007481
7482/**
7483 * Test touch should not work if outside of surface.
7484 */
7485class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7486protected:
7487 void halfDisplayToCenterHorizontal(int32_t orientation) {
7488 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007489 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007490
7491 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7492 internalViewport->orientation = orientation;
7493 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7494 internalViewport->logicalLeft = 0;
7495 internalViewport->logicalTop = 0;
7496 internalViewport->logicalRight = DISPLAY_HEIGHT;
7497 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7498
7499 internalViewport->physicalLeft = 0;
7500 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7501 internalViewport->physicalRight = DISPLAY_HEIGHT;
7502 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7503
7504 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7505 internalViewport->deviceHeight = DISPLAY_WIDTH;
7506 } else {
7507 internalViewport->logicalLeft = 0;
7508 internalViewport->logicalTop = 0;
7509 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7510 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7511
7512 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7513 internalViewport->physicalTop = 0;
7514 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7515 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7516
7517 internalViewport->deviceWidth = DISPLAY_WIDTH;
7518 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7519 }
7520
7521 mFakePolicy->updateViewport(internalViewport.value());
7522 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7523 }
7524
7525 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7526 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7527 int32_t yExpected) {
7528 // touch on outside area should not work.
7529 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7530 processSync(mapper);
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7532
7533 // touch on inside area should receive the event.
7534 NotifyMotionArgs args;
7535 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7536 processSync(mapper);
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7538 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7539 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7540
7541 // Reset.
7542 mapper.reset(ARBITRARY_TIME);
7543 }
7544};
7545
7546TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7547 addConfigurationProperty("touch.deviceType", "touchScreen");
7548 prepareDisplay(DISPLAY_ORIENTATION_0);
7549 prepareAxes(POSITION);
7550 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7551
7552 // Touch on center of normal display should work.
7553 const int32_t x = DISPLAY_WIDTH / 4;
7554 const int32_t y = DISPLAY_HEIGHT / 2;
7555 processPosition(mapper, toRawX(x), toRawY(y));
7556 processSync(mapper);
7557 NotifyMotionArgs args;
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7560 0.0f, 0.0f, 0.0f, 0.0f));
7561 // Reset.
7562 mapper.reset(ARBITRARY_TIME);
7563
7564 // Let physical display be different to device, and make surface and physical could be 1:1.
7565 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7566
7567 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7568 const int32_t yExpected = y;
7569 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7570}
7571
7572TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7573 addConfigurationProperty("touch.deviceType", "touchScreen");
7574 prepareDisplay(DISPLAY_ORIENTATION_0);
7575 prepareAxes(POSITION);
7576 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7577
7578 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7579 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7580
7581 const int32_t x = DISPLAY_WIDTH / 4;
7582 const int32_t y = DISPLAY_HEIGHT / 2;
7583
7584 // expect x/y = swap x/y then reverse y.
7585 const int32_t xExpected = y;
7586 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7587 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7588}
7589
7590TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7591 addConfigurationProperty("touch.deviceType", "touchScreen");
7592 prepareDisplay(DISPLAY_ORIENTATION_0);
7593 prepareAxes(POSITION);
7594 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7595
7596 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7597 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7598
7599 const int32_t x = DISPLAY_WIDTH / 4;
7600 const int32_t y = DISPLAY_HEIGHT / 2;
7601
7602 // expect x/y = swap x/y then reverse x.
7603 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7604 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7605 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7606}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007607
7608TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7609 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7610 std::shared_ptr<FakePointerController> fakePointerController =
7611 std::make_shared<FakePointerController>();
7612 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7613 fakePointerController->setPosition(0, 0);
7614 fakePointerController->setButtonState(0);
7615
7616 // prepare device and capture
7617 prepareDisplay(DISPLAY_ORIENTATION_0);
7618 prepareAxes(POSITION | ID | SLOT);
7619 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7620 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7621 mFakePolicy->setPointerCapture(true);
7622 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7623 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7624
7625 // captured touchpad should be a touchpad source
7626 NotifyDeviceResetArgs resetArgs;
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7628 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7629
7630 // run captured pointer tests - note that this is unscaled, so input listener events should be
7631 // identical to what the hardware sends (accounting for any
7632 // calibration).
7633 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007634 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007635 processId(mapper, 1);
7636 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7637 processKey(mapper, BTN_TOUCH, 1);
7638 processSync(mapper);
7639
7640 // expect coord[0] to contain initial location of touch 0
7641 NotifyMotionArgs args;
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7643 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7644 ASSERT_EQ(1U, args.pointerCount);
7645 ASSERT_EQ(0, args.pointerProperties[0].id);
7646 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7647 ASSERT_NO_FATAL_FAILURE(
7648 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7649
7650 // FINGER 1 DOWN
7651 processSlot(mapper, 1);
7652 processId(mapper, 2);
7653 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7654 processSync(mapper);
7655
7656 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007658 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7659 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007660 ASSERT_EQ(2U, args.pointerCount);
7661 ASSERT_EQ(0, args.pointerProperties[0].id);
7662 ASSERT_EQ(1, args.pointerProperties[1].id);
7663 ASSERT_NO_FATAL_FAILURE(
7664 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7665 ASSERT_NO_FATAL_FAILURE(
7666 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7667
7668 // FINGER 1 MOVE
7669 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7670 processSync(mapper);
7671
7672 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7673 // from move
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7676 ASSERT_NO_FATAL_FAILURE(
7677 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7678 ASSERT_NO_FATAL_FAILURE(
7679 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7680
7681 // FINGER 0 MOVE
7682 processSlot(mapper, 0);
7683 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7684 processSync(mapper);
7685
7686 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7689 ASSERT_NO_FATAL_FAILURE(
7690 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7691 ASSERT_NO_FATAL_FAILURE(
7692 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7693
7694 // BUTTON DOWN
7695 processKey(mapper, BTN_LEFT, 1);
7696 processSync(mapper);
7697
7698 // touchinputmapper design sends a move before button press
7699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7703
7704 // BUTTON UP
7705 processKey(mapper, BTN_LEFT, 0);
7706 processSync(mapper);
7707
7708 // touchinputmapper design sends a move after button release
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7710 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7713
7714 // FINGER 0 UP
7715 processId(mapper, -1);
7716 processSync(mapper);
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7718 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7719
7720 // FINGER 1 MOVE
7721 processSlot(mapper, 1);
7722 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7723 processSync(mapper);
7724
7725 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7728 ASSERT_EQ(1U, args.pointerCount);
7729 ASSERT_EQ(1, args.pointerProperties[0].id);
7730 ASSERT_NO_FATAL_FAILURE(
7731 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7732
7733 // FINGER 1 UP
7734 processId(mapper, -1);
7735 processKey(mapper, BTN_TOUCH, 0);
7736 processSync(mapper);
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7738 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7739
7740 // non captured touchpad should be a mouse source
7741 mFakePolicy->setPointerCapture(false);
7742 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7744 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7745}
7746
7747TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7748 std::shared_ptr<FakePointerController> fakePointerController =
7749 std::make_shared<FakePointerController>();
7750 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7751 fakePointerController->setPosition(0, 0);
7752 fakePointerController->setButtonState(0);
7753
7754 // prepare device and capture
7755 prepareDisplay(DISPLAY_ORIENTATION_0);
7756 prepareAxes(POSITION | ID | SLOT);
7757 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7758 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7759 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7760 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7761 // run uncaptured pointer tests - pushes out generic events
7762 // FINGER 0 DOWN
7763 processId(mapper, 3);
7764 processPosition(mapper, 100, 100);
7765 processKey(mapper, BTN_TOUCH, 1);
7766 processSync(mapper);
7767
7768 // start at (100,100), cursor should be at (0,0) * scale
7769 NotifyMotionArgs args;
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7771 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7772 ASSERT_NO_FATAL_FAILURE(
7773 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7774
7775 // FINGER 0 MOVE
7776 processPosition(mapper, 200, 200);
7777 processSync(mapper);
7778
7779 // compute scaling to help with touch position checking
7780 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7781 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7782 float scale =
7783 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7784
7785 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7789 0, 0, 0, 0, 0, 0, 0));
7790}
7791
7792TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7793 std::shared_ptr<FakePointerController> fakePointerController =
7794 std::make_shared<FakePointerController>();
7795
7796 prepareDisplay(DISPLAY_ORIENTATION_0);
7797 prepareAxes(POSITION | ID | SLOT);
7798 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7799 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7800 mFakePolicy->setPointerCapture(false);
7801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7802
7803 // uncaptured touchpad should be a pointer device
7804 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7805
7806 // captured touchpad should be a touchpad device
7807 mFakePolicy->setPointerCapture(true);
7808 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7809 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7810}
7811
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812} // namespace android