blob: 9823a1cbefe30790fae6cf5bdb4e469d1f14ad0e [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);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002380 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 }
2382
2383 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002384 int32_t orientation, const std::string& uniqueId,
2385 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002386 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002387 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002388 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2389 }
2390
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002391 void clearViewports() {
2392 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393 }
2394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002395 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2396 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397 RawEvent event;
2398 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002399 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400 event.type = type;
2401 event.code = code;
2402 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002403 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404 }
2405
2406 static void assertMotionRange(const InputDeviceInfo& info,
2407 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2408 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002409 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2411 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2412 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2413 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2414 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2415 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2416 }
2417
2418 static void assertPointerCoords(const PointerCoords& coords,
2419 float x, float y, float pressure, float size,
2420 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2421 float orientation, float distance) {
2422 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2423 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2424 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2425 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2426 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2427 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2428 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2429 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2430 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2431 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2432 }
2433
Michael Wright17db18e2020-06-26 20:51:44 +01002434 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002436 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 ASSERT_NEAR(x, actualX, 1);
2438 ASSERT_NEAR(y, actualY, 1);
2439 }
2440};
2441
2442const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002443const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002444const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2446const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002447const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2448 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002449const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450
2451// --- SwitchInputMapperTest ---
2452
2453class SwitchInputMapperTest : public InputMapperTest {
2454protected:
2455};
2456
2457TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002458 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002460 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461}
2462
2463TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002464 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002466 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002467 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002469 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002470 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471}
2472
2473TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002476 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2477 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2478 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2479 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480
2481 NotifySwitchArgs args;
2482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2483 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002484 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2485 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002486 args.switchMask);
2487 ASSERT_EQ(uint32_t(0), args.policyFlags);
2488}
2489
2490
2491// --- KeyboardInputMapperTest ---
2492
2493class KeyboardInputMapperTest : public InputMapperTest {
2494protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002495 const std::string UNIQUE_ID = "local:0";
2496
2497 void prepareDisplay(int32_t orientation);
2498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002499 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002500 int32_t originalKeyCode, int32_t rotatedKeyCode,
2501 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502};
2503
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002504/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2505 * orientation.
2506 */
2507void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002508 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2509 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002510}
2511
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002512void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002513 int32_t originalScanCode, int32_t originalKeyCode,
2514 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 NotifyKeyArgs args;
2516
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002517 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2519 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2520 ASSERT_EQ(originalScanCode, args.scanCode);
2521 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002522 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002524 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2526 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2527 ASSERT_EQ(originalScanCode, args.scanCode);
2528 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002529 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530}
2531
Michael Wrightd02c5b62014-02-10 15:10:22 -08002532TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002533 KeyboardInputMapper& mapper =
2534 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2535 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002536
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002537 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538}
2539
2540TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2541 const int32_t USAGE_A = 0x070004;
2542 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2544 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002546 KeyboardInputMapper& mapper =
2547 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2548 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549
2550 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002551 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552 NotifyKeyArgs args;
2553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2554 ASSERT_EQ(DEVICE_ID, args.deviceId);
2555 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2556 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2557 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2558 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2559 ASSERT_EQ(KEY_HOME, args.scanCode);
2560 ASSERT_EQ(AMETA_NONE, args.metaState);
2561 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2562 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2563 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2564
2565 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002566 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2568 ASSERT_EQ(DEVICE_ID, args.deviceId);
2569 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2570 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2571 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2572 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2573 ASSERT_EQ(KEY_HOME, args.scanCode);
2574 ASSERT_EQ(AMETA_NONE, args.metaState);
2575 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2576 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2577 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2578
2579 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002580 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2581 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2583 ASSERT_EQ(DEVICE_ID, args.deviceId);
2584 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2585 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2586 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2587 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2588 ASSERT_EQ(0, args.scanCode);
2589 ASSERT_EQ(AMETA_NONE, args.metaState);
2590 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2591 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2592 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2593
2594 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002595 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2596 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2598 ASSERT_EQ(DEVICE_ID, args.deviceId);
2599 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2600 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2601 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2602 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2603 ASSERT_EQ(0, args.scanCode);
2604 ASSERT_EQ(AMETA_NONE, args.metaState);
2605 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2606 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2607 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2608
2609 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002610 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2611 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2613 ASSERT_EQ(DEVICE_ID, args.deviceId);
2614 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2615 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2616 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2617 ASSERT_EQ(0, args.keyCode);
2618 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2619 ASSERT_EQ(AMETA_NONE, args.metaState);
2620 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2621 ASSERT_EQ(0U, args.policyFlags);
2622 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2623
2624 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002625 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2626 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2628 ASSERT_EQ(DEVICE_ID, args.deviceId);
2629 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2630 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2631 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2632 ASSERT_EQ(0, args.keyCode);
2633 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2634 ASSERT_EQ(AMETA_NONE, args.metaState);
2635 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2636 ASSERT_EQ(0U, args.policyFlags);
2637 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2638}
2639
2640TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002641 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2642 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002644 KeyboardInputMapper& mapper =
2645 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2646 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
2648 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002649 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650
2651 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002652 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653 NotifyKeyArgs args;
2654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2655 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002656 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2658
2659 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002660 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2662 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002663 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664
2665 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002666 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2668 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002669 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670
2671 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002672 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2674 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002675 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2677}
2678
2679TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002680 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2681 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2682 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2683 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002685 KeyboardInputMapper& mapper =
2686 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2687 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002689 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2691 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2692 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2693 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2694 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2695 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2696 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2697 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2698}
2699
2700TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002701 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002707 KeyboardInputMapper& mapper =
2708 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2709 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002711 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002712 ASSERT_NO_FATAL_FAILURE(
2713 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2714 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2715 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2716 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2717 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2718 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2719 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002721 clearViewports();
2722 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002723 ASSERT_NO_FATAL_FAILURE(
2724 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2725 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2726 AKEYCODE_DPAD_UP, DISPLAY_ID));
2727 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2728 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2729 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2730 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002732 clearViewports();
2733 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002734 ASSERT_NO_FATAL_FAILURE(
2735 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2736 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2737 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2738 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2739 AKEYCODE_DPAD_UP, DISPLAY_ID));
2740 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2741 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002743 clearViewports();
2744 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002745 ASSERT_NO_FATAL_FAILURE(
2746 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2747 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2748 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2749 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2750 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2751 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2752 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753
2754 // Special case: if orientation changes while key is down, we still emit the same keycode
2755 // in the key up as we did in the key down.
2756 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002757 clearViewports();
2758 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002759 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2761 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2762 ASSERT_EQ(KEY_UP, args.scanCode);
2763 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2764
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002765 clearViewports();
2766 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002767 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2769 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2770 ASSERT_EQ(KEY_UP, args.scanCode);
2771 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2772}
2773
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002774TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2775 // If the keyboard is not orientation aware,
2776 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002777 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002778
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 KeyboardInputMapper& mapper =
2780 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2781 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002782 NotifyKeyArgs args;
2783
2784 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002785 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002787 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2789 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2790
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002791 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002792 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002794 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2796 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2797}
2798
2799TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2800 // If the keyboard is orientation aware,
2801 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002802 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002803
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002804 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 KeyboardInputMapper& mapper =
2806 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2807 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002808 NotifyKeyArgs args;
2809
2810 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2811 // ^--- already checked by the previous test
2812
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002813 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002814 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002815 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002817 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2819 ASSERT_EQ(DISPLAY_ID, args.displayId);
2820
2821 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002822 clearViewports();
2823 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002824 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002825 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002827 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2829 ASSERT_EQ(newDisplayId, args.displayId);
2830}
2831
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002833 KeyboardInputMapper& mapper =
2834 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2835 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002837 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002838 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002840 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002841 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842}
2843
2844TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002845 KeyboardInputMapper& mapper =
2846 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2847 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002849 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002850 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002852 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002853 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854}
2855
2856TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 KeyboardInputMapper& mapper =
2858 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2859 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002861 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862
2863 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2864 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002865 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866 ASSERT_TRUE(flags[0]);
2867 ASSERT_FALSE(flags[1]);
2868}
2869
2870TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002871 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2872 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2873 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2874 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2875 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2876 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002878 KeyboardInputMapper& mapper =
2879 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2880 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881
2882 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002883 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2884 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2885 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886
2887 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002888 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2889 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002890 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2891 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2892 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002893 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894
2895 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002896 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2897 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002898 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2899 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2900 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002901 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902
2903 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002904 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2905 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002906 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2907 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2908 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002909 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910
2911 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002912 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2913 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002914 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2915 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2916 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002917 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918
2919 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002920 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2921 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002922 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2923 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2924 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002925 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926
2927 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002928 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2929 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002930 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2931 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2932 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002933 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934}
2935
Arthur Hung2c9a3342019-07-23 14:18:59 +08002936TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2937 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002938 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2939 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2940 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2941 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002942
2943 // keyboard 2.
2944 const std::string USB2 = "USB2";
2945 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002946 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002947 InputDeviceIdentifier identifier;
2948 identifier.name = "KEYBOARD2";
2949 identifier.location = USB2;
2950 std::unique_ptr<InputDevice> device2 =
2951 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002952 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07002953 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME,
2954 Flags<InputDeviceClass>(0) /*classes*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002955 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2956 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2957 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2958 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002960 KeyboardInputMapper& mapper =
2961 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2962 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002964 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002965 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002966 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002967 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2968 device2->reset(ARBITRARY_TIME);
2969
2970 // Prepared displays and associated info.
2971 constexpr uint8_t hdmi1 = 0;
2972 constexpr uint8_t hdmi2 = 1;
2973 const std::string SECONDARY_UNIQUE_ID = "local:1";
2974
2975 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2976 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2977
2978 // No associated display viewport found, should disable the device.
2979 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2980 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2981 ASSERT_FALSE(device2->isEnabled());
2982
2983 // Prepare second display.
2984 constexpr int32_t newDisplayId = 2;
2985 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002986 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002987 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002988 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002989 // Default device will reconfigure above, need additional reconfiguration for another device.
2990 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2991 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2992
2993 // Device should be enabled after the associated display is found.
2994 ASSERT_TRUE(mDevice->isEnabled());
2995 ASSERT_TRUE(device2->isEnabled());
2996
2997 // Test pad key events
2998 ASSERT_NO_FATAL_FAILURE(
2999 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3000 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3001 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3002 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3003 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3004 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3005 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3006
3007 ASSERT_NO_FATAL_FAILURE(
3008 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3009 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3010 AKEYCODE_DPAD_RIGHT, newDisplayId));
3011 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3012 AKEYCODE_DPAD_DOWN, newDisplayId));
3013 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3014 AKEYCODE_DPAD_LEFT, newDisplayId));
3015}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003017// --- KeyboardInputMapperTest_ExternalDevice ---
3018
3019class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3020protected:
3021 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07003022 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003023 }
3024};
3025
3026TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003027 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3028 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003029
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003030 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3032 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3033 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003034
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003035 KeyboardInputMapper& mapper =
3036 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3037 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003038
3039 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3040 NotifyKeyArgs args;
3041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3042 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3043
3044 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3046 ASSERT_EQ(uint32_t(0), args.policyFlags);
3047
3048 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3050 ASSERT_EQ(uint32_t(0), args.policyFlags);
3051
3052 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3054 ASSERT_EQ(uint32_t(0), args.policyFlags);
3055
3056 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3058 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3059
3060 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3062 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3063}
3064
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003065TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003066 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003067
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003068 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3069 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3070 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003071
Powei Fengd041c5d2019-05-03 17:11:33 -07003072 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003073 KeyboardInputMapper& mapper =
3074 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3075 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003076
3077 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3078 NotifyKeyArgs args;
3079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3080 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3081
3082 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3084 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3085
3086 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3088 ASSERT_EQ(uint32_t(0), args.policyFlags);
3089
3090 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3092 ASSERT_EQ(uint32_t(0), args.policyFlags);
3093
3094 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3096 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3097
3098 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3100 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3101}
3102
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103// --- CursorInputMapperTest ---
3104
3105class CursorInputMapperTest : public InputMapperTest {
3106protected:
3107 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3108
Michael Wright17db18e2020-06-26 20:51:44 +01003109 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
Prabir Pradhan28efc192019-11-05 01:10:04 +00003111 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 InputMapperTest::SetUp();
3113
Michael Wright17db18e2020-06-26 20:51:44 +01003114 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003115 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 }
3117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003118 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3119 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003120
3121 void prepareDisplay(int32_t orientation) {
3122 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003123 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003124 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3125 orientation, uniqueId, NO_PORT, viewportType);
3126 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127};
3128
3129const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003131void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3132 int32_t originalY, int32_t rotatedX,
3133 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134 NotifyMotionArgs args;
3135
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003136 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3137 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3138 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3142 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3143 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3144 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3145}
3146
3147TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003149 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003151 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152}
3153
3154TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003156 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003158 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159}
3160
3161TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003163 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164
3165 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003166 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167
3168 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003169 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3170 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3172 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3173
3174 // When the bounds are set, then there should be a valid motion range.
3175 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3176
3177 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003178 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179
3180 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3181 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3182 1, 800 - 1, 0.0f, 0.0f));
3183 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3184 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3185 2, 480 - 1, 0.0f, 0.0f));
3186 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3187 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3188 0.0f, 1.0f, 0.0f, 0.0f));
3189}
3190
3191TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003193 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
3195 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197
3198 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3199 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3200 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3201 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3202 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3203 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3204 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3205 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3206 0.0f, 1.0f, 0.0f, 0.0f));
3207}
3208
3209TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003211 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212
3213 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3214
3215 NotifyMotionArgs args;
3216
3217 // Button press.
3218 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003219 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3220 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3222 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3223 ASSERT_EQ(DEVICE_ID, args.deviceId);
3224 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3225 ASSERT_EQ(uint32_t(0), args.policyFlags);
3226 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3227 ASSERT_EQ(0, args.flags);
3228 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3229 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3230 ASSERT_EQ(0, args.edgeFlags);
3231 ASSERT_EQ(uint32_t(1), args.pointerCount);
3232 ASSERT_EQ(0, args.pointerProperties[0].id);
3233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3235 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3236 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3237 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3238 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3239
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3241 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3242 ASSERT_EQ(DEVICE_ID, args.deviceId);
3243 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3244 ASSERT_EQ(uint32_t(0), args.policyFlags);
3245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3246 ASSERT_EQ(0, args.flags);
3247 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3248 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3249 ASSERT_EQ(0, args.edgeFlags);
3250 ASSERT_EQ(uint32_t(1), args.pointerCount);
3251 ASSERT_EQ(0, args.pointerProperties[0].id);
3252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3254 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3255 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3256 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3257 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3258
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003260 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3261 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3263 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3264 ASSERT_EQ(DEVICE_ID, args.deviceId);
3265 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3266 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3268 ASSERT_EQ(0, args.flags);
3269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3270 ASSERT_EQ(0, args.buttonState);
3271 ASSERT_EQ(0, args.edgeFlags);
3272 ASSERT_EQ(uint32_t(1), args.pointerCount);
3273 ASSERT_EQ(0, args.pointerProperties[0].id);
3274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3276 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3277 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3278 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3279 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3280
3281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3282 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3283 ASSERT_EQ(DEVICE_ID, args.deviceId);
3284 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3285 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3287 ASSERT_EQ(0, args.flags);
3288 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3289 ASSERT_EQ(0, args.buttonState);
3290 ASSERT_EQ(0, args.edgeFlags);
3291 ASSERT_EQ(uint32_t(1), args.pointerCount);
3292 ASSERT_EQ(0, args.pointerProperties[0].id);
3293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3295 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3296 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3297 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3298 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3299}
3300
3301TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003303 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304
3305 NotifyMotionArgs args;
3306
3307 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003308 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3309 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3313 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3314
3315 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003316 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3317 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3321 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3322}
3323
3324TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003326 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327
3328 NotifyMotionArgs args;
3329
3330 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003331 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3332 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3334 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3336 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3337
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3341 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3342
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003344 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3345 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3349 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3350
3351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3354 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3355}
3356
3357TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
3361 NotifyMotionArgs args;
3362
3363 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003364 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3365 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3366 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3367 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3371 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3372 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3373
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3377 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3378 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3379
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003381 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3382 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3383 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3387 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3388 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3389
3390 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003391 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3392 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3396 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3397
3398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3401 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3402}
3403
3404TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003406 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003408 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3410 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3411 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3412 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3413 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3414 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3415 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3416 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3417}
3418
3419TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 addConfigurationProperty("cursor.mode", "navigation");
3421 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003422 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003424 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3433
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003434 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3436 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3437 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3438 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3439 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3440 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3443
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003444 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3448 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3449 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3450 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3451 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3452 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3453
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003454 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3456 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3457 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3458 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3459 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3460 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3461 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3462 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3463}
3464
3465TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
3469 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3470 mFakePointerController->setPosition(100, 200);
3471 mFakePointerController->setButtonState(0);
3472
3473 NotifyMotionArgs motionArgs;
3474 NotifyKeyArgs keyArgs;
3475
3476 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003477 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3478 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3480 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3481 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3482 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3484 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3485
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3488 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3489 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3491 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3492
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003493 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3494 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497 ASSERT_EQ(0, motionArgs.buttonState);
3498 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3500 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3501
3502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003503 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504 ASSERT_EQ(0, motionArgs.buttonState);
3505 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3507 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3508
3509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003511 ASSERT_EQ(0, motionArgs.buttonState);
3512 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3514 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3515
3516 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003517 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3518 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3519 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3521 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3522 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3523 motionArgs.buttonState);
3524 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3525 mFakePointerController->getButtonState());
3526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3527 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3528
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3531 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3532 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3533 mFakePointerController->getButtonState());
3534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3535 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3536
3537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3539 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3540 motionArgs.buttonState);
3541 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3542 mFakePointerController->getButtonState());
3543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3544 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3545
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003546 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3547 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3551 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3553 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3554
3555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003557 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3558 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3560 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3561
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003562 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3563 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3566 ASSERT_EQ(0, motionArgs.buttonState);
3567 ASSERT_EQ(0, mFakePointerController->getButtonState());
3568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3569 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 -08003570 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3571 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003572
3573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574 ASSERT_EQ(0, motionArgs.buttonState);
3575 ASSERT_EQ(0, mFakePointerController->getButtonState());
3576 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3578 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 -08003579
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3581 ASSERT_EQ(0, motionArgs.buttonState);
3582 ASSERT_EQ(0, mFakePointerController->getButtonState());
3583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3585 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3586
3587 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003588 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3589 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3591 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3592 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003593
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3597 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3599 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3600
3601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3602 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3603 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3604 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3606 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3607
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003608 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3609 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003611 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612 ASSERT_EQ(0, motionArgs.buttonState);
3613 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3615 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3616
3617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003619 ASSERT_EQ(0, motionArgs.buttonState);
3620 ASSERT_EQ(0, mFakePointerController->getButtonState());
3621
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3623 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3625 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3626 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3627
3628 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003629 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3630 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3632 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3633 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003634
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003636 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3638 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3640 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3641
3642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3644 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3645 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3647 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3648
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003649 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3650 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003652 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653 ASSERT_EQ(0, motionArgs.buttonState);
3654 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3656 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003657
3658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3660 ASSERT_EQ(0, motionArgs.buttonState);
3661 ASSERT_EQ(0, mFakePointerController->getButtonState());
3662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3663 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3664
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3666 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3667 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3668
3669 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003670 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3671 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3673 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3674 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003675
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3679 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3681 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3682
3683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3685 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3686 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3688 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3689
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003690 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3691 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003693 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694 ASSERT_EQ(0, motionArgs.buttonState);
3695 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3697 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003698
3699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3701 ASSERT_EQ(0, motionArgs.buttonState);
3702 ASSERT_EQ(0, mFakePointerController->getButtonState());
3703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3704 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3705
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3707 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3708 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3709
3710 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003711 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3712 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3714 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3715 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003716
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3720 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3722 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3723
3724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3726 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3727 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3729 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3730
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003731 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3732 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735 ASSERT_EQ(0, motionArgs.buttonState);
3736 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3738 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003739
3740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3742 ASSERT_EQ(0, motionArgs.buttonState);
3743 ASSERT_EQ(0, mFakePointerController->getButtonState());
3744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3745 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3746
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3748 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3749 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3750}
3751
3752TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755
3756 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3757 mFakePointerController->setPosition(100, 200);
3758 mFakePointerController->setButtonState(0);
3759
3760 NotifyMotionArgs args;
3761
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003762 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3763 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3764 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003766 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3767 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3769 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 +01003770 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003771}
3772
3773TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003774 addConfigurationProperty("cursor.mode", "pointer");
3775 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003777
3778 NotifyDeviceResetArgs resetArgs;
3779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3780 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3781 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3782
3783 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3784 mFakePointerController->setPosition(100, 200);
3785 mFakePointerController->setButtonState(0);
3786
3787 NotifyMotionArgs args;
3788
3789 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003790 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3791 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3792 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3794 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3797 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 +01003798 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003799
3800 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003801 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3802 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3804 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3805 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3807 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3809 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3812 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3813
3814 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003815 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3816 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3818 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3819 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3821 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3823 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3824 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3826 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3827
3828 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003829 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3830 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3831 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3833 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3836 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 +01003837 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003838
3839 // Disable pointer capture and check that the device generation got bumped
3840 // and events are generated the usual way.
3841 const uint32_t generation = mFakeContext->getGeneration();
3842 mFakePolicy->setPointerCapture(false);
3843 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3844 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3845
3846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3847 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3848 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3849
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003850 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3851 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3852 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3854 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3857 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 +01003858 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859}
3860
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003861TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003862 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003863
Garfield Tan888a6a42020-01-09 11:39:16 -08003864 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003865 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003866 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3867 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003868 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003869 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3870 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3871
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003872 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3873 mFakePointerController->setPosition(100, 200);
3874 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003875
3876 NotifyMotionArgs args;
3877 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3878 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3879 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3881 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3882 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3884 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 +01003885 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003886 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3887}
3888
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889// --- TouchInputMapperTest ---
3890
3891class TouchInputMapperTest : public InputMapperTest {
3892protected:
3893 static const int32_t RAW_X_MIN;
3894 static const int32_t RAW_X_MAX;
3895 static const int32_t RAW_Y_MIN;
3896 static const int32_t RAW_Y_MAX;
3897 static const int32_t RAW_TOUCH_MIN;
3898 static const int32_t RAW_TOUCH_MAX;
3899 static const int32_t RAW_TOOL_MIN;
3900 static const int32_t RAW_TOOL_MAX;
3901 static const int32_t RAW_PRESSURE_MIN;
3902 static const int32_t RAW_PRESSURE_MAX;
3903 static const int32_t RAW_ORIENTATION_MIN;
3904 static const int32_t RAW_ORIENTATION_MAX;
3905 static const int32_t RAW_DISTANCE_MIN;
3906 static const int32_t RAW_DISTANCE_MAX;
3907 static const int32_t RAW_TILT_MIN;
3908 static const int32_t RAW_TILT_MAX;
3909 static const int32_t RAW_ID_MIN;
3910 static const int32_t RAW_ID_MAX;
3911 static const int32_t RAW_SLOT_MIN;
3912 static const int32_t RAW_SLOT_MAX;
3913 static const float X_PRECISION;
3914 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003915 static const float X_PRECISION_VIRTUAL;
3916 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917
3918 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003919 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920
3921 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3922
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003923 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003924 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003925
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926 enum Axes {
3927 POSITION = 1 << 0,
3928 TOUCH = 1 << 1,
3929 TOOL = 1 << 2,
3930 PRESSURE = 1 << 3,
3931 ORIENTATION = 1 << 4,
3932 MINOR = 1 << 5,
3933 ID = 1 << 6,
3934 DISTANCE = 1 << 7,
3935 TILT = 1 << 8,
3936 SLOT = 1 << 9,
3937 TOOL_TYPE = 1 << 10,
3938 };
3939
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003940 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3941 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003942 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003944 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 int32_t toRawX(float displayX);
3946 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003947 float toCookedX(float rawX, float rawY);
3948 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003950 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003952 float toDisplayY(int32_t rawY, int32_t displayHeight);
3953
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954};
3955
3956const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3957const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3958const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3959const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3960const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3961const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3962const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3963const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003964const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3965const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3967const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3968const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3969const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3970const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3971const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3972const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3973const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3974const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3975const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3976const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3977const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003978const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3979 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3980const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3981 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003982const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3983 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984
3985const float TouchInputMapperTest::GEOMETRIC_SCALE =
3986 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3987 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3988
3989const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3990 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3991 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3992};
3993
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003994void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003995 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3996 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003997}
3998
3999void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4000 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4001 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002}
4003
Santos Cordonfa5cf462017-04-05 10:37:00 -07004004void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004005 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4006 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4007 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004008}
4009
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004011 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4012 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4013 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015}
4016
Jason Gerecke489fda82012-09-07 17:19:40 -07004017void TouchInputMapperTest::prepareLocationCalibration() {
4018 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4019}
4020
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021int32_t TouchInputMapperTest::toRawX(float displayX) {
4022 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4023}
4024
4025int32_t TouchInputMapperTest::toRawY(float displayY) {
4026 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4027}
4028
Jason Gerecke489fda82012-09-07 17:19:40 -07004029float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4030 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4031 return rawX;
4032}
4033
4034float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4035 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4036 return rawY;
4037}
4038
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004040 return toDisplayX(rawX, DISPLAY_WIDTH);
4041}
4042
4043float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4044 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045}
4046
4047float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004048 return toDisplayY(rawY, DISPLAY_HEIGHT);
4049}
4050
4051float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4052 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053}
4054
4055
4056// --- SingleTouchInputMapperTest ---
4057
4058class SingleTouchInputMapperTest : public TouchInputMapperTest {
4059protected:
4060 void prepareButtons();
4061 void prepareAxes(int axes);
4062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4064 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4065 void processUp(SingleTouchInputMapper& mappery);
4066 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4067 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4068 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4069 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4070 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4071 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072};
4073
4074void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004075 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076}
4077
4078void SingleTouchInputMapperTest::prepareAxes(int axes) {
4079 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082 }
4083 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004084 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4085 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086 }
4087 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4089 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 }
4091 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4093 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 }
4095 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098 }
4099}
4100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004101void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004102 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4103 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4104 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105}
4106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004107void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004108 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4109 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110}
4111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004112void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004113 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114}
4115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004117 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118}
4119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004120void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4121 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004122 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123}
4124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004125void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004126 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127}
4128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004129void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4130 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004131 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4132 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133}
4134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4136 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004137 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138}
4139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004141 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142}
4143
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 prepareButtons();
4146 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004149 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150}
4151
4152TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004153 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4154 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 prepareButtons();
4156 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004157 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004159 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160}
4161
4162TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163 prepareButtons();
4164 prepareAxes(POSITION);
4165 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004166 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004168 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169}
4170
4171TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 prepareButtons();
4173 prepareAxes(POSITION);
4174 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004175 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004177 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178}
4179
4180TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 addConfigurationProperty("touch.deviceType", "touchScreen");
4182 prepareDisplay(DISPLAY_ORIENTATION_0);
4183 prepareButtons();
4184 prepareAxes(POSITION);
4185 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004186 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187
4188 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004189 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190
4191 // Virtual key is down.
4192 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4193 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4194 processDown(mapper, x, y);
4195 processSync(mapper);
4196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004198 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
4200 // Virtual key is up.
4201 processUp(mapper);
4202 processSync(mapper);
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004205 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206}
4207
4208TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 addConfigurationProperty("touch.deviceType", "touchScreen");
4210 prepareDisplay(DISPLAY_ORIENTATION_0);
4211 prepareButtons();
4212 prepareAxes(POSITION);
4213 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004214 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
4216 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004217 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218
4219 // Virtual key is down.
4220 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4221 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4222 processDown(mapper, x, y);
4223 processSync(mapper);
4224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227
4228 // Virtual key is up.
4229 processUp(mapper);
4230 processSync(mapper);
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004233 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234}
4235
4236TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 addConfigurationProperty("touch.deviceType", "touchScreen");
4238 prepareDisplay(DISPLAY_ORIENTATION_0);
4239 prepareButtons();
4240 prepareAxes(POSITION);
4241 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004242 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243
4244 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4245 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004246 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 ASSERT_TRUE(flags[0]);
4248 ASSERT_FALSE(flags[1]);
4249}
4250
4251TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 addConfigurationProperty("touch.deviceType", "touchScreen");
4253 prepareDisplay(DISPLAY_ORIENTATION_0);
4254 prepareButtons();
4255 prepareAxes(POSITION);
4256 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004257 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258
4259 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4260
4261 NotifyKeyArgs args;
4262
4263 // Press virtual key.
4264 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4265 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4266 processDown(mapper, x, y);
4267 processSync(mapper);
4268
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4270 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4271 ASSERT_EQ(DEVICE_ID, args.deviceId);
4272 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4273 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4274 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4275 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4276 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4277 ASSERT_EQ(KEY_HOME, args.scanCode);
4278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4279 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4280
4281 // Release virtual key.
4282 processUp(mapper);
4283 processSync(mapper);
4284
4285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4286 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4287 ASSERT_EQ(DEVICE_ID, args.deviceId);
4288 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4289 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4290 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4291 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4292 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4293 ASSERT_EQ(KEY_HOME, args.scanCode);
4294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4295 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4296
4297 // Should not have sent any motions.
4298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4299}
4300
4301TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 addConfigurationProperty("touch.deviceType", "touchScreen");
4303 prepareDisplay(DISPLAY_ORIENTATION_0);
4304 prepareButtons();
4305 prepareAxes(POSITION);
4306 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004307 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308
4309 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4310
4311 NotifyKeyArgs keyArgs;
4312
4313 // Press virtual key.
4314 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4315 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4316 processDown(mapper, x, y);
4317 processSync(mapper);
4318
4319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4320 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4321 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4322 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4323 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4324 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4325 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4326 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4327 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4329 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4330
4331 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4332 // into the display area.
4333 y -= 100;
4334 processMove(mapper, x, y);
4335 processSync(mapper);
4336
4337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4338 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4339 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4340 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4341 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4342 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4343 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4344 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4345 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4346 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4347 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4348 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4349
4350 NotifyMotionArgs motionArgs;
4351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4353 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4355 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4357 ASSERT_EQ(0, motionArgs.flags);
4358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4359 ASSERT_EQ(0, motionArgs.buttonState);
4360 ASSERT_EQ(0, motionArgs.edgeFlags);
4361 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4365 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4366 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4367 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4369
4370 // Keep moving out of bounds. Should generate a pointer move.
4371 y -= 50;
4372 processMove(mapper, x, y);
4373 processSync(mapper);
4374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4377 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4379 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4381 ASSERT_EQ(0, motionArgs.flags);
4382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4383 ASSERT_EQ(0, motionArgs.buttonState);
4384 ASSERT_EQ(0, motionArgs.edgeFlags);
4385 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4389 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4390 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4391 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4393
4394 // Release out of bounds. Should generate a pointer up.
4395 processUp(mapper);
4396 processSync(mapper);
4397
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4400 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4401 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4402 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4404 ASSERT_EQ(0, motionArgs.flags);
4405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4406 ASSERT_EQ(0, motionArgs.buttonState);
4407 ASSERT_EQ(0, motionArgs.edgeFlags);
4408 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4409 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4412 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4416
4417 // Should not have sent any more keys or motions.
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4420}
4421
4422TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 addConfigurationProperty("touch.deviceType", "touchScreen");
4424 prepareDisplay(DISPLAY_ORIENTATION_0);
4425 prepareButtons();
4426 prepareAxes(POSITION);
4427 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004428 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429
4430 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4431
4432 NotifyMotionArgs motionArgs;
4433
4434 // Initially go down out of bounds.
4435 int32_t x = -10;
4436 int32_t y = -10;
4437 processDown(mapper, x, y);
4438 processSync(mapper);
4439
4440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4441
4442 // Move into the display area. Should generate a pointer down.
4443 x = 50;
4444 y = 75;
4445 processMove(mapper, x, y);
4446 processSync(mapper);
4447
4448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4450 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4451 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4452 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4454 ASSERT_EQ(0, motionArgs.flags);
4455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4456 ASSERT_EQ(0, motionArgs.buttonState);
4457 ASSERT_EQ(0, motionArgs.edgeFlags);
4458 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4459 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4462 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4463 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4464 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4466
4467 // Release. Should generate a pointer up.
4468 processUp(mapper);
4469 processSync(mapper);
4470
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4473 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4474 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4475 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4476 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4477 ASSERT_EQ(0, motionArgs.flags);
4478 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4479 ASSERT_EQ(0, motionArgs.buttonState);
4480 ASSERT_EQ(0, motionArgs.edgeFlags);
4481 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4485 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4486 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4487 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4489
4490 // Should not have sent any more keys or motions.
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4493}
4494
Santos Cordonfa5cf462017-04-05 10:37:00 -07004495TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004496 addConfigurationProperty("touch.deviceType", "touchScreen");
4497 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4498
4499 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4500 prepareButtons();
4501 prepareAxes(POSITION);
4502 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004503 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004504
4505 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4506
4507 NotifyMotionArgs motionArgs;
4508
4509 // Down.
4510 int32_t x = 100;
4511 int32_t y = 125;
4512 processDown(mapper, x, y);
4513 processSync(mapper);
4514
4515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4516 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4517 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4518 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4519 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4520 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4522 ASSERT_EQ(0, motionArgs.flags);
4523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4524 ASSERT_EQ(0, motionArgs.buttonState);
4525 ASSERT_EQ(0, motionArgs.edgeFlags);
4526 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4530 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4531 1, 0, 0, 0, 0, 0, 0, 0));
4532 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4533 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4534 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4535
4536 // Move.
4537 x += 50;
4538 y += 75;
4539 processMove(mapper, x, y);
4540 processSync(mapper);
4541
4542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4544 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4545 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4546 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4547 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4549 ASSERT_EQ(0, motionArgs.flags);
4550 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4551 ASSERT_EQ(0, motionArgs.buttonState);
4552 ASSERT_EQ(0, motionArgs.edgeFlags);
4553 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4557 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4558 1, 0, 0, 0, 0, 0, 0, 0));
4559 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4560 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4562
4563 // Up.
4564 processUp(mapper);
4565 processSync(mapper);
4566
4567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4569 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4570 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4571 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4572 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4574 ASSERT_EQ(0, motionArgs.flags);
4575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4576 ASSERT_EQ(0, motionArgs.buttonState);
4577 ASSERT_EQ(0, motionArgs.edgeFlags);
4578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4582 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4583 1, 0, 0, 0, 0, 0, 0, 0));
4584 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4585 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4587
4588 // Should not have sent any more keys or motions.
4589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4591}
4592
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 addConfigurationProperty("touch.deviceType", "touchScreen");
4595 prepareDisplay(DISPLAY_ORIENTATION_0);
4596 prepareButtons();
4597 prepareAxes(POSITION);
4598 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004599 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600
4601 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4602
4603 NotifyMotionArgs motionArgs;
4604
4605 // Down.
4606 int32_t x = 100;
4607 int32_t y = 125;
4608 processDown(mapper, x, y);
4609 processSync(mapper);
4610
4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4613 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4614 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4615 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4617 ASSERT_EQ(0, motionArgs.flags);
4618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4619 ASSERT_EQ(0, motionArgs.buttonState);
4620 ASSERT_EQ(0, motionArgs.edgeFlags);
4621 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4622 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4625 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4626 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4627 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4629
4630 // Move.
4631 x += 50;
4632 y += 75;
4633 processMove(mapper, x, y);
4634 processSync(mapper);
4635
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4638 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4639 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4640 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4642 ASSERT_EQ(0, motionArgs.flags);
4643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4644 ASSERT_EQ(0, motionArgs.buttonState);
4645 ASSERT_EQ(0, motionArgs.edgeFlags);
4646 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4650 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4651 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4652 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4654
4655 // Up.
4656 processUp(mapper);
4657 processSync(mapper);
4658
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4661 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4662 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4663 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4664 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4665 ASSERT_EQ(0, motionArgs.flags);
4666 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4667 ASSERT_EQ(0, motionArgs.buttonState);
4668 ASSERT_EQ(0, motionArgs.edgeFlags);
4669 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4673 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4674 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4675 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4676 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4677
4678 // Should not have sent any more keys or motions.
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4681}
4682
4683TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 addConfigurationProperty("touch.deviceType", "touchScreen");
4685 prepareButtons();
4686 prepareAxes(POSITION);
4687 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004688 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689
4690 NotifyMotionArgs args;
4691
4692 // Rotation 90.
4693 prepareDisplay(DISPLAY_ORIENTATION_90);
4694 processDown(mapper, toRawX(50), toRawY(75));
4695 processSync(mapper);
4696
4697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4698 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4699 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4700
4701 processUp(mapper);
4702 processSync(mapper);
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4704}
4705
4706TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 addConfigurationProperty("touch.deviceType", "touchScreen");
4708 prepareButtons();
4709 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004710 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
4712 NotifyMotionArgs args;
4713
4714 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004715 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 prepareDisplay(DISPLAY_ORIENTATION_0);
4717 processDown(mapper, toRawX(50), toRawY(75));
4718 processSync(mapper);
4719
4720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4721 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4722 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4723
4724 processUp(mapper);
4725 processSync(mapper);
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4727
4728 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004729 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730 prepareDisplay(DISPLAY_ORIENTATION_90);
4731 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4732 processSync(mapper);
4733
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4735 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4736 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4737
4738 processUp(mapper);
4739 processSync(mapper);
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4741
4742 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004743 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 prepareDisplay(DISPLAY_ORIENTATION_180);
4745 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4746 processSync(mapper);
4747
4748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4749 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4750 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4751
4752 processUp(mapper);
4753 processSync(mapper);
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4755
4756 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004757 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758 prepareDisplay(DISPLAY_ORIENTATION_270);
4759 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4760 processSync(mapper);
4761
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4763 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4764 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4765
4766 processUp(mapper);
4767 processSync(mapper);
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4769}
4770
4771TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 addConfigurationProperty("touch.deviceType", "touchScreen");
4773 prepareDisplay(DISPLAY_ORIENTATION_0);
4774 prepareButtons();
4775 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004776 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777
4778 // These calculations are based on the input device calibration documentation.
4779 int32_t rawX = 100;
4780 int32_t rawY = 200;
4781 int32_t rawPressure = 10;
4782 int32_t rawToolMajor = 12;
4783 int32_t rawDistance = 2;
4784 int32_t rawTiltX = 30;
4785 int32_t rawTiltY = 110;
4786
4787 float x = toDisplayX(rawX);
4788 float y = toDisplayY(rawY);
4789 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4790 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4791 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4792 float distance = float(rawDistance);
4793
4794 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4795 float tiltScale = M_PI / 180;
4796 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4797 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4798 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4799 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4800
4801 processDown(mapper, rawX, rawY);
4802 processPressure(mapper, rawPressure);
4803 processToolMajor(mapper, rawToolMajor);
4804 processDistance(mapper, rawDistance);
4805 processTilt(mapper, rawTiltX, rawTiltY);
4806 processSync(mapper);
4807
4808 NotifyMotionArgs args;
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4811 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4812 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4813}
4814
Jason Gerecke489fda82012-09-07 17:19:40 -07004815TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004816 addConfigurationProperty("touch.deviceType", "touchScreen");
4817 prepareDisplay(DISPLAY_ORIENTATION_0);
4818 prepareLocationCalibration();
4819 prepareButtons();
4820 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004821 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004822
4823 int32_t rawX = 100;
4824 int32_t rawY = 200;
4825
4826 float x = toDisplayX(toCookedX(rawX, rawY));
4827 float y = toDisplayY(toCookedY(rawX, rawY));
4828
4829 processDown(mapper, rawX, rawY);
4830 processSync(mapper);
4831
4832 NotifyMotionArgs args;
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4835 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4836}
4837
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 addConfigurationProperty("touch.deviceType", "touchScreen");
4840 prepareDisplay(DISPLAY_ORIENTATION_0);
4841 prepareButtons();
4842 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004843 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844
4845 NotifyMotionArgs motionArgs;
4846 NotifyKeyArgs keyArgs;
4847
4848 processDown(mapper, 100, 200);
4849 processSync(mapper);
4850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4851 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4852 ASSERT_EQ(0, motionArgs.buttonState);
4853
4854 // press BTN_LEFT, release BTN_LEFT
4855 processKey(mapper, BTN_LEFT, 1);
4856 processSync(mapper);
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4859 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4860
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4863 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4864
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 processKey(mapper, BTN_LEFT, 0);
4866 processSync(mapper);
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004868 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004870
4871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004873 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874
4875 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4876 processKey(mapper, BTN_RIGHT, 1);
4877 processKey(mapper, BTN_MIDDLE, 1);
4878 processSync(mapper);
4879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4881 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4882 motionArgs.buttonState);
4883
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4886 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4887
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4890 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4891 motionArgs.buttonState);
4892
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 processKey(mapper, BTN_RIGHT, 0);
4894 processSync(mapper);
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004896 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004898
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004901 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902
4903 processKey(mapper, BTN_MIDDLE, 0);
4904 processSync(mapper);
4905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004908
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004911 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912
4913 // press BTN_BACK, release BTN_BACK
4914 processKey(mapper, BTN_BACK, 1);
4915 processSync(mapper);
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4917 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4918 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004919
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4923
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4925 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4926 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927
4928 processKey(mapper, BTN_BACK, 0);
4929 processSync(mapper);
4930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004931 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004933
4934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004936 ASSERT_EQ(0, motionArgs.buttonState);
4937
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4939 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4940 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4941
4942 // press BTN_SIDE, release BTN_SIDE
4943 processKey(mapper, BTN_SIDE, 1);
4944 processSync(mapper);
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4946 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4947 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004948
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004951 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4952
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4954 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4955 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956
4957 processKey(mapper, BTN_SIDE, 0);
4958 processSync(mapper);
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004962
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004965 ASSERT_EQ(0, motionArgs.buttonState);
4966
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4968 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4969 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4970
4971 // press BTN_FORWARD, release BTN_FORWARD
4972 processKey(mapper, BTN_FORWARD, 1);
4973 processSync(mapper);
4974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4975 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4976 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004977
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004980 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4981
4982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4983 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4984 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985
4986 processKey(mapper, BTN_FORWARD, 0);
4987 processSync(mapper);
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004991
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004994 ASSERT_EQ(0, motionArgs.buttonState);
4995
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4997 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4998 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4999
5000 // press BTN_EXTRA, release BTN_EXTRA
5001 processKey(mapper, BTN_EXTRA, 1);
5002 processSync(mapper);
5003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5004 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5005 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005006
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005009 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5010
5011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5013 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014
5015 processKey(mapper, BTN_EXTRA, 0);
5016 processSync(mapper);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005020
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005023 ASSERT_EQ(0, motionArgs.buttonState);
5024
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5026 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5027 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5028
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5030
Michael Wrightd02c5b62014-02-10 15:10:22 -08005031 // press BTN_STYLUS, release BTN_STYLUS
5032 processKey(mapper, BTN_STYLUS, 1);
5033 processSync(mapper);
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005036 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5037
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5040 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041
5042 processKey(mapper, BTN_STYLUS, 0);
5043 processSync(mapper);
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005047
5048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005050 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005051
5052 // press BTN_STYLUS2, release BTN_STYLUS2
5053 processKey(mapper, BTN_STYLUS2, 1);
5054 processSync(mapper);
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005057 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5058
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5061 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062
5063 processKey(mapper, BTN_STYLUS2, 0);
5064 processSync(mapper);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005068
5069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005071 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072
5073 // release touch
5074 processUp(mapper);
5075 processSync(mapper);
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5077 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5078 ASSERT_EQ(0, motionArgs.buttonState);
5079}
5080
5081TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 addConfigurationProperty("touch.deviceType", "touchScreen");
5083 prepareDisplay(DISPLAY_ORIENTATION_0);
5084 prepareButtons();
5085 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005086 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087
5088 NotifyMotionArgs motionArgs;
5089
5090 // default tool type is finger
5091 processDown(mapper, 100, 200);
5092 processSync(mapper);
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5094 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5096
5097 // eraser
5098 processKey(mapper, BTN_TOOL_RUBBER, 1);
5099 processSync(mapper);
5100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5103
5104 // stylus
5105 processKey(mapper, BTN_TOOL_RUBBER, 0);
5106 processKey(mapper, BTN_TOOL_PEN, 1);
5107 processSync(mapper);
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5111
5112 // brush
5113 processKey(mapper, BTN_TOOL_PEN, 0);
5114 processKey(mapper, BTN_TOOL_BRUSH, 1);
5115 processSync(mapper);
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5119
5120 // pencil
5121 processKey(mapper, BTN_TOOL_BRUSH, 0);
5122 processKey(mapper, BTN_TOOL_PENCIL, 1);
5123 processSync(mapper);
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5127
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005128 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 processKey(mapper, BTN_TOOL_PENCIL, 0);
5130 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5131 processSync(mapper);
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5135
5136 // mouse
5137 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5138 processKey(mapper, BTN_TOOL_MOUSE, 1);
5139 processSync(mapper);
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5143
5144 // lens
5145 processKey(mapper, BTN_TOOL_MOUSE, 0);
5146 processKey(mapper, BTN_TOOL_LENS, 1);
5147 processSync(mapper);
5148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5151
5152 // double-tap
5153 processKey(mapper, BTN_TOOL_LENS, 0);
5154 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5155 processSync(mapper);
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5159
5160 // triple-tap
5161 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5162 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5163 processSync(mapper);
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5167
5168 // quad-tap
5169 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5170 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5171 processSync(mapper);
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5175
5176 // finger
5177 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5178 processKey(mapper, BTN_TOOL_FINGER, 1);
5179 processSync(mapper);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5183
5184 // stylus trumps finger
5185 processKey(mapper, BTN_TOOL_PEN, 1);
5186 processSync(mapper);
5187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5190
5191 // eraser trumps stylus
5192 processKey(mapper, BTN_TOOL_RUBBER, 1);
5193 processSync(mapper);
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5197
5198 // mouse trumps eraser
5199 processKey(mapper, BTN_TOOL_MOUSE, 1);
5200 processSync(mapper);
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5204
5205 // back to default tool type
5206 processKey(mapper, BTN_TOOL_MOUSE, 0);
5207 processKey(mapper, BTN_TOOL_RUBBER, 0);
5208 processKey(mapper, BTN_TOOL_PEN, 0);
5209 processKey(mapper, BTN_TOOL_FINGER, 0);
5210 processSync(mapper);
5211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5214}
5215
5216TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217 addConfigurationProperty("touch.deviceType", "touchScreen");
5218 prepareDisplay(DISPLAY_ORIENTATION_0);
5219 prepareButtons();
5220 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005221 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005222 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223
5224 NotifyMotionArgs motionArgs;
5225
5226 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5227 processKey(mapper, BTN_TOOL_FINGER, 1);
5228 processMove(mapper, 100, 200);
5229 processSync(mapper);
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5233 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5234
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5238 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5239
5240 // move a little
5241 processMove(mapper, 150, 250);
5242 processSync(mapper);
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5246 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5247
5248 // down when BTN_TOUCH is pressed, pressure defaults to 1
5249 processKey(mapper, BTN_TOUCH, 1);
5250 processSync(mapper);
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5254 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5255
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5259 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5260
5261 // up when BTN_TOUCH is released, hover restored
5262 processKey(mapper, BTN_TOUCH, 0);
5263 processSync(mapper);
5264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5265 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5267 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5272 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5273
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5278
5279 // exit hover when pointer goes away
5280 processKey(mapper, BTN_TOOL_FINGER, 0);
5281 processSync(mapper);
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5285 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5286}
5287
5288TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005289 addConfigurationProperty("touch.deviceType", "touchScreen");
5290 prepareDisplay(DISPLAY_ORIENTATION_0);
5291 prepareButtons();
5292 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005293 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294
5295 NotifyMotionArgs motionArgs;
5296
5297 // initially hovering because pressure is 0
5298 processDown(mapper, 100, 200);
5299 processPressure(mapper, 0);
5300 processSync(mapper);
5301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5302 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5304 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5305
5306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5309 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5310
5311 // move a little
5312 processMove(mapper, 150, 250);
5313 processSync(mapper);
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5317 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5318
5319 // down when pressure is non-zero
5320 processPressure(mapper, RAW_PRESSURE_MAX);
5321 processSync(mapper);
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5325 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5326
5327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5328 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5330 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5331
5332 // up when pressure becomes 0, hover restored
5333 processPressure(mapper, 0);
5334 processSync(mapper);
5335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5336 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5338 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5339
5340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5341 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5343 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5344
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5348 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5349
5350 // exit hover when pointer goes away
5351 processUp(mapper);
5352 processSync(mapper);
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5354 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5357}
5358
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359// --- MultiTouchInputMapperTest ---
5360
5361class MultiTouchInputMapperTest : public TouchInputMapperTest {
5362protected:
5363 void prepareAxes(int axes);
5364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5366 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5367 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5368 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5369 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5370 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5371 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5372 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5373 void processId(MultiTouchInputMapper& mapper, int32_t id);
5374 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5375 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5376 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5377 void processMTSync(MultiTouchInputMapper& mapper);
5378 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379};
5380
5381void MultiTouchInputMapperTest::prepareAxes(int axes) {
5382 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005383 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5384 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385 }
5386 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005387 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5388 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005390 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5391 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392 }
5393 }
5394 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005395 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5396 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005398 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5399 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 }
5401 }
5402 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005403 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5404 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405 }
5406 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005407 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5408 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409 }
5410 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005411 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5412 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 }
5414 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005415 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5416 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 }
5418 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005419 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5420 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421 }
5422 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005423 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424 }
5425}
5426
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5428 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005429 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5430 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431}
5432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005433void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5434 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005435 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436}
5437
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005438void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5439 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005440 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441}
5442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005443void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005444 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445}
5446
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005447void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005448 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449}
5450
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005451void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5452 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005453 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005454}
5455
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005456void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005457 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458}
5459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005460void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005461 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462}
5463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005465 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466}
5467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005468void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005469 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470}
5471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005472void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005473 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474}
5475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005476void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5477 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005478 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005479}
5480
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005481void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005482 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483}
5484
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005485void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005486 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487}
5488
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490 addConfigurationProperty("touch.deviceType", "touchScreen");
5491 prepareDisplay(DISPLAY_ORIENTATION_0);
5492 prepareAxes(POSITION);
5493 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495
5496 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5497
5498 NotifyMotionArgs motionArgs;
5499
5500 // Two fingers down at once.
5501 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5502 processPosition(mapper, x1, y1);
5503 processMTSync(mapper);
5504 processPosition(mapper, x2, y2);
5505 processMTSync(mapper);
5506 processSync(mapper);
5507
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5509 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5510 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5511 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5512 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5513 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5514 ASSERT_EQ(0, motionArgs.flags);
5515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5516 ASSERT_EQ(0, motionArgs.buttonState);
5517 ASSERT_EQ(0, motionArgs.edgeFlags);
5518 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5519 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5522 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5523 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5524 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5525 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5526
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5528 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5529 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5530 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5531 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5532 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5533 motionArgs.action);
5534 ASSERT_EQ(0, motionArgs.flags);
5535 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5536 ASSERT_EQ(0, motionArgs.buttonState);
5537 ASSERT_EQ(0, motionArgs.edgeFlags);
5538 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5541 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5544 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5546 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5547 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5548 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5550
5551 // Move.
5552 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5553 processPosition(mapper, x1, y1);
5554 processMTSync(mapper);
5555 processPosition(mapper, x2, y2);
5556 processMTSync(mapper);
5557 processSync(mapper);
5558
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5560 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5561 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5562 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5563 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5565 ASSERT_EQ(0, motionArgs.flags);
5566 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5567 ASSERT_EQ(0, motionArgs.buttonState);
5568 ASSERT_EQ(0, motionArgs.edgeFlags);
5569 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5572 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5575 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5577 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5578 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5579 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5580 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5581
5582 // First finger up.
5583 x2 += 15; y2 -= 20;
5584 processPosition(mapper, x2, y2);
5585 processMTSync(mapper);
5586 processSync(mapper);
5587
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5589 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5590 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5591 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5592 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5593 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5594 motionArgs.action);
5595 ASSERT_EQ(0, motionArgs.flags);
5596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5597 ASSERT_EQ(0, motionArgs.buttonState);
5598 ASSERT_EQ(0, motionArgs.edgeFlags);
5599 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5600 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5602 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5605 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5607 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5608 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5609 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5610 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5614 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5616 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5618 ASSERT_EQ(0, motionArgs.flags);
5619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5620 ASSERT_EQ(0, motionArgs.buttonState);
5621 ASSERT_EQ(0, motionArgs.edgeFlags);
5622 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5623 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5626 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5627 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5628 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5630
5631 // Move.
5632 x2 += 20; y2 -= 25;
5633 processPosition(mapper, x2, y2);
5634 processMTSync(mapper);
5635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5640 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5641 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5643 ASSERT_EQ(0, motionArgs.flags);
5644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5645 ASSERT_EQ(0, motionArgs.buttonState);
5646 ASSERT_EQ(0, motionArgs.edgeFlags);
5647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5648 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5651 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5652 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5653 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5655
5656 // New finger down.
5657 int32_t x3 = 700, y3 = 300;
5658 processPosition(mapper, x2, y2);
5659 processMTSync(mapper);
5660 processPosition(mapper, x3, y3);
5661 processMTSync(mapper);
5662 processSync(mapper);
5663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5666 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5667 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5668 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5669 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5670 motionArgs.action);
5671 ASSERT_EQ(0, motionArgs.flags);
5672 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5673 ASSERT_EQ(0, motionArgs.buttonState);
5674 ASSERT_EQ(0, motionArgs.edgeFlags);
5675 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5678 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5681 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5683 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5684 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5685 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5686 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5687
5688 // Second finger up.
5689 x3 += 30; y3 -= 20;
5690 processPosition(mapper, x3, y3);
5691 processMTSync(mapper);
5692 processSync(mapper);
5693
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5696 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5697 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5698 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5699 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5700 motionArgs.action);
5701 ASSERT_EQ(0, motionArgs.flags);
5702 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5703 ASSERT_EQ(0, motionArgs.buttonState);
5704 ASSERT_EQ(0, motionArgs.edgeFlags);
5705 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5706 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5707 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5708 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5711 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5713 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5714 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5715 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5716 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5720 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5721 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5722 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5724 ASSERT_EQ(0, motionArgs.flags);
5725 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5726 ASSERT_EQ(0, motionArgs.buttonState);
5727 ASSERT_EQ(0, motionArgs.edgeFlags);
5728 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5729 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5732 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5733 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5734 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5736
5737 // Last finger up.
5738 processMTSync(mapper);
5739 processSync(mapper);
5740
5741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5742 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5743 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5744 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5745 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5746 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5747 ASSERT_EQ(0, motionArgs.flags);
5748 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5749 ASSERT_EQ(0, motionArgs.buttonState);
5750 ASSERT_EQ(0, motionArgs.edgeFlags);
5751 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5752 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5755 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5756 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5757 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5758 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5759
5760 // Should not have sent any more keys or motions.
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5763}
5764
5765TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766 addConfigurationProperty("touch.deviceType", "touchScreen");
5767 prepareDisplay(DISPLAY_ORIENTATION_0);
5768 prepareAxes(POSITION | ID);
5769 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005770 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771
5772 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5773
5774 NotifyMotionArgs motionArgs;
5775
5776 // Two fingers down at once.
5777 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5778 processPosition(mapper, x1, y1);
5779 processId(mapper, 1);
5780 processMTSync(mapper);
5781 processPosition(mapper, x2, y2);
5782 processId(mapper, 2);
5783 processMTSync(mapper);
5784 processSync(mapper);
5785
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5788 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5789 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5792 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5793
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5795 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5796 motionArgs.action);
5797 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5800 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5803 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5806
5807 // Move.
5808 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5809 processPosition(mapper, x1, y1);
5810 processId(mapper, 1);
5811 processMTSync(mapper);
5812 processPosition(mapper, x2, y2);
5813 processId(mapper, 2);
5814 processMTSync(mapper);
5815 processSync(mapper);
5816
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5819 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5822 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5825 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5827 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5828
5829 // First finger up.
5830 x2 += 15; y2 -= 20;
5831 processPosition(mapper, x2, y2);
5832 processId(mapper, 2);
5833 processMTSync(mapper);
5834 processSync(mapper);
5835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5838 motionArgs.action);
5839 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5845 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5847 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5848
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5852 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5856
5857 // Move.
5858 x2 += 20; y2 -= 25;
5859 processPosition(mapper, x2, y2);
5860 processId(mapper, 2);
5861 processMTSync(mapper);
5862 processSync(mapper);
5863
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5867 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5870 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5871
5872 // New finger down.
5873 int32_t x3 = 700, y3 = 300;
5874 processPosition(mapper, x2, y2);
5875 processId(mapper, 2);
5876 processMTSync(mapper);
5877 processPosition(mapper, x3, y3);
5878 processId(mapper, 3);
5879 processMTSync(mapper);
5880 processSync(mapper);
5881
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5883 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5884 motionArgs.action);
5885 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5886 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5888 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5891 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5893 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5894
5895 // Second finger up.
5896 x3 += 30; y3 -= 20;
5897 processPosition(mapper, x3, y3);
5898 processId(mapper, 3);
5899 processMTSync(mapper);
5900 processSync(mapper);
5901
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5904 motionArgs.action);
5905 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5908 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5911 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5913 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5917 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5918 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5921 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5922
5923 // Last finger up.
5924 processMTSync(mapper);
5925 processSync(mapper);
5926
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5928 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5929 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5934
5935 // Should not have sent any more keys or motions.
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5938}
5939
5940TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 addConfigurationProperty("touch.deviceType", "touchScreen");
5942 prepareDisplay(DISPLAY_ORIENTATION_0);
5943 prepareAxes(POSITION | ID | SLOT);
5944 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946
5947 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5948
5949 NotifyMotionArgs motionArgs;
5950
5951 // Two fingers down at once.
5952 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5953 processPosition(mapper, x1, y1);
5954 processId(mapper, 1);
5955 processSlot(mapper, 1);
5956 processPosition(mapper, x2, y2);
5957 processId(mapper, 2);
5958 processSync(mapper);
5959
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5962 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5963 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5966 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5967
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5970 motionArgs.action);
5971 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5972 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5974 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5977 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5979 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5980
5981 // Move.
5982 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5983 processSlot(mapper, 0);
5984 processPosition(mapper, x1, y1);
5985 processSlot(mapper, 1);
5986 processPosition(mapper, x2, y2);
5987 processSync(mapper);
5988
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5991 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5992 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5994 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5999 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6000
6001 // First finger up.
6002 x2 += 15; y2 -= 20;
6003 processSlot(mapper, 0);
6004 processId(mapper, -1);
6005 processSlot(mapper, 1);
6006 processPosition(mapper, x2, y2);
6007 processSync(mapper);
6008
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6011 motionArgs.action);
6012 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6013 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6015 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6016 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6018 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6020 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6021
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6024 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6025 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6028 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6029
6030 // Move.
6031 x2 += 20; y2 -= 25;
6032 processPosition(mapper, x2, y2);
6033 processSync(mapper);
6034
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6037 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6038 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6041 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6042
6043 // New finger down.
6044 int32_t x3 = 700, y3 = 300;
6045 processPosition(mapper, x2, y2);
6046 processSlot(mapper, 0);
6047 processId(mapper, 3);
6048 processPosition(mapper, x3, y3);
6049 processSync(mapper);
6050
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6053 motionArgs.action);
6054 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6055 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6057 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6060 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6062 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6063
6064 // Second finger up.
6065 x3 += 30; y3 -= 20;
6066 processSlot(mapper, 1);
6067 processId(mapper, -1);
6068 processSlot(mapper, 0);
6069 processPosition(mapper, x3, y3);
6070 processSync(mapper);
6071
6072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6073 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6074 motionArgs.action);
6075 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6076 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6078 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6081 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6083 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6084
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6087 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6088 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6091 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6092
6093 // Last finger up.
6094 processId(mapper, -1);
6095 processSync(mapper);
6096
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6099 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6103 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6104
6105 // Should not have sent any more keys or motions.
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6108}
6109
6110TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 addConfigurationProperty("touch.deviceType", "touchScreen");
6112 prepareDisplay(DISPLAY_ORIENTATION_0);
6113 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006114 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115
6116 // These calculations are based on the input device calibration documentation.
6117 int32_t rawX = 100;
6118 int32_t rawY = 200;
6119 int32_t rawTouchMajor = 7;
6120 int32_t rawTouchMinor = 6;
6121 int32_t rawToolMajor = 9;
6122 int32_t rawToolMinor = 8;
6123 int32_t rawPressure = 11;
6124 int32_t rawDistance = 0;
6125 int32_t rawOrientation = 3;
6126 int32_t id = 5;
6127
6128 float x = toDisplayX(rawX);
6129 float y = toDisplayY(rawY);
6130 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6131 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6132 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6133 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6134 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6135 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6136 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6137 float distance = float(rawDistance);
6138
6139 processPosition(mapper, rawX, rawY);
6140 processTouchMajor(mapper, rawTouchMajor);
6141 processTouchMinor(mapper, rawTouchMinor);
6142 processToolMajor(mapper, rawToolMajor);
6143 processToolMinor(mapper, rawToolMinor);
6144 processPressure(mapper, rawPressure);
6145 processOrientation(mapper, rawOrientation);
6146 processDistance(mapper, rawDistance);
6147 processId(mapper, id);
6148 processMTSync(mapper);
6149 processSync(mapper);
6150
6151 NotifyMotionArgs args;
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6153 ASSERT_EQ(0, args.pointerProperties[0].id);
6154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6155 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6156 orientation, distance));
6157}
6158
6159TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 addConfigurationProperty("touch.deviceType", "touchScreen");
6161 prepareDisplay(DISPLAY_ORIENTATION_0);
6162 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6163 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006164 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165
6166 // These calculations are based on the input device calibration documentation.
6167 int32_t rawX = 100;
6168 int32_t rawY = 200;
6169 int32_t rawTouchMajor = 140;
6170 int32_t rawTouchMinor = 120;
6171 int32_t rawToolMajor = 180;
6172 int32_t rawToolMinor = 160;
6173
6174 float x = toDisplayX(rawX);
6175 float y = toDisplayY(rawY);
6176 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6177 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6178 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6179 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6180 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6181
6182 processPosition(mapper, rawX, rawY);
6183 processTouchMajor(mapper, rawTouchMajor);
6184 processTouchMinor(mapper, rawTouchMinor);
6185 processToolMajor(mapper, rawToolMajor);
6186 processToolMinor(mapper, rawToolMinor);
6187 processMTSync(mapper);
6188 processSync(mapper);
6189
6190 NotifyMotionArgs args;
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6193 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6194}
6195
6196TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197 addConfigurationProperty("touch.deviceType", "touchScreen");
6198 prepareDisplay(DISPLAY_ORIENTATION_0);
6199 prepareAxes(POSITION | TOUCH | TOOL);
6200 addConfigurationProperty("touch.size.calibration", "diameter");
6201 addConfigurationProperty("touch.size.scale", "10");
6202 addConfigurationProperty("touch.size.bias", "160");
6203 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006204 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205
6206 // These calculations are based on the input device calibration documentation.
6207 // Note: We only provide a single common touch/tool value because the device is assumed
6208 // not to emit separate values for each pointer (isSummed = 1).
6209 int32_t rawX = 100;
6210 int32_t rawY = 200;
6211 int32_t rawX2 = 150;
6212 int32_t rawY2 = 250;
6213 int32_t rawTouchMajor = 5;
6214 int32_t rawToolMajor = 8;
6215
6216 float x = toDisplayX(rawX);
6217 float y = toDisplayY(rawY);
6218 float x2 = toDisplayX(rawX2);
6219 float y2 = toDisplayY(rawY2);
6220 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6221 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6222 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6223
6224 processPosition(mapper, rawX, rawY);
6225 processTouchMajor(mapper, rawTouchMajor);
6226 processToolMajor(mapper, rawToolMajor);
6227 processMTSync(mapper);
6228 processPosition(mapper, rawX2, rawY2);
6229 processTouchMajor(mapper, rawTouchMajor);
6230 processToolMajor(mapper, rawToolMajor);
6231 processMTSync(mapper);
6232 processSync(mapper);
6233
6234 NotifyMotionArgs args;
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6236 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6237
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6240 args.action);
6241 ASSERT_EQ(size_t(2), args.pointerCount);
6242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6243 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6245 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6246}
6247
6248TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 addConfigurationProperty("touch.deviceType", "touchScreen");
6250 prepareDisplay(DISPLAY_ORIENTATION_0);
6251 prepareAxes(POSITION | TOUCH | TOOL);
6252 addConfigurationProperty("touch.size.calibration", "area");
6253 addConfigurationProperty("touch.size.scale", "43");
6254 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006255 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256
6257 // These calculations are based on the input device calibration documentation.
6258 int32_t rawX = 100;
6259 int32_t rawY = 200;
6260 int32_t rawTouchMajor = 5;
6261 int32_t rawToolMajor = 8;
6262
6263 float x = toDisplayX(rawX);
6264 float y = toDisplayY(rawY);
6265 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6266 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6267 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6268
6269 processPosition(mapper, rawX, rawY);
6270 processTouchMajor(mapper, rawTouchMajor);
6271 processToolMajor(mapper, rawToolMajor);
6272 processMTSync(mapper);
6273 processSync(mapper);
6274
6275 NotifyMotionArgs args;
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6278 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6279}
6280
6281TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 addConfigurationProperty("touch.deviceType", "touchScreen");
6283 prepareDisplay(DISPLAY_ORIENTATION_0);
6284 prepareAxes(POSITION | PRESSURE);
6285 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6286 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006287 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288
Michael Wrightaa449c92017-12-13 21:21:43 +00006289 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006290 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006291 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6292 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6293 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6294
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 // These calculations are based on the input device calibration documentation.
6296 int32_t rawX = 100;
6297 int32_t rawY = 200;
6298 int32_t rawPressure = 60;
6299
6300 float x = toDisplayX(rawX);
6301 float y = toDisplayY(rawY);
6302 float pressure = float(rawPressure) * 0.01f;
6303
6304 processPosition(mapper, rawX, rawY);
6305 processPressure(mapper, rawPressure);
6306 processMTSync(mapper);
6307 processSync(mapper);
6308
6309 NotifyMotionArgs args;
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6312 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6313}
6314
6315TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 addConfigurationProperty("touch.deviceType", "touchScreen");
6317 prepareDisplay(DISPLAY_ORIENTATION_0);
6318 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006319 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 NotifyMotionArgs motionArgs;
6322 NotifyKeyArgs keyArgs;
6323
6324 processId(mapper, 1);
6325 processPosition(mapper, 100, 200);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6329 ASSERT_EQ(0, motionArgs.buttonState);
6330
6331 // press BTN_LEFT, release BTN_LEFT
6332 processKey(mapper, BTN_LEFT, 1);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6337
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6340 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6341
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 processKey(mapper, BTN_LEFT, 0);
6343 processSync(mapper);
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347
6348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006350 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351
6352 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6353 processKey(mapper, BTN_RIGHT, 1);
6354 processKey(mapper, BTN_MIDDLE, 1);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6358 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6359 motionArgs.buttonState);
6360
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6363 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6364
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6367 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6368 motionArgs.buttonState);
6369
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 processKey(mapper, BTN_RIGHT, 0);
6371 processSync(mapper);
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379
6380 processKey(mapper, BTN_MIDDLE, 0);
6381 processSync(mapper);
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006385
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389
6390 // press BTN_BACK, release BTN_BACK
6391 processKey(mapper, BTN_BACK, 1);
6392 processSync(mapper);
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6394 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6395 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6403 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404
6405 processKey(mapper, BTN_BACK, 0);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006409 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006410
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413 ASSERT_EQ(0, motionArgs.buttonState);
6414
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6416 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6417 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6418
6419 // press BTN_SIDE, release BTN_SIDE
6420 processKey(mapper, BTN_SIDE, 1);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6423 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6424 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006425
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006428 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6432 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433
6434 processKey(mapper, BTN_SIDE, 0);
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006442 ASSERT_EQ(0, motionArgs.buttonState);
6443
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6445 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6446 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6447
6448 // press BTN_FORWARD, release BTN_FORWARD
6449 processKey(mapper, BTN_FORWARD, 1);
6450 processSync(mapper);
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6452 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6453 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006457 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6458
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6461 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462
6463 processKey(mapper, BTN_FORWARD, 0);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006467 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006471 ASSERT_EQ(0, motionArgs.buttonState);
6472
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6474 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6475 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6476
6477 // press BTN_EXTRA, release BTN_EXTRA
6478 processKey(mapper, BTN_EXTRA, 1);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6481 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6482 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006483
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006486 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6487
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006491
6492 processKey(mapper, BTN_EXTRA, 0);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006500 ASSERT_EQ(0, motionArgs.buttonState);
6501
Michael Wrightd02c5b62014-02-10 15:10:22 -08006502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6503 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6504 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6505
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6507
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508 // press BTN_STYLUS, release BTN_STYLUS
6509 processKey(mapper, BTN_STYLUS, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006513 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6514
6515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6517 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518
6519 processKey(mapper, BTN_STYLUS, 0);
6520 processSync(mapper);
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006522 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006523 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006524
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006527 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528
6529 // press BTN_STYLUS2, release BTN_STYLUS2
6530 processKey(mapper, BTN_STYLUS2, 1);
6531 processSync(mapper);
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006534 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6535
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6538 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539
6540 processKey(mapper, BTN_STYLUS2, 0);
6541 processSync(mapper);
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006545
6546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006548 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006549
6550 // release touch
6551 processId(mapper, -1);
6552 processSync(mapper);
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6554 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6555 ASSERT_EQ(0, motionArgs.buttonState);
6556}
6557
6558TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559 addConfigurationProperty("touch.deviceType", "touchScreen");
6560 prepareDisplay(DISPLAY_ORIENTATION_0);
6561 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563
6564 NotifyMotionArgs motionArgs;
6565
6566 // default tool type is finger
6567 processId(mapper, 1);
6568 processPosition(mapper, 100, 200);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6573
6574 // eraser
6575 processKey(mapper, BTN_TOOL_RUBBER, 1);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6580
6581 // stylus
6582 processKey(mapper, BTN_TOOL_RUBBER, 0);
6583 processKey(mapper, BTN_TOOL_PEN, 1);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6588
6589 // brush
6590 processKey(mapper, BTN_TOOL_PEN, 0);
6591 processKey(mapper, BTN_TOOL_BRUSH, 1);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6596
6597 // pencil
6598 processKey(mapper, BTN_TOOL_BRUSH, 0);
6599 processKey(mapper, BTN_TOOL_PENCIL, 1);
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6604
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006605 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006606 processKey(mapper, BTN_TOOL_PENCIL, 0);
6607 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6608 processSync(mapper);
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6612
6613 // mouse
6614 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6615 processKey(mapper, BTN_TOOL_MOUSE, 1);
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6620
6621 // lens
6622 processKey(mapper, BTN_TOOL_MOUSE, 0);
6623 processKey(mapper, BTN_TOOL_LENS, 1);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6628
6629 // double-tap
6630 processKey(mapper, BTN_TOOL_LENS, 0);
6631 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6636
6637 // triple-tap
6638 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6639 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6644
6645 // quad-tap
6646 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6647 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6652
6653 // finger
6654 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6655 processKey(mapper, BTN_TOOL_FINGER, 1);
6656 processSync(mapper);
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6660
6661 // stylus trumps finger
6662 processKey(mapper, BTN_TOOL_PEN, 1);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6666 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6667
6668 // eraser trumps stylus
6669 processKey(mapper, BTN_TOOL_RUBBER, 1);
6670 processSync(mapper);
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6674
6675 // mouse trumps eraser
6676 processKey(mapper, BTN_TOOL_MOUSE, 1);
6677 processSync(mapper);
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6681
6682 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6683 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6684 processSync(mapper);
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6688
6689 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6690 processToolType(mapper, MT_TOOL_PEN);
6691 processSync(mapper);
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6695
6696 // back to default tool type
6697 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6698 processKey(mapper, BTN_TOOL_MOUSE, 0);
6699 processKey(mapper, BTN_TOOL_RUBBER, 0);
6700 processKey(mapper, BTN_TOOL_PEN, 0);
6701 processKey(mapper, BTN_TOOL_FINGER, 0);
6702 processSync(mapper);
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6705 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6706}
6707
6708TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006709 addConfigurationProperty("touch.deviceType", "touchScreen");
6710 prepareDisplay(DISPLAY_ORIENTATION_0);
6711 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006712 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006713 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006714
6715 NotifyMotionArgs motionArgs;
6716
6717 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6718 processId(mapper, 1);
6719 processPosition(mapper, 100, 200);
6720 processSync(mapper);
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6724 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6725
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6727 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6729 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6730
6731 // move a little
6732 processPosition(mapper, 150, 250);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6738
6739 // down when BTN_TOUCH is pressed, pressure defaults to 1
6740 processKey(mapper, BTN_TOUCH, 1);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6745 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6746
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6751
6752 // up when BTN_TOUCH is released, hover restored
6753 processKey(mapper, BTN_TOUCH, 0);
6754 processSync(mapper);
6755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6756 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6758 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6759
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6764
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6769
6770 // exit hover when pointer goes away
6771 processId(mapper, -1);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6777}
6778
6779TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006780 addConfigurationProperty("touch.deviceType", "touchScreen");
6781 prepareDisplay(DISPLAY_ORIENTATION_0);
6782 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006783 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006784
6785 NotifyMotionArgs motionArgs;
6786
6787 // initially hovering because pressure is 0
6788 processId(mapper, 1);
6789 processPosition(mapper, 100, 200);
6790 processPressure(mapper, 0);
6791 processSync(mapper);
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6793 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6795 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6796
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6798 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6800 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6801
6802 // move a little
6803 processPosition(mapper, 150, 250);
6804 processSync(mapper);
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6806 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6808 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6809
6810 // down when pressure becomes non-zero
6811 processPressure(mapper, RAW_PRESSURE_MAX);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6814 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6816 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6817
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6821 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6822
6823 // up when pressure becomes 0, hover restored
6824 processPressure(mapper, 0);
6825 processSync(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6827 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6829 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6830
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6832 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6834 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6835
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6837 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6839 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6840
6841 // exit hover when pointer goes away
6842 processId(mapper, -1);
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6847 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6848}
6849
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006850/**
6851 * Set the input device port <--> display port associations, and check that the
6852 * events are routed to the display that matches the display port.
6853 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6854 */
6855TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006856 const std::string usb2 = "USB2";
6857 const uint8_t hdmi1 = 0;
6858 const uint8_t hdmi2 = 1;
6859 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006860 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006861
6862 addConfigurationProperty("touch.deviceType", "touchScreen");
6863 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006865
6866 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6867 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6868
6869 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6870 // for this input device is specified, and the matching viewport is not present,
6871 // the input device should be disabled (at the mapper level).
6872
6873 // Add viewport for display 2 on hdmi2
6874 prepareSecondaryDisplay(type, hdmi2);
6875 // Send a touch event
6876 processPosition(mapper, 100, 100);
6877 processSync(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6879
6880 // Add viewport for display 1 on hdmi1
6881 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6882 // Send a touch event again
6883 processPosition(mapper, 100, 100);
6884 processSync(mapper);
6885
6886 NotifyMotionArgs args;
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6888 ASSERT_EQ(DISPLAY_ID, args.displayId);
6889}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006891TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006892 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006893 std::shared_ptr<FakePointerController> fakePointerController =
6894 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006895 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006896 fakePointerController->setPosition(100, 200);
6897 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006898 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6899
Garfield Tan888a6a42020-01-09 11:39:16 -08006900 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006901 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006902
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006903 prepareDisplay(DISPLAY_ORIENTATION_0);
6904 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006905 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006906
6907 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006908 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006909
6910 NotifyMotionArgs motionArgs;
6911 processPosition(mapper, 100, 100);
6912 processSync(mapper);
6913
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6915 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6916 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6917}
6918
Arthur Hung7c645402019-01-25 17:45:42 +08006919TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6920 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006921 prepareAxes(POSITION | ID | SLOT);
6922 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006923 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006924
6925 // Create the second touch screen device, and enable multi fingers.
6926 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006927 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006928 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006929 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006930 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006931 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006932 std::unique_ptr<InputDevice> device2 =
6933 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006934 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07006935 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME,
6936 Flags<InputDeviceClass>(0) /*classes*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006937 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6938 0 /*flat*/, 0 /*fuzz*/);
6939 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6940 0 /*flat*/, 0 /*fuzz*/);
6941 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6942 0 /*flat*/, 0 /*fuzz*/);
6943 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6944 0 /*flat*/, 0 /*fuzz*/);
6945 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6946 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6947 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006948
6949 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006950 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006951 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6952 device2->reset(ARBITRARY_TIME);
6953
6954 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006955 std::shared_ptr<FakePointerController> fakePointerController =
6956 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006957 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6958 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6959
6960 // Setup policy for associated displays and show touches.
6961 const uint8_t hdmi1 = 0;
6962 const uint8_t hdmi2 = 1;
6963 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6964 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6965 mFakePolicy->setShowTouches(true);
6966
6967 // Create displays.
6968 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006969 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006970
6971 // Default device will reconfigure above, need additional reconfiguration for another device.
6972 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006973 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006974
6975 // Two fingers down at default display.
6976 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6977 processPosition(mapper, x1, y1);
6978 processId(mapper, 1);
6979 processSlot(mapper, 1);
6980 processPosition(mapper, x2, y2);
6981 processId(mapper, 2);
6982 processSync(mapper);
6983
6984 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6985 fakePointerController->getSpots().find(DISPLAY_ID);
6986 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6987 ASSERT_EQ(size_t(2), iter->second.size());
6988
6989 // Two fingers down at second display.
6990 processPosition(mapper2, x1, y1);
6991 processId(mapper2, 1);
6992 processSlot(mapper2, 1);
6993 processPosition(mapper2, x2, y2);
6994 processId(mapper2, 2);
6995 processSync(mapper2);
6996
6997 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6998 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6999 ASSERT_EQ(size_t(2), iter->second.size());
7000}
7001
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007002TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007003 prepareAxes(POSITION);
7004 addConfigurationProperty("touch.deviceType", "touchScreen");
7005 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007007
7008 NotifyMotionArgs motionArgs;
7009 // Unrotated video frame
7010 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7011 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007012 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007013 processPosition(mapper, 100, 200);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7016 ASSERT_EQ(frames, motionArgs.videoFrames);
7017
7018 // Subsequent touch events should not have any videoframes
7019 // This is implemented separately in FakeEventHub,
7020 // but that should match the behaviour of TouchVideoDevice.
7021 processPosition(mapper, 200, 200);
7022 processSync(mapper);
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7025}
7026
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007027TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007028 prepareAxes(POSITION);
7029 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007030 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007031 // Unrotated video frame
7032 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7033 NotifyMotionArgs motionArgs;
7034
7035 // Test all 4 orientations
7036 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7037 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7038 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7039 clearViewports();
7040 prepareDisplay(orientation);
7041 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007042 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007043 processPosition(mapper, 100, 200);
7044 processSync(mapper);
7045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7046 frames[0].rotate(orientation);
7047 ASSERT_EQ(frames, motionArgs.videoFrames);
7048 }
7049}
7050
7051TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007052 prepareAxes(POSITION);
7053 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007054 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007055 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7056 // so mix these.
7057 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7058 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7059 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7060 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7061 NotifyMotionArgs motionArgs;
7062
7063 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007064 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007065 processPosition(mapper, 100, 200);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7068 std::for_each(frames.begin(), frames.end(),
7069 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7070 ASSERT_EQ(frames, motionArgs.videoFrames);
7071}
7072
Arthur Hung9da14732019-09-02 16:16:58 +08007073/**
7074 * If we had defined port associations, but the viewport is not ready, the touch device would be
7075 * expected to be disabled, and it should be enabled after the viewport has found.
7076 */
7077TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007078 constexpr uint8_t hdmi2 = 1;
7079 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007080 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007081
7082 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7083
7084 addConfigurationProperty("touch.deviceType", "touchScreen");
7085 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007086 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007087
7088 ASSERT_EQ(mDevice->isEnabled(), false);
7089
7090 // Add display on hdmi2, the device should be enabled and can receive touch event.
7091 prepareSecondaryDisplay(type, hdmi2);
7092 ASSERT_EQ(mDevice->isEnabled(), true);
7093
7094 // Send a touch event.
7095 processPosition(mapper, 100, 100);
7096 processSync(mapper);
7097
7098 NotifyMotionArgs args;
7099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7100 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7101}
7102
Arthur Hung6cd19a42019-08-30 19:04:12 +08007103
Arthur Hung6cd19a42019-08-30 19:04:12 +08007104
Arthur Hung421eb1c2020-01-16 00:09:42 +08007105TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007106 addConfigurationProperty("touch.deviceType", "touchScreen");
7107 prepareDisplay(DISPLAY_ORIENTATION_0);
7108 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007109 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007110
7111 NotifyMotionArgs motionArgs;
7112
7113 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7114 // finger down
7115 processId(mapper, 1);
7116 processPosition(mapper, x1, y1);
7117 processSync(mapper);
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7121
7122 // finger move
7123 processId(mapper, 1);
7124 processPosition(mapper, x2, y2);
7125 processSync(mapper);
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7129
7130 // finger up.
7131 processId(mapper, -1);
7132 processSync(mapper);
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7134 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7136
7137 // new finger down
7138 processId(mapper, 1);
7139 processPosition(mapper, x3, y3);
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7144}
7145
7146/**
arthurhungcc7f9802020-04-30 17:55:40 +08007147 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7148 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007149 */
arthurhungcc7f9802020-04-30 17:55:40 +08007150TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007151 addConfigurationProperty("touch.deviceType", "touchScreen");
7152 prepareDisplay(DISPLAY_ORIENTATION_0);
7153 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007154 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007155
7156 NotifyMotionArgs motionArgs;
7157
7158 // default tool type is finger
7159 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007160 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007161 processPosition(mapper, x1, y1);
7162 processSync(mapper);
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7164 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7166
7167 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7168 processToolType(mapper, MT_TOOL_PALM);
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7171 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7172
7173 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007174 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007175 processPosition(mapper, x2, y2);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7178
7179 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007180 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007181 processSync(mapper);
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7183
7184 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007185 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007186 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007187 processPosition(mapper, x3, y3);
7188 processSync(mapper);
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7192}
7193
arthurhungbf89a482020-04-17 17:37:55 +08007194/**
arthurhungcc7f9802020-04-30 17:55:40 +08007195 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7196 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007197 */
arthurhungcc7f9802020-04-30 17:55:40 +08007198TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007199 addConfigurationProperty("touch.deviceType", "touchScreen");
7200 prepareDisplay(DISPLAY_ORIENTATION_0);
7201 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7202 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7203
7204 NotifyMotionArgs motionArgs;
7205
7206 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007207 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7208 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007209 processPosition(mapper, x1, y1);
7210 processSync(mapper);
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7214
7215 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007216 processSlot(mapper, SECOND_SLOT);
7217 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007218 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007219 processSync(mapper);
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7222 motionArgs.action);
7223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7224
7225 // If the tool type of the first finger changes to MT_TOOL_PALM,
7226 // we expect to receive ACTION_POINTER_UP with cancel flag.
7227 processSlot(mapper, FIRST_SLOT);
7228 processId(mapper, FIRST_TRACKING_ID);
7229 processToolType(mapper, MT_TOOL_PALM);
7230 processSync(mapper);
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7232 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7233 motionArgs.action);
7234 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7235
7236 // The following MOVE events of second finger should be processed.
7237 processSlot(mapper, SECOND_SLOT);
7238 processId(mapper, SECOND_TRACKING_ID);
7239 processPosition(mapper, x2 + 1, y2 + 1);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7243 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7244
7245 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7246 // it. Second finger receive move.
7247 processSlot(mapper, FIRST_SLOT);
7248 processId(mapper, INVALID_TRACKING_ID);
7249 processSync(mapper);
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7252 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7253
7254 // Second finger keeps moving.
7255 processSlot(mapper, SECOND_SLOT);
7256 processId(mapper, SECOND_TRACKING_ID);
7257 processPosition(mapper, x2 + 2, y2 + 2);
7258 processSync(mapper);
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7261 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7262
7263 // Second finger up.
7264 processId(mapper, INVALID_TRACKING_ID);
7265 processSync(mapper);
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7268 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7269}
7270
7271/**
7272 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7273 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7274 */
7275TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7276 addConfigurationProperty("touch.deviceType", "touchScreen");
7277 prepareDisplay(DISPLAY_ORIENTATION_0);
7278 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7279 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7280
7281 NotifyMotionArgs motionArgs;
7282
7283 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7284 // First finger down.
7285 processId(mapper, FIRST_TRACKING_ID);
7286 processPosition(mapper, x1, y1);
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7291
7292 // Second finger down.
7293 processSlot(mapper, SECOND_SLOT);
7294 processId(mapper, SECOND_TRACKING_ID);
7295 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7299 motionArgs.action);
7300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7301
arthurhungcc7f9802020-04-30 17:55:40 +08007302 // If the tool type of the first finger changes to MT_TOOL_PALM,
7303 // we expect to receive ACTION_POINTER_UP with cancel flag.
7304 processSlot(mapper, FIRST_SLOT);
7305 processId(mapper, FIRST_TRACKING_ID);
7306 processToolType(mapper, MT_TOOL_PALM);
7307 processSync(mapper);
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7309 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7310 motionArgs.action);
7311 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7312
7313 // Second finger keeps moving.
7314 processSlot(mapper, SECOND_SLOT);
7315 processId(mapper, SECOND_TRACKING_ID);
7316 processPosition(mapper, x2 + 1, y2 + 1);
7317 processSync(mapper);
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7320
7321 // second finger becomes palm, receive cancel due to only 1 finger is active.
7322 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007323 processToolType(mapper, MT_TOOL_PALM);
7324 processSync(mapper);
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7327
arthurhungcc7f9802020-04-30 17:55:40 +08007328 // third finger down.
7329 processSlot(mapper, THIRD_SLOT);
7330 processId(mapper, THIRD_TRACKING_ID);
7331 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007332 processPosition(mapper, x3, y3);
7333 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007337 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7338
7339 // third finger move
7340 processId(mapper, THIRD_TRACKING_ID);
7341 processPosition(mapper, x3 + 1, y3 + 1);
7342 processSync(mapper);
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7345
7346 // first finger up, third finger receive move.
7347 processSlot(mapper, FIRST_SLOT);
7348 processId(mapper, INVALID_TRACKING_ID);
7349 processSync(mapper);
7350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7352 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7353
7354 // second finger up, third finger receive move.
7355 processSlot(mapper, SECOND_SLOT);
7356 processId(mapper, INVALID_TRACKING_ID);
7357 processSync(mapper);
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7360 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7361
7362 // third finger up.
7363 processSlot(mapper, THIRD_SLOT);
7364 processId(mapper, INVALID_TRACKING_ID);
7365 processSync(mapper);
7366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7368 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7369}
7370
7371/**
7372 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7373 * and the active finger could still be allowed to receive the events
7374 */
7375TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7376 addConfigurationProperty("touch.deviceType", "touchScreen");
7377 prepareDisplay(DISPLAY_ORIENTATION_0);
7378 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7379 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7380
7381 NotifyMotionArgs motionArgs;
7382
7383 // default tool type is finger
7384 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7385 processId(mapper, FIRST_TRACKING_ID);
7386 processPosition(mapper, x1, y1);
7387 processSync(mapper);
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7389 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7391
7392 // Second finger down.
7393 processSlot(mapper, SECOND_SLOT);
7394 processId(mapper, SECOND_TRACKING_ID);
7395 processPosition(mapper, x2, y2);
7396 processSync(mapper);
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7398 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7399 motionArgs.action);
7400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7401
7402 // If the tool type of the second finger changes to MT_TOOL_PALM,
7403 // we expect to receive ACTION_POINTER_UP with cancel flag.
7404 processId(mapper, SECOND_TRACKING_ID);
7405 processToolType(mapper, MT_TOOL_PALM);
7406 processSync(mapper);
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7408 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7409 motionArgs.action);
7410 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7411
7412 // The following MOVE event should be processed.
7413 processSlot(mapper, FIRST_SLOT);
7414 processId(mapper, FIRST_TRACKING_ID);
7415 processPosition(mapper, x1 + 1, y1 + 1);
7416 processSync(mapper);
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7419 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7420
7421 // second finger up.
7422 processSlot(mapper, SECOND_SLOT);
7423 processId(mapper, INVALID_TRACKING_ID);
7424 processSync(mapper);
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7427
7428 // first finger keep moving
7429 processSlot(mapper, FIRST_SLOT);
7430 processId(mapper, FIRST_TRACKING_ID);
7431 processPosition(mapper, x1 + 2, y1 + 2);
7432 processSync(mapper);
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7435
7436 // first finger up.
7437 processId(mapper, INVALID_TRACKING_ID);
7438 processSync(mapper);
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7440 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7441 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007442}
7443
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007444// --- MultiTouchInputMapperTest_ExternalDevice ---
7445
7446class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7447protected:
7448 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07007449 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007450 }
7451};
7452
7453/**
7454 * Expect fallback to internal viewport if device is external and external viewport is not present.
7455 */
7456TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7457 prepareAxes(POSITION);
7458 addConfigurationProperty("touch.deviceType", "touchScreen");
7459 prepareDisplay(DISPLAY_ORIENTATION_0);
7460 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7461
7462 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7463
7464 NotifyMotionArgs motionArgs;
7465
7466 // Expect the event to be sent to the internal viewport,
7467 // because an external viewport is not present.
7468 processPosition(mapper, 100, 100);
7469 processSync(mapper);
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7471 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7472
7473 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007474 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007475 processPosition(mapper, 100, 100);
7476 processSync(mapper);
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7479}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007480
7481/**
7482 * Test touch should not work if outside of surface.
7483 */
7484class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7485protected:
7486 void halfDisplayToCenterHorizontal(int32_t orientation) {
7487 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007488 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007489
7490 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7491 internalViewport->orientation = orientation;
7492 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7493 internalViewport->logicalLeft = 0;
7494 internalViewport->logicalTop = 0;
7495 internalViewport->logicalRight = DISPLAY_HEIGHT;
7496 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7497
7498 internalViewport->physicalLeft = 0;
7499 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7500 internalViewport->physicalRight = DISPLAY_HEIGHT;
7501 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7502
7503 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7504 internalViewport->deviceHeight = DISPLAY_WIDTH;
7505 } else {
7506 internalViewport->logicalLeft = 0;
7507 internalViewport->logicalTop = 0;
7508 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7509 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7510
7511 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7512 internalViewport->physicalTop = 0;
7513 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7514 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7515
7516 internalViewport->deviceWidth = DISPLAY_WIDTH;
7517 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7518 }
7519
7520 mFakePolicy->updateViewport(internalViewport.value());
7521 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7522 }
7523
7524 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7525 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7526 int32_t yExpected) {
7527 // touch on outside area should not work.
7528 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7529 processSync(mapper);
7530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7531
7532 // touch on inside area should receive the event.
7533 NotifyMotionArgs args;
7534 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7535 processSync(mapper);
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7537 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7538 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7539
7540 // Reset.
7541 mapper.reset(ARBITRARY_TIME);
7542 }
7543};
7544
7545TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7546 addConfigurationProperty("touch.deviceType", "touchScreen");
7547 prepareDisplay(DISPLAY_ORIENTATION_0);
7548 prepareAxes(POSITION);
7549 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7550
7551 // Touch on center of normal display should work.
7552 const int32_t x = DISPLAY_WIDTH / 4;
7553 const int32_t y = DISPLAY_HEIGHT / 2;
7554 processPosition(mapper, toRawX(x), toRawY(y));
7555 processSync(mapper);
7556 NotifyMotionArgs args;
7557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7559 0.0f, 0.0f, 0.0f, 0.0f));
7560 // Reset.
7561 mapper.reset(ARBITRARY_TIME);
7562
7563 // Let physical display be different to device, and make surface and physical could be 1:1.
7564 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7565
7566 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7567 const int32_t yExpected = y;
7568 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7569}
7570
7571TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7572 addConfigurationProperty("touch.deviceType", "touchScreen");
7573 prepareDisplay(DISPLAY_ORIENTATION_0);
7574 prepareAxes(POSITION);
7575 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7576
7577 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7578 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7579
7580 const int32_t x = DISPLAY_WIDTH / 4;
7581 const int32_t y = DISPLAY_HEIGHT / 2;
7582
7583 // expect x/y = swap x/y then reverse y.
7584 const int32_t xExpected = y;
7585 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7586 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7587}
7588
7589TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7590 addConfigurationProperty("touch.deviceType", "touchScreen");
7591 prepareDisplay(DISPLAY_ORIENTATION_0);
7592 prepareAxes(POSITION);
7593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7594
7595 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7596 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7597
7598 const int32_t x = DISPLAY_WIDTH / 4;
7599 const int32_t y = DISPLAY_HEIGHT / 2;
7600
7601 // expect x/y = swap x/y then reverse x.
7602 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7603 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7604 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7605}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007606
7607TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7608 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7609 std::shared_ptr<FakePointerController> fakePointerController =
7610 std::make_shared<FakePointerController>();
7611 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7612 fakePointerController->setPosition(0, 0);
7613 fakePointerController->setButtonState(0);
7614
7615 // prepare device and capture
7616 prepareDisplay(DISPLAY_ORIENTATION_0);
7617 prepareAxes(POSITION | ID | SLOT);
7618 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7619 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7620 mFakePolicy->setPointerCapture(true);
7621 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7622 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7623
7624 // captured touchpad should be a touchpad source
7625 NotifyDeviceResetArgs resetArgs;
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7627 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7628
7629 // run captured pointer tests - note that this is unscaled, so input listener events should be
7630 // identical to what the hardware sends (accounting for any
7631 // calibration).
7632 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007633 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007634 processId(mapper, 1);
7635 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7636 processKey(mapper, BTN_TOUCH, 1);
7637 processSync(mapper);
7638
7639 // expect coord[0] to contain initial location of touch 0
7640 NotifyMotionArgs args;
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7643 ASSERT_EQ(1U, args.pointerCount);
7644 ASSERT_EQ(0, args.pointerProperties[0].id);
7645 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7646 ASSERT_NO_FATAL_FAILURE(
7647 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7648
7649 // FINGER 1 DOWN
7650 processSlot(mapper, 1);
7651 processId(mapper, 2);
7652 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7653 processSync(mapper);
7654
7655 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007657 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7658 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007659 ASSERT_EQ(2U, args.pointerCount);
7660 ASSERT_EQ(0, args.pointerProperties[0].id);
7661 ASSERT_EQ(1, args.pointerProperties[1].id);
7662 ASSERT_NO_FATAL_FAILURE(
7663 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7664 ASSERT_NO_FATAL_FAILURE(
7665 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7666
7667 // FINGER 1 MOVE
7668 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7669 processSync(mapper);
7670
7671 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7672 // from move
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7675 ASSERT_NO_FATAL_FAILURE(
7676 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7677 ASSERT_NO_FATAL_FAILURE(
7678 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7679
7680 // FINGER 0 MOVE
7681 processSlot(mapper, 0);
7682 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7683 processSync(mapper);
7684
7685 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7688 ASSERT_NO_FATAL_FAILURE(
7689 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7690 ASSERT_NO_FATAL_FAILURE(
7691 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7692
7693 // BUTTON DOWN
7694 processKey(mapper, BTN_LEFT, 1);
7695 processSync(mapper);
7696
7697 // touchinputmapper design sends a move before button press
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7701 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7702
7703 // BUTTON UP
7704 processKey(mapper, BTN_LEFT, 0);
7705 processSync(mapper);
7706
7707 // touchinputmapper design sends a move after button release
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7712
7713 // FINGER 0 UP
7714 processId(mapper, -1);
7715 processSync(mapper);
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7717 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7718
7719 // FINGER 1 MOVE
7720 processSlot(mapper, 1);
7721 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7722 processSync(mapper);
7723
7724 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7727 ASSERT_EQ(1U, args.pointerCount);
7728 ASSERT_EQ(1, args.pointerProperties[0].id);
7729 ASSERT_NO_FATAL_FAILURE(
7730 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7731
7732 // FINGER 1 UP
7733 processId(mapper, -1);
7734 processKey(mapper, BTN_TOUCH, 0);
7735 processSync(mapper);
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7737 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7738
7739 // non captured touchpad should be a mouse source
7740 mFakePolicy->setPointerCapture(false);
7741 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7743 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7744}
7745
7746TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7747 std::shared_ptr<FakePointerController> fakePointerController =
7748 std::make_shared<FakePointerController>();
7749 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7750 fakePointerController->setPosition(0, 0);
7751 fakePointerController->setButtonState(0);
7752
7753 // prepare device and capture
7754 prepareDisplay(DISPLAY_ORIENTATION_0);
7755 prepareAxes(POSITION | ID | SLOT);
7756 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7757 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7758 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7760 // run uncaptured pointer tests - pushes out generic events
7761 // FINGER 0 DOWN
7762 processId(mapper, 3);
7763 processPosition(mapper, 100, 100);
7764 processKey(mapper, BTN_TOUCH, 1);
7765 processSync(mapper);
7766
7767 // start at (100,100), cursor should be at (0,0) * scale
7768 NotifyMotionArgs args;
7769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7771 ASSERT_NO_FATAL_FAILURE(
7772 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7773
7774 // FINGER 0 MOVE
7775 processPosition(mapper, 200, 200);
7776 processSync(mapper);
7777
7778 // compute scaling to help with touch position checking
7779 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7780 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7781 float scale =
7782 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7783
7784 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7788 0, 0, 0, 0, 0, 0, 0));
7789}
7790
7791TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7792 std::shared_ptr<FakePointerController> fakePointerController =
7793 std::make_shared<FakePointerController>();
7794
7795 prepareDisplay(DISPLAY_ORIENTATION_0);
7796 prepareAxes(POSITION | ID | SLOT);
7797 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7798 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7799 mFakePolicy->setPointerCapture(false);
7800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7801
7802 // uncaptured touchpad should be a pointer device
7803 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7804
7805 // captured touchpad should be a touchpad device
7806 mFakePolicy->setPointerCapture(true);
7807 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7808 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7809}
7810
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811} // namespace android