blob: a974d09797085cbff5a40fd5fc580d7dcae190b0 [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);
Chris Ye42b06822020-08-07 11:39:33 -07002313 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002314 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002315 }
2316
2317 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002318 int32_t orientation, const std::string& uniqueId,
2319 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002320 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002321 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002322 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2323 }
2324
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002325 void clearViewports() {
2326 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327 }
2328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002329 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2330 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002331 RawEvent event;
2332 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002333 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002334 event.type = type;
2335 event.code = code;
2336 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002337 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338 }
2339
2340 static void assertMotionRange(const InputDeviceInfo& info,
2341 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2342 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002343 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002344 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2345 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2346 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2347 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2348 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2349 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2350 }
2351
2352 static void assertPointerCoords(const PointerCoords& coords,
2353 float x, float y, float pressure, float size,
2354 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2355 float orientation, float distance) {
2356 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2357 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2358 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2359 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2360 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2361 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2362 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2363 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2364 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2365 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2366 }
2367
Michael Wright17db18e2020-06-26 20:51:44 +01002368 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002369 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002370 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371 ASSERT_NEAR(x, actualX, 1);
2372 ASSERT_NEAR(y, actualY, 1);
2373 }
2374};
2375
2376const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002377const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002378const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2380const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002381const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2382 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002383const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384
2385// --- SwitchInputMapperTest ---
2386
2387class SwitchInputMapperTest : public InputMapperTest {
2388protected:
2389};
2390
2391TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002392 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002394 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395}
2396
2397TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002398 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002400 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002401 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002403 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002404 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405}
2406
2407TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002408 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002410 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2411 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2412 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2413 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
2415 NotifySwitchArgs args;
2416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2417 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002418 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2419 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 args.switchMask);
2421 ASSERT_EQ(uint32_t(0), args.policyFlags);
2422}
2423
2424
2425// --- KeyboardInputMapperTest ---
2426
2427class KeyboardInputMapperTest : public InputMapperTest {
2428protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002429 const std::string UNIQUE_ID = "local:0";
2430
2431 void prepareDisplay(int32_t orientation);
2432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002433 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002434 int32_t originalKeyCode, int32_t rotatedKeyCode,
2435 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002436};
2437
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002438/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2439 * orientation.
2440 */
2441void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002442 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2443 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002444}
2445
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002446void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002447 int32_t originalScanCode, int32_t originalKeyCode,
2448 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449 NotifyKeyArgs args;
2450
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002451 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2453 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2454 ASSERT_EQ(originalScanCode, args.scanCode);
2455 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002456 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002458 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2460 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2461 ASSERT_EQ(originalScanCode, args.scanCode);
2462 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002463 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002464}
2465
Michael Wrightd02c5b62014-02-10 15:10:22 -08002466TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002467 KeyboardInputMapper& mapper =
2468 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2469 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002471 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472}
2473
2474TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2475 const int32_t USAGE_A = 0x070004;
2476 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2478 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002479
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002480 KeyboardInputMapper& mapper =
2481 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2482 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
2484 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002485 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002486 NotifyKeyArgs args;
2487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2488 ASSERT_EQ(DEVICE_ID, args.deviceId);
2489 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2490 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2491 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2492 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2493 ASSERT_EQ(KEY_HOME, args.scanCode);
2494 ASSERT_EQ(AMETA_NONE, args.metaState);
2495 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2496 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2497 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2498
2499 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002500 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2502 ASSERT_EQ(DEVICE_ID, args.deviceId);
2503 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2504 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2505 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2506 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2507 ASSERT_EQ(KEY_HOME, args.scanCode);
2508 ASSERT_EQ(AMETA_NONE, args.metaState);
2509 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2510 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2511 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2512
2513 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002514 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2515 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2517 ASSERT_EQ(DEVICE_ID, args.deviceId);
2518 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2519 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2520 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2521 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2522 ASSERT_EQ(0, args.scanCode);
2523 ASSERT_EQ(AMETA_NONE, args.metaState);
2524 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2525 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2526 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2527
2528 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002529 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2530 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2532 ASSERT_EQ(DEVICE_ID, args.deviceId);
2533 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2534 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2535 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2536 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2537 ASSERT_EQ(0, args.scanCode);
2538 ASSERT_EQ(AMETA_NONE, args.metaState);
2539 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2540 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2541 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2542
2543 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002544 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2545 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2547 ASSERT_EQ(DEVICE_ID, args.deviceId);
2548 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2549 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2550 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2551 ASSERT_EQ(0, args.keyCode);
2552 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2553 ASSERT_EQ(AMETA_NONE, args.metaState);
2554 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2555 ASSERT_EQ(0U, args.policyFlags);
2556 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2557
2558 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002559 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2560 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2562 ASSERT_EQ(DEVICE_ID, args.deviceId);
2563 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2564 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2565 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2566 ASSERT_EQ(0, args.keyCode);
2567 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2568 ASSERT_EQ(AMETA_NONE, args.metaState);
2569 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2570 ASSERT_EQ(0U, args.policyFlags);
2571 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2572}
2573
2574TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 KeyboardInputMapper& mapper =
2579 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2580 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581
2582 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002583 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584
2585 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002586 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587 NotifyKeyArgs args;
2588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2592
2593 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002594 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598
2599 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002600 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002603 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604
2605 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002606 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2608 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002609 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2611}
2612
2613TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2617 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002619 KeyboardInputMapper& mapper =
2620 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2621 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002623 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2625 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2626 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2627 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2628 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2629 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2631 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2632}
2633
2634TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2638 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002641 KeyboardInputMapper& mapper =
2642 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2643 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002645 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002646 ASSERT_NO_FATAL_FAILURE(
2647 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2649 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2650 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2651 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2652 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2653 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002655 clearViewports();
2656 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002657 ASSERT_NO_FATAL_FAILURE(
2658 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2660 AKEYCODE_DPAD_UP, DISPLAY_ID));
2661 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2662 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2663 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2664 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002666 clearViewports();
2667 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002668 ASSERT_NO_FATAL_FAILURE(
2669 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2670 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2671 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2672 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2673 AKEYCODE_DPAD_UP, DISPLAY_ID));
2674 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2675 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002677 clearViewports();
2678 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002679 ASSERT_NO_FATAL_FAILURE(
2680 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2681 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2682 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2683 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2684 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2685 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2686 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687
2688 // Special case: if orientation changes while key is down, we still emit the same keycode
2689 // in the key up as we did in the key down.
2690 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002691 clearViewports();
2692 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002693 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2696 ASSERT_EQ(KEY_UP, args.scanCode);
2697 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2698
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002699 clearViewports();
2700 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002701 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2703 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2704 ASSERT_EQ(KEY_UP, args.scanCode);
2705 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2706}
2707
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002708TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2709 // If the keyboard is not orientation aware,
2710 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002711 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002713 KeyboardInputMapper& mapper =
2714 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2715 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002716 NotifyKeyArgs args;
2717
2718 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002719 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002721 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2723 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2724
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002725 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002726 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002728 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2730 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2731}
2732
2733TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2734 // If the keyboard is orientation aware,
2735 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002737
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002738 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002739 KeyboardInputMapper& mapper =
2740 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2741 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002742 NotifyKeyArgs args;
2743
2744 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2745 // ^--- already checked by the previous test
2746
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002747 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002748 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002749 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002751 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2753 ASSERT_EQ(DISPLAY_ID, args.displayId);
2754
2755 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002756 clearViewports();
2757 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002758 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002759 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002761 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2763 ASSERT_EQ(newDisplayId, args.displayId);
2764}
2765
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 KeyboardInputMapper& mapper =
2768 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2769 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002771 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002773
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002774 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002775 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776}
2777
2778TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 KeyboardInputMapper& mapper =
2780 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2781 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002783 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002784 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002786 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788}
2789
2790TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002791 KeyboardInputMapper& mapper =
2792 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2793 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002795 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
2797 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2798 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002799 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800 ASSERT_TRUE(flags[0]);
2801 ASSERT_FALSE(flags[1]);
2802}
2803
2804TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002805 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2806 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2807 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2808 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2809 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2810 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002812 KeyboardInputMapper& mapper =
2813 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2814 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815
2816 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820
2821 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002822 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2823 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002824 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2826 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002827 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828
2829 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002830 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2831 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002832 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2833 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2834 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002835 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
2837 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002838 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2839 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002840 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2841 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2842 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002843 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844
2845 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002846 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2847 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002848 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2849 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2850 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852
2853 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002854 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2855 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002856 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2857 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2858 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002859 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860
2861 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002862 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2863 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2865 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2866 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002867 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868}
2869
Arthur Hung2c9a3342019-07-23 14:18:59 +08002870TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2871 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2874 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2875 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002876
2877 // keyboard 2.
2878 const std::string USB2 = "USB2";
2879 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002880 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002881 InputDeviceIdentifier identifier;
2882 identifier.name = "KEYBOARD2";
2883 identifier.location = USB2;
2884 std::unique_ptr<InputDevice> device2 =
2885 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002886 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07002887 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME,
2888 Flags<InputDeviceClass>(0) /*classes*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002889 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2890 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2891 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2892 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 KeyboardInputMapper& mapper =
2895 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2896 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002898 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002899 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002900 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002901 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2902 device2->reset(ARBITRARY_TIME);
2903
2904 // Prepared displays and associated info.
2905 constexpr uint8_t hdmi1 = 0;
2906 constexpr uint8_t hdmi2 = 1;
2907 const std::string SECONDARY_UNIQUE_ID = "local:1";
2908
2909 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2910 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2911
2912 // No associated display viewport found, should disable the device.
2913 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2914 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2915 ASSERT_FALSE(device2->isEnabled());
2916
2917 // Prepare second display.
2918 constexpr int32_t newDisplayId = 2;
2919 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002920 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002921 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002922 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002923 // Default device will reconfigure above, need additional reconfiguration for another device.
2924 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2925 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2926
2927 // Device should be enabled after the associated display is found.
2928 ASSERT_TRUE(mDevice->isEnabled());
2929 ASSERT_TRUE(device2->isEnabled());
2930
2931 // Test pad key events
2932 ASSERT_NO_FATAL_FAILURE(
2933 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2934 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2935 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2936 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2937 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2938 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2939 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2940
2941 ASSERT_NO_FATAL_FAILURE(
2942 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2944 AKEYCODE_DPAD_RIGHT, newDisplayId));
2945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2946 AKEYCODE_DPAD_DOWN, newDisplayId));
2947 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2948 AKEYCODE_DPAD_LEFT, newDisplayId));
2949}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002951// --- KeyboardInputMapperTest_ExternalDevice ---
2952
2953class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2954protected:
2955 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07002956 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002957 }
2958};
2959
2960TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002961 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2962 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002963
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2967 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002969 KeyboardInputMapper& mapper =
2970 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2971 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002972
2973 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2974 NotifyKeyArgs args;
2975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2976 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2977
2978 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2980 ASSERT_EQ(uint32_t(0), args.policyFlags);
2981
2982 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2984 ASSERT_EQ(uint32_t(0), args.policyFlags);
2985
2986 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2988 ASSERT_EQ(uint32_t(0), args.policyFlags);
2989
2990 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2992 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2993
2994 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2996 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2997}
2998
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002999TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003000 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003001
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003005
Powei Fengd041c5d2019-05-03 17:11:33 -07003006 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003007 KeyboardInputMapper& mapper =
3008 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3009 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003010
3011 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3012 NotifyKeyArgs args;
3013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3014 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3015
3016 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3018 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3019
3020 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3022 ASSERT_EQ(uint32_t(0), args.policyFlags);
3023
3024 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3026 ASSERT_EQ(uint32_t(0), args.policyFlags);
3027
3028 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3030 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3031
3032 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3034 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3035}
3036
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037// --- CursorInputMapperTest ---
3038
3039class CursorInputMapperTest : public InputMapperTest {
3040protected:
3041 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3042
Michael Wright17db18e2020-06-26 20:51:44 +01003043 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044
Prabir Pradhan28efc192019-11-05 01:10:04 +00003045 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046 InputMapperTest::SetUp();
3047
Michael Wright17db18e2020-06-26 20:51:44 +01003048 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003049 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050 }
3051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003052 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3053 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003054
3055 void prepareDisplay(int32_t orientation) {
3056 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003057 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003058 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3059 orientation, uniqueId, NO_PORT, viewportType);
3060 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061};
3062
3063const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003065void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3066 int32_t originalY, int32_t rotatedX,
3067 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 NotifyMotionArgs args;
3069
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003070 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3071 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3072 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3076 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3077 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3078 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3079}
3080
3081TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086}
3087
3088TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003090 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093}
3094
3095TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
3099 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101
3102 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003103 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3104 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3106 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3107
3108 // When the bounds are set, then there should be a valid motion range.
3109 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3110
3111 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003112 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113
3114 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3115 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3116 1, 800 - 1, 0.0f, 0.0f));
3117 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3118 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3119 2, 480 - 1, 0.0f, 0.0f));
3120 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3121 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3122 0.0f, 1.0f, 0.0f, 0.0f));
3123}
3124
3125TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128
3129 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003130 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131
3132 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3133 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3134 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3135 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3136 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3137 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3138 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3139 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3140 0.0f, 1.0f, 0.0f, 0.0f));
3141}
3142
3143TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003145 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146
3147 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3148
3149 NotifyMotionArgs args;
3150
3151 // Button press.
3152 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003153 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3154 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3156 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3157 ASSERT_EQ(DEVICE_ID, args.deviceId);
3158 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3159 ASSERT_EQ(uint32_t(0), args.policyFlags);
3160 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3161 ASSERT_EQ(0, args.flags);
3162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3163 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3164 ASSERT_EQ(0, args.edgeFlags);
3165 ASSERT_EQ(uint32_t(1), args.pointerCount);
3166 ASSERT_EQ(0, args.pointerProperties[0].id);
3167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3169 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3171 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3173
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3175 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3176 ASSERT_EQ(DEVICE_ID, args.deviceId);
3177 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3178 ASSERT_EQ(uint32_t(0), args.policyFlags);
3179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3180 ASSERT_EQ(0, args.flags);
3181 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3182 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3183 ASSERT_EQ(0, args.edgeFlags);
3184 ASSERT_EQ(uint32_t(1), args.pointerCount);
3185 ASSERT_EQ(0, args.pointerProperties[0].id);
3186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3188 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3189 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3190 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3191 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3192
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003194 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3195 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3197 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3198 ASSERT_EQ(DEVICE_ID, args.deviceId);
3199 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3200 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3202 ASSERT_EQ(0, args.flags);
3203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3204 ASSERT_EQ(0, args.buttonState);
3205 ASSERT_EQ(0, args.edgeFlags);
3206 ASSERT_EQ(uint32_t(1), args.pointerCount);
3207 ASSERT_EQ(0, args.pointerProperties[0].id);
3208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3210 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3211 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3212 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3213 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3214
3215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3216 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3217 ASSERT_EQ(DEVICE_ID, args.deviceId);
3218 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3219 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3221 ASSERT_EQ(0, args.flags);
3222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3223 ASSERT_EQ(0, args.buttonState);
3224 ASSERT_EQ(0, args.edgeFlags);
3225 ASSERT_EQ(uint32_t(1), args.pointerCount);
3226 ASSERT_EQ(0, args.pointerProperties[0].id);
3227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3229 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3230 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3231 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3232 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3233}
3234
3235TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003237 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238
3239 NotifyMotionArgs args;
3240
3241 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003242 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3243 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3247 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3248
3249 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003250 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3251 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3255 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3256}
3257
3258TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003260 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261
3262 NotifyMotionArgs args;
3263
3264 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003265 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3266 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3268 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3270 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3271
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3275 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3276
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003278 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3279 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3283 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3284
3285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3288 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3289}
3290
3291TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294
3295 NotifyMotionArgs args;
3296
3297 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003298 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3299 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3300 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3301 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3303 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3305 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3306 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3307
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3311 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3312 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3313
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003315 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3316 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3317 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3321 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3322 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3323
3324 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003325 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3326 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3330 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3331
3332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3335 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3336}
3337
3338TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003340 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003342 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3345 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3346 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3351}
3352
3353TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 addConfigurationProperty("cursor.mode", "navigation");
3355 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003358 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3365 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3366 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3367
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003368 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3377
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003378 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3384 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3385 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3386 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3387
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003388 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3395 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3396 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3397}
3398
3399TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402
3403 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3404 mFakePointerController->setPosition(100, 200);
3405 mFakePointerController->setButtonState(0);
3406
3407 NotifyMotionArgs motionArgs;
3408 NotifyKeyArgs keyArgs;
3409
3410 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003411 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3412 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3415 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3416 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3418 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3419
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3422 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3423 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3425 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3426
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003427 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3428 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431 ASSERT_EQ(0, motionArgs.buttonState);
3432 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3434 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3435
3436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 ASSERT_EQ(0, motionArgs.buttonState);
3439 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3441 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3442
3443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003445 ASSERT_EQ(0, motionArgs.buttonState);
3446 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3448 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3449
3450 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003451 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3452 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3453 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3456 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3457 motionArgs.buttonState);
3458 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3459 mFakePointerController->getButtonState());
3460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3461 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3462
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3465 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3466 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3467 mFakePointerController->getButtonState());
3468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3469 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3470
3471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3472 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3473 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3474 motionArgs.buttonState);
3475 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3476 mFakePointerController->getButtonState());
3477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3478 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3479
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003480 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3481 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3485 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3487 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3488
3489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003491 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3492 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3494 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3495
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003496 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3497 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3500 ASSERT_EQ(0, motionArgs.buttonState);
3501 ASSERT_EQ(0, mFakePointerController->getButtonState());
3502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3503 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 -08003504 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3505 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003506
3507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508 ASSERT_EQ(0, motionArgs.buttonState);
3509 ASSERT_EQ(0, mFakePointerController->getButtonState());
3510 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3512 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 -08003513
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3515 ASSERT_EQ(0, motionArgs.buttonState);
3516 ASSERT_EQ(0, mFakePointerController->getButtonState());
3517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3519 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3520
3521 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003522 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3523 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3525 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3526 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003527
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3533 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3534
3535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3537 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3538 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3540 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3541
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003542 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3543 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_EQ(0, motionArgs.buttonState);
3547 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3549 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3550
3551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003553 ASSERT_EQ(0, motionArgs.buttonState);
3554 ASSERT_EQ(0, mFakePointerController->getButtonState());
3555
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3557 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3559 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3560 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3561
3562 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003563 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3564 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3566 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3567 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003568
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3572 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3574 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3575
3576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3577 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3578 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3579 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3581 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3582
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003583 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3584 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 ASSERT_EQ(0, motionArgs.buttonState);
3588 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3590 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 -08003591
3592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3593 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3594 ASSERT_EQ(0, motionArgs.buttonState);
3595 ASSERT_EQ(0, mFakePointerController->getButtonState());
3596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3597 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3598
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3600 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3601 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3602
3603 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003604 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3605 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3607 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3608 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003609
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3613 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3615 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3616
3617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3620 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3622 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3623
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003624 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3625 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_EQ(0, motionArgs.buttonState);
3629 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3631 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 -08003632
3633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3635 ASSERT_EQ(0, motionArgs.buttonState);
3636 ASSERT_EQ(0, mFakePointerController->getButtonState());
3637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3638 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3639
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3641 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3642 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3643
3644 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003645 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3646 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3648 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3649 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003650
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3654 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3656 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3657
3658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3659 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3660 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3661 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3663 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3664
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003665 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3666 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669 ASSERT_EQ(0, motionArgs.buttonState);
3670 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3672 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 -08003673
3674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3676 ASSERT_EQ(0, motionArgs.buttonState);
3677 ASSERT_EQ(0, mFakePointerController->getButtonState());
3678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3679 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3680
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3682 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3683 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3684}
3685
3686TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689
3690 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3691 mFakePointerController->setPosition(100, 200);
3692 mFakePointerController->setButtonState(0);
3693
3694 NotifyMotionArgs args;
3695
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003696 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3697 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3698 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003700 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3703 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 +01003704 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003705}
3706
3707TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003708 addConfigurationProperty("cursor.mode", "pointer");
3709 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003710 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003711
3712 NotifyDeviceResetArgs resetArgs;
3713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3714 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3715 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3716
3717 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3718 mFakePointerController->setPosition(100, 200);
3719 mFakePointerController->setButtonState(0);
3720
3721 NotifyMotionArgs args;
3722
3723 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003724 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3725 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3726 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3728 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3731 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 +01003732 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003733
3734 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003735 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3736 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3738 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3741 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3743 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3746 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3747
3748 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003749 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3750 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3752 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3755 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3757 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3758 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3760 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3761
3762 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003763 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3764 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3765 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3767 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3770 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 +01003771 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003772
3773 // Disable pointer capture and check that the device generation got bumped
3774 // and events are generated the usual way.
3775 const uint32_t generation = mFakeContext->getGeneration();
3776 mFakePolicy->setPointerCapture(false);
3777 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3778 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3779
3780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3781 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3782 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3783
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003784 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3785 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3786 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3788 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3791 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 +01003792 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793}
3794
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003795TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003797
Garfield Tan888a6a42020-01-09 11:39:16 -08003798 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003799 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003800 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3801 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003802 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003803 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3804 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3805
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003806 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3807 mFakePointerController->setPosition(100, 200);
3808 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003809
3810 NotifyMotionArgs args;
3811 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3812 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3813 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3815 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3816 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3818 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 +01003819 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003820 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3821}
3822
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823// --- TouchInputMapperTest ---
3824
3825class TouchInputMapperTest : public InputMapperTest {
3826protected:
3827 static const int32_t RAW_X_MIN;
3828 static const int32_t RAW_X_MAX;
3829 static const int32_t RAW_Y_MIN;
3830 static const int32_t RAW_Y_MAX;
3831 static const int32_t RAW_TOUCH_MIN;
3832 static const int32_t RAW_TOUCH_MAX;
3833 static const int32_t RAW_TOOL_MIN;
3834 static const int32_t RAW_TOOL_MAX;
3835 static const int32_t RAW_PRESSURE_MIN;
3836 static const int32_t RAW_PRESSURE_MAX;
3837 static const int32_t RAW_ORIENTATION_MIN;
3838 static const int32_t RAW_ORIENTATION_MAX;
3839 static const int32_t RAW_DISTANCE_MIN;
3840 static const int32_t RAW_DISTANCE_MAX;
3841 static const int32_t RAW_TILT_MIN;
3842 static const int32_t RAW_TILT_MAX;
3843 static const int32_t RAW_ID_MIN;
3844 static const int32_t RAW_ID_MAX;
3845 static const int32_t RAW_SLOT_MIN;
3846 static const int32_t RAW_SLOT_MAX;
3847 static const float X_PRECISION;
3848 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003849 static const float X_PRECISION_VIRTUAL;
3850 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851
3852 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003853 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854
3855 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3856
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003857 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003858 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003859
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860 enum Axes {
3861 POSITION = 1 << 0,
3862 TOUCH = 1 << 1,
3863 TOOL = 1 << 2,
3864 PRESSURE = 1 << 3,
3865 ORIENTATION = 1 << 4,
3866 MINOR = 1 << 5,
3867 ID = 1 << 6,
3868 DISTANCE = 1 << 7,
3869 TILT = 1 << 8,
3870 SLOT = 1 << 9,
3871 TOOL_TYPE = 1 << 10,
3872 };
3873
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003874 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3875 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003876 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003878 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 int32_t toRawX(float displayX);
3880 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003881 float toCookedX(float rawX, float rawY);
3882 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003884 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003886 float toDisplayY(int32_t rawY, int32_t displayHeight);
3887
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888};
3889
3890const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3891const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3892const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3893const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3894const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3895const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3896const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3897const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003898const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3899const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3901const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3902const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3903const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3904const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3905const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3906const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3907const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3908const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3909const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3910const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3911const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003912const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3913 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3914const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3915 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003916const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3917 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919const float TouchInputMapperTest::GEOMETRIC_SCALE =
3920 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3921 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3922
3923const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3924 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3925 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3926};
3927
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003928void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003929 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3930 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003931}
3932
3933void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3934 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3935 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936}
3937
Santos Cordonfa5cf462017-04-05 10:37:00 -07003938void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003939 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
3940 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
3941 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003942}
3943
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003945 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3946 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3947 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949}
3950
Jason Gerecke489fda82012-09-07 17:19:40 -07003951void TouchInputMapperTest::prepareLocationCalibration() {
3952 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3953}
3954
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955int32_t TouchInputMapperTest::toRawX(float displayX) {
3956 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3957}
3958
3959int32_t TouchInputMapperTest::toRawY(float displayY) {
3960 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3961}
3962
Jason Gerecke489fda82012-09-07 17:19:40 -07003963float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3964 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3965 return rawX;
3966}
3967
3968float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3969 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3970 return rawY;
3971}
3972
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003974 return toDisplayX(rawX, DISPLAY_WIDTH);
3975}
3976
3977float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3978 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979}
3980
3981float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003982 return toDisplayY(rawY, DISPLAY_HEIGHT);
3983}
3984
3985float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3986 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987}
3988
3989
3990// --- SingleTouchInputMapperTest ---
3991
3992class SingleTouchInputMapperTest : public TouchInputMapperTest {
3993protected:
3994 void prepareButtons();
3995 void prepareAxes(int axes);
3996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3998 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3999 void processUp(SingleTouchInputMapper& mappery);
4000 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4001 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4002 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4003 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4004 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4005 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006};
4007
4008void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004009 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010}
4011
4012void SingleTouchInputMapperTest::prepareAxes(int axes) {
4013 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4015 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 }
4017 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004018 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4019 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020 }
4021 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004022 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4023 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024 }
4025 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004026 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4027 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004028 }
4029 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004030 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4031 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032 }
4033}
4034
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004035void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004036 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4037 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4038 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039}
4040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004041void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004042 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4043 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044}
4045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004046void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004047 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048}
4049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004051 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052}
4053
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004054void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4055 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004056 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057}
4058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004059void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004060 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061}
4062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4064 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004065 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4066 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067}
4068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004069void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4070 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004071 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072}
4073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004074void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004075 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076}
4077
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 prepareButtons();
4080 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004081 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004083 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084}
4085
4086TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004087 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4088 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 prepareButtons();
4090 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004091 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004093 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094}
4095
4096TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 prepareButtons();
4098 prepareAxes(POSITION);
4099 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004100 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004102 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103}
4104
4105TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106 prepareButtons();
4107 prepareAxes(POSITION);
4108 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004109 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004111 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112}
4113
4114TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 addConfigurationProperty("touch.deviceType", "touchScreen");
4116 prepareDisplay(DISPLAY_ORIENTATION_0);
4117 prepareButtons();
4118 prepareAxes(POSITION);
4119 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004120 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
4122 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004123 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124
4125 // Virtual key is down.
4126 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4127 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4128 processDown(mapper, x, y);
4129 processSync(mapper);
4130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004132 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133
4134 // Virtual key is up.
4135 processUp(mapper);
4136 processSync(mapper);
4137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004139 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140}
4141
4142TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 addConfigurationProperty("touch.deviceType", "touchScreen");
4144 prepareDisplay(DISPLAY_ORIENTATION_0);
4145 prepareButtons();
4146 prepareAxes(POSITION);
4147 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004148 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149
4150 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152
4153 // Virtual key is down.
4154 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4155 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4156 processDown(mapper, x, y);
4157 processSync(mapper);
4158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004160 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161
4162 // Virtual key is up.
4163 processUp(mapper);
4164 processSync(mapper);
4165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004167 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168}
4169
4170TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 addConfigurationProperty("touch.deviceType", "touchScreen");
4172 prepareDisplay(DISPLAY_ORIENTATION_0);
4173 prepareButtons();
4174 prepareAxes(POSITION);
4175 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004176 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
4178 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4179 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004180 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 ASSERT_TRUE(flags[0]);
4182 ASSERT_FALSE(flags[1]);
4183}
4184
4185TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186 addConfigurationProperty("touch.deviceType", "touchScreen");
4187 prepareDisplay(DISPLAY_ORIENTATION_0);
4188 prepareButtons();
4189 prepareAxes(POSITION);
4190 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004191 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192
4193 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4194
4195 NotifyKeyArgs args;
4196
4197 // Press virtual key.
4198 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4199 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4200 processDown(mapper, x, y);
4201 processSync(mapper);
4202
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4204 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4205 ASSERT_EQ(DEVICE_ID, args.deviceId);
4206 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4207 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4208 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4209 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4210 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4211 ASSERT_EQ(KEY_HOME, args.scanCode);
4212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4213 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4214
4215 // Release virtual key.
4216 processUp(mapper);
4217 processSync(mapper);
4218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4220 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4221 ASSERT_EQ(DEVICE_ID, args.deviceId);
4222 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4223 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4224 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4225 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4226 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4227 ASSERT_EQ(KEY_HOME, args.scanCode);
4228 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4229 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4230
4231 // Should not have sent any motions.
4232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4233}
4234
4235TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 addConfigurationProperty("touch.deviceType", "touchScreen");
4237 prepareDisplay(DISPLAY_ORIENTATION_0);
4238 prepareButtons();
4239 prepareAxes(POSITION);
4240 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004241 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
4243 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4244
4245 NotifyKeyArgs keyArgs;
4246
4247 // Press virtual key.
4248 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4249 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4250 processDown(mapper, x, y);
4251 processSync(mapper);
4252
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4254 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4255 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4256 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4257 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4258 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4259 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4260 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4261 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4263 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4264
4265 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4266 // into the display area.
4267 y -= 100;
4268 processMove(mapper, x, y);
4269 processSync(mapper);
4270
4271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4272 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4273 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4274 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4275 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4276 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4277 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4278 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4279 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4280 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4281 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4282 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4283
4284 NotifyMotionArgs motionArgs;
4285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4287 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4288 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4289 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4291 ASSERT_EQ(0, motionArgs.flags);
4292 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4293 ASSERT_EQ(0, motionArgs.buttonState);
4294 ASSERT_EQ(0, motionArgs.edgeFlags);
4295 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4296 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4299 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4300 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4301 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4303
4304 // Keep moving out of bounds. Should generate a pointer move.
4305 y -= 50;
4306 processMove(mapper, x, y);
4307 processSync(mapper);
4308
4309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4311 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4312 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4313 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4315 ASSERT_EQ(0, motionArgs.flags);
4316 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4317 ASSERT_EQ(0, motionArgs.buttonState);
4318 ASSERT_EQ(0, motionArgs.edgeFlags);
4319 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4323 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4324 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4325 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4327
4328 // Release out of bounds. Should generate a pointer up.
4329 processUp(mapper);
4330 processSync(mapper);
4331
4332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4333 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4334 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4335 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4336 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4338 ASSERT_EQ(0, motionArgs.flags);
4339 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4340 ASSERT_EQ(0, motionArgs.buttonState);
4341 ASSERT_EQ(0, motionArgs.edgeFlags);
4342 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4343 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4346 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4347 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4348 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4349 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4350
4351 // Should not have sent any more keys or motions.
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4354}
4355
4356TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 addConfigurationProperty("touch.deviceType", "touchScreen");
4358 prepareDisplay(DISPLAY_ORIENTATION_0);
4359 prepareButtons();
4360 prepareAxes(POSITION);
4361 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004362 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363
4364 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4365
4366 NotifyMotionArgs motionArgs;
4367
4368 // Initially go down out of bounds.
4369 int32_t x = -10;
4370 int32_t y = -10;
4371 processDown(mapper, x, y);
4372 processSync(mapper);
4373
4374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4375
4376 // Move into the display area. Should generate a pointer down.
4377 x = 50;
4378 y = 75;
4379 processMove(mapper, x, y);
4380 processSync(mapper);
4381
4382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4383 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4384 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4385 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4386 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4388 ASSERT_EQ(0, motionArgs.flags);
4389 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4390 ASSERT_EQ(0, motionArgs.buttonState);
4391 ASSERT_EQ(0, motionArgs.edgeFlags);
4392 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4393 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4396 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4397 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4398 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4400
4401 // Release. Should generate a pointer up.
4402 processUp(mapper);
4403 processSync(mapper);
4404
4405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4407 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4410 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4411 ASSERT_EQ(0, motionArgs.flags);
4412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4413 ASSERT_EQ(0, motionArgs.buttonState);
4414 ASSERT_EQ(0, motionArgs.edgeFlags);
4415 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4419 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4420 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4421 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4423
4424 // Should not have sent any more keys or motions.
4425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4427}
4428
Santos Cordonfa5cf462017-04-05 10:37:00 -07004429TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004430 addConfigurationProperty("touch.deviceType", "touchScreen");
4431 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4432
4433 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4434 prepareButtons();
4435 prepareAxes(POSITION);
4436 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004437 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004438
4439 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4440
4441 NotifyMotionArgs motionArgs;
4442
4443 // Down.
4444 int32_t x = 100;
4445 int32_t y = 125;
4446 processDown(mapper, x, y);
4447 processSync(mapper);
4448
4449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4452 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4453 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4454 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4456 ASSERT_EQ(0, motionArgs.flags);
4457 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4458 ASSERT_EQ(0, motionArgs.buttonState);
4459 ASSERT_EQ(0, motionArgs.edgeFlags);
4460 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4464 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4465 1, 0, 0, 0, 0, 0, 0, 0));
4466 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4467 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4468 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4469
4470 // Move.
4471 x += 50;
4472 y += 75;
4473 processMove(mapper, x, y);
4474 processSync(mapper);
4475
4476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4478 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4479 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4480 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4481 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4483 ASSERT_EQ(0, motionArgs.flags);
4484 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4485 ASSERT_EQ(0, motionArgs.buttonState);
4486 ASSERT_EQ(0, motionArgs.edgeFlags);
4487 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4488 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4491 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4492 1, 0, 0, 0, 0, 0, 0, 0));
4493 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4494 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4496
4497 // Up.
4498 processUp(mapper);
4499 processSync(mapper);
4500
4501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4503 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4504 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4505 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4506 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4508 ASSERT_EQ(0, motionArgs.flags);
4509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4510 ASSERT_EQ(0, motionArgs.buttonState);
4511 ASSERT_EQ(0, motionArgs.edgeFlags);
4512 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4516 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4517 1, 0, 0, 0, 0, 0, 0, 0));
4518 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4519 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4520 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4521
4522 // Should not have sent any more keys or motions.
4523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4525}
4526
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 addConfigurationProperty("touch.deviceType", "touchScreen");
4529 prepareDisplay(DISPLAY_ORIENTATION_0);
4530 prepareButtons();
4531 prepareAxes(POSITION);
4532 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004533 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
4535 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4536
4537 NotifyMotionArgs motionArgs;
4538
4539 // Down.
4540 int32_t x = 100;
4541 int32_t y = 125;
4542 processDown(mapper, x, y);
4543 processSync(mapper);
4544
4545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4547 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4548 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4549 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4550 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4551 ASSERT_EQ(0, motionArgs.flags);
4552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4553 ASSERT_EQ(0, motionArgs.buttonState);
4554 ASSERT_EQ(0, motionArgs.edgeFlags);
4555 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4556 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4559 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4560 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4561 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4563
4564 // Move.
4565 x += 50;
4566 y += 75;
4567 processMove(mapper, x, y);
4568 processSync(mapper);
4569
4570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4572 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4576 ASSERT_EQ(0, motionArgs.flags);
4577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4578 ASSERT_EQ(0, motionArgs.buttonState);
4579 ASSERT_EQ(0, motionArgs.edgeFlags);
4580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4584 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4585 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4586 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4588
4589 // Up.
4590 processUp(mapper);
4591 processSync(mapper);
4592
4593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4594 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4595 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4596 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4597 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4599 ASSERT_EQ(0, motionArgs.flags);
4600 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4601 ASSERT_EQ(0, motionArgs.buttonState);
4602 ASSERT_EQ(0, motionArgs.edgeFlags);
4603 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4604 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4607 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4608 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4609 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4610 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4611
4612 // Should not have sent any more keys or motions.
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4615}
4616
4617TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 addConfigurationProperty("touch.deviceType", "touchScreen");
4619 prepareButtons();
4620 prepareAxes(POSITION);
4621 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004622 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623
4624 NotifyMotionArgs args;
4625
4626 // Rotation 90.
4627 prepareDisplay(DISPLAY_ORIENTATION_90);
4628 processDown(mapper, toRawX(50), toRawY(75));
4629 processSync(mapper);
4630
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4632 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4633 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4634
4635 processUp(mapper);
4636 processSync(mapper);
4637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4638}
4639
4640TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 addConfigurationProperty("touch.deviceType", "touchScreen");
4642 prepareButtons();
4643 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004644 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645
4646 NotifyMotionArgs args;
4647
4648 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004649 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 prepareDisplay(DISPLAY_ORIENTATION_0);
4651 processDown(mapper, toRawX(50), toRawY(75));
4652 processSync(mapper);
4653
4654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4655 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4656 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4657
4658 processUp(mapper);
4659 processSync(mapper);
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4661
4662 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004663 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 prepareDisplay(DISPLAY_ORIENTATION_90);
4665 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4666 processSync(mapper);
4667
4668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4669 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4670 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4671
4672 processUp(mapper);
4673 processSync(mapper);
4674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4675
4676 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004677 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 prepareDisplay(DISPLAY_ORIENTATION_180);
4679 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4680 processSync(mapper);
4681
4682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4683 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4684 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4685
4686 processUp(mapper);
4687 processSync(mapper);
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4689
4690 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004691 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 prepareDisplay(DISPLAY_ORIENTATION_270);
4693 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4694 processSync(mapper);
4695
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4697 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4698 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4699
4700 processUp(mapper);
4701 processSync(mapper);
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4703}
4704
4705TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 addConfigurationProperty("touch.deviceType", "touchScreen");
4707 prepareDisplay(DISPLAY_ORIENTATION_0);
4708 prepareButtons();
4709 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004710 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
4712 // These calculations are based on the input device calibration documentation.
4713 int32_t rawX = 100;
4714 int32_t rawY = 200;
4715 int32_t rawPressure = 10;
4716 int32_t rawToolMajor = 12;
4717 int32_t rawDistance = 2;
4718 int32_t rawTiltX = 30;
4719 int32_t rawTiltY = 110;
4720
4721 float x = toDisplayX(rawX);
4722 float y = toDisplayY(rawY);
4723 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4724 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4725 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4726 float distance = float(rawDistance);
4727
4728 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4729 float tiltScale = M_PI / 180;
4730 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4731 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4732 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4733 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4734
4735 processDown(mapper, rawX, rawY);
4736 processPressure(mapper, rawPressure);
4737 processToolMajor(mapper, rawToolMajor);
4738 processDistance(mapper, rawDistance);
4739 processTilt(mapper, rawTiltX, rawTiltY);
4740 processSync(mapper);
4741
4742 NotifyMotionArgs args;
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4745 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4746 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4747}
4748
Jason Gerecke489fda82012-09-07 17:19:40 -07004749TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004750 addConfigurationProperty("touch.deviceType", "touchScreen");
4751 prepareDisplay(DISPLAY_ORIENTATION_0);
4752 prepareLocationCalibration();
4753 prepareButtons();
4754 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004755 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004756
4757 int32_t rawX = 100;
4758 int32_t rawY = 200;
4759
4760 float x = toDisplayX(toCookedX(rawX, rawY));
4761 float y = toDisplayY(toCookedY(rawX, rawY));
4762
4763 processDown(mapper, rawX, rawY);
4764 processSync(mapper);
4765
4766 NotifyMotionArgs args;
4767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4769 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4770}
4771
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 addConfigurationProperty("touch.deviceType", "touchScreen");
4774 prepareDisplay(DISPLAY_ORIENTATION_0);
4775 prepareButtons();
4776 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004777 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778
4779 NotifyMotionArgs motionArgs;
4780 NotifyKeyArgs keyArgs;
4781
4782 processDown(mapper, 100, 200);
4783 processSync(mapper);
4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4785 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4786 ASSERT_EQ(0, motionArgs.buttonState);
4787
4788 // press BTN_LEFT, release BTN_LEFT
4789 processKey(mapper, BTN_LEFT, 1);
4790 processSync(mapper);
4791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4793 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4794
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4797 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4798
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799 processKey(mapper, BTN_LEFT, 0);
4800 processSync(mapper);
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004804
4805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004807 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808
4809 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4810 processKey(mapper, BTN_RIGHT, 1);
4811 processKey(mapper, BTN_MIDDLE, 1);
4812 processSync(mapper);
4813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4815 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4816 motionArgs.buttonState);
4817
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4819 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4820 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4821
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4824 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4825 motionArgs.buttonState);
4826
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827 processKey(mapper, BTN_RIGHT, 0);
4828 processSync(mapper);
4829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004835 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836
4837 processKey(mapper, BTN_MIDDLE, 0);
4838 processSync(mapper);
4839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004840 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004842
4843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004845 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846
4847 // press BTN_BACK, release BTN_BACK
4848 processKey(mapper, BTN_BACK, 1);
4849 processSync(mapper);
4850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4851 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4852 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004853
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004856 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4857
4858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4859 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4860 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861
4862 processKey(mapper, BTN_BACK, 0);
4863 processSync(mapper);
4864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004865 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004867
4868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004870 ASSERT_EQ(0, motionArgs.buttonState);
4871
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4873 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4874 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4875
4876 // press BTN_SIDE, release BTN_SIDE
4877 processKey(mapper, BTN_SIDE, 1);
4878 processSync(mapper);
4879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4880 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4881 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004882
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4886
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4889 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890
4891 processKey(mapper, BTN_SIDE, 0);
4892 processSync(mapper);
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004896
4897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004899 ASSERT_EQ(0, motionArgs.buttonState);
4900
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4902 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4903 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4904
4905 // press BTN_FORWARD, release BTN_FORWARD
4906 processKey(mapper, BTN_FORWARD, 1);
4907 processSync(mapper);
4908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4909 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4910 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004911
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004914 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4915
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4918 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
4920 processKey(mapper, BTN_FORWARD, 0);
4921 processSync(mapper);
4922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004923 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004925
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004928 ASSERT_EQ(0, motionArgs.buttonState);
4929
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4931 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4932 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4933
4934 // press BTN_EXTRA, release BTN_EXTRA
4935 processKey(mapper, BTN_EXTRA, 1);
4936 processSync(mapper);
4937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4938 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4939 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004940
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004943 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4944
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4946 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4947 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948
4949 processKey(mapper, BTN_EXTRA, 0);
4950 processSync(mapper);
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004954
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004957 ASSERT_EQ(0, motionArgs.buttonState);
4958
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4960 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4961 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4962
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4964
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965 // press BTN_STYLUS, release BTN_STYLUS
4966 processKey(mapper, BTN_STYLUS, 1);
4967 processSync(mapper);
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004970 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4971
4972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4973 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4974 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975
4976 processKey(mapper, BTN_STYLUS, 0);
4977 processSync(mapper);
4978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004981
4982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004984 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985
4986 // press BTN_STYLUS2, release BTN_STYLUS2
4987 processKey(mapper, BTN_STYLUS2, 1);
4988 processSync(mapper);
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004991 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4992
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4995 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996
4997 processKey(mapper, BTN_STYLUS2, 0);
4998 processSync(mapper);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005002
5003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005005 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006
5007 // release touch
5008 processUp(mapper);
5009 processSync(mapper);
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5011 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5012 ASSERT_EQ(0, motionArgs.buttonState);
5013}
5014
5015TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 addConfigurationProperty("touch.deviceType", "touchScreen");
5017 prepareDisplay(DISPLAY_ORIENTATION_0);
5018 prepareButtons();
5019 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005020 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021
5022 NotifyMotionArgs motionArgs;
5023
5024 // default tool type is finger
5025 processDown(mapper, 100, 200);
5026 processSync(mapper);
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5028 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5030
5031 // eraser
5032 processKey(mapper, BTN_TOOL_RUBBER, 1);
5033 processSync(mapper);
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5037
5038 // stylus
5039 processKey(mapper, BTN_TOOL_RUBBER, 0);
5040 processKey(mapper, BTN_TOOL_PEN, 1);
5041 processSync(mapper);
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5045
5046 // brush
5047 processKey(mapper, BTN_TOOL_PEN, 0);
5048 processKey(mapper, BTN_TOOL_BRUSH, 1);
5049 processSync(mapper);
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5053
5054 // pencil
5055 processKey(mapper, BTN_TOOL_BRUSH, 0);
5056 processKey(mapper, BTN_TOOL_PENCIL, 1);
5057 processSync(mapper);
5058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5061
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005062 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 processKey(mapper, BTN_TOOL_PENCIL, 0);
5064 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5065 processSync(mapper);
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5069
5070 // mouse
5071 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5072 processKey(mapper, BTN_TOOL_MOUSE, 1);
5073 processSync(mapper);
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5077
5078 // lens
5079 processKey(mapper, BTN_TOOL_MOUSE, 0);
5080 processKey(mapper, BTN_TOOL_LENS, 1);
5081 processSync(mapper);
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5085
5086 // double-tap
5087 processKey(mapper, BTN_TOOL_LENS, 0);
5088 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5089 processSync(mapper);
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5093
5094 // triple-tap
5095 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5096 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5097 processSync(mapper);
5098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5101
5102 // quad-tap
5103 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5104 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5105 processSync(mapper);
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5109
5110 // finger
5111 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5112 processKey(mapper, BTN_TOOL_FINGER, 1);
5113 processSync(mapper);
5114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5117
5118 // stylus trumps finger
5119 processKey(mapper, BTN_TOOL_PEN, 1);
5120 processSync(mapper);
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5124
5125 // eraser trumps stylus
5126 processKey(mapper, BTN_TOOL_RUBBER, 1);
5127 processSync(mapper);
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5131
5132 // mouse trumps eraser
5133 processKey(mapper, BTN_TOOL_MOUSE, 1);
5134 processSync(mapper);
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5138
5139 // back to default tool type
5140 processKey(mapper, BTN_TOOL_MOUSE, 0);
5141 processKey(mapper, BTN_TOOL_RUBBER, 0);
5142 processKey(mapper, BTN_TOOL_PEN, 0);
5143 processKey(mapper, BTN_TOOL_FINGER, 0);
5144 processSync(mapper);
5145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5148}
5149
5150TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 addConfigurationProperty("touch.deviceType", "touchScreen");
5152 prepareDisplay(DISPLAY_ORIENTATION_0);
5153 prepareButtons();
5154 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005155 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005156 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157
5158 NotifyMotionArgs motionArgs;
5159
5160 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5161 processKey(mapper, BTN_TOOL_FINGER, 1);
5162 processMove(mapper, 100, 200);
5163 processSync(mapper);
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5167 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5168
5169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5170 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5172 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5173
5174 // move a little
5175 processMove(mapper, 150, 250);
5176 processSync(mapper);
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5180 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5181
5182 // down when BTN_TOUCH is pressed, pressure defaults to 1
5183 processKey(mapper, BTN_TOUCH, 1);
5184 processSync(mapper);
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5188 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5189
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5193 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5194
5195 // up when BTN_TOUCH is released, hover restored
5196 processKey(mapper, BTN_TOUCH, 0);
5197 processSync(mapper);
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5199 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5201 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5202
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5206 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5207
5208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5211 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5212
5213 // exit hover when pointer goes away
5214 processKey(mapper, BTN_TOOL_FINGER, 0);
5215 processSync(mapper);
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5217 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5219 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5220}
5221
5222TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223 addConfigurationProperty("touch.deviceType", "touchScreen");
5224 prepareDisplay(DISPLAY_ORIENTATION_0);
5225 prepareButtons();
5226 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005227 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228
5229 NotifyMotionArgs motionArgs;
5230
5231 // initially hovering because pressure is 0
5232 processDown(mapper, 100, 200);
5233 processPressure(mapper, 0);
5234 processSync(mapper);
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5238 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5239
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5241 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5243 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5244
5245 // move a little
5246 processMove(mapper, 150, 250);
5247 processSync(mapper);
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5249 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5251 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5252
5253 // down when pressure is non-zero
5254 processPressure(mapper, RAW_PRESSURE_MAX);
5255 processSync(mapper);
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5259 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5260
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5264 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5265
5266 // up when pressure becomes 0, hover restored
5267 processPressure(mapper, 0);
5268 processSync(mapper);
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5272 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5273
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5278
5279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5280 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5282 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5283
5284 // exit hover when pointer goes away
5285 processUp(mapper);
5286 processSync(mapper);
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5288 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5290 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5291}
5292
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293// --- MultiTouchInputMapperTest ---
5294
5295class MultiTouchInputMapperTest : public TouchInputMapperTest {
5296protected:
5297 void prepareAxes(int axes);
5298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5300 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5301 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5302 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5303 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5304 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5305 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5306 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5307 void processId(MultiTouchInputMapper& mapper, int32_t id);
5308 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5309 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5310 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5311 void processMTSync(MultiTouchInputMapper& mapper);
5312 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313};
5314
5315void MultiTouchInputMapperTest::prepareAxes(int axes) {
5316 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5318 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 }
5320 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005321 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5322 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005324 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5325 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326 }
5327 }
5328 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5330 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005332 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5333 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334 }
5335 }
5336 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5338 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 }
5340 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005341 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5342 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343 }
5344 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005345 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5346 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347 }
5348 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005349 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5350 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351 }
5352 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005353 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5354 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355 }
5356 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005357 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358 }
5359}
5360
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005361void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5362 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005363 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5364 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365}
5366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005367void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5368 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005369 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370}
5371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005372void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5373 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005374 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375}
5376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005377void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005378 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379}
5380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005382 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383}
5384
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005385void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5386 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005387 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388}
5389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005390void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005391 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392}
5393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005394void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005395 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396}
5397
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005398void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005399 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400}
5401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005403 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404}
5405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005406void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005407 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408}
5409
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005410void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5411 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005412 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413}
5414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005416 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417}
5418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005419void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005420 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421}
5422
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424 addConfigurationProperty("touch.deviceType", "touchScreen");
5425 prepareDisplay(DISPLAY_ORIENTATION_0);
5426 prepareAxes(POSITION);
5427 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005428 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429
5430 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5431
5432 NotifyMotionArgs motionArgs;
5433
5434 // Two fingers down at once.
5435 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5436 processPosition(mapper, x1, y1);
5437 processMTSync(mapper);
5438 processPosition(mapper, x2, y2);
5439 processMTSync(mapper);
5440 processSync(mapper);
5441
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5444 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5445 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5446 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5447 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5448 ASSERT_EQ(0, motionArgs.flags);
5449 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5450 ASSERT_EQ(0, motionArgs.buttonState);
5451 ASSERT_EQ(0, motionArgs.edgeFlags);
5452 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5453 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5456 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5457 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5458 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5460
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5462 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5463 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5464 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5465 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5466 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5467 motionArgs.action);
5468 ASSERT_EQ(0, motionArgs.flags);
5469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5470 ASSERT_EQ(0, motionArgs.buttonState);
5471 ASSERT_EQ(0, motionArgs.edgeFlags);
5472 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5475 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5478 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5480 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5481 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5482 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5484
5485 // Move.
5486 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5487 processPosition(mapper, x1, y1);
5488 processMTSync(mapper);
5489 processPosition(mapper, x2, y2);
5490 processMTSync(mapper);
5491 processSync(mapper);
5492
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5494 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5495 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5496 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5497 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5499 ASSERT_EQ(0, motionArgs.flags);
5500 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5501 ASSERT_EQ(0, motionArgs.buttonState);
5502 ASSERT_EQ(0, motionArgs.edgeFlags);
5503 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5504 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5506 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5509 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5511 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5512 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5513 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5514 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5515
5516 // First finger up.
5517 x2 += 15; y2 -= 20;
5518 processPosition(mapper, x2, y2);
5519 processMTSync(mapper);
5520 processSync(mapper);
5521
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5523 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5524 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5525 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5526 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5527 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5528 motionArgs.action);
5529 ASSERT_EQ(0, motionArgs.flags);
5530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5531 ASSERT_EQ(0, motionArgs.buttonState);
5532 ASSERT_EQ(0, motionArgs.edgeFlags);
5533 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5536 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5539 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5541 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5542 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5543 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5545
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5547 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5548 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5549 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5550 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5552 ASSERT_EQ(0, motionArgs.flags);
5553 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5554 ASSERT_EQ(0, motionArgs.buttonState);
5555 ASSERT_EQ(0, motionArgs.edgeFlags);
5556 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5557 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5560 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5561 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5562 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5563 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5564
5565 // Move.
5566 x2 += 20; y2 -= 25;
5567 processPosition(mapper, x2, y2);
5568 processMTSync(mapper);
5569 processSync(mapper);
5570
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5572 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5573 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5574 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5575 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5577 ASSERT_EQ(0, motionArgs.flags);
5578 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5579 ASSERT_EQ(0, motionArgs.buttonState);
5580 ASSERT_EQ(0, motionArgs.edgeFlags);
5581 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5582 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5585 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5586 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5587 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5588 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5589
5590 // New finger down.
5591 int32_t x3 = 700, y3 = 300;
5592 processPosition(mapper, x2, y2);
5593 processMTSync(mapper);
5594 processPosition(mapper, x3, y3);
5595 processMTSync(mapper);
5596 processSync(mapper);
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5600 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5601 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5602 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5603 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5604 motionArgs.action);
5605 ASSERT_EQ(0, motionArgs.flags);
5606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5607 ASSERT_EQ(0, motionArgs.buttonState);
5608 ASSERT_EQ(0, motionArgs.edgeFlags);
5609 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5612 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5615 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5617 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5618 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5619 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5620 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5621
5622 // Second finger up.
5623 x3 += 30; y3 -= 20;
5624 processPosition(mapper, x3, y3);
5625 processMTSync(mapper);
5626 processSync(mapper);
5627
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5630 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5631 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5632 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5633 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5634 motionArgs.action);
5635 ASSERT_EQ(0, motionArgs.flags);
5636 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5637 ASSERT_EQ(0, motionArgs.buttonState);
5638 ASSERT_EQ(0, motionArgs.edgeFlags);
5639 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5640 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5642 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5645 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5647 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5648 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5649 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5650 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5651
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5654 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5655 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5656 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5658 ASSERT_EQ(0, motionArgs.flags);
5659 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5660 ASSERT_EQ(0, motionArgs.buttonState);
5661 ASSERT_EQ(0, motionArgs.edgeFlags);
5662 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5666 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5667 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5668 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5670
5671 // Last finger up.
5672 processMTSync(mapper);
5673 processSync(mapper);
5674
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5676 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5677 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5678 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5679 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5680 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5681 ASSERT_EQ(0, motionArgs.flags);
5682 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5683 ASSERT_EQ(0, motionArgs.buttonState);
5684 ASSERT_EQ(0, motionArgs.edgeFlags);
5685 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5689 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5690 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5691 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5693
5694 // Should not have sent any more keys or motions.
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5697}
5698
5699TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 addConfigurationProperty("touch.deviceType", "touchScreen");
5701 prepareDisplay(DISPLAY_ORIENTATION_0);
5702 prepareAxes(POSITION | ID);
5703 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705
5706 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5707
5708 NotifyMotionArgs motionArgs;
5709
5710 // Two fingers down at once.
5711 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5712 processPosition(mapper, x1, y1);
5713 processId(mapper, 1);
5714 processMTSync(mapper);
5715 processPosition(mapper, x2, y2);
5716 processId(mapper, 2);
5717 processMTSync(mapper);
5718 processSync(mapper);
5719
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5722 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5723 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5726 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5727
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5729 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5730 motionArgs.action);
5731 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5734 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5737 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5740
5741 // Move.
5742 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5743 processPosition(mapper, x1, y1);
5744 processId(mapper, 1);
5745 processMTSync(mapper);
5746 processPosition(mapper, x2, y2);
5747 processId(mapper, 2);
5748 processMTSync(mapper);
5749 processSync(mapper);
5750
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5753 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5756 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5759 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5761 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5762
5763 // First finger up.
5764 x2 += 15; y2 -= 20;
5765 processPosition(mapper, x2, y2);
5766 processId(mapper, 2);
5767 processMTSync(mapper);
5768 processSync(mapper);
5769
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5771 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5772 motionArgs.action);
5773 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5774 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5776 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5779 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5781 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5782
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5785 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5786 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5789 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5790
5791 // Move.
5792 x2 += 20; y2 -= 25;
5793 processPosition(mapper, x2, y2);
5794 processId(mapper, 2);
5795 processMTSync(mapper);
5796 processSync(mapper);
5797
5798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5800 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5801 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5804 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5805
5806 // New finger down.
5807 int32_t x3 = 700, y3 = 300;
5808 processPosition(mapper, x2, y2);
5809 processId(mapper, 2);
5810 processMTSync(mapper);
5811 processPosition(mapper, x3, y3);
5812 processId(mapper, 3);
5813 processMTSync(mapper);
5814 processSync(mapper);
5815
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5818 motionArgs.action);
5819 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5822 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5825 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5827 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5828
5829 // Second finger up.
5830 x3 += 30; y3 -= 20;
5831 processPosition(mapper, x3, y3);
5832 processId(mapper, 3);
5833 processMTSync(mapper);
5834 processSync(mapper);
5835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5838 motionArgs.action);
5839 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5845 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5847 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5848
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5856
5857 // Last finger up.
5858 processMTSync(mapper);
5859 processSync(mapper);
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5863 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5867 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5868
5869 // Should not have sent any more keys or motions.
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5872}
5873
5874TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875 addConfigurationProperty("touch.deviceType", "touchScreen");
5876 prepareDisplay(DISPLAY_ORIENTATION_0);
5877 prepareAxes(POSITION | ID | SLOT);
5878 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005879 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880
5881 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5882
5883 NotifyMotionArgs motionArgs;
5884
5885 // Two fingers down at once.
5886 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5887 processPosition(mapper, x1, y1);
5888 processId(mapper, 1);
5889 processSlot(mapper, 1);
5890 processPosition(mapper, x2, y2);
5891 processId(mapper, 2);
5892 processSync(mapper);
5893
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5896 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5897 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5900 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5901
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5904 motionArgs.action);
5905 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5908 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5911 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5913 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5914
5915 // Move.
5916 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5917 processSlot(mapper, 0);
5918 processPosition(mapper, x1, y1);
5919 processSlot(mapper, 1);
5920 processPosition(mapper, x2, y2);
5921 processSync(mapper);
5922
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5925 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5926 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5928 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5931 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5933 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5934
5935 // First finger up.
5936 x2 += 15; y2 -= 20;
5937 processSlot(mapper, 0);
5938 processId(mapper, -1);
5939 processSlot(mapper, 1);
5940 processPosition(mapper, x2, y2);
5941 processSync(mapper);
5942
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5945 motionArgs.action);
5946 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5947 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5949 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5952 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5954 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5958 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5959 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5962 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5963
5964 // Move.
5965 x2 += 20; y2 -= 25;
5966 processPosition(mapper, x2, y2);
5967 processSync(mapper);
5968
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5971 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5972 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5975 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5976
5977 // New finger down.
5978 int32_t x3 = 700, y3 = 300;
5979 processPosition(mapper, x2, y2);
5980 processSlot(mapper, 0);
5981 processId(mapper, 3);
5982 processPosition(mapper, x3, y3);
5983 processSync(mapper);
5984
5985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5986 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5987 motionArgs.action);
5988 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5989 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5991 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5994 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5996 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5997
5998 // Second finger up.
5999 x3 += 30; y3 -= 20;
6000 processSlot(mapper, 1);
6001 processId(mapper, -1);
6002 processSlot(mapper, 0);
6003 processPosition(mapper, x3, y3);
6004 processSync(mapper);
6005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6008 motionArgs.action);
6009 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6010 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6012 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6015 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6017 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6021 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6022 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6025 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6026
6027 // Last finger up.
6028 processId(mapper, -1);
6029 processSync(mapper);
6030
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6032 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6033 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6034 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6037 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6038
6039 // Should not have sent any more keys or motions.
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6042}
6043
6044TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 addConfigurationProperty("touch.deviceType", "touchScreen");
6046 prepareDisplay(DISPLAY_ORIENTATION_0);
6047 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006048 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049
6050 // These calculations are based on the input device calibration documentation.
6051 int32_t rawX = 100;
6052 int32_t rawY = 200;
6053 int32_t rawTouchMajor = 7;
6054 int32_t rawTouchMinor = 6;
6055 int32_t rawToolMajor = 9;
6056 int32_t rawToolMinor = 8;
6057 int32_t rawPressure = 11;
6058 int32_t rawDistance = 0;
6059 int32_t rawOrientation = 3;
6060 int32_t id = 5;
6061
6062 float x = toDisplayX(rawX);
6063 float y = toDisplayY(rawY);
6064 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6065 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6066 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6067 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6068 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6069 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6070 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6071 float distance = float(rawDistance);
6072
6073 processPosition(mapper, rawX, rawY);
6074 processTouchMajor(mapper, rawTouchMajor);
6075 processTouchMinor(mapper, rawTouchMinor);
6076 processToolMajor(mapper, rawToolMajor);
6077 processToolMinor(mapper, rawToolMinor);
6078 processPressure(mapper, rawPressure);
6079 processOrientation(mapper, rawOrientation);
6080 processDistance(mapper, rawDistance);
6081 processId(mapper, id);
6082 processMTSync(mapper);
6083 processSync(mapper);
6084
6085 NotifyMotionArgs args;
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6087 ASSERT_EQ(0, args.pointerProperties[0].id);
6088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6089 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6090 orientation, distance));
6091}
6092
6093TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 addConfigurationProperty("touch.deviceType", "touchScreen");
6095 prepareDisplay(DISPLAY_ORIENTATION_0);
6096 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6097 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006098 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099
6100 // These calculations are based on the input device calibration documentation.
6101 int32_t rawX = 100;
6102 int32_t rawY = 200;
6103 int32_t rawTouchMajor = 140;
6104 int32_t rawTouchMinor = 120;
6105 int32_t rawToolMajor = 180;
6106 int32_t rawToolMinor = 160;
6107
6108 float x = toDisplayX(rawX);
6109 float y = toDisplayY(rawY);
6110 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6111 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6112 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6113 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6114 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6115
6116 processPosition(mapper, rawX, rawY);
6117 processTouchMajor(mapper, rawTouchMajor);
6118 processTouchMinor(mapper, rawTouchMinor);
6119 processToolMajor(mapper, rawToolMajor);
6120 processToolMinor(mapper, rawToolMinor);
6121 processMTSync(mapper);
6122 processSync(mapper);
6123
6124 NotifyMotionArgs args;
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6127 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6128}
6129
6130TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 addConfigurationProperty("touch.deviceType", "touchScreen");
6132 prepareDisplay(DISPLAY_ORIENTATION_0);
6133 prepareAxes(POSITION | TOUCH | TOOL);
6134 addConfigurationProperty("touch.size.calibration", "diameter");
6135 addConfigurationProperty("touch.size.scale", "10");
6136 addConfigurationProperty("touch.size.bias", "160");
6137 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006138 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139
6140 // These calculations are based on the input device calibration documentation.
6141 // Note: We only provide a single common touch/tool value because the device is assumed
6142 // not to emit separate values for each pointer (isSummed = 1).
6143 int32_t rawX = 100;
6144 int32_t rawY = 200;
6145 int32_t rawX2 = 150;
6146 int32_t rawY2 = 250;
6147 int32_t rawTouchMajor = 5;
6148 int32_t rawToolMajor = 8;
6149
6150 float x = toDisplayX(rawX);
6151 float y = toDisplayY(rawY);
6152 float x2 = toDisplayX(rawX2);
6153 float y2 = toDisplayY(rawY2);
6154 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6155 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6156 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6157
6158 processPosition(mapper, rawX, rawY);
6159 processTouchMajor(mapper, rawTouchMajor);
6160 processToolMajor(mapper, rawToolMajor);
6161 processMTSync(mapper);
6162 processPosition(mapper, rawX2, rawY2);
6163 processTouchMajor(mapper, rawTouchMajor);
6164 processToolMajor(mapper, rawToolMajor);
6165 processMTSync(mapper);
6166 processSync(mapper);
6167
6168 NotifyMotionArgs args;
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6171
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6174 args.action);
6175 ASSERT_EQ(size_t(2), args.pointerCount);
6176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6177 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6179 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6180}
6181
6182TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183 addConfigurationProperty("touch.deviceType", "touchScreen");
6184 prepareDisplay(DISPLAY_ORIENTATION_0);
6185 prepareAxes(POSITION | TOUCH | TOOL);
6186 addConfigurationProperty("touch.size.calibration", "area");
6187 addConfigurationProperty("touch.size.scale", "43");
6188 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006189 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190
6191 // These calculations are based on the input device calibration documentation.
6192 int32_t rawX = 100;
6193 int32_t rawY = 200;
6194 int32_t rawTouchMajor = 5;
6195 int32_t rawToolMajor = 8;
6196
6197 float x = toDisplayX(rawX);
6198 float y = toDisplayY(rawY);
6199 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6200 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6201 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6202
6203 processPosition(mapper, rawX, rawY);
6204 processTouchMajor(mapper, rawTouchMajor);
6205 processToolMajor(mapper, rawToolMajor);
6206 processMTSync(mapper);
6207 processSync(mapper);
6208
6209 NotifyMotionArgs args;
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6212 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6213}
6214
6215TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 addConfigurationProperty("touch.deviceType", "touchScreen");
6217 prepareDisplay(DISPLAY_ORIENTATION_0);
6218 prepareAxes(POSITION | PRESSURE);
6219 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6220 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006221 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222
Michael Wrightaa449c92017-12-13 21:21:43 +00006223 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006224 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006225 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6226 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6227 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6228
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 // These calculations are based on the input device calibration documentation.
6230 int32_t rawX = 100;
6231 int32_t rawY = 200;
6232 int32_t rawPressure = 60;
6233
6234 float x = toDisplayX(rawX);
6235 float y = toDisplayY(rawY);
6236 float pressure = float(rawPressure) * 0.01f;
6237
6238 processPosition(mapper, rawX, rawY);
6239 processPressure(mapper, rawPressure);
6240 processMTSync(mapper);
6241 processSync(mapper);
6242
6243 NotifyMotionArgs args;
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6246 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6247}
6248
6249TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 addConfigurationProperty("touch.deviceType", "touchScreen");
6251 prepareDisplay(DISPLAY_ORIENTATION_0);
6252 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006253 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254
6255 NotifyMotionArgs motionArgs;
6256 NotifyKeyArgs keyArgs;
6257
6258 processId(mapper, 1);
6259 processPosition(mapper, 100, 200);
6260 processSync(mapper);
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6263 ASSERT_EQ(0, motionArgs.buttonState);
6264
6265 // press BTN_LEFT, release BTN_LEFT
6266 processKey(mapper, BTN_LEFT, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6271
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6274 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6275
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 processKey(mapper, BTN_LEFT, 0);
6277 processSync(mapper);
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006281
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285
6286 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6287 processKey(mapper, BTN_RIGHT, 1);
6288 processKey(mapper, BTN_MIDDLE, 1);
6289 processSync(mapper);
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6292 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6293 motionArgs.buttonState);
6294
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6302 motionArgs.buttonState);
6303
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 processKey(mapper, BTN_RIGHT, 0);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313
6314 processKey(mapper, BTN_MIDDLE, 0);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006322 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323
6324 // press BTN_BACK, release BTN_BACK
6325 processKey(mapper, BTN_BACK, 1);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6328 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6329 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6334
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6337 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 processKey(mapper, BTN_BACK, 0);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(0, motionArgs.buttonState);
6348
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6350 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6351 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6352
6353 // press BTN_SIDE, release BTN_SIDE
6354 processKey(mapper, BTN_SIDE, 1);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6357 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6358 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006359
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6366 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 processKey(mapper, BTN_SIDE, 0);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(0, motionArgs.buttonState);
6377
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6379 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6380 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6381
6382 // press BTN_FORWARD, release BTN_FORWARD
6383 processKey(mapper, BTN_FORWARD, 1);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6387 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6395 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396
6397 processKey(mapper, BTN_FORWARD, 0);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(0, motionArgs.buttonState);
6406
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6408 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6409 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6410
6411 // press BTN_EXTRA, release BTN_EXTRA
6412 processKey(mapper, BTN_EXTRA, 1);
6413 processSync(mapper);
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6415 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6416 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6424 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425
6426 processKey(mapper, BTN_EXTRA, 0);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006431
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006434 ASSERT_EQ(0, motionArgs.buttonState);
6435
Michael Wrightd02c5b62014-02-10 15:10:22 -08006436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6437 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6438 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6439
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6441
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 // press BTN_STYLUS, release BTN_STYLUS
6443 processKey(mapper, BTN_STYLUS, 1);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006447 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6448
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6451 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452
6453 processKey(mapper, BTN_STYLUS, 0);
6454 processSync(mapper);
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462
6463 // press BTN_STYLUS2, release BTN_STYLUS2
6464 processKey(mapper, BTN_STYLUS2, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6469
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6472 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473
6474 processKey(mapper, BTN_STYLUS2, 0);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483
6484 // release touch
6485 processId(mapper, -1);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6489 ASSERT_EQ(0, motionArgs.buttonState);
6490}
6491
6492TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493 addConfigurationProperty("touch.deviceType", "touchScreen");
6494 prepareDisplay(DISPLAY_ORIENTATION_0);
6495 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006496 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006497
6498 NotifyMotionArgs motionArgs;
6499
6500 // default tool type is finger
6501 processId(mapper, 1);
6502 processPosition(mapper, 100, 200);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6507
6508 // eraser
6509 processKey(mapper, BTN_TOOL_RUBBER, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6514
6515 // stylus
6516 processKey(mapper, BTN_TOOL_RUBBER, 0);
6517 processKey(mapper, BTN_TOOL_PEN, 1);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6522
6523 // brush
6524 processKey(mapper, BTN_TOOL_PEN, 0);
6525 processKey(mapper, BTN_TOOL_BRUSH, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6530
6531 // pencil
6532 processKey(mapper, BTN_TOOL_BRUSH, 0);
6533 processKey(mapper, BTN_TOOL_PENCIL, 1);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6538
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006539 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006540 processKey(mapper, BTN_TOOL_PENCIL, 0);
6541 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6546
6547 // mouse
6548 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6549 processKey(mapper, BTN_TOOL_MOUSE, 1);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6554
6555 // lens
6556 processKey(mapper, BTN_TOOL_MOUSE, 0);
6557 processKey(mapper, BTN_TOOL_LENS, 1);
6558 processSync(mapper);
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6562
6563 // double-tap
6564 processKey(mapper, BTN_TOOL_LENS, 0);
6565 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6566 processSync(mapper);
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6570
6571 // triple-tap
6572 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6573 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6574 processSync(mapper);
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6578
6579 // quad-tap
6580 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6581 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6586
6587 // finger
6588 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6589 processKey(mapper, BTN_TOOL_FINGER, 1);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6594
6595 // stylus trumps finger
6596 processKey(mapper, BTN_TOOL_PEN, 1);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6601
6602 // eraser trumps stylus
6603 processKey(mapper, BTN_TOOL_RUBBER, 1);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6608
6609 // mouse trumps eraser
6610 processKey(mapper, BTN_TOOL_MOUSE, 1);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6615
6616 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6617 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6622
6623 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6624 processToolType(mapper, MT_TOOL_PEN);
6625 processSync(mapper);
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6629
6630 // back to default tool type
6631 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6632 processKey(mapper, BTN_TOOL_MOUSE, 0);
6633 processKey(mapper, BTN_TOOL_RUBBER, 0);
6634 processKey(mapper, BTN_TOOL_PEN, 0);
6635 processKey(mapper, BTN_TOOL_FINGER, 0);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6640}
6641
6642TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006643 addConfigurationProperty("touch.deviceType", "touchScreen");
6644 prepareDisplay(DISPLAY_ORIENTATION_0);
6645 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006646 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006647 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006648
6649 NotifyMotionArgs motionArgs;
6650
6651 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6652 processId(mapper, 1);
6653 processPosition(mapper, 100, 200);
6654 processSync(mapper);
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6658 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6659
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6663 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6664
6665 // move a little
6666 processPosition(mapper, 150, 250);
6667 processSync(mapper);
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6671 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6672
6673 // down when BTN_TOUCH is pressed, pressure defaults to 1
6674 processKey(mapper, BTN_TOUCH, 1);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6679 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6680
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6685
6686 // up when BTN_TOUCH is released, hover restored
6687 processKey(mapper, BTN_TOUCH, 0);
6688 processSync(mapper);
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6692 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6693
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6698
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6702 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6703
6704 // exit hover when pointer goes away
6705 processId(mapper, -1);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6710 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6711}
6712
6713TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006714 addConfigurationProperty("touch.deviceType", "touchScreen");
6715 prepareDisplay(DISPLAY_ORIENTATION_0);
6716 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006717 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718
6719 NotifyMotionArgs motionArgs;
6720
6721 // initially hovering because pressure is 0
6722 processId(mapper, 1);
6723 processPosition(mapper, 100, 200);
6724 processPressure(mapper, 0);
6725 processSync(mapper);
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6727 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6729 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6730
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6734 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6735
6736 // move a little
6737 processPosition(mapper, 150, 250);
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6742 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6743
6744 // down when pressure becomes non-zero
6745 processPressure(mapper, RAW_PRESSURE_MAX);
6746 processSync(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6751
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6755 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6756
6757 // up when pressure becomes 0, hover restored
6758 processPressure(mapper, 0);
6759 processSync(mapper);
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6764
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6769
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6771 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6773 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6774
6775 // exit hover when pointer goes away
6776 processId(mapper, -1);
6777 processSync(mapper);
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6781 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6782}
6783
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006784/**
6785 * Set the input device port <--> display port associations, and check that the
6786 * events are routed to the display that matches the display port.
6787 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6788 */
6789TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006790 const std::string usb2 = "USB2";
6791 const uint8_t hdmi1 = 0;
6792 const uint8_t hdmi2 = 1;
6793 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006794 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006795
6796 addConfigurationProperty("touch.deviceType", "touchScreen");
6797 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006798 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006799
6800 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6801 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6802
6803 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6804 // for this input device is specified, and the matching viewport is not present,
6805 // the input device should be disabled (at the mapper level).
6806
6807 // Add viewport for display 2 on hdmi2
6808 prepareSecondaryDisplay(type, hdmi2);
6809 // Send a touch event
6810 processPosition(mapper, 100, 100);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6813
6814 // Add viewport for display 1 on hdmi1
6815 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6816 // Send a touch event again
6817 processPosition(mapper, 100, 100);
6818 processSync(mapper);
6819
6820 NotifyMotionArgs args;
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6822 ASSERT_EQ(DISPLAY_ID, args.displayId);
6823}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006824
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006825TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006826 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006827 std::shared_ptr<FakePointerController> fakePointerController =
6828 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006829 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006830 fakePointerController->setPosition(100, 200);
6831 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006832 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6833
Garfield Tan888a6a42020-01-09 11:39:16 -08006834 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006835 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006836
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006837 prepareDisplay(DISPLAY_ORIENTATION_0);
6838 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006840
6841 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006842 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006843
6844 NotifyMotionArgs motionArgs;
6845 processPosition(mapper, 100, 100);
6846 processSync(mapper);
6847
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6850 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6851}
6852
Arthur Hung7c645402019-01-25 17:45:42 +08006853TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6854 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006855 prepareAxes(POSITION | ID | SLOT);
6856 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006857 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006858
6859 // Create the second touch screen device, and enable multi fingers.
6860 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006861 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006862 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006863 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006864 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006865 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006866 std::unique_ptr<InputDevice> device2 =
6867 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006868 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07006869 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME,
6870 Flags<InputDeviceClass>(0) /*classes*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006871 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6872 0 /*flat*/, 0 /*fuzz*/);
6873 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6874 0 /*flat*/, 0 /*fuzz*/);
6875 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6876 0 /*flat*/, 0 /*fuzz*/);
6877 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6878 0 /*flat*/, 0 /*fuzz*/);
6879 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6880 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6881 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006882
6883 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006884 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006885 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6886 device2->reset(ARBITRARY_TIME);
6887
6888 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006889 std::shared_ptr<FakePointerController> fakePointerController =
6890 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006891 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6892 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6893
6894 // Setup policy for associated displays and show touches.
6895 const uint8_t hdmi1 = 0;
6896 const uint8_t hdmi2 = 1;
6897 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6898 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6899 mFakePolicy->setShowTouches(true);
6900
6901 // Create displays.
6902 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006903 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006904
6905 // Default device will reconfigure above, need additional reconfiguration for another device.
6906 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006907 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006908
6909 // Two fingers down at default display.
6910 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6911 processPosition(mapper, x1, y1);
6912 processId(mapper, 1);
6913 processSlot(mapper, 1);
6914 processPosition(mapper, x2, y2);
6915 processId(mapper, 2);
6916 processSync(mapper);
6917
6918 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6919 fakePointerController->getSpots().find(DISPLAY_ID);
6920 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6921 ASSERT_EQ(size_t(2), iter->second.size());
6922
6923 // Two fingers down at second display.
6924 processPosition(mapper2, x1, y1);
6925 processId(mapper2, 1);
6926 processSlot(mapper2, 1);
6927 processPosition(mapper2, x2, y2);
6928 processId(mapper2, 2);
6929 processSync(mapper2);
6930
6931 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6932 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6933 ASSERT_EQ(size_t(2), iter->second.size());
6934}
6935
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006936TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006937 prepareAxes(POSITION);
6938 addConfigurationProperty("touch.deviceType", "touchScreen");
6939 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006940 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006941
6942 NotifyMotionArgs motionArgs;
6943 // Unrotated video frame
6944 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6945 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006946 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006947 processPosition(mapper, 100, 200);
6948 processSync(mapper);
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6950 ASSERT_EQ(frames, motionArgs.videoFrames);
6951
6952 // Subsequent touch events should not have any videoframes
6953 // This is implemented separately in FakeEventHub,
6954 // but that should match the behaviour of TouchVideoDevice.
6955 processPosition(mapper, 200, 200);
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6958 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6959}
6960
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006961TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006962 prepareAxes(POSITION);
6963 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006964 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006965 // Unrotated video frame
6966 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6967 NotifyMotionArgs motionArgs;
6968
6969 // Test all 4 orientations
6970 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6971 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6972 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6973 clearViewports();
6974 prepareDisplay(orientation);
6975 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006976 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006977 processPosition(mapper, 100, 200);
6978 processSync(mapper);
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6980 frames[0].rotate(orientation);
6981 ASSERT_EQ(frames, motionArgs.videoFrames);
6982 }
6983}
6984
6985TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006986 prepareAxes(POSITION);
6987 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006988 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006989 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6990 // so mix these.
6991 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6992 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6993 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6994 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6995 NotifyMotionArgs motionArgs;
6996
6997 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006998 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006999 processPosition(mapper, 100, 200);
7000 processSync(mapper);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7002 std::for_each(frames.begin(), frames.end(),
7003 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7004 ASSERT_EQ(frames, motionArgs.videoFrames);
7005}
7006
Arthur Hung9da14732019-09-02 16:16:58 +08007007/**
7008 * If we had defined port associations, but the viewport is not ready, the touch device would be
7009 * expected to be disabled, and it should be enabled after the viewport has found.
7010 */
7011TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007012 constexpr uint8_t hdmi2 = 1;
7013 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007014 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007015
7016 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7017
7018 addConfigurationProperty("touch.deviceType", "touchScreen");
7019 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007020 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007021
7022 ASSERT_EQ(mDevice->isEnabled(), false);
7023
7024 // Add display on hdmi2, the device should be enabled and can receive touch event.
7025 prepareSecondaryDisplay(type, hdmi2);
7026 ASSERT_EQ(mDevice->isEnabled(), true);
7027
7028 // Send a touch event.
7029 processPosition(mapper, 100, 100);
7030 processSync(mapper);
7031
7032 NotifyMotionArgs args;
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7034 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7035}
7036
Arthur Hung6cd19a42019-08-30 19:04:12 +08007037
Arthur Hung6cd19a42019-08-30 19:04:12 +08007038
Arthur Hung421eb1c2020-01-16 00:09:42 +08007039TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007040 addConfigurationProperty("touch.deviceType", "touchScreen");
7041 prepareDisplay(DISPLAY_ORIENTATION_0);
7042 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007043 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007044
7045 NotifyMotionArgs motionArgs;
7046
7047 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7048 // finger down
7049 processId(mapper, 1);
7050 processPosition(mapper, x1, y1);
7051 processSync(mapper);
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7055
7056 // finger move
7057 processId(mapper, 1);
7058 processPosition(mapper, x2, y2);
7059 processSync(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7063
7064 // finger up.
7065 processId(mapper, -1);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7068 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7070
7071 // new finger down
7072 processId(mapper, 1);
7073 processPosition(mapper, x3, y3);
7074 processSync(mapper);
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7076 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7078}
7079
7080/**
arthurhungcc7f9802020-04-30 17:55:40 +08007081 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7082 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007083 */
arthurhungcc7f9802020-04-30 17:55:40 +08007084TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007085 addConfigurationProperty("touch.deviceType", "touchScreen");
7086 prepareDisplay(DISPLAY_ORIENTATION_0);
7087 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007088 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007089
7090 NotifyMotionArgs motionArgs;
7091
7092 // default tool type is finger
7093 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007094 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007095 processPosition(mapper, x1, y1);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7098 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7100
7101 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7102 processToolType(mapper, MT_TOOL_PALM);
7103 processSync(mapper);
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7105 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7106
7107 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007108 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007109 processPosition(mapper, x2, y2);
7110 processSync(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7112
7113 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007114 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007115 processSync(mapper);
7116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7117
7118 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007119 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007120 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007121 processPosition(mapper, x3, y3);
7122 processSync(mapper);
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7126}
7127
arthurhungbf89a482020-04-17 17:37:55 +08007128/**
arthurhungcc7f9802020-04-30 17:55:40 +08007129 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7130 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007131 */
arthurhungcc7f9802020-04-30 17:55:40 +08007132TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007133 addConfigurationProperty("touch.deviceType", "touchScreen");
7134 prepareDisplay(DISPLAY_ORIENTATION_0);
7135 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7136 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7137
7138 NotifyMotionArgs motionArgs;
7139
7140 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007141 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7142 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007143 processPosition(mapper, x1, y1);
7144 processSync(mapper);
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7148
7149 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007150 processSlot(mapper, SECOND_SLOT);
7151 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007152 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007153 processSync(mapper);
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7155 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7156 motionArgs.action);
7157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7158
7159 // If the tool type of the first finger changes to MT_TOOL_PALM,
7160 // we expect to receive ACTION_POINTER_UP with cancel flag.
7161 processSlot(mapper, FIRST_SLOT);
7162 processId(mapper, FIRST_TRACKING_ID);
7163 processToolType(mapper, MT_TOOL_PALM);
7164 processSync(mapper);
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7166 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7167 motionArgs.action);
7168 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7169
7170 // The following MOVE events of second finger should be processed.
7171 processSlot(mapper, SECOND_SLOT);
7172 processId(mapper, SECOND_TRACKING_ID);
7173 processPosition(mapper, x2 + 1, y2 + 1);
7174 processSync(mapper);
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7177 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7178
7179 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7180 // it. Second finger receive move.
7181 processSlot(mapper, FIRST_SLOT);
7182 processId(mapper, INVALID_TRACKING_ID);
7183 processSync(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7186 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7187
7188 // Second finger keeps moving.
7189 processSlot(mapper, SECOND_SLOT);
7190 processId(mapper, SECOND_TRACKING_ID);
7191 processPosition(mapper, x2 + 2, y2 + 2);
7192 processSync(mapper);
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7195 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7196
7197 // Second finger up.
7198 processId(mapper, INVALID_TRACKING_ID);
7199 processSync(mapper);
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7202 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7203}
7204
7205/**
7206 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7207 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7208 */
7209TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7210 addConfigurationProperty("touch.deviceType", "touchScreen");
7211 prepareDisplay(DISPLAY_ORIENTATION_0);
7212 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7213 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7214
7215 NotifyMotionArgs motionArgs;
7216
7217 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7218 // First finger down.
7219 processId(mapper, FIRST_TRACKING_ID);
7220 processPosition(mapper, x1, y1);
7221 processSync(mapper);
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7225
7226 // Second finger down.
7227 processSlot(mapper, SECOND_SLOT);
7228 processId(mapper, SECOND_TRACKING_ID);
7229 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007230 processSync(mapper);
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7232 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7233 motionArgs.action);
7234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7235
arthurhungcc7f9802020-04-30 17:55:40 +08007236 // If the tool type of the first finger changes to MT_TOOL_PALM,
7237 // we expect to receive ACTION_POINTER_UP with cancel flag.
7238 processSlot(mapper, FIRST_SLOT);
7239 processId(mapper, FIRST_TRACKING_ID);
7240 processToolType(mapper, MT_TOOL_PALM);
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7244 motionArgs.action);
7245 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7246
7247 // Second finger keeps moving.
7248 processSlot(mapper, SECOND_SLOT);
7249 processId(mapper, SECOND_TRACKING_ID);
7250 processPosition(mapper, x2 + 1, y2 + 1);
7251 processSync(mapper);
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7254
7255 // second finger becomes palm, receive cancel due to only 1 finger is active.
7256 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007257 processToolType(mapper, MT_TOOL_PALM);
7258 processSync(mapper);
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7260 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7261
arthurhungcc7f9802020-04-30 17:55:40 +08007262 // third finger down.
7263 processSlot(mapper, THIRD_SLOT);
7264 processId(mapper, THIRD_TRACKING_ID);
7265 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007266 processPosition(mapper, x3, y3);
7267 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007271 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7272
7273 // third finger move
7274 processId(mapper, THIRD_TRACKING_ID);
7275 processPosition(mapper, x3 + 1, y3 + 1);
7276 processSync(mapper);
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7279
7280 // first finger up, third finger receive move.
7281 processSlot(mapper, FIRST_SLOT);
7282 processId(mapper, INVALID_TRACKING_ID);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7286 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7287
7288 // second finger up, third finger receive move.
7289 processSlot(mapper, SECOND_SLOT);
7290 processId(mapper, INVALID_TRACKING_ID);
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7294 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7295
7296 // third finger up.
7297 processSlot(mapper, THIRD_SLOT);
7298 processId(mapper, INVALID_TRACKING_ID);
7299 processSync(mapper);
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7301 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7302 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7303}
7304
7305/**
7306 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7307 * and the active finger could still be allowed to receive the events
7308 */
7309TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7310 addConfigurationProperty("touch.deviceType", "touchScreen");
7311 prepareDisplay(DISPLAY_ORIENTATION_0);
7312 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7313 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7314
7315 NotifyMotionArgs motionArgs;
7316
7317 // default tool type is finger
7318 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7319 processId(mapper, FIRST_TRACKING_ID);
7320 processPosition(mapper, x1, y1);
7321 processSync(mapper);
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7323 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7325
7326 // Second finger down.
7327 processSlot(mapper, SECOND_SLOT);
7328 processId(mapper, SECOND_TRACKING_ID);
7329 processPosition(mapper, x2, y2);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7333 motionArgs.action);
7334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7335
7336 // If the tool type of the second finger changes to MT_TOOL_PALM,
7337 // we expect to receive ACTION_POINTER_UP with cancel flag.
7338 processId(mapper, SECOND_TRACKING_ID);
7339 processToolType(mapper, MT_TOOL_PALM);
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7343 motionArgs.action);
7344 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7345
7346 // The following MOVE event should be processed.
7347 processSlot(mapper, FIRST_SLOT);
7348 processId(mapper, FIRST_TRACKING_ID);
7349 processPosition(mapper, x1 + 1, y1 + 1);
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7353 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7354
7355 // second finger up.
7356 processSlot(mapper, SECOND_SLOT);
7357 processId(mapper, INVALID_TRACKING_ID);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7361
7362 // first finger keep moving
7363 processSlot(mapper, FIRST_SLOT);
7364 processId(mapper, FIRST_TRACKING_ID);
7365 processPosition(mapper, x1 + 2, y1 + 2);
7366 processSync(mapper);
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7369
7370 // first finger up.
7371 processId(mapper, INVALID_TRACKING_ID);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7375 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007376}
7377
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007378// --- MultiTouchInputMapperTest_ExternalDevice ---
7379
7380class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7381protected:
7382 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07007383 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007384 }
7385};
7386
7387/**
7388 * Expect fallback to internal viewport if device is external and external viewport is not present.
7389 */
7390TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7391 prepareAxes(POSITION);
7392 addConfigurationProperty("touch.deviceType", "touchScreen");
7393 prepareDisplay(DISPLAY_ORIENTATION_0);
7394 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7395
7396 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7397
7398 NotifyMotionArgs motionArgs;
7399
7400 // Expect the event to be sent to the internal viewport,
7401 // because an external viewport is not present.
7402 processPosition(mapper, 100, 100);
7403 processSync(mapper);
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7405 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7406
7407 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007408 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007409 processPosition(mapper, 100, 100);
7410 processSync(mapper);
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7413}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007414
7415/**
7416 * Test touch should not work if outside of surface.
7417 */
7418class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7419protected:
7420 void halfDisplayToCenterHorizontal(int32_t orientation) {
7421 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007422 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007423
7424 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7425 internalViewport->orientation = orientation;
7426 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7427 internalViewport->logicalLeft = 0;
7428 internalViewport->logicalTop = 0;
7429 internalViewport->logicalRight = DISPLAY_HEIGHT;
7430 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7431
7432 internalViewport->physicalLeft = 0;
7433 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7434 internalViewport->physicalRight = DISPLAY_HEIGHT;
7435 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7436
7437 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7438 internalViewport->deviceHeight = DISPLAY_WIDTH;
7439 } else {
7440 internalViewport->logicalLeft = 0;
7441 internalViewport->logicalTop = 0;
7442 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7443 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7444
7445 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7446 internalViewport->physicalTop = 0;
7447 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7448 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7449
7450 internalViewport->deviceWidth = DISPLAY_WIDTH;
7451 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7452 }
7453
7454 mFakePolicy->updateViewport(internalViewport.value());
7455 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7456 }
7457
7458 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7459 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7460 int32_t yExpected) {
7461 // touch on outside area should not work.
7462 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7463 processSync(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7465
7466 // touch on inside area should receive the event.
7467 NotifyMotionArgs args;
7468 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7469 processSync(mapper);
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7471 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7472 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7473
7474 // Reset.
7475 mapper.reset(ARBITRARY_TIME);
7476 }
7477};
7478
7479TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7480 addConfigurationProperty("touch.deviceType", "touchScreen");
7481 prepareDisplay(DISPLAY_ORIENTATION_0);
7482 prepareAxes(POSITION);
7483 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7484
7485 // Touch on center of normal display should work.
7486 const int32_t x = DISPLAY_WIDTH / 4;
7487 const int32_t y = DISPLAY_HEIGHT / 2;
7488 processPosition(mapper, toRawX(x), toRawY(y));
7489 processSync(mapper);
7490 NotifyMotionArgs args;
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7493 0.0f, 0.0f, 0.0f, 0.0f));
7494 // Reset.
7495 mapper.reset(ARBITRARY_TIME);
7496
7497 // Let physical display be different to device, and make surface and physical could be 1:1.
7498 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7499
7500 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7501 const int32_t yExpected = y;
7502 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7503}
7504
7505TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7506 addConfigurationProperty("touch.deviceType", "touchScreen");
7507 prepareDisplay(DISPLAY_ORIENTATION_0);
7508 prepareAxes(POSITION);
7509 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7510
7511 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7512 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7513
7514 const int32_t x = DISPLAY_WIDTH / 4;
7515 const int32_t y = DISPLAY_HEIGHT / 2;
7516
7517 // expect x/y = swap x/y then reverse y.
7518 const int32_t xExpected = y;
7519 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7520 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7521}
7522
7523TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7524 addConfigurationProperty("touch.deviceType", "touchScreen");
7525 prepareDisplay(DISPLAY_ORIENTATION_0);
7526 prepareAxes(POSITION);
7527 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7528
7529 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7530 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7531
7532 const int32_t x = DISPLAY_WIDTH / 4;
7533 const int32_t y = DISPLAY_HEIGHT / 2;
7534
7535 // expect x/y = swap x/y then reverse x.
7536 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7537 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7538 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7539}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007540
7541TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7542 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7543 std::shared_ptr<FakePointerController> fakePointerController =
7544 std::make_shared<FakePointerController>();
7545 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7546 fakePointerController->setPosition(0, 0);
7547 fakePointerController->setButtonState(0);
7548
7549 // prepare device and capture
7550 prepareDisplay(DISPLAY_ORIENTATION_0);
7551 prepareAxes(POSITION | ID | SLOT);
7552 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7553 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7554 mFakePolicy->setPointerCapture(true);
7555 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7556 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7557
7558 // captured touchpad should be a touchpad source
7559 NotifyDeviceResetArgs resetArgs;
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7561 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7562
7563 // run captured pointer tests - note that this is unscaled, so input listener events should be
7564 // identical to what the hardware sends (accounting for any
7565 // calibration).
7566 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007567 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007568 processId(mapper, 1);
7569 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7570 processKey(mapper, BTN_TOUCH, 1);
7571 processSync(mapper);
7572
7573 // expect coord[0] to contain initial location of touch 0
7574 NotifyMotionArgs args;
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7576 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7577 ASSERT_EQ(1U, args.pointerCount);
7578 ASSERT_EQ(0, args.pointerProperties[0].id);
7579 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7580 ASSERT_NO_FATAL_FAILURE(
7581 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7582
7583 // FINGER 1 DOWN
7584 processSlot(mapper, 1);
7585 processId(mapper, 2);
7586 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7587 processSync(mapper);
7588
7589 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007591 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7592 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007593 ASSERT_EQ(2U, args.pointerCount);
7594 ASSERT_EQ(0, args.pointerProperties[0].id);
7595 ASSERT_EQ(1, args.pointerProperties[1].id);
7596 ASSERT_NO_FATAL_FAILURE(
7597 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7598 ASSERT_NO_FATAL_FAILURE(
7599 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7600
7601 // FINGER 1 MOVE
7602 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7603 processSync(mapper);
7604
7605 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7606 // from move
7607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7609 ASSERT_NO_FATAL_FAILURE(
7610 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7611 ASSERT_NO_FATAL_FAILURE(
7612 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7613
7614 // FINGER 0 MOVE
7615 processSlot(mapper, 0);
7616 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7617 processSync(mapper);
7618
7619 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7622 ASSERT_NO_FATAL_FAILURE(
7623 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7624 ASSERT_NO_FATAL_FAILURE(
7625 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7626
7627 // BUTTON DOWN
7628 processKey(mapper, BTN_LEFT, 1);
7629 processSync(mapper);
7630
7631 // touchinputmapper design sends a move before button press
7632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7635 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7636
7637 // BUTTON UP
7638 processKey(mapper, BTN_LEFT, 0);
7639 processSync(mapper);
7640
7641 // touchinputmapper design sends a move after button release
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7646
7647 // FINGER 0 UP
7648 processId(mapper, -1);
7649 processSync(mapper);
7650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7651 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7652
7653 // FINGER 1 MOVE
7654 processSlot(mapper, 1);
7655 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7656 processSync(mapper);
7657
7658 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7661 ASSERT_EQ(1U, args.pointerCount);
7662 ASSERT_EQ(1, args.pointerProperties[0].id);
7663 ASSERT_NO_FATAL_FAILURE(
7664 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7665
7666 // FINGER 1 UP
7667 processId(mapper, -1);
7668 processKey(mapper, BTN_TOUCH, 0);
7669 processSync(mapper);
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7672
7673 // non captured touchpad should be a mouse source
7674 mFakePolicy->setPointerCapture(false);
7675 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7677 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7678}
7679
7680TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7681 std::shared_ptr<FakePointerController> fakePointerController =
7682 std::make_shared<FakePointerController>();
7683 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7684 fakePointerController->setPosition(0, 0);
7685 fakePointerController->setButtonState(0);
7686
7687 // prepare device and capture
7688 prepareDisplay(DISPLAY_ORIENTATION_0);
7689 prepareAxes(POSITION | ID | SLOT);
7690 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7691 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7692 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7693 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7694 // run uncaptured pointer tests - pushes out generic events
7695 // FINGER 0 DOWN
7696 processId(mapper, 3);
7697 processPosition(mapper, 100, 100);
7698 processKey(mapper, BTN_TOUCH, 1);
7699 processSync(mapper);
7700
7701 // start at (100,100), cursor should be at (0,0) * scale
7702 NotifyMotionArgs args;
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7705 ASSERT_NO_FATAL_FAILURE(
7706 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7707
7708 // FINGER 0 MOVE
7709 processPosition(mapper, 200, 200);
7710 processSync(mapper);
7711
7712 // compute scaling to help with touch position checking
7713 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7714 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7715 float scale =
7716 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7717
7718 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7720 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7722 0, 0, 0, 0, 0, 0, 0));
7723}
7724
7725TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7726 std::shared_ptr<FakePointerController> fakePointerController =
7727 std::make_shared<FakePointerController>();
7728
7729 prepareDisplay(DISPLAY_ORIENTATION_0);
7730 prepareAxes(POSITION | ID | SLOT);
7731 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7732 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7733 mFakePolicy->setPointerCapture(false);
7734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7735
7736 // uncaptured touchpad should be a pointer device
7737 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7738
7739 // captured touchpad should be a touchpad device
7740 mFakePolicy->setPointerCapture(true);
7741 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7742 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7743}
7744
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745} // namespace android