blob: dbca7a21982291bbd1278fec5317dcfcce83d9f3 [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. Lewis0cab12d2019-11-05 02:17:02 +00001140 std::shared_ptr<InputDevice> mNextDevice;
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)
1146 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
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. Lewis0cab12d2019-11-05 02:17:02 +00001150 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = 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) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001168 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001169 mNextDevice = nullptr;
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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 mReader->setNextDevice(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);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455 mReader->setNextDevice(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);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001664 mReader->setNextDevice(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);
1697 mReader->setNextDevice(device);
1698 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);
Arthur Hungc23540e2018-11-29 20:42:11 +08001713 mReader->setNextDevice(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
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001750// --- InputReaderIntegrationTest ---
1751
1752// These tests create and interact with the InputReader only through its interface.
1753// The InputReader is started during SetUp(), which starts its processing in its own
1754// thread. The tests use linux uinput to emulate input devices.
1755// NOTE: Interacting with the physical device while these tests are running may cause
1756// the tests to fail.
1757class InputReaderIntegrationTest : public testing::Test {
1758protected:
1759 sp<TestInputListener> mTestListener;
1760 sp<FakeInputReaderPolicy> mFakePolicy;
1761 sp<InputReaderInterface> mReader;
1762
1763 virtual void SetUp() override {
1764 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001765 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1766 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001767
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001768 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001769 ASSERT_EQ(mReader->start(), OK);
1770
1771 // Since this test is run on a real device, all the input devices connected
1772 // to the test device will show up in mReader. We wait for those input devices to
1773 // show up before beginning the tests.
1774 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1775 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1776 }
1777
1778 virtual void TearDown() override {
1779 ASSERT_EQ(mReader->stop(), OK);
1780 mTestListener.clear();
1781 mFakePolicy.clear();
1782 }
1783};
1784
1785TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1786 // An invalid input device that is only used for this test.
1787 class InvalidUinputDevice : public UinputDevice {
1788 public:
1789 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1790
1791 private:
1792 void configureDevice(int fd, uinput_user_dev* device) override {}
1793 };
1794
1795 const size_t numDevices = mFakePolicy->getInputDevices().size();
1796
1797 // UinputDevice does not set any event or key bits, so InputReader should not
1798 // consider it as a valid device.
1799 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1800 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1801 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1802 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1803
1804 invalidDevice.reset();
1805 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1806 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1807 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1808}
1809
1810TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1811 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1812
1813 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1814 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1815 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1816 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1817
1818 // Find the test device by its name.
1819 std::vector<InputDeviceInfo> inputDevices;
1820 mReader->getInputDevices(inputDevices);
1821 InputDeviceInfo* keyboardInfo = nullptr;
1822 const char* keyboardName = keyboard->getName();
1823 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1824 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1825 keyboardInfo = &inputDevices[i];
1826 break;
1827 }
1828 }
1829 ASSERT_NE(keyboardInfo, nullptr);
1830 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1831 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1832 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1833
1834 keyboard.reset();
1835 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1836 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1837 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1838}
1839
1840TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1841 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1842 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1843
1844 NotifyConfigurationChangedArgs configChangedArgs;
1845 ASSERT_NO_FATAL_FAILURE(
1846 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001847 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001848 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1849
1850 NotifyKeyArgs keyArgs;
1851 keyboard->pressAndReleaseHomeKey();
1852 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1853 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001854 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001855 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001856 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1857 prevTimestamp = keyArgs.eventTime;
1858
1859 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1860 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001861 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001862 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1863}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001864
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001865/**
1866 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1867 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1868 * are passed to the listener.
1869 */
1870static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1871TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1872 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1873 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1874 NotifyKeyArgs keyArgs;
1875
1876 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1877 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1878 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1879 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1880
1881 controller->pressAndReleaseKey(BTN_GEAR_UP);
1882 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1883 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1884 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1885}
1886
Arthur Hungaab25622020-01-16 11:22:11 +08001887// --- TouchProcessTest ---
1888class TouchIntegrationTest : public InputReaderIntegrationTest {
1889protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001890 const std::string UNIQUE_ID = "local:0";
1891
1892 virtual void SetUp() override {
1893 InputReaderIntegrationTest::SetUp();
1894 // At least add an internal display.
1895 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1896 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001897 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001898
1899 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1900 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1901 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1902 }
1903
1904 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1905 int32_t orientation, const std::string& uniqueId,
1906 std::optional<uint8_t> physicalPort,
1907 ViewportType viewportType) {
1908 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1909 physicalPort, viewportType);
1910 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1911 }
1912
1913 std::unique_ptr<UinputTouchScreen> mDevice;
1914};
1915
1916TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1917 NotifyMotionArgs args;
1918 const Point centerPoint = mDevice->getCenterPoint();
1919
1920 // ACTION_DOWN
1921 mDevice->sendDown(centerPoint);
1922 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1924
1925 // ACTION_MOVE
1926 mDevice->sendMove(centerPoint + Point(1, 1));
1927 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1929
1930 // ACTION_UP
1931 mDevice->sendUp();
1932 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1933 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1934}
1935
1936TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1937 NotifyMotionArgs args;
1938 const Point centerPoint = mDevice->getCenterPoint();
1939
1940 // ACTION_DOWN
1941 mDevice->sendDown(centerPoint);
1942 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1943 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1944
1945 // ACTION_POINTER_DOWN (Second slot)
1946 const Point secondPoint = centerPoint + Point(100, 100);
1947 mDevice->sendSlot(SECOND_SLOT);
1948 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1949 mDevice->sendDown(secondPoint + Point(1, 1));
1950 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1951 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1952 args.action);
1953
1954 // ACTION_MOVE (Second slot)
1955 mDevice->sendMove(secondPoint);
1956 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1958
1959 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001960 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08001961 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08001962 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08001963 args.action);
1964
1965 // ACTION_UP
1966 mDevice->sendSlot(FIRST_SLOT);
1967 mDevice->sendUp();
1968 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1969 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1970}
1971
1972TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1973 NotifyMotionArgs args;
1974 const Point centerPoint = mDevice->getCenterPoint();
1975
1976 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001977 mDevice->sendSlot(FIRST_SLOT);
1978 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001979 mDevice->sendDown(centerPoint);
1980 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1981 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1982
arthurhungcc7f9802020-04-30 17:55:40 +08001983 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001984 const Point secondPoint = centerPoint + Point(100, 100);
1985 mDevice->sendSlot(SECOND_SLOT);
1986 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1987 mDevice->sendDown(secondPoint);
1988 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1989 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1990 args.action);
1991
arthurhungcc7f9802020-04-30 17:55:40 +08001992 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001993 mDevice->sendMove(secondPoint + Point(1, 1));
1994 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1996
arthurhungcc7f9802020-04-30 17:55:40 +08001997 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1998 // a palm event.
1999 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002000 mDevice->sendToolType(MT_TOOL_PALM);
2001 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002002 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2003 args.action);
2004 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002005
arthurhungcc7f9802020-04-30 17:55:40 +08002006 // Send up to second slot, expect first slot send moving.
2007 mDevice->sendPointerUp();
2008 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002010
arthurhungcc7f9802020-04-30 17:55:40 +08002011 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002012 mDevice->sendSlot(FIRST_SLOT);
2013 mDevice->sendUp();
2014
arthurhungcc7f9802020-04-30 17:55:40 +08002015 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2016 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002017}
2018
Michael Wrightd02c5b62014-02-10 15:10:22 -08002019// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002020class InputDeviceTest : public testing::Test {
2021protected:
2022 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002023 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002024 static const int32_t DEVICE_ID;
2025 static const int32_t DEVICE_GENERATION;
2026 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002027 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002028 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002029
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002030 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002031 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002032 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002033 FakeInputReaderContext* mFakeContext;
2034
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002035 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002036
Prabir Pradhan28efc192019-11-05 01:10:04 +00002037 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002038 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002040 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002041 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2042
Chris Ye1b0c7342020-07-28 21:57:03 -07002043 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002044 InputDeviceIdentifier identifier;
2045 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002046 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002047 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2048 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002049 }
2050
Prabir Pradhan28efc192019-11-05 01:10:04 +00002051 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002052 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002053 delete mFakeContext;
2054 mFakeListener.clear();
2055 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002056 }
2057};
2058
2059const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002060const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002061const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002062const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2063const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002064const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2065 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002066const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002067
2068TEST_F(InputDeviceTest, ImmutableProperties) {
2069 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002070 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002071 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002072}
2073
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002074TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2075 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002076}
2077
Michael Wrightd02c5b62014-02-10 15:10:22 -08002078TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2079 // Configuration.
2080 InputReaderConfiguration config;
2081 mDevice->configure(ARBITRARY_TIME, &config, 0);
2082
2083 // Reset.
2084 mDevice->reset(ARBITRARY_TIME);
2085
2086 NotifyDeviceResetArgs resetArgs;
2087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2088 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2089 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2090
2091 // Metadata.
2092 ASSERT_TRUE(mDevice->isIgnored());
2093 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2094
2095 InputDeviceInfo info;
2096 mDevice->getDeviceInfo(&info);
2097 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002098 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002099 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2100 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2101
2102 // State queries.
2103 ASSERT_EQ(0, mDevice->getMetaState());
2104
2105 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2106 << "Ignored device should return unknown key code state.";
2107 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2108 << "Ignored device should return unknown scan code state.";
2109 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2110 << "Ignored device should return unknown switch state.";
2111
2112 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2113 uint8_t flags[2] = { 0, 1 };
2114 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2115 << "Ignored device should never mark any key codes.";
2116 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2117 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2118}
2119
2120TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2121 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002122 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002123
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002124 FakeInputMapper& mapper1 =
2125 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002126 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2127 mapper1.setMetaState(AMETA_ALT_ON);
2128 mapper1.addSupportedKeyCode(AKEYCODE_A);
2129 mapper1.addSupportedKeyCode(AKEYCODE_B);
2130 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2131 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2132 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2133 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2134 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002135
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002136 FakeInputMapper& mapper2 =
2137 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002138 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002139
2140 InputReaderConfiguration config;
2141 mDevice->configure(ARBITRARY_TIME, &config, 0);
2142
2143 String8 propertyValue;
2144 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2145 << "Device should have read configuration during configuration phase.";
2146 ASSERT_STREQ("value", propertyValue.string());
2147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002148 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2149 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002150
2151 // Reset
2152 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002153 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2154 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002155
2156 NotifyDeviceResetArgs resetArgs;
2157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2158 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2159 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2160
2161 // Metadata.
2162 ASSERT_FALSE(mDevice->isIgnored());
2163 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2164
2165 InputDeviceInfo info;
2166 mDevice->getDeviceInfo(&info);
2167 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002168 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002169 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2170 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2171
2172 // State queries.
2173 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2174 << "Should query mappers and combine meta states.";
2175
2176 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2177 << "Should return unknown key code state when source not supported.";
2178 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2179 << "Should return unknown scan code state when source not supported.";
2180 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2181 << "Should return unknown switch state when source not supported.";
2182
2183 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2184 << "Should query mapper when source is supported.";
2185 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2186 << "Should query mapper when source is supported.";
2187 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2188 << "Should query mapper when source is supported.";
2189
2190 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2191 uint8_t flags[4] = { 0, 0, 0, 1 };
2192 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2193 << "Should do nothing when source is unsupported.";
2194 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2195 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2196 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2197 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2198
2199 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2200 << "Should query mapper when source is supported.";
2201 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2202 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2203 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2204 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2205
2206 // Event handling.
2207 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002208 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002209 mDevice->process(&event, 1);
2210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002211 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2212 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002213}
2214
Arthur Hung2c9a3342019-07-23 14:18:59 +08002215// A single input device is associated with a specific display. Check that:
2216// 1. Device is disabled if the viewport corresponding to the associated display is not found
2217// 2. Device is disabled when setEnabled API is called
2218TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002219 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002220
2221 // First Configuration.
2222 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2223
2224 // Device should be enabled by default.
2225 ASSERT_TRUE(mDevice->isEnabled());
2226
2227 // Prepare associated info.
2228 constexpr uint8_t hdmi = 1;
2229 const std::string UNIQUE_ID = "local:1";
2230
2231 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2232 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2233 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2234 // Device should be disabled because it is associated with a specific display via
2235 // input port <-> display port association, but the corresponding display is not found
2236 ASSERT_FALSE(mDevice->isEnabled());
2237
2238 // Prepare displays.
2239 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002240 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002241 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2242 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2243 ASSERT_TRUE(mDevice->isEnabled());
2244
2245 // Device should be disabled after set disable.
2246 mFakePolicy->addDisabledDevice(mDevice->getId());
2247 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2248 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2249 ASSERT_FALSE(mDevice->isEnabled());
2250
2251 // Device should still be disabled even found the associated display.
2252 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2253 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2254 ASSERT_FALSE(mDevice->isEnabled());
2255}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256
2257// --- InputMapperTest ---
2258
2259class InputMapperTest : public testing::Test {
2260protected:
2261 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002262 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002263 static const int32_t DEVICE_ID;
2264 static const int32_t DEVICE_GENERATION;
2265 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002266 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002267 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002269 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002271 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002272 FakeInputReaderContext* mFakeContext;
2273 InputDevice* mDevice;
2274
Chris Ye1b0c7342020-07-28 21:57:03 -07002275 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002276 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002278 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2280 InputDeviceIdentifier identifier;
2281 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002282 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002283 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002285 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 }
2287
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002288 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2289
Prabir Pradhan28efc192019-11-05 01:10:04 +00002290 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291 delete mDevice;
2292 delete mFakeContext;
2293 mFakeListener.clear();
2294 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002295 }
2296
2297 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002298 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002299 }
2300
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002301 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002302 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2303 mFakeContext->updatePointerDisplay();
2304 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002305 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2306 }
2307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002308 template <class T, typename... Args>
2309 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002310 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002311 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002313 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314 }
2315
2316 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002317 int32_t orientation, const std::string& uniqueId,
2318 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002319 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002320 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002321 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2322 }
2323
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002324 void clearViewports() {
2325 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326 }
2327
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002328 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2329 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002330 RawEvent event;
2331 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002332 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333 event.type = type;
2334 event.code = code;
2335 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002336 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337 }
2338
2339 static void assertMotionRange(const InputDeviceInfo& info,
2340 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2341 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002342 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2344 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2345 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2346 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2347 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2348 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2349 }
2350
2351 static void assertPointerCoords(const PointerCoords& coords,
2352 float x, float y, float pressure, float size,
2353 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2354 float orientation, float distance) {
2355 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2356 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2357 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2358 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2359 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2360 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2361 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2362 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2363 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2364 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2365 }
2366
Michael Wright17db18e2020-06-26 20:51:44 +01002367 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002369 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370 ASSERT_NEAR(x, actualX, 1);
2371 ASSERT_NEAR(y, actualY, 1);
2372 }
2373};
2374
2375const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002376const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002377const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2379const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002380const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2381 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002382const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383
2384// --- SwitchInputMapperTest ---
2385
2386class SwitchInputMapperTest : public InputMapperTest {
2387protected:
2388};
2389
2390TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002391 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002393 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394}
2395
2396TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002397 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002399 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002400 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002402 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002403 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404}
2405
2406TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002407 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002409 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2410 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2411 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2412 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413
2414 NotifySwitchArgs args;
2415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2416 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002417 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2418 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 args.switchMask);
2420 ASSERT_EQ(uint32_t(0), args.policyFlags);
2421}
2422
2423
2424// --- KeyboardInputMapperTest ---
2425
2426class KeyboardInputMapperTest : public InputMapperTest {
2427protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002428 const std::string UNIQUE_ID = "local:0";
2429
2430 void prepareDisplay(int32_t orientation);
2431
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002432 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002433 int32_t originalKeyCode, int32_t rotatedKeyCode,
2434 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435};
2436
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002437/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2438 * orientation.
2439 */
2440void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002441 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2442 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002443}
2444
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002445void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002446 int32_t originalScanCode, int32_t originalKeyCode,
2447 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448 NotifyKeyArgs args;
2449
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002450 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2452 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2453 ASSERT_EQ(originalScanCode, args.scanCode);
2454 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002455 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002457 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2460 ASSERT_EQ(originalScanCode, args.scanCode);
2461 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002462 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463}
2464
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002466 KeyboardInputMapper& mapper =
2467 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2468 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002470 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471}
2472
2473TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2474 const int32_t USAGE_A = 0x070004;
2475 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002476 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2477 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002479 KeyboardInputMapper& mapper =
2480 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2481 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482
2483 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002484 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485 NotifyKeyArgs args;
2486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2487 ASSERT_EQ(DEVICE_ID, args.deviceId);
2488 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2489 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2490 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2491 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2492 ASSERT_EQ(KEY_HOME, args.scanCode);
2493 ASSERT_EQ(AMETA_NONE, args.metaState);
2494 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2495 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2496 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2497
2498 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002499 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2501 ASSERT_EQ(DEVICE_ID, args.deviceId);
2502 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2503 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2504 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2505 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2506 ASSERT_EQ(KEY_HOME, args.scanCode);
2507 ASSERT_EQ(AMETA_NONE, args.metaState);
2508 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2509 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2510 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2511
2512 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002513 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2514 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2516 ASSERT_EQ(DEVICE_ID, args.deviceId);
2517 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2518 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2519 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2520 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2521 ASSERT_EQ(0, args.scanCode);
2522 ASSERT_EQ(AMETA_NONE, args.metaState);
2523 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2524 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2525 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2526
2527 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002528 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2529 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2531 ASSERT_EQ(DEVICE_ID, args.deviceId);
2532 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2533 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2535 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2536 ASSERT_EQ(0, args.scanCode);
2537 ASSERT_EQ(AMETA_NONE, args.metaState);
2538 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2539 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2540 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2541
2542 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002543 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2544 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2546 ASSERT_EQ(DEVICE_ID, args.deviceId);
2547 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2548 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2549 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2550 ASSERT_EQ(0, args.keyCode);
2551 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2552 ASSERT_EQ(AMETA_NONE, args.metaState);
2553 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2554 ASSERT_EQ(0U, args.policyFlags);
2555 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2556
2557 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002558 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2559 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2561 ASSERT_EQ(DEVICE_ID, args.deviceId);
2562 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2563 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2564 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2565 ASSERT_EQ(0, args.keyCode);
2566 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2567 ASSERT_EQ(AMETA_NONE, args.metaState);
2568 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2569 ASSERT_EQ(0U, args.policyFlags);
2570 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2571}
2572
2573TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002577 KeyboardInputMapper& mapper =
2578 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2579 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580
2581 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583
2584 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002585 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002586 NotifyKeyArgs args;
2587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2591
2592 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002593 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2595 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597
2598 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002599 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2601 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603
2604 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002605 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2607 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002608 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2610}
2611
2612TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 KeyboardInputMapper& mapper =
2619 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2620 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002622 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2624 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2626 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2627 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2628 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2629 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2630 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2631}
2632
2633TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002640 KeyboardInputMapper& mapper =
2641 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2642 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002644 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002645 ASSERT_NO_FATAL_FAILURE(
2646 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2648 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2649 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2650 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2651 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2652 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002654 clearViewports();
2655 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002656 ASSERT_NO_FATAL_FAILURE(
2657 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2659 AKEYCODE_DPAD_UP, DISPLAY_ID));
2660 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2661 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2663 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002665 clearViewports();
2666 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002667 ASSERT_NO_FATAL_FAILURE(
2668 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2670 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2671 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2672 AKEYCODE_DPAD_UP, DISPLAY_ID));
2673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2674 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002676 clearViewports();
2677 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002678 ASSERT_NO_FATAL_FAILURE(
2679 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2681 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2682 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2683 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2684 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2685 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686
2687 // Special case: if orientation changes while key is down, we still emit the same keycode
2688 // in the key up as we did in the key down.
2689 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002690 clearViewports();
2691 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002692 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2694 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2695 ASSERT_EQ(KEY_UP, args.scanCode);
2696 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2697
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002698 clearViewports();
2699 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002700 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2702 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2703 ASSERT_EQ(KEY_UP, args.scanCode);
2704 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2705}
2706
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002707TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2708 // If the keyboard is not orientation aware,
2709 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002710 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002712 KeyboardInputMapper& mapper =
2713 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002715 NotifyKeyArgs args;
2716
2717 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002718 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002720 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2722 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2723
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002724 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002725 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002727 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2729 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2730}
2731
2732TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2733 // If the keyboard is orientation aware,
2734 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002735 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002736
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002737 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002738 KeyboardInputMapper& mapper =
2739 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002741 NotifyKeyArgs args;
2742
2743 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2744 // ^--- already checked by the previous test
2745
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002746 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002747 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002748 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002750 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2752 ASSERT_EQ(DISPLAY_ID, args.displayId);
2753
2754 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002755 clearViewports();
2756 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002757 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002758 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002760 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2762 ASSERT_EQ(newDisplayId, args.displayId);
2763}
2764
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 KeyboardInputMapper& mapper =
2767 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2768 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002770 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002771 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002773 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002774 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775}
2776
2777TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002778 KeyboardInputMapper& mapper =
2779 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2780 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002782 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002783 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002785 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002786 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787}
2788
2789TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002790 KeyboardInputMapper& mapper =
2791 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2792 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795
2796 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2797 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002798 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 ASSERT_TRUE(flags[0]);
2800 ASSERT_FALSE(flags[1]);
2801}
2802
2803TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002804 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2805 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2806 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2808 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2809 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002811 KeyboardInputMapper& mapper =
2812 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2813 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814
2815 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819
2820 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002821 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2822 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827
2828 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002829 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2830 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2832 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002834 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835
2836 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002837 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2838 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002839 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2840 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002842 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
2844 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002845 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2846 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002847 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2848 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2849 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002850 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
2852 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002853 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2854 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2856 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2857 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002858 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859
2860 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002861 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2862 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2865 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002866 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867}
2868
Arthur Hung2c9a3342019-07-23 14:18:59 +08002869TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2870 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2874 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002875
2876 // keyboard 2.
2877 const std::string USB2 = "USB2";
2878 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002879 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002880 InputDeviceIdentifier identifier;
2881 identifier.name = "KEYBOARD2";
2882 identifier.location = USB2;
2883 std::unique_ptr<InputDevice> device2 =
2884 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002885 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07002886 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME,
2887 Flags<InputDeviceClass>(0) /*classes*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002888 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2889 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2890 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2891 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002893 KeyboardInputMapper& mapper =
2894 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2895 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002897 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002898 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002899 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002900 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2901 device2->reset(ARBITRARY_TIME);
2902
2903 // Prepared displays and associated info.
2904 constexpr uint8_t hdmi1 = 0;
2905 constexpr uint8_t hdmi2 = 1;
2906 const std::string SECONDARY_UNIQUE_ID = "local:1";
2907
2908 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2909 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2910
2911 // No associated display viewport found, should disable the device.
2912 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2913 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2914 ASSERT_FALSE(device2->isEnabled());
2915
2916 // Prepare second display.
2917 constexpr int32_t newDisplayId = 2;
2918 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002919 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002920 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002921 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002922 // Default device will reconfigure above, need additional reconfiguration for another device.
2923 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2924 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2925
2926 // Device should be enabled after the associated display is found.
2927 ASSERT_TRUE(mDevice->isEnabled());
2928 ASSERT_TRUE(device2->isEnabled());
2929
2930 // Test pad key events
2931 ASSERT_NO_FATAL_FAILURE(
2932 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2933 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2934 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2935 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2936 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2937 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2938 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2939
2940 ASSERT_NO_FATAL_FAILURE(
2941 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2942 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2943 AKEYCODE_DPAD_RIGHT, newDisplayId));
2944 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2945 AKEYCODE_DPAD_DOWN, newDisplayId));
2946 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2947 AKEYCODE_DPAD_LEFT, newDisplayId));
2948}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002950// --- KeyboardInputMapperTest_ExternalDevice ---
2951
2952class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2953protected:
2954 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07002955 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002956 }
2957};
2958
2959TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002960 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2961 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002962
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2966 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002968 KeyboardInputMapper& mapper =
2969 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2970 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002971
2972 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2973 NotifyKeyArgs args;
2974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2975 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2976
2977 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2979 ASSERT_EQ(uint32_t(0), args.policyFlags);
2980
2981 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(uint32_t(0), args.policyFlags);
2984
2985 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2987 ASSERT_EQ(uint32_t(0), args.policyFlags);
2988
2989 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2991 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2992
2993 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2996}
2997
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002998TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002999 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003000
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003004
Powei Fengd041c5d2019-05-03 17:11:33 -07003005 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003006 KeyboardInputMapper& mapper =
3007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003009
3010 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3011 NotifyKeyArgs args;
3012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3013 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3014
3015 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3017 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3018
3019 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3021 ASSERT_EQ(uint32_t(0), args.policyFlags);
3022
3023 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3025 ASSERT_EQ(uint32_t(0), args.policyFlags);
3026
3027 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3029 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3030
3031 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3033 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3034}
3035
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036// --- CursorInputMapperTest ---
3037
3038class CursorInputMapperTest : public InputMapperTest {
3039protected:
3040 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3041
Michael Wright17db18e2020-06-26 20:51:44 +01003042 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043
Prabir Pradhan28efc192019-11-05 01:10:04 +00003044 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 InputMapperTest::SetUp();
3046
Michael Wright17db18e2020-06-26 20:51:44 +01003047 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003048 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 }
3050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003051 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3052 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003053
3054 void prepareDisplay(int32_t orientation) {
3055 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003056 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003057 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3058 orientation, uniqueId, NO_PORT, viewportType);
3059 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060};
3061
3062const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3065 int32_t originalY, int32_t rotatedX,
3066 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 NotifyMotionArgs args;
3068
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003069 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3070 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3071 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3075 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3076 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3077 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3078}
3079
3080TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003082 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085}
3086
3087TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
3094TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003096 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097
3098 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
3101 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003102 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3103 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3105 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3106
3107 // When the bounds are set, then there should be a valid motion range.
3108 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3109
3110 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003111 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112
3113 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3114 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3115 1, 800 - 1, 0.0f, 0.0f));
3116 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3117 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3118 2, 480 - 1, 0.0f, 0.0f));
3119 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3120 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3121 0.0f, 1.0f, 0.0f, 0.0f));
3122}
3123
3124TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127
3128 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003129 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130
3131 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3132 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3133 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3134 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3135 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3136 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3137 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3138 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3139 0.0f, 1.0f, 0.0f, 0.0f));
3140}
3141
3142TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003144 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145
3146 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3147
3148 NotifyMotionArgs args;
3149
3150 // Button press.
3151 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003152 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3153 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3155 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3156 ASSERT_EQ(DEVICE_ID, args.deviceId);
3157 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3158 ASSERT_EQ(uint32_t(0), args.policyFlags);
3159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3160 ASSERT_EQ(0, args.flags);
3161 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3162 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3163 ASSERT_EQ(0, args.edgeFlags);
3164 ASSERT_EQ(uint32_t(1), args.pointerCount);
3165 ASSERT_EQ(0, args.pointerProperties[0].id);
3166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3168 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3169 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3170 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3171 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3172
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3174 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3175 ASSERT_EQ(DEVICE_ID, args.deviceId);
3176 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3177 ASSERT_EQ(uint32_t(0), args.policyFlags);
3178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3179 ASSERT_EQ(0, args.flags);
3180 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3181 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3182 ASSERT_EQ(0, args.edgeFlags);
3183 ASSERT_EQ(uint32_t(1), args.pointerCount);
3184 ASSERT_EQ(0, args.pointerProperties[0].id);
3185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3187 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3188 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3189 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3190 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3191
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003193 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3194 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3196 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3197 ASSERT_EQ(DEVICE_ID, args.deviceId);
3198 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3199 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3201 ASSERT_EQ(0, args.flags);
3202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3203 ASSERT_EQ(0, args.buttonState);
3204 ASSERT_EQ(0, args.edgeFlags);
3205 ASSERT_EQ(uint32_t(1), args.pointerCount);
3206 ASSERT_EQ(0, args.pointerProperties[0].id);
3207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3209 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3210 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3211 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3212 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3213
3214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3215 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3216 ASSERT_EQ(DEVICE_ID, args.deviceId);
3217 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3218 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3220 ASSERT_EQ(0, args.flags);
3221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3222 ASSERT_EQ(0, args.buttonState);
3223 ASSERT_EQ(0, args.edgeFlags);
3224 ASSERT_EQ(uint32_t(1), args.pointerCount);
3225 ASSERT_EQ(0, args.pointerProperties[0].id);
3226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3228 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3229 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3230 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3231 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3232}
3233
3234TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003236 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237
3238 NotifyMotionArgs args;
3239
3240 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003241 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3242 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3246 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3247
3248 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003249 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3250 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3254 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3255}
3256
3257TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003259 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260
3261 NotifyMotionArgs args;
3262
3263 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003264 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3265 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3269 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3270
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3274 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3275
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003277 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3278 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3282 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3283
3284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3287 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3288}
3289
3290TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003292 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
3294 NotifyMotionArgs args;
3295
3296 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003297 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3298 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3299 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3300 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3302 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3304 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3305 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3306
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3308 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3310 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3311 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3312
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003314 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3315 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3316 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3320 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3321 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3322
3323 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003324 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3325 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3329 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3330
3331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3334 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3335}
3336
3337TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003339 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003341 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3345 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3346 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3350}
3351
3352TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 addConfigurationProperty("cursor.mode", "navigation");
3354 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003355 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003357 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3365 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3366
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003367 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3376
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003377 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3384 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3385 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3386
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003387 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3395 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3396}
3397
3398TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003400 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401
3402 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3403 mFakePointerController->setPosition(100, 200);
3404 mFakePointerController->setButtonState(0);
3405
3406 NotifyMotionArgs motionArgs;
3407 NotifyKeyArgs keyArgs;
3408
3409 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003410 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3411 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3413 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3414 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3415 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3417 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3418
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3421 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3422 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3424 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3425
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003426 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3427 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_EQ(0, motionArgs.buttonState);
3431 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3433 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3434
3435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003436 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437 ASSERT_EQ(0, motionArgs.buttonState);
3438 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3440 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3441
3442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003444 ASSERT_EQ(0, motionArgs.buttonState);
3445 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3447 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3448
3449 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003450 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3451 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3452 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3455 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3456 motionArgs.buttonState);
3457 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3458 mFakePointerController->getButtonState());
3459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3460 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3461
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3464 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3465 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3466 mFakePointerController->getButtonState());
3467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3468 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3469
3470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3472 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3473 motionArgs.buttonState);
3474 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3475 mFakePointerController->getButtonState());
3476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3477 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3478
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003479 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3480 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3484 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3486 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3487
3488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003490 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3491 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3493 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3494
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003495 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3496 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3499 ASSERT_EQ(0, motionArgs.buttonState);
3500 ASSERT_EQ(0, mFakePointerController->getButtonState());
3501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3502 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 -08003503 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3504 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003505
3506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507 ASSERT_EQ(0, motionArgs.buttonState);
3508 ASSERT_EQ(0, mFakePointerController->getButtonState());
3509 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3511 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 -08003512
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3514 ASSERT_EQ(0, motionArgs.buttonState);
3515 ASSERT_EQ(0, mFakePointerController->getButtonState());
3516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3518 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3519
3520 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003521 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3522 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3524 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3525 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003526
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3530 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3532 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3533
3534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3536 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3537 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3539 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3540
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003541 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3542 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545 ASSERT_EQ(0, motionArgs.buttonState);
3546 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3548 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3549
3550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003552 ASSERT_EQ(0, motionArgs.buttonState);
3553 ASSERT_EQ(0, mFakePointerController->getButtonState());
3554
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3556 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3558 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3559 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3560
3561 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003562 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3563 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3566 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003567
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3571 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3573 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3574
3575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3577 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3578 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3580 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3581
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003582 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3583 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_EQ(0, motionArgs.buttonState);
3587 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3589 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 -08003590
3591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3593 ASSERT_EQ(0, motionArgs.buttonState);
3594 ASSERT_EQ(0, mFakePointerController->getButtonState());
3595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3596 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3597
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3600 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3601
3602 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003603 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3604 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3607 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3614 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3615
3616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3621 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3622
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003623 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3624 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_EQ(0, motionArgs.buttonState);
3628 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3630 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003631
3632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3634 ASSERT_EQ(0, motionArgs.buttonState);
3635 ASSERT_EQ(0, mFakePointerController->getButtonState());
3636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3637 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3638
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3641 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3642
3643 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003644 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3645 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3647 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3648 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003649
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3653 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3655 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3656
3657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3659 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3660 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3662 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3663
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003664 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3665 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_EQ(0, motionArgs.buttonState);
3669 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3671 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003672
3673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3675 ASSERT_EQ(0, motionArgs.buttonState);
3676 ASSERT_EQ(0, mFakePointerController->getButtonState());
3677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3678 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3679
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3681 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3682 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3683}
3684
3685TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003687 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688
3689 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3690 mFakePointerController->setPosition(100, 200);
3691 mFakePointerController->setButtonState(0);
3692
3693 NotifyMotionArgs args;
3694
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003695 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3696 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3697 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003699 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3702 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 +01003703 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003704}
3705
3706TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003707 addConfigurationProperty("cursor.mode", "pointer");
3708 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003709 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003710
3711 NotifyDeviceResetArgs resetArgs;
3712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3713 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3714 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3715
3716 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3717 mFakePointerController->setPosition(100, 200);
3718 mFakePointerController->setButtonState(0);
3719
3720 NotifyMotionArgs args;
3721
3722 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003723 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3724 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3725 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3727 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3730 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 +01003731 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003732
3733 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003734 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3735 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3737 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3740 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3742 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3745 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3746
3747 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003748 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3749 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3751 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3754 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3756 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3757 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3759 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3760
3761 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003762 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3763 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3764 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3766 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3769 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 +01003770 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003771
3772 // Disable pointer capture and check that the device generation got bumped
3773 // and events are generated the usual way.
3774 const uint32_t generation = mFakeContext->getGeneration();
3775 mFakePolicy->setPointerCapture(false);
3776 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3777 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3778
3779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3780 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3781 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3782
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003783 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3784 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3785 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3787 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3790 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 +01003791 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792}
3793
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003794TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003796
Garfield Tan888a6a42020-01-09 11:39:16 -08003797 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003798 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003799 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3800 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003801 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003802 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3803 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3804
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003805 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3806 mFakePointerController->setPosition(100, 200);
3807 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003808
3809 NotifyMotionArgs args;
3810 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3811 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3812 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3814 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3815 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3817 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 +01003818 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003819 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3820}
3821
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822// --- TouchInputMapperTest ---
3823
3824class TouchInputMapperTest : public InputMapperTest {
3825protected:
3826 static const int32_t RAW_X_MIN;
3827 static const int32_t RAW_X_MAX;
3828 static const int32_t RAW_Y_MIN;
3829 static const int32_t RAW_Y_MAX;
3830 static const int32_t RAW_TOUCH_MIN;
3831 static const int32_t RAW_TOUCH_MAX;
3832 static const int32_t RAW_TOOL_MIN;
3833 static const int32_t RAW_TOOL_MAX;
3834 static const int32_t RAW_PRESSURE_MIN;
3835 static const int32_t RAW_PRESSURE_MAX;
3836 static const int32_t RAW_ORIENTATION_MIN;
3837 static const int32_t RAW_ORIENTATION_MAX;
3838 static const int32_t RAW_DISTANCE_MIN;
3839 static const int32_t RAW_DISTANCE_MAX;
3840 static const int32_t RAW_TILT_MIN;
3841 static const int32_t RAW_TILT_MAX;
3842 static const int32_t RAW_ID_MIN;
3843 static const int32_t RAW_ID_MAX;
3844 static const int32_t RAW_SLOT_MIN;
3845 static const int32_t RAW_SLOT_MAX;
3846 static const float X_PRECISION;
3847 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003848 static const float X_PRECISION_VIRTUAL;
3849 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850
3851 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003852 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853
3854 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3855
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003856 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003857 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003858
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859 enum Axes {
3860 POSITION = 1 << 0,
3861 TOUCH = 1 << 1,
3862 TOOL = 1 << 2,
3863 PRESSURE = 1 << 3,
3864 ORIENTATION = 1 << 4,
3865 MINOR = 1 << 5,
3866 ID = 1 << 6,
3867 DISTANCE = 1 << 7,
3868 TILT = 1 << 8,
3869 SLOT = 1 << 9,
3870 TOOL_TYPE = 1 << 10,
3871 };
3872
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003873 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3874 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003875 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003877 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878 int32_t toRawX(float displayX);
3879 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003880 float toCookedX(float rawX, float rawY);
3881 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003883 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003885 float toDisplayY(int32_t rawY, int32_t displayHeight);
3886
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887};
3888
3889const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3890const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3891const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3892const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3893const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3894const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3895const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3896const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003897const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3898const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3900const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3901const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3902const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3903const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3904const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3905const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3906const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3907const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3908const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3909const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3910const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003911const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3912 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3913const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3914 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003915const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3916 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917
3918const float TouchInputMapperTest::GEOMETRIC_SCALE =
3919 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3920 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3921
3922const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3923 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3924 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3925};
3926
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003927void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003928 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3929 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003930}
3931
3932void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3933 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3934 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935}
3936
Santos Cordonfa5cf462017-04-05 10:37:00 -07003937void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003938 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
3939 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
3940 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003941}
3942
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003944 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3945 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3946 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3947 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948}
3949
Jason Gerecke489fda82012-09-07 17:19:40 -07003950void TouchInputMapperTest::prepareLocationCalibration() {
3951 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3952}
3953
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954int32_t TouchInputMapperTest::toRawX(float displayX) {
3955 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3956}
3957
3958int32_t TouchInputMapperTest::toRawY(float displayY) {
3959 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3960}
3961
Jason Gerecke489fda82012-09-07 17:19:40 -07003962float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3963 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3964 return rawX;
3965}
3966
3967float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3968 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3969 return rawY;
3970}
3971
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003973 return toDisplayX(rawX, DISPLAY_WIDTH);
3974}
3975
3976float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3977 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978}
3979
3980float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003981 return toDisplayY(rawY, DISPLAY_HEIGHT);
3982}
3983
3984float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3985 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986}
3987
3988
3989// --- SingleTouchInputMapperTest ---
3990
3991class SingleTouchInputMapperTest : public TouchInputMapperTest {
3992protected:
3993 void prepareButtons();
3994 void prepareAxes(int axes);
3995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003996 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3997 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3998 void processUp(SingleTouchInputMapper& mappery);
3999 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4000 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4001 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4002 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4003 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4004 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005};
4006
4007void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004008 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009}
4010
4011void SingleTouchInputMapperTest::prepareAxes(int axes) {
4012 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 }
4016 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004017 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4018 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 }
4020 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4022 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 }
4024 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004025 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4026 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027 }
4028 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004029 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4030 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 }
4032}
4033
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004034void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004035 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4036 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4037 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038}
4039
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004040void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004041 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4042 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004046 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047}
4048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004049void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004050 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051}
4052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004053void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4054 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004055 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056}
4057
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004058void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004059 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060}
4061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004062void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4063 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004064 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4065 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066}
4067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004068void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4069 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004070 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071}
4072
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004073void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004074 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075}
4076
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 prepareButtons();
4079 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004080 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004082 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083}
4084
4085TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004086 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4087 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088 prepareButtons();
4089 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004090 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004092 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093}
4094
4095TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096 prepareButtons();
4097 prepareAxes(POSITION);
4098 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004099 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004101 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102}
4103
4104TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 prepareButtons();
4106 prepareAxes(POSITION);
4107 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004110 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111}
4112
4113TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 addConfigurationProperty("touch.deviceType", "touchScreen");
4115 prepareDisplay(DISPLAY_ORIENTATION_0);
4116 prepareButtons();
4117 prepareAxes(POSITION);
4118 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004119 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120
4121 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004122 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123
4124 // Virtual key is down.
4125 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4126 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4127 processDown(mapper, x, y);
4128 processSync(mapper);
4129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004131 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132
4133 // Virtual key is up.
4134 processUp(mapper);
4135 processSync(mapper);
4136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139}
4140
4141TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 addConfigurationProperty("touch.deviceType", "touchScreen");
4143 prepareDisplay(DISPLAY_ORIENTATION_0);
4144 prepareButtons();
4145 prepareAxes(POSITION);
4146 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148
4149 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004150 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151
4152 // Virtual key is down.
4153 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4154 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4155 processDown(mapper, x, y);
4156 processSync(mapper);
4157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004159 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 // Virtual key is up.
4162 processUp(mapper);
4163 processSync(mapper);
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004166 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167}
4168
4169TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 addConfigurationProperty("touch.deviceType", "touchScreen");
4171 prepareDisplay(DISPLAY_ORIENTATION_0);
4172 prepareButtons();
4173 prepareAxes(POSITION);
4174 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004175 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
4177 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4178 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004179 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_TRUE(flags[0]);
4181 ASSERT_FALSE(flags[1]);
4182}
4183
4184TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185 addConfigurationProperty("touch.deviceType", "touchScreen");
4186 prepareDisplay(DISPLAY_ORIENTATION_0);
4187 prepareButtons();
4188 prepareAxes(POSITION);
4189 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004190 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
4192 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4193
4194 NotifyKeyArgs args;
4195
4196 // Press virtual key.
4197 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4198 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4199 processDown(mapper, x, y);
4200 processSync(mapper);
4201
4202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4203 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4204 ASSERT_EQ(DEVICE_ID, args.deviceId);
4205 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4206 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4207 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4208 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4209 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4210 ASSERT_EQ(KEY_HOME, args.scanCode);
4211 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4212 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4213
4214 // Release virtual key.
4215 processUp(mapper);
4216 processSync(mapper);
4217
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4219 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4220 ASSERT_EQ(DEVICE_ID, args.deviceId);
4221 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4222 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4223 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4224 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4225 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4226 ASSERT_EQ(KEY_HOME, args.scanCode);
4227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4228 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4229
4230 // Should not have sent any motions.
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4232}
4233
4234TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 addConfigurationProperty("touch.deviceType", "touchScreen");
4236 prepareDisplay(DISPLAY_ORIENTATION_0);
4237 prepareButtons();
4238 prepareAxes(POSITION);
4239 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004240 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
4242 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4243
4244 NotifyKeyArgs keyArgs;
4245
4246 // Press virtual key.
4247 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4248 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4249 processDown(mapper, x, y);
4250 processSync(mapper);
4251
4252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4253 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4254 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4255 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4256 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4258 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4259 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4260 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4262 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4263
4264 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4265 // into the display area.
4266 y -= 100;
4267 processMove(mapper, x, y);
4268 processSync(mapper);
4269
4270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4271 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4272 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4274 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4276 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4277 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4278 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4279 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4281 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4282
4283 NotifyMotionArgs motionArgs;
4284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4286 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4287 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4288 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4290 ASSERT_EQ(0, motionArgs.flags);
4291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4292 ASSERT_EQ(0, motionArgs.buttonState);
4293 ASSERT_EQ(0, motionArgs.edgeFlags);
4294 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4295 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4298 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4299 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4300 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4301 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4302
4303 // Keep moving out of bounds. Should generate a pointer move.
4304 y -= 50;
4305 processMove(mapper, x, y);
4306 processSync(mapper);
4307
4308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4310 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4311 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4312 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4314 ASSERT_EQ(0, motionArgs.flags);
4315 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4316 ASSERT_EQ(0, motionArgs.buttonState);
4317 ASSERT_EQ(0, motionArgs.edgeFlags);
4318 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4322 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4323 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4324 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4326
4327 // Release out of bounds. Should generate a pointer up.
4328 processUp(mapper);
4329 processSync(mapper);
4330
4331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4333 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4334 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4335 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4336 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4337 ASSERT_EQ(0, motionArgs.flags);
4338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4339 ASSERT_EQ(0, motionArgs.buttonState);
4340 ASSERT_EQ(0, motionArgs.edgeFlags);
4341 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4342 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4345 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4346 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4347 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4349
4350 // Should not have sent any more keys or motions.
4351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4353}
4354
4355TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 addConfigurationProperty("touch.deviceType", "touchScreen");
4357 prepareDisplay(DISPLAY_ORIENTATION_0);
4358 prepareButtons();
4359 prepareAxes(POSITION);
4360 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004361 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362
4363 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4364
4365 NotifyMotionArgs motionArgs;
4366
4367 // Initially go down out of bounds.
4368 int32_t x = -10;
4369 int32_t y = -10;
4370 processDown(mapper, x, y);
4371 processSync(mapper);
4372
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4374
4375 // Move into the display area. Should generate a pointer down.
4376 x = 50;
4377 y = 75;
4378 processMove(mapper, x, y);
4379 processSync(mapper);
4380
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4386 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4387 ASSERT_EQ(0, motionArgs.flags);
4388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4389 ASSERT_EQ(0, motionArgs.buttonState);
4390 ASSERT_EQ(0, motionArgs.edgeFlags);
4391 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4392 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4395 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4399
4400 // Release. Should generate a pointer up.
4401 processUp(mapper);
4402 processSync(mapper);
4403
4404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4406 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4407 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4408 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4410 ASSERT_EQ(0, motionArgs.flags);
4411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4412 ASSERT_EQ(0, motionArgs.buttonState);
4413 ASSERT_EQ(0, motionArgs.edgeFlags);
4414 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4415 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4418 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4419 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4420 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4421 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4422
4423 // Should not have sent any more keys or motions.
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4426}
4427
Santos Cordonfa5cf462017-04-05 10:37:00 -07004428TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004429 addConfigurationProperty("touch.deviceType", "touchScreen");
4430 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4431
4432 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4433 prepareButtons();
4434 prepareAxes(POSITION);
4435 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004436 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004437
4438 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4439
4440 NotifyMotionArgs motionArgs;
4441
4442 // Down.
4443 int32_t x = 100;
4444 int32_t y = 125;
4445 processDown(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(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4455 ASSERT_EQ(0, motionArgs.flags);
4456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4457 ASSERT_EQ(0, motionArgs.buttonState);
4458 ASSERT_EQ(0, motionArgs.edgeFlags);
4459 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4463 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4464 1, 0, 0, 0, 0, 0, 0, 0));
4465 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4466 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4468
4469 // Move.
4470 x += 50;
4471 y += 75;
4472 processMove(mapper, x, y);
4473 processSync(mapper);
4474
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4476 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4477 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4478 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4479 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4480 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4482 ASSERT_EQ(0, motionArgs.flags);
4483 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4484 ASSERT_EQ(0, motionArgs.buttonState);
4485 ASSERT_EQ(0, motionArgs.edgeFlags);
4486 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4487 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4490 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4491 1, 0, 0, 0, 0, 0, 0, 0));
4492 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4493 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4494 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4495
4496 // Up.
4497 processUp(mapper);
4498 processSync(mapper);
4499
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4501 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4502 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4503 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4504 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4505 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4507 ASSERT_EQ(0, motionArgs.flags);
4508 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4509 ASSERT_EQ(0, motionArgs.buttonState);
4510 ASSERT_EQ(0, motionArgs.edgeFlags);
4511 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4512 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4515 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4516 1, 0, 0, 0, 0, 0, 0, 0));
4517 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4518 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4520
4521 // Should not have sent any more keys or motions.
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4524}
4525
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 addConfigurationProperty("touch.deviceType", "touchScreen");
4528 prepareDisplay(DISPLAY_ORIENTATION_0);
4529 prepareButtons();
4530 prepareAxes(POSITION);
4531 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004532 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
4534 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4535
4536 NotifyMotionArgs motionArgs;
4537
4538 // Down.
4539 int32_t x = 100;
4540 int32_t y = 125;
4541 processDown(mapper, x, y);
4542 processSync(mapper);
4543
4544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4545 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4546 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4547 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4548 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4550 ASSERT_EQ(0, motionArgs.flags);
4551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4552 ASSERT_EQ(0, motionArgs.buttonState);
4553 ASSERT_EQ(0, motionArgs.edgeFlags);
4554 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4558 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4559 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4560 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4562
4563 // Move.
4564 x += 50;
4565 y += 75;
4566 processMove(mapper, x, y);
4567 processSync(mapper);
4568
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4571 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4573 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4575 ASSERT_EQ(0, motionArgs.flags);
4576 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4577 ASSERT_EQ(0, motionArgs.buttonState);
4578 ASSERT_EQ(0, motionArgs.edgeFlags);
4579 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4580 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4583 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4584 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4585 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4587
4588 // Up.
4589 processUp(mapper);
4590 processSync(mapper);
4591
4592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4593 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4594 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4595 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4596 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4598 ASSERT_EQ(0, motionArgs.flags);
4599 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4600 ASSERT_EQ(0, motionArgs.buttonState);
4601 ASSERT_EQ(0, motionArgs.edgeFlags);
4602 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4603 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4606 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4607 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4608 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4609 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4610
4611 // Should not have sent any more keys or motions.
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4614}
4615
4616TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 addConfigurationProperty("touch.deviceType", "touchScreen");
4618 prepareButtons();
4619 prepareAxes(POSITION);
4620 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004621 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622
4623 NotifyMotionArgs args;
4624
4625 // Rotation 90.
4626 prepareDisplay(DISPLAY_ORIENTATION_90);
4627 processDown(mapper, toRawX(50), toRawY(75));
4628 processSync(mapper);
4629
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4631 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4632 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4633
4634 processUp(mapper);
4635 processSync(mapper);
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4637}
4638
4639TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 addConfigurationProperty("touch.deviceType", "touchScreen");
4641 prepareButtons();
4642 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004643 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644
4645 NotifyMotionArgs args;
4646
4647 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004648 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 prepareDisplay(DISPLAY_ORIENTATION_0);
4650 processDown(mapper, toRawX(50), toRawY(75));
4651 processSync(mapper);
4652
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4654 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4655 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4656
4657 processUp(mapper);
4658 processSync(mapper);
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4660
4661 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004662 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 prepareDisplay(DISPLAY_ORIENTATION_90);
4664 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4665 processSync(mapper);
4666
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4668 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4669 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4670
4671 processUp(mapper);
4672 processSync(mapper);
4673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4674
4675 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004676 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677 prepareDisplay(DISPLAY_ORIENTATION_180);
4678 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4679 processSync(mapper);
4680
4681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4682 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4683 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4684
4685 processUp(mapper);
4686 processSync(mapper);
4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4688
4689 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004690 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 prepareDisplay(DISPLAY_ORIENTATION_270);
4692 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4693 processSync(mapper);
4694
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4696 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4697 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4698
4699 processUp(mapper);
4700 processSync(mapper);
4701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4702}
4703
4704TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 addConfigurationProperty("touch.deviceType", "touchScreen");
4706 prepareDisplay(DISPLAY_ORIENTATION_0);
4707 prepareButtons();
4708 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004709 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710
4711 // These calculations are based on the input device calibration documentation.
4712 int32_t rawX = 100;
4713 int32_t rawY = 200;
4714 int32_t rawPressure = 10;
4715 int32_t rawToolMajor = 12;
4716 int32_t rawDistance = 2;
4717 int32_t rawTiltX = 30;
4718 int32_t rawTiltY = 110;
4719
4720 float x = toDisplayX(rawX);
4721 float y = toDisplayY(rawY);
4722 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4723 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4724 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4725 float distance = float(rawDistance);
4726
4727 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4728 float tiltScale = M_PI / 180;
4729 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4730 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4731 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4732 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4733
4734 processDown(mapper, rawX, rawY);
4735 processPressure(mapper, rawPressure);
4736 processToolMajor(mapper, rawToolMajor);
4737 processDistance(mapper, rawDistance);
4738 processTilt(mapper, rawTiltX, rawTiltY);
4739 processSync(mapper);
4740
4741 NotifyMotionArgs args;
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4744 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4745 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4746}
4747
Jason Gerecke489fda82012-09-07 17:19:40 -07004748TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004749 addConfigurationProperty("touch.deviceType", "touchScreen");
4750 prepareDisplay(DISPLAY_ORIENTATION_0);
4751 prepareLocationCalibration();
4752 prepareButtons();
4753 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004754 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004755
4756 int32_t rawX = 100;
4757 int32_t rawY = 200;
4758
4759 float x = toDisplayX(toCookedX(rawX, rawY));
4760 float y = toDisplayY(toCookedY(rawX, rawY));
4761
4762 processDown(mapper, rawX, rawY);
4763 processSync(mapper);
4764
4765 NotifyMotionArgs args;
4766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4768 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4769}
4770
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 addConfigurationProperty("touch.deviceType", "touchScreen");
4773 prepareDisplay(DISPLAY_ORIENTATION_0);
4774 prepareButtons();
4775 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004776 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777
4778 NotifyMotionArgs motionArgs;
4779 NotifyKeyArgs keyArgs;
4780
4781 processDown(mapper, 100, 200);
4782 processSync(mapper);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4785 ASSERT_EQ(0, motionArgs.buttonState);
4786
4787 // press BTN_LEFT, release BTN_LEFT
4788 processKey(mapper, BTN_LEFT, 1);
4789 processSync(mapper);
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4792 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4793
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4796 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4797
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 processKey(mapper, BTN_LEFT, 0);
4799 processSync(mapper);
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004803
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004806 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
4808 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4809 processKey(mapper, BTN_RIGHT, 1);
4810 processKey(mapper, BTN_MIDDLE, 1);
4811 processSync(mapper);
4812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4814 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4815 motionArgs.buttonState);
4816
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4819 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4820
4821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4823 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4824 motionArgs.buttonState);
4825
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 processKey(mapper, BTN_RIGHT, 0);
4827 processSync(mapper);
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004831
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004834 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835
4836 processKey(mapper, BTN_MIDDLE, 0);
4837 processSync(mapper);
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004844 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845
4846 // press BTN_BACK, release BTN_BACK
4847 processKey(mapper, BTN_BACK, 1);
4848 processSync(mapper);
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4850 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4851 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004852
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004855 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4856
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4859 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860
4861 processKey(mapper, BTN_BACK, 0);
4862 processSync(mapper);
4863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004866
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004869 ASSERT_EQ(0, motionArgs.buttonState);
4870
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4872 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4873 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4874
4875 // press BTN_SIDE, release BTN_SIDE
4876 processKey(mapper, BTN_SIDE, 1);
4877 processSync(mapper);
4878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4879 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4880 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004881
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004884 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4885
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4888 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889
4890 processKey(mapper, BTN_SIDE, 0);
4891 processSync(mapper);
4892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004895
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004898 ASSERT_EQ(0, motionArgs.buttonState);
4899
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4901 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4902 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4903
4904 // press BTN_FORWARD, release BTN_FORWARD
4905 processKey(mapper, BTN_FORWARD, 1);
4906 processSync(mapper);
4907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4908 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4909 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004910
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004913 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4914
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4917 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918
4919 processKey(mapper, BTN_FORWARD, 0);
4920 processSync(mapper);
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004924
4925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004927 ASSERT_EQ(0, motionArgs.buttonState);
4928
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4930 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4931 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4932
4933 // press BTN_EXTRA, release BTN_EXTRA
4934 processKey(mapper, BTN_EXTRA, 1);
4935 processSync(mapper);
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4937 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4938 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004942 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4943
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4946 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
4948 processKey(mapper, BTN_EXTRA, 0);
4949 processSync(mapper);
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004953
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004956 ASSERT_EQ(0, motionArgs.buttonState);
4957
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4959 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4960 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4961
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4963
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 // press BTN_STYLUS, release BTN_STYLUS
4965 processKey(mapper, BTN_STYLUS, 1);
4966 processSync(mapper);
4967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004969 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4970
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4973 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974
4975 processKey(mapper, BTN_STYLUS, 0);
4976 processSync(mapper);
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004980
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004983 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984
4985 // press BTN_STYLUS2, release BTN_STYLUS2
4986 processKey(mapper, BTN_STYLUS2, 1);
4987 processSync(mapper);
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004990 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4991
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4994 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995
4996 processKey(mapper, BTN_STYLUS2, 0);
4997 processSync(mapper);
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005001
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005004 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005
5006 // release touch
5007 processUp(mapper);
5008 processSync(mapper);
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5011 ASSERT_EQ(0, motionArgs.buttonState);
5012}
5013
5014TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 addConfigurationProperty("touch.deviceType", "touchScreen");
5016 prepareDisplay(DISPLAY_ORIENTATION_0);
5017 prepareButtons();
5018 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020
5021 NotifyMotionArgs motionArgs;
5022
5023 // default tool type is finger
5024 processDown(mapper, 100, 200);
5025 processSync(mapper);
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5029
5030 // eraser
5031 processKey(mapper, BTN_TOOL_RUBBER, 1);
5032 processSync(mapper);
5033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5036
5037 // stylus
5038 processKey(mapper, BTN_TOOL_RUBBER, 0);
5039 processKey(mapper, BTN_TOOL_PEN, 1);
5040 processSync(mapper);
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5044
5045 // brush
5046 processKey(mapper, BTN_TOOL_PEN, 0);
5047 processKey(mapper, BTN_TOOL_BRUSH, 1);
5048 processSync(mapper);
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5052
5053 // pencil
5054 processKey(mapper, BTN_TOOL_BRUSH, 0);
5055 processKey(mapper, BTN_TOOL_PENCIL, 1);
5056 processSync(mapper);
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5060
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005061 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 processKey(mapper, BTN_TOOL_PENCIL, 0);
5063 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5064 processSync(mapper);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5068
5069 // mouse
5070 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5071 processKey(mapper, BTN_TOOL_MOUSE, 1);
5072 processSync(mapper);
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5076
5077 // lens
5078 processKey(mapper, BTN_TOOL_MOUSE, 0);
5079 processKey(mapper, BTN_TOOL_LENS, 1);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5084
5085 // double-tap
5086 processKey(mapper, BTN_TOOL_LENS, 0);
5087 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5088 processSync(mapper);
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5092
5093 // triple-tap
5094 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5095 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5096 processSync(mapper);
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5100
5101 // quad-tap
5102 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5103 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5104 processSync(mapper);
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5108
5109 // finger
5110 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5111 processKey(mapper, BTN_TOOL_FINGER, 1);
5112 processSync(mapper);
5113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5116
5117 // stylus trumps finger
5118 processKey(mapper, BTN_TOOL_PEN, 1);
5119 processSync(mapper);
5120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5123
5124 // eraser trumps stylus
5125 processKey(mapper, BTN_TOOL_RUBBER, 1);
5126 processSync(mapper);
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5130
5131 // mouse trumps eraser
5132 processKey(mapper, BTN_TOOL_MOUSE, 1);
5133 processSync(mapper);
5134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5137
5138 // back to default tool type
5139 processKey(mapper, BTN_TOOL_MOUSE, 0);
5140 processKey(mapper, BTN_TOOL_RUBBER, 0);
5141 processKey(mapper, BTN_TOOL_PEN, 0);
5142 processKey(mapper, BTN_TOOL_FINGER, 0);
5143 processSync(mapper);
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5147}
5148
5149TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 addConfigurationProperty("touch.deviceType", "touchScreen");
5151 prepareDisplay(DISPLAY_ORIENTATION_0);
5152 prepareButtons();
5153 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005154 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005155 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156
5157 NotifyMotionArgs motionArgs;
5158
5159 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5160 processKey(mapper, BTN_TOOL_FINGER, 1);
5161 processMove(mapper, 100, 200);
5162 processSync(mapper);
5163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5164 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5166 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5169 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5171 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5172
5173 // move a little
5174 processMove(mapper, 150, 250);
5175 processSync(mapper);
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5177 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5179 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5180
5181 // down when BTN_TOUCH is pressed, pressure defaults to 1
5182 processKey(mapper, BTN_TOUCH, 1);
5183 processSync(mapper);
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5185 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5187 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5188
5189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5192 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5193
5194 // up when BTN_TOUCH is released, hover restored
5195 processKey(mapper, BTN_TOUCH, 0);
5196 processSync(mapper);
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5198 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5200 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5201
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5203 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5205 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5206
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5208 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5210 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5211
5212 // exit hover when pointer goes away
5213 processKey(mapper, BTN_TOOL_FINGER, 0);
5214 processSync(mapper);
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5218 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5219}
5220
5221TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222 addConfigurationProperty("touch.deviceType", "touchScreen");
5223 prepareDisplay(DISPLAY_ORIENTATION_0);
5224 prepareButtons();
5225 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005226 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227
5228 NotifyMotionArgs motionArgs;
5229
5230 // initially hovering because pressure is 0
5231 processDown(mapper, 100, 200);
5232 processPressure(mapper, 0);
5233 processSync(mapper);
5234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5237 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5242 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5243
5244 // move a little
5245 processMove(mapper, 150, 250);
5246 processSync(mapper);
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5250 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5251
5252 // down when pressure is non-zero
5253 processPressure(mapper, RAW_PRESSURE_MAX);
5254 processSync(mapper);
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5258 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5259
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5263 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5264
5265 // up when pressure becomes 0, hover restored
5266 processPressure(mapper, 0);
5267 processSync(mapper);
5268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5269 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5271 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5272
5273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5276 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5277
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5281 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5282
5283 // exit hover when pointer goes away
5284 processUp(mapper);
5285 processSync(mapper);
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5289 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5290}
5291
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292// --- MultiTouchInputMapperTest ---
5293
5294class MultiTouchInputMapperTest : public TouchInputMapperTest {
5295protected:
5296 void prepareAxes(int axes);
5297
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005298 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5299 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5300 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5301 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5302 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5303 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5304 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5305 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5306 void processId(MultiTouchInputMapper& mapper, int32_t id);
5307 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5308 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5309 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5310 void processMTSync(MultiTouchInputMapper& mapper);
5311 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312};
5313
5314void MultiTouchInputMapperTest::prepareAxes(int axes) {
5315 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 }
5319 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005320 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5321 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005323 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5324 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 }
5326 }
5327 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5329 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5332 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 }
5334 }
5335 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005336 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5337 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 }
5339 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005340 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5341 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 }
5343 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005344 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5345 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 }
5347 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005348 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5349 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 }
5351 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005352 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5353 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 }
5355 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005356 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 }
5358}
5359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005360void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5361 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005362 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5363 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364}
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5367 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005368 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369}
5370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5372 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005373 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374}
5375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005376void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005377 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378}
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005381 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5385 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005386 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387}
5388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005389void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005390 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391}
5392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005394 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395}
5396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005398 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399}
5400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005402 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403}
5404
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005405void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005406 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407}
5408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5410 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005411 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412}
5413
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005415 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416}
5417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005419 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420}
5421
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 addConfigurationProperty("touch.deviceType", "touchScreen");
5424 prepareDisplay(DISPLAY_ORIENTATION_0);
5425 prepareAxes(POSITION);
5426 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428
5429 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5430
5431 NotifyMotionArgs motionArgs;
5432
5433 // Two fingers down at once.
5434 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5435 processPosition(mapper, x1, y1);
5436 processMTSync(mapper);
5437 processPosition(mapper, x2, y2);
5438 processMTSync(mapper);
5439 processSync(mapper);
5440
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5443 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5444 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5445 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5447 ASSERT_EQ(0, motionArgs.flags);
5448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5449 ASSERT_EQ(0, motionArgs.buttonState);
5450 ASSERT_EQ(0, motionArgs.edgeFlags);
5451 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5452 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5455 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5456 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5457 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5459
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5463 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5464 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5465 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5466 motionArgs.action);
5467 ASSERT_EQ(0, motionArgs.flags);
5468 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5469 ASSERT_EQ(0, motionArgs.buttonState);
5470 ASSERT_EQ(0, motionArgs.edgeFlags);
5471 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5472 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5474 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5477 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5479 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5480 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5481 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5483
5484 // Move.
5485 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5486 processPosition(mapper, x1, y1);
5487 processMTSync(mapper);
5488 processPosition(mapper, x2, y2);
5489 processMTSync(mapper);
5490 processSync(mapper);
5491
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5493 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5494 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5495 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5496 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5498 ASSERT_EQ(0, motionArgs.flags);
5499 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5500 ASSERT_EQ(0, motionArgs.buttonState);
5501 ASSERT_EQ(0, motionArgs.edgeFlags);
5502 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5505 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5508 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5510 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5511 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5512 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5513 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5514
5515 // First finger up.
5516 x2 += 15; y2 -= 20;
5517 processPosition(mapper, x2, y2);
5518 processMTSync(mapper);
5519 processSync(mapper);
5520
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5525 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5526 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5527 motionArgs.action);
5528 ASSERT_EQ(0, motionArgs.flags);
5529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5530 ASSERT_EQ(0, motionArgs.buttonState);
5531 ASSERT_EQ(0, motionArgs.edgeFlags);
5532 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5535 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5538 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5540 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5541 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5542 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5547 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5548 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5549 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5551 ASSERT_EQ(0, motionArgs.flags);
5552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5553 ASSERT_EQ(0, motionArgs.buttonState);
5554 ASSERT_EQ(0, motionArgs.edgeFlags);
5555 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5556 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5559 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5560 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5561 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5563
5564 // Move.
5565 x2 += 20; y2 -= 25;
5566 processPosition(mapper, x2, y2);
5567 processMTSync(mapper);
5568 processSync(mapper);
5569
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5572 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5576 ASSERT_EQ(0, motionArgs.flags);
5577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5578 ASSERT_EQ(0, motionArgs.buttonState);
5579 ASSERT_EQ(0, motionArgs.edgeFlags);
5580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5581 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5584 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5585 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5586 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5588
5589 // New finger down.
5590 int32_t x3 = 700, y3 = 300;
5591 processPosition(mapper, x2, y2);
5592 processMTSync(mapper);
5593 processPosition(mapper, x3, y3);
5594 processMTSync(mapper);
5595 processSync(mapper);
5596
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5598 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5599 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5600 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5601 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5602 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5603 motionArgs.action);
5604 ASSERT_EQ(0, motionArgs.flags);
5605 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5606 ASSERT_EQ(0, motionArgs.buttonState);
5607 ASSERT_EQ(0, motionArgs.edgeFlags);
5608 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5611 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5614 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5616 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5617 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5618 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5620
5621 // Second finger up.
5622 x3 += 30; y3 -= 20;
5623 processPosition(mapper, x3, y3);
5624 processMTSync(mapper);
5625 processSync(mapper);
5626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5629 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5630 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5631 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5633 motionArgs.action);
5634 ASSERT_EQ(0, motionArgs.flags);
5635 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5636 ASSERT_EQ(0, motionArgs.buttonState);
5637 ASSERT_EQ(0, motionArgs.edgeFlags);
5638 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5639 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5641 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5644 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5646 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5647 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5648 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5649 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5650
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5653 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5654 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5655 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5657 ASSERT_EQ(0, motionArgs.flags);
5658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5659 ASSERT_EQ(0, motionArgs.buttonState);
5660 ASSERT_EQ(0, motionArgs.edgeFlags);
5661 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5665 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5666 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5667 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5668 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5669
5670 // Last finger up.
5671 processMTSync(mapper);
5672 processSync(mapper);
5673
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5675 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5676 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5677 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5678 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5679 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5680 ASSERT_EQ(0, motionArgs.flags);
5681 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5682 ASSERT_EQ(0, motionArgs.buttonState);
5683 ASSERT_EQ(0, motionArgs.edgeFlags);
5684 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5688 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5689 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5690 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5691 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5692
5693 // Should not have sent any more keys or motions.
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5696}
5697
5698TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 addConfigurationProperty("touch.deviceType", "touchScreen");
5700 prepareDisplay(DISPLAY_ORIENTATION_0);
5701 prepareAxes(POSITION | ID);
5702 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704
5705 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5706
5707 NotifyMotionArgs motionArgs;
5708
5709 // Two fingers down at once.
5710 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5711 processPosition(mapper, x1, y1);
5712 processId(mapper, 1);
5713 processMTSync(mapper);
5714 processPosition(mapper, x2, y2);
5715 processId(mapper, 2);
5716 processMTSync(mapper);
5717 processSync(mapper);
5718
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5721 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5722 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5725 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5726
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5728 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5729 motionArgs.action);
5730 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5731 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5733 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5736 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5738 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5739
5740 // Move.
5741 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5742 processPosition(mapper, x1, y1);
5743 processId(mapper, 1);
5744 processMTSync(mapper);
5745 processPosition(mapper, x2, y2);
5746 processId(mapper, 2);
5747 processMTSync(mapper);
5748 processSync(mapper);
5749
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5752 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5755 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5758 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5761
5762 // First finger up.
5763 x2 += 15; y2 -= 20;
5764 processPosition(mapper, x2, y2);
5765 processId(mapper, 2);
5766 processMTSync(mapper);
5767 processSync(mapper);
5768
5769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5771 motionArgs.action);
5772 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5775 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5778 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5780 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5781
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5784 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5785 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5788 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5789
5790 // Move.
5791 x2 += 20; y2 -= 25;
5792 processPosition(mapper, x2, y2);
5793 processId(mapper, 2);
5794 processMTSync(mapper);
5795 processSync(mapper);
5796
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5799 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5800 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5803 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5804
5805 // New finger down.
5806 int32_t x3 = 700, y3 = 300;
5807 processPosition(mapper, x2, y2);
5808 processId(mapper, 2);
5809 processMTSync(mapper);
5810 processPosition(mapper, x3, y3);
5811 processId(mapper, 3);
5812 processMTSync(mapper);
5813 processSync(mapper);
5814
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5817 motionArgs.action);
5818 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5821 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5824 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5826 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5827
5828 // Second finger up.
5829 x3 += 30; y3 -= 20;
5830 processPosition(mapper, x3, y3);
5831 processId(mapper, 3);
5832 processMTSync(mapper);
5833 processSync(mapper);
5834
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5837 motionArgs.action);
5838 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5841 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5844 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5846 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5850 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5854 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5855
5856 // Last finger up.
5857 processMTSync(mapper);
5858 processSync(mapper);
5859
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5862 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5863 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5866 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5867
5868 // Should not have sent any more keys or motions.
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5871}
5872
5873TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 addConfigurationProperty("touch.deviceType", "touchScreen");
5875 prepareDisplay(DISPLAY_ORIENTATION_0);
5876 prepareAxes(POSITION | ID | SLOT);
5877 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005878 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5881
5882 NotifyMotionArgs motionArgs;
5883
5884 // Two fingers down at once.
5885 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5886 processPosition(mapper, x1, y1);
5887 processId(mapper, 1);
5888 processSlot(mapper, 1);
5889 processPosition(mapper, x2, y2);
5890 processId(mapper, 2);
5891 processSync(mapper);
5892
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5894 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5895 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5896 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5899 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5900
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5903 motionArgs.action);
5904 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5905 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5907 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5910 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5912 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5913
5914 // Move.
5915 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5916 processSlot(mapper, 0);
5917 processPosition(mapper, x1, y1);
5918 processSlot(mapper, 1);
5919 processPosition(mapper, x2, y2);
5920 processSync(mapper);
5921
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5924 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5925 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5927 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5930 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5932 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5933
5934 // First finger up.
5935 x2 += 15; y2 -= 20;
5936 processSlot(mapper, 0);
5937 processId(mapper, -1);
5938 processSlot(mapper, 1);
5939 processPosition(mapper, x2, y2);
5940 processSync(mapper);
5941
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5944 motionArgs.action);
5945 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5946 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5948 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5951 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5953 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5954
5955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5957 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5958 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5961 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5962
5963 // Move.
5964 x2 += 20; y2 -= 25;
5965 processPosition(mapper, x2, y2);
5966 processSync(mapper);
5967
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5970 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5971 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5974 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5975
5976 // New finger down.
5977 int32_t x3 = 700, y3 = 300;
5978 processPosition(mapper, x2, y2);
5979 processSlot(mapper, 0);
5980 processId(mapper, 3);
5981 processPosition(mapper, x3, y3);
5982 processSync(mapper);
5983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5986 motionArgs.action);
5987 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5988 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5990 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5993 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5995 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5996
5997 // Second finger up.
5998 x3 += 30; y3 -= 20;
5999 processSlot(mapper, 1);
6000 processId(mapper, -1);
6001 processSlot(mapper, 0);
6002 processPosition(mapper, x3, y3);
6003 processSync(mapper);
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6007 motionArgs.action);
6008 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6009 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6011 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6014 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6016 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6017
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6020 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6024 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6025
6026 // Last finger up.
6027 processId(mapper, -1);
6028 processSync(mapper);
6029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6032 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6033 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6036 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6037
6038 // Should not have sent any more keys or motions.
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6041}
6042
6043TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 addConfigurationProperty("touch.deviceType", "touchScreen");
6045 prepareDisplay(DISPLAY_ORIENTATION_0);
6046 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006047 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048
6049 // These calculations are based on the input device calibration documentation.
6050 int32_t rawX = 100;
6051 int32_t rawY = 200;
6052 int32_t rawTouchMajor = 7;
6053 int32_t rawTouchMinor = 6;
6054 int32_t rawToolMajor = 9;
6055 int32_t rawToolMinor = 8;
6056 int32_t rawPressure = 11;
6057 int32_t rawDistance = 0;
6058 int32_t rawOrientation = 3;
6059 int32_t id = 5;
6060
6061 float x = toDisplayX(rawX);
6062 float y = toDisplayY(rawY);
6063 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6064 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6065 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6066 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6067 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6068 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6069 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6070 float distance = float(rawDistance);
6071
6072 processPosition(mapper, rawX, rawY);
6073 processTouchMajor(mapper, rawTouchMajor);
6074 processTouchMinor(mapper, rawTouchMinor);
6075 processToolMajor(mapper, rawToolMajor);
6076 processToolMinor(mapper, rawToolMinor);
6077 processPressure(mapper, rawPressure);
6078 processOrientation(mapper, rawOrientation);
6079 processDistance(mapper, rawDistance);
6080 processId(mapper, id);
6081 processMTSync(mapper);
6082 processSync(mapper);
6083
6084 NotifyMotionArgs args;
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6086 ASSERT_EQ(0, args.pointerProperties[0].id);
6087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6088 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6089 orientation, distance));
6090}
6091
6092TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 addConfigurationProperty("touch.deviceType", "touchScreen");
6094 prepareDisplay(DISPLAY_ORIENTATION_0);
6095 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6096 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006097 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098
6099 // These calculations are based on the input device calibration documentation.
6100 int32_t rawX = 100;
6101 int32_t rawY = 200;
6102 int32_t rawTouchMajor = 140;
6103 int32_t rawTouchMinor = 120;
6104 int32_t rawToolMajor = 180;
6105 int32_t rawToolMinor = 160;
6106
6107 float x = toDisplayX(rawX);
6108 float y = toDisplayY(rawY);
6109 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6110 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6111 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6112 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6113 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6114
6115 processPosition(mapper, rawX, rawY);
6116 processTouchMajor(mapper, rawTouchMajor);
6117 processTouchMinor(mapper, rawTouchMinor);
6118 processToolMajor(mapper, rawToolMajor);
6119 processToolMinor(mapper, rawToolMinor);
6120 processMTSync(mapper);
6121 processSync(mapper);
6122
6123 NotifyMotionArgs args;
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6126 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6127}
6128
6129TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130 addConfigurationProperty("touch.deviceType", "touchScreen");
6131 prepareDisplay(DISPLAY_ORIENTATION_0);
6132 prepareAxes(POSITION | TOUCH | TOOL);
6133 addConfigurationProperty("touch.size.calibration", "diameter");
6134 addConfigurationProperty("touch.size.scale", "10");
6135 addConfigurationProperty("touch.size.bias", "160");
6136 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 // These calculations are based on the input device calibration documentation.
6140 // Note: We only provide a single common touch/tool value because the device is assumed
6141 // not to emit separate values for each pointer (isSummed = 1).
6142 int32_t rawX = 100;
6143 int32_t rawY = 200;
6144 int32_t rawX2 = 150;
6145 int32_t rawY2 = 250;
6146 int32_t rawTouchMajor = 5;
6147 int32_t rawToolMajor = 8;
6148
6149 float x = toDisplayX(rawX);
6150 float y = toDisplayY(rawY);
6151 float x2 = toDisplayX(rawX2);
6152 float y2 = toDisplayY(rawY2);
6153 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6154 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6155 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6156
6157 processPosition(mapper, rawX, rawY);
6158 processTouchMajor(mapper, rawTouchMajor);
6159 processToolMajor(mapper, rawToolMajor);
6160 processMTSync(mapper);
6161 processPosition(mapper, rawX2, rawY2);
6162 processTouchMajor(mapper, rawTouchMajor);
6163 processToolMajor(mapper, rawToolMajor);
6164 processMTSync(mapper);
6165 processSync(mapper);
6166
6167 NotifyMotionArgs args;
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6170
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6173 args.action);
6174 ASSERT_EQ(size_t(2), args.pointerCount);
6175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6176 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6178 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6179}
6180
6181TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 addConfigurationProperty("touch.deviceType", "touchScreen");
6183 prepareDisplay(DISPLAY_ORIENTATION_0);
6184 prepareAxes(POSITION | TOUCH | TOOL);
6185 addConfigurationProperty("touch.size.calibration", "area");
6186 addConfigurationProperty("touch.size.scale", "43");
6187 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006188 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
6190 // These calculations are based on the input device calibration documentation.
6191 int32_t rawX = 100;
6192 int32_t rawY = 200;
6193 int32_t rawTouchMajor = 5;
6194 int32_t rawToolMajor = 8;
6195
6196 float x = toDisplayX(rawX);
6197 float y = toDisplayY(rawY);
6198 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6199 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6200 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6201
6202 processPosition(mapper, rawX, rawY);
6203 processTouchMajor(mapper, rawTouchMajor);
6204 processToolMajor(mapper, rawToolMajor);
6205 processMTSync(mapper);
6206 processSync(mapper);
6207
6208 NotifyMotionArgs args;
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6211 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6212}
6213
6214TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 addConfigurationProperty("touch.deviceType", "touchScreen");
6216 prepareDisplay(DISPLAY_ORIENTATION_0);
6217 prepareAxes(POSITION | PRESSURE);
6218 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6219 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
Michael Wrightaa449c92017-12-13 21:21:43 +00006222 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006223 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006224 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6225 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6226 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6227
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228 // These calculations are based on the input device calibration documentation.
6229 int32_t rawX = 100;
6230 int32_t rawY = 200;
6231 int32_t rawPressure = 60;
6232
6233 float x = toDisplayX(rawX);
6234 float y = toDisplayY(rawY);
6235 float pressure = float(rawPressure) * 0.01f;
6236
6237 processPosition(mapper, rawX, rawY);
6238 processPressure(mapper, rawPressure);
6239 processMTSync(mapper);
6240 processSync(mapper);
6241
6242 NotifyMotionArgs args;
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6245 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6246}
6247
6248TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 addConfigurationProperty("touch.deviceType", "touchScreen");
6250 prepareDisplay(DISPLAY_ORIENTATION_0);
6251 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253
6254 NotifyMotionArgs motionArgs;
6255 NotifyKeyArgs keyArgs;
6256
6257 processId(mapper, 1);
6258 processPosition(mapper, 100, 200);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6262 ASSERT_EQ(0, motionArgs.buttonState);
6263
6264 // press BTN_LEFT, release BTN_LEFT
6265 processKey(mapper, BTN_LEFT, 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6270
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6273 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6274
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 processKey(mapper, BTN_LEFT, 0);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006280
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284
6285 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6286 processKey(mapper, BTN_RIGHT, 1);
6287 processKey(mapper, BTN_MIDDLE, 1);
6288 processSync(mapper);
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6291 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6292 motionArgs.buttonState);
6293
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6296 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6297
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6300 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6301 motionArgs.buttonState);
6302
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 processKey(mapper, BTN_RIGHT, 0);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006308
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006311 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312
6313 processKey(mapper, BTN_MIDDLE, 0);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006318
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006321 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322
6323 // press BTN_BACK, release BTN_BACK
6324 processKey(mapper, BTN_BACK, 1);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6327 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6328 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006332 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6333
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337
6338 processKey(mapper, BTN_BACK, 0);
6339 processSync(mapper);
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006343
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346 ASSERT_EQ(0, motionArgs.buttonState);
6347
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6350 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6351
6352 // press BTN_SIDE, release BTN_SIDE
6353 processKey(mapper, BTN_SIDE, 1);
6354 processSync(mapper);
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6357 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6365 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
6367 processKey(mapper, BTN_SIDE, 0);
6368 processSync(mapper);
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006372
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375 ASSERT_EQ(0, motionArgs.buttonState);
6376
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6378 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6379 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6380
6381 // press BTN_FORWARD, release BTN_FORWARD
6382 processKey(mapper, BTN_FORWARD, 1);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6386 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6394 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395
6396 processKey(mapper, BTN_FORWARD, 0);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006401
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006404 ASSERT_EQ(0, motionArgs.buttonState);
6405
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6407 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6408 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6409
6410 // press BTN_EXTRA, release BTN_EXTRA
6411 processKey(mapper, BTN_EXTRA, 1);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6414 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6415 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006419 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6423 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424
6425 processKey(mapper, BTN_EXTRA, 0);
6426 processSync(mapper);
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006430
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006433 ASSERT_EQ(0, motionArgs.buttonState);
6434
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6436 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6437 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6438
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6440
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 // press BTN_STYLUS, release BTN_STYLUS
6442 processKey(mapper, BTN_STYLUS, 1);
6443 processSync(mapper);
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006446 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6447
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6450 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451
6452 processKey(mapper, BTN_STYLUS, 0);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006457
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006460 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461
6462 // press BTN_STYLUS2, release BTN_STYLUS2
6463 processKey(mapper, BTN_STYLUS2, 1);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006467 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6468
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6471 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472
6473 processKey(mapper, BTN_STYLUS2, 0);
6474 processSync(mapper);
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006478
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006481 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482
6483 // release touch
6484 processId(mapper, -1);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6488 ASSERT_EQ(0, motionArgs.buttonState);
6489}
6490
6491TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492 addConfigurationProperty("touch.deviceType", "touchScreen");
6493 prepareDisplay(DISPLAY_ORIENTATION_0);
6494 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006495 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496
6497 NotifyMotionArgs motionArgs;
6498
6499 // default tool type is finger
6500 processId(mapper, 1);
6501 processPosition(mapper, 100, 200);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6506
6507 // eraser
6508 processKey(mapper, BTN_TOOL_RUBBER, 1);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6513
6514 // stylus
6515 processKey(mapper, BTN_TOOL_RUBBER, 0);
6516 processKey(mapper, BTN_TOOL_PEN, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6521
6522 // brush
6523 processKey(mapper, BTN_TOOL_PEN, 0);
6524 processKey(mapper, BTN_TOOL_BRUSH, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6529
6530 // pencil
6531 processKey(mapper, BTN_TOOL_BRUSH, 0);
6532 processKey(mapper, BTN_TOOL_PENCIL, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6537
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006538 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 processKey(mapper, BTN_TOOL_PENCIL, 0);
6540 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6541 processSync(mapper);
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6545
6546 // mouse
6547 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6548 processKey(mapper, BTN_TOOL_MOUSE, 1);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6553
6554 // lens
6555 processKey(mapper, BTN_TOOL_MOUSE, 0);
6556 processKey(mapper, BTN_TOOL_LENS, 1);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6561
6562 // double-tap
6563 processKey(mapper, BTN_TOOL_LENS, 0);
6564 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6565 processSync(mapper);
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6569
6570 // triple-tap
6571 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6572 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6577
6578 // quad-tap
6579 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6580 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6585
6586 // finger
6587 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6588 processKey(mapper, BTN_TOOL_FINGER, 1);
6589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6593
6594 // stylus trumps finger
6595 processKey(mapper, BTN_TOOL_PEN, 1);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6600
6601 // eraser trumps stylus
6602 processKey(mapper, BTN_TOOL_RUBBER, 1);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6607
6608 // mouse trumps eraser
6609 processKey(mapper, BTN_TOOL_MOUSE, 1);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6614
6615 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6616 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6621
6622 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6623 processToolType(mapper, MT_TOOL_PEN);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6628
6629 // back to default tool type
6630 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6631 processKey(mapper, BTN_TOOL_MOUSE, 0);
6632 processKey(mapper, BTN_TOOL_RUBBER, 0);
6633 processKey(mapper, BTN_TOOL_PEN, 0);
6634 processKey(mapper, BTN_TOOL_FINGER, 0);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6639}
6640
6641TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006642 addConfigurationProperty("touch.deviceType", "touchScreen");
6643 prepareDisplay(DISPLAY_ORIENTATION_0);
6644 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006645 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006647
6648 NotifyMotionArgs motionArgs;
6649
6650 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6651 processId(mapper, 1);
6652 processPosition(mapper, 100, 200);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6658
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6663
6664 // move a little
6665 processPosition(mapper, 150, 250);
6666 processSync(mapper);
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // down when BTN_TOUCH is pressed, pressure defaults to 1
6673 processKey(mapper, BTN_TOUCH, 1);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6679
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6684
6685 // up when BTN_TOUCH is released, hover restored
6686 processKey(mapper, BTN_TOUCH, 0);
6687 processSync(mapper);
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6691 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6692
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6697
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6701 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6702
6703 // exit hover when pointer goes away
6704 processId(mapper, -1);
6705 processSync(mapper);
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6709 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6710}
6711
6712TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006713 addConfigurationProperty("touch.deviceType", "touchScreen");
6714 prepareDisplay(DISPLAY_ORIENTATION_0);
6715 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006717
6718 NotifyMotionArgs motionArgs;
6719
6720 // initially hovering because pressure is 0
6721 processId(mapper, 1);
6722 processPosition(mapper, 100, 200);
6723 processPressure(mapper, 0);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6728 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6729
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6733 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6734
6735 // move a little
6736 processPosition(mapper, 150, 250);
6737 processSync(mapper);
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6742
6743 // down when pressure becomes non-zero
6744 processPressure(mapper, RAW_PRESSURE_MAX);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6749 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6750
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6754 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6755
6756 // up when pressure becomes 0, hover restored
6757 processPressure(mapper, 0);
6758 processSync(mapper);
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6762 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6763
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6767 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6768
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6772 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6773
6774 // exit hover when pointer goes away
6775 processId(mapper, -1);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6780 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6781}
6782
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006783/**
6784 * Set the input device port <--> display port associations, and check that the
6785 * events are routed to the display that matches the display port.
6786 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6787 */
6788TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006789 const std::string usb2 = "USB2";
6790 const uint8_t hdmi1 = 0;
6791 const uint8_t hdmi2 = 1;
6792 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006793 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006794
6795 addConfigurationProperty("touch.deviceType", "touchScreen");
6796 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006797 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006798
6799 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6800 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6801
6802 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6803 // for this input device is specified, and the matching viewport is not present,
6804 // the input device should be disabled (at the mapper level).
6805
6806 // Add viewport for display 2 on hdmi2
6807 prepareSecondaryDisplay(type, hdmi2);
6808 // Send a touch event
6809 processPosition(mapper, 100, 100);
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6812
6813 // Add viewport for display 1 on hdmi1
6814 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6815 // Send a touch event again
6816 processPosition(mapper, 100, 100);
6817 processSync(mapper);
6818
6819 NotifyMotionArgs args;
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6821 ASSERT_EQ(DISPLAY_ID, args.displayId);
6822}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006823
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006824TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006825 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006826 std::shared_ptr<FakePointerController> fakePointerController =
6827 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006828 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006829 fakePointerController->setPosition(100, 200);
6830 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006831 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6832
Garfield Tan888a6a42020-01-09 11:39:16 -08006833 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006834 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006835
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006836 prepareDisplay(DISPLAY_ORIENTATION_0);
6837 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006839
6840 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006841 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006842
6843 NotifyMotionArgs motionArgs;
6844 processPosition(mapper, 100, 100);
6845 processSync(mapper);
6846
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6848 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6849 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6850}
6851
Arthur Hung7c645402019-01-25 17:45:42 +08006852TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6853 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006854 prepareAxes(POSITION | ID | SLOT);
6855 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006856 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006857
6858 // Create the second touch screen device, and enable multi fingers.
6859 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006860 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006861 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006862 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006863 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006864 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006865 std::unique_ptr<InputDevice> device2 =
6866 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006867 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07006868 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME,
6869 Flags<InputDeviceClass>(0) /*classes*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006870 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6871 0 /*flat*/, 0 /*fuzz*/);
6872 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6873 0 /*flat*/, 0 /*fuzz*/);
6874 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6875 0 /*flat*/, 0 /*fuzz*/);
6876 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6877 0 /*flat*/, 0 /*fuzz*/);
6878 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6879 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6880 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006881
6882 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006883 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006884 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6885 device2->reset(ARBITRARY_TIME);
6886
6887 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006888 std::shared_ptr<FakePointerController> fakePointerController =
6889 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006890 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6891 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6892
6893 // Setup policy for associated displays and show touches.
6894 const uint8_t hdmi1 = 0;
6895 const uint8_t hdmi2 = 1;
6896 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6897 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6898 mFakePolicy->setShowTouches(true);
6899
6900 // Create displays.
6901 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006902 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006903
6904 // Default device will reconfigure above, need additional reconfiguration for another device.
6905 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006906 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006907
6908 // Two fingers down at default display.
6909 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6910 processPosition(mapper, x1, y1);
6911 processId(mapper, 1);
6912 processSlot(mapper, 1);
6913 processPosition(mapper, x2, y2);
6914 processId(mapper, 2);
6915 processSync(mapper);
6916
6917 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6918 fakePointerController->getSpots().find(DISPLAY_ID);
6919 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6920 ASSERT_EQ(size_t(2), iter->second.size());
6921
6922 // Two fingers down at second display.
6923 processPosition(mapper2, x1, y1);
6924 processId(mapper2, 1);
6925 processSlot(mapper2, 1);
6926 processPosition(mapper2, x2, y2);
6927 processId(mapper2, 2);
6928 processSync(mapper2);
6929
6930 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6931 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6932 ASSERT_EQ(size_t(2), iter->second.size());
6933}
6934
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006935TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006936 prepareAxes(POSITION);
6937 addConfigurationProperty("touch.deviceType", "touchScreen");
6938 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006939 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006940
6941 NotifyMotionArgs motionArgs;
6942 // Unrotated video frame
6943 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6944 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006945 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006946 processPosition(mapper, 100, 200);
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6949 ASSERT_EQ(frames, motionArgs.videoFrames);
6950
6951 // Subsequent touch events should not have any videoframes
6952 // This is implemented separately in FakeEventHub,
6953 // but that should match the behaviour of TouchVideoDevice.
6954 processPosition(mapper, 200, 200);
6955 processSync(mapper);
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6957 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6958}
6959
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006960TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006961 prepareAxes(POSITION);
6962 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006963 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006964 // Unrotated video frame
6965 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6966 NotifyMotionArgs motionArgs;
6967
6968 // Test all 4 orientations
6969 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6970 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6971 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6972 clearViewports();
6973 prepareDisplay(orientation);
6974 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006975 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006976 processPosition(mapper, 100, 200);
6977 processSync(mapper);
6978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6979 frames[0].rotate(orientation);
6980 ASSERT_EQ(frames, motionArgs.videoFrames);
6981 }
6982}
6983
6984TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006985 prepareAxes(POSITION);
6986 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006987 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006988 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6989 // so mix these.
6990 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6991 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6992 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6993 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6994 NotifyMotionArgs motionArgs;
6995
6996 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006997 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006998 processPosition(mapper, 100, 200);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7001 std::for_each(frames.begin(), frames.end(),
7002 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7003 ASSERT_EQ(frames, motionArgs.videoFrames);
7004}
7005
Arthur Hung9da14732019-09-02 16:16:58 +08007006/**
7007 * If we had defined port associations, but the viewport is not ready, the touch device would be
7008 * expected to be disabled, and it should be enabled after the viewport has found.
7009 */
7010TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007011 constexpr uint8_t hdmi2 = 1;
7012 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007013 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007014
7015 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7016
7017 addConfigurationProperty("touch.deviceType", "touchScreen");
7018 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007020
7021 ASSERT_EQ(mDevice->isEnabled(), false);
7022
7023 // Add display on hdmi2, the device should be enabled and can receive touch event.
7024 prepareSecondaryDisplay(type, hdmi2);
7025 ASSERT_EQ(mDevice->isEnabled(), true);
7026
7027 // Send a touch event.
7028 processPosition(mapper, 100, 100);
7029 processSync(mapper);
7030
7031 NotifyMotionArgs args;
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7033 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7034}
7035
Arthur Hung6cd19a42019-08-30 19:04:12 +08007036
Arthur Hung6cd19a42019-08-30 19:04:12 +08007037
Arthur Hung421eb1c2020-01-16 00:09:42 +08007038TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007039 addConfigurationProperty("touch.deviceType", "touchScreen");
7040 prepareDisplay(DISPLAY_ORIENTATION_0);
7041 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007042 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007043
7044 NotifyMotionArgs motionArgs;
7045
7046 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7047 // finger down
7048 processId(mapper, 1);
7049 processPosition(mapper, x1, y1);
7050 processSync(mapper);
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7054
7055 // finger move
7056 processId(mapper, 1);
7057 processPosition(mapper, x2, y2);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7062
7063 // finger up.
7064 processId(mapper, -1);
7065 processSync(mapper);
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7069
7070 // new finger down
7071 processId(mapper, 1);
7072 processPosition(mapper, x3, y3);
7073 processSync(mapper);
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7077}
7078
7079/**
arthurhungcc7f9802020-04-30 17:55:40 +08007080 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7081 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007082 */
arthurhungcc7f9802020-04-30 17:55:40 +08007083TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007084 addConfigurationProperty("touch.deviceType", "touchScreen");
7085 prepareDisplay(DISPLAY_ORIENTATION_0);
7086 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007088
7089 NotifyMotionArgs motionArgs;
7090
7091 // default tool type is finger
7092 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007093 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007094 processPosition(mapper, x1, y1);
7095 processSync(mapper);
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7099
7100 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7101 processToolType(mapper, MT_TOOL_PALM);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7104 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7105
7106 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007107 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007108 processPosition(mapper, x2, y2);
7109 processSync(mapper);
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7111
7112 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007113 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007114 processSync(mapper);
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7116
7117 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007118 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007119 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007120 processPosition(mapper, x3, y3);
7121 processSync(mapper);
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7125}
7126
arthurhungbf89a482020-04-17 17:37:55 +08007127/**
arthurhungcc7f9802020-04-30 17:55:40 +08007128 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7129 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007130 */
arthurhungcc7f9802020-04-30 17:55:40 +08007131TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007132 addConfigurationProperty("touch.deviceType", "touchScreen");
7133 prepareDisplay(DISPLAY_ORIENTATION_0);
7134 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7135 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7136
7137 NotifyMotionArgs motionArgs;
7138
7139 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007140 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7141 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007142 processPosition(mapper, x1, y1);
7143 processSync(mapper);
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7147
7148 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007149 processSlot(mapper, SECOND_SLOT);
7150 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007151 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007152 processSync(mapper);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7154 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7155 motionArgs.action);
7156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7157
7158 // If the tool type of the first finger changes to MT_TOOL_PALM,
7159 // we expect to receive ACTION_POINTER_UP with cancel flag.
7160 processSlot(mapper, FIRST_SLOT);
7161 processId(mapper, FIRST_TRACKING_ID);
7162 processToolType(mapper, MT_TOOL_PALM);
7163 processSync(mapper);
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7165 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7166 motionArgs.action);
7167 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7168
7169 // The following MOVE events of second finger should be processed.
7170 processSlot(mapper, SECOND_SLOT);
7171 processId(mapper, SECOND_TRACKING_ID);
7172 processPosition(mapper, x2 + 1, y2 + 1);
7173 processSync(mapper);
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7176 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7177
7178 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7179 // it. Second finger receive move.
7180 processSlot(mapper, FIRST_SLOT);
7181 processId(mapper, INVALID_TRACKING_ID);
7182 processSync(mapper);
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7185 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7186
7187 // Second finger keeps moving.
7188 processSlot(mapper, SECOND_SLOT);
7189 processId(mapper, SECOND_TRACKING_ID);
7190 processPosition(mapper, x2 + 2, y2 + 2);
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7194 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7195
7196 // Second finger up.
7197 processId(mapper, INVALID_TRACKING_ID);
7198 processSync(mapper);
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7200 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7201 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7202}
7203
7204/**
7205 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7206 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7207 */
7208TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7209 addConfigurationProperty("touch.deviceType", "touchScreen");
7210 prepareDisplay(DISPLAY_ORIENTATION_0);
7211 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7212 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7213
7214 NotifyMotionArgs motionArgs;
7215
7216 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7217 // First finger down.
7218 processId(mapper, FIRST_TRACKING_ID);
7219 processPosition(mapper, x1, y1);
7220 processSync(mapper);
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7222 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7224
7225 // Second finger down.
7226 processSlot(mapper, SECOND_SLOT);
7227 processId(mapper, SECOND_TRACKING_ID);
7228 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007229 processSync(mapper);
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7231 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7232 motionArgs.action);
7233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7234
arthurhungcc7f9802020-04-30 17:55:40 +08007235 // If the tool type of the first finger changes to MT_TOOL_PALM,
7236 // we expect to receive ACTION_POINTER_UP with cancel flag.
7237 processSlot(mapper, FIRST_SLOT);
7238 processId(mapper, FIRST_TRACKING_ID);
7239 processToolType(mapper, MT_TOOL_PALM);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7243 motionArgs.action);
7244 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7245
7246 // Second finger keeps moving.
7247 processSlot(mapper, SECOND_SLOT);
7248 processId(mapper, SECOND_TRACKING_ID);
7249 processPosition(mapper, x2 + 1, y2 + 1);
7250 processSync(mapper);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7253
7254 // second finger becomes palm, receive cancel due to only 1 finger is active.
7255 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007256 processToolType(mapper, MT_TOOL_PALM);
7257 processSync(mapper);
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7259 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7260
arthurhungcc7f9802020-04-30 17:55:40 +08007261 // third finger down.
7262 processSlot(mapper, THIRD_SLOT);
7263 processId(mapper, THIRD_TRACKING_ID);
7264 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007265 processPosition(mapper, x3, y3);
7266 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7268 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007270 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7271
7272 // third finger move
7273 processId(mapper, THIRD_TRACKING_ID);
7274 processPosition(mapper, x3 + 1, y3 + 1);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7278
7279 // first finger up, third finger receive move.
7280 processSlot(mapper, FIRST_SLOT);
7281 processId(mapper, INVALID_TRACKING_ID);
7282 processSync(mapper);
7283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7285 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7286
7287 // second finger up, third finger receive move.
7288 processSlot(mapper, SECOND_SLOT);
7289 processId(mapper, INVALID_TRACKING_ID);
7290 processSync(mapper);
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7293 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7294
7295 // third finger up.
7296 processSlot(mapper, THIRD_SLOT);
7297 processId(mapper, INVALID_TRACKING_ID);
7298 processSync(mapper);
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7300 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7301 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7302}
7303
7304/**
7305 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7306 * and the active finger could still be allowed to receive the events
7307 */
7308TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7309 addConfigurationProperty("touch.deviceType", "touchScreen");
7310 prepareDisplay(DISPLAY_ORIENTATION_0);
7311 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7312 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7313
7314 NotifyMotionArgs motionArgs;
7315
7316 // default tool type is finger
7317 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7318 processId(mapper, FIRST_TRACKING_ID);
7319 processPosition(mapper, x1, y1);
7320 processSync(mapper);
7321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7322 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7324
7325 // Second finger down.
7326 processSlot(mapper, SECOND_SLOT);
7327 processId(mapper, SECOND_TRACKING_ID);
7328 processPosition(mapper, x2, y2);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7331 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7332 motionArgs.action);
7333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7334
7335 // If the tool type of the second finger changes to MT_TOOL_PALM,
7336 // we expect to receive ACTION_POINTER_UP with cancel flag.
7337 processId(mapper, SECOND_TRACKING_ID);
7338 processToolType(mapper, MT_TOOL_PALM);
7339 processSync(mapper);
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7341 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7342 motionArgs.action);
7343 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7344
7345 // The following MOVE event should be processed.
7346 processSlot(mapper, FIRST_SLOT);
7347 processId(mapper, FIRST_TRACKING_ID);
7348 processPosition(mapper, x1 + 1, y1 + 1);
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.
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
7361 // first finger keep moving
7362 processSlot(mapper, FIRST_SLOT);
7363 processId(mapper, FIRST_TRACKING_ID);
7364 processPosition(mapper, x1 + 2, y1 + 2);
7365 processSync(mapper);
7366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7368
7369 // first finger up.
7370 processId(mapper, INVALID_TRACKING_ID);
7371 processSync(mapper);
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7373 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7374 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007375}
7376
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007377// --- MultiTouchInputMapperTest_ExternalDevice ---
7378
7379class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7380protected:
7381 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07007382 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007383 }
7384};
7385
7386/**
7387 * Expect fallback to internal viewport if device is external and external viewport is not present.
7388 */
7389TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7390 prepareAxes(POSITION);
7391 addConfigurationProperty("touch.deviceType", "touchScreen");
7392 prepareDisplay(DISPLAY_ORIENTATION_0);
7393 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7394
7395 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7396
7397 NotifyMotionArgs motionArgs;
7398
7399 // Expect the event to be sent to the internal viewport,
7400 // because an external viewport is not present.
7401 processPosition(mapper, 100, 100);
7402 processSync(mapper);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7405
7406 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007407 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007408 processPosition(mapper, 100, 100);
7409 processSync(mapper);
7410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7411 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7412}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007413
7414/**
7415 * Test touch should not work if outside of surface.
7416 */
7417class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7418protected:
7419 void halfDisplayToCenterHorizontal(int32_t orientation) {
7420 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007421 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007422
7423 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7424 internalViewport->orientation = orientation;
7425 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7426 internalViewport->logicalLeft = 0;
7427 internalViewport->logicalTop = 0;
7428 internalViewport->logicalRight = DISPLAY_HEIGHT;
7429 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7430
7431 internalViewport->physicalLeft = 0;
7432 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7433 internalViewport->physicalRight = DISPLAY_HEIGHT;
7434 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7435
7436 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7437 internalViewport->deviceHeight = DISPLAY_WIDTH;
7438 } else {
7439 internalViewport->logicalLeft = 0;
7440 internalViewport->logicalTop = 0;
7441 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7442 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7443
7444 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7445 internalViewport->physicalTop = 0;
7446 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7447 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7448
7449 internalViewport->deviceWidth = DISPLAY_WIDTH;
7450 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7451 }
7452
7453 mFakePolicy->updateViewport(internalViewport.value());
7454 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7455 }
7456
7457 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7458 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7459 int32_t yExpected) {
7460 // touch on outside area should not work.
7461 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7462 processSync(mapper);
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7464
7465 // touch on inside area should receive the event.
7466 NotifyMotionArgs args;
7467 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7468 processSync(mapper);
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7470 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7471 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7472
7473 // Reset.
7474 mapper.reset(ARBITRARY_TIME);
7475 }
7476};
7477
7478TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7479 addConfigurationProperty("touch.deviceType", "touchScreen");
7480 prepareDisplay(DISPLAY_ORIENTATION_0);
7481 prepareAxes(POSITION);
7482 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7483
7484 // Touch on center of normal display should work.
7485 const int32_t x = DISPLAY_WIDTH / 4;
7486 const int32_t y = DISPLAY_HEIGHT / 2;
7487 processPosition(mapper, toRawX(x), toRawY(y));
7488 processSync(mapper);
7489 NotifyMotionArgs args;
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7492 0.0f, 0.0f, 0.0f, 0.0f));
7493 // Reset.
7494 mapper.reset(ARBITRARY_TIME);
7495
7496 // Let physical display be different to device, and make surface and physical could be 1:1.
7497 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7498
7499 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7500 const int32_t yExpected = y;
7501 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7502}
7503
7504TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7505 addConfigurationProperty("touch.deviceType", "touchScreen");
7506 prepareDisplay(DISPLAY_ORIENTATION_0);
7507 prepareAxes(POSITION);
7508 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7509
7510 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7511 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7512
7513 const int32_t x = DISPLAY_WIDTH / 4;
7514 const int32_t y = DISPLAY_HEIGHT / 2;
7515
7516 // expect x/y = swap x/y then reverse y.
7517 const int32_t xExpected = y;
7518 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7519 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7520}
7521
7522TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7523 addConfigurationProperty("touch.deviceType", "touchScreen");
7524 prepareDisplay(DISPLAY_ORIENTATION_0);
7525 prepareAxes(POSITION);
7526 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7527
7528 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7529 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7530
7531 const int32_t x = DISPLAY_WIDTH / 4;
7532 const int32_t y = DISPLAY_HEIGHT / 2;
7533
7534 // expect x/y = swap x/y then reverse x.
7535 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7536 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7537 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7538}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007539
7540TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7541 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7542 std::shared_ptr<FakePointerController> fakePointerController =
7543 std::make_shared<FakePointerController>();
7544 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7545 fakePointerController->setPosition(0, 0);
7546 fakePointerController->setButtonState(0);
7547
7548 // prepare device and capture
7549 prepareDisplay(DISPLAY_ORIENTATION_0);
7550 prepareAxes(POSITION | ID | SLOT);
7551 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7552 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7553 mFakePolicy->setPointerCapture(true);
7554 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7555 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7556
7557 // captured touchpad should be a touchpad source
7558 NotifyDeviceResetArgs resetArgs;
7559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7560 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7561
7562 // run captured pointer tests - note that this is unscaled, so input listener events should be
7563 // identical to what the hardware sends (accounting for any
7564 // calibration).
7565 // FINGER 0 DOWN
7566 processId(mapper, 1);
7567 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7568 processKey(mapper, BTN_TOUCH, 1);
7569 processSync(mapper);
7570
7571 // expect coord[0] to contain initial location of touch 0
7572 NotifyMotionArgs args;
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7574 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7575 ASSERT_EQ(1U, args.pointerCount);
7576 ASSERT_EQ(0, args.pointerProperties[0].id);
7577 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7578 ASSERT_NO_FATAL_FAILURE(
7579 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7580
7581 // FINGER 1 DOWN
7582 processSlot(mapper, 1);
7583 processId(mapper, 2);
7584 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7585 processSync(mapper);
7586
7587 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7589 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | 0x0100, args.action);
7590 ASSERT_EQ(2U, args.pointerCount);
7591 ASSERT_EQ(0, args.pointerProperties[0].id);
7592 ASSERT_EQ(1, args.pointerProperties[1].id);
7593 ASSERT_NO_FATAL_FAILURE(
7594 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7595 ASSERT_NO_FATAL_FAILURE(
7596 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7597
7598 // FINGER 1 MOVE
7599 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7600 processSync(mapper);
7601
7602 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7603 // from move
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7606 ASSERT_NO_FATAL_FAILURE(
7607 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7608 ASSERT_NO_FATAL_FAILURE(
7609 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7610
7611 // FINGER 0 MOVE
7612 processSlot(mapper, 0);
7613 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7614 processSync(mapper);
7615
7616 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7619 ASSERT_NO_FATAL_FAILURE(
7620 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7621 ASSERT_NO_FATAL_FAILURE(
7622 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7623
7624 // BUTTON DOWN
7625 processKey(mapper, BTN_LEFT, 1);
7626 processSync(mapper);
7627
7628 // touchinputmapper design sends a move before button press
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7632 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7633
7634 // BUTTON UP
7635 processKey(mapper, BTN_LEFT, 0);
7636 processSync(mapper);
7637
7638 // touchinputmapper design sends a move after button release
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7643
7644 // FINGER 0 UP
7645 processId(mapper, -1);
7646 processSync(mapper);
7647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7648 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7649
7650 // FINGER 1 MOVE
7651 processSlot(mapper, 1);
7652 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7653 processSync(mapper);
7654
7655 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7658 ASSERT_EQ(1U, args.pointerCount);
7659 ASSERT_EQ(1, args.pointerProperties[0].id);
7660 ASSERT_NO_FATAL_FAILURE(
7661 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7662
7663 // FINGER 1 UP
7664 processId(mapper, -1);
7665 processKey(mapper, BTN_TOUCH, 0);
7666 processSync(mapper);
7667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7668 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7669
7670 // non captured touchpad should be a mouse source
7671 mFakePolicy->setPointerCapture(false);
7672 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7674 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7675}
7676
7677TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7678 std::shared_ptr<FakePointerController> fakePointerController =
7679 std::make_shared<FakePointerController>();
7680 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7681 fakePointerController->setPosition(0, 0);
7682 fakePointerController->setButtonState(0);
7683
7684 // prepare device and capture
7685 prepareDisplay(DISPLAY_ORIENTATION_0);
7686 prepareAxes(POSITION | ID | SLOT);
7687 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7688 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7689 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7690 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7691 // run uncaptured pointer tests - pushes out generic events
7692 // FINGER 0 DOWN
7693 processId(mapper, 3);
7694 processPosition(mapper, 100, 100);
7695 processKey(mapper, BTN_TOUCH, 1);
7696 processSync(mapper);
7697
7698 // start at (100,100), cursor should be at (0,0) * scale
7699 NotifyMotionArgs args;
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7702 ASSERT_NO_FATAL_FAILURE(
7703 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7704
7705 // FINGER 0 MOVE
7706 processPosition(mapper, 200, 200);
7707 processSync(mapper);
7708
7709 // compute scaling to help with touch position checking
7710 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7711 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7712 float scale =
7713 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7714
7715 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7719 0, 0, 0, 0, 0, 0, 0));
7720}
7721
7722TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7723 std::shared_ptr<FakePointerController> fakePointerController =
7724 std::make_shared<FakePointerController>();
7725
7726 prepareDisplay(DISPLAY_ORIENTATION_0);
7727 prepareAxes(POSITION | ID | SLOT);
7728 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7729 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7730 mFakePolicy->setPointerCapture(false);
7731 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7732
7733 // uncaptured touchpad should be a pointer device
7734 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7735
7736 // captured touchpad should be a touchpad device
7737 mFakePolicy->setPointerCapture(true);
7738 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7739 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7740}
7741
Michael Wrightd02c5b62014-02-10 15:10:22 -08007742} // namespace android