blob: 21dd3c798bda12eb02d4e05e286572050963629d [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;
40
41// Timeout for waiting for an expected event
42static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
43
Michael Wrightd02c5b62014-02-10 15:10:22 -080044// An arbitrary time value.
45static const nsecs_t ARBITRARY_TIME = 1234;
46
47// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080048static constexpr int32_t DISPLAY_ID = 0;
49static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
50static constexpr int32_t DISPLAY_WIDTH = 480;
51static constexpr int32_t DISPLAY_HEIGHT = 800;
52static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
53static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
54static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070055static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070056static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t FIRST_SLOT = 0;
59static constexpr int32_t SECOND_SLOT = 1;
60static constexpr int32_t THIRD_SLOT = 2;
61static constexpr int32_t INVALID_TRACKING_ID = -1;
62static constexpr int32_t FIRST_TRACKING_ID = 0;
63static constexpr int32_t SECOND_TRACKING_ID = 1;
64static constexpr int32_t THIRD_TRACKING_ID = 2;
65
Michael Wrightd02c5b62014-02-10 15:10:22 -080066// Error tolerance for floating point assertions.
67static const float EPSILON = 0.001f;
68
69template<typename T>
70static inline T min(T a, T b) {
71 return a < b ? a : b;
72}
73
74static inline float avg(float x, float y) {
75 return (x + y) / 2;
76}
77
78
79// --- FakePointerController ---
80
81class FakePointerController : public PointerControllerInterface {
82 bool mHaveBounds;
83 float mMinX, mMinY, mMaxX, mMaxY;
84 float mX, mY;
85 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080086 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
Michael Wrightd02c5b62014-02-10 15:10:22 -080088public:
89 FakePointerController() :
90 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080091 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 }
93
Michael Wright17db18e2020-06-26 20:51:44 +010094 virtual ~FakePointerController() {}
95
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 void setBounds(float minX, float minY, float maxX, float maxY) {
97 mHaveBounds = true;
98 mMinX = minX;
99 mMinY = minY;
100 mMaxX = maxX;
101 mMaxY = maxY;
102 }
103
104 virtual void setPosition(float x, float y) {
105 mX = x;
106 mY = y;
107 }
108
109 virtual void setButtonState(int32_t buttonState) {
110 mButtonState = buttonState;
111 }
112
113 virtual int32_t getButtonState() const {
114 return mButtonState;
115 }
116
117 virtual void getPosition(float* outX, float* outY) const {
118 *outX = mX;
119 *outY = mY;
120 }
121
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122 virtual int32_t getDisplayId() const {
123 return mDisplayId;
124 }
125
Garfield Tan888a6a42020-01-09 11:39:16 -0800126 virtual void setDisplayViewport(const DisplayViewport& viewport) {
127 mDisplayId = viewport.displayId;
128 }
129
Arthur Hung7c645402019-01-25 17:45:42 +0800130 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
131 return mSpotsByDisplay;
132 }
133
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134private:
135 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
136 *outMinX = mMinX;
137 *outMinY = mMinY;
138 *outMaxX = mMaxX;
139 *outMaxY = mMaxY;
140 return mHaveBounds;
141 }
142
143 virtual void move(float deltaX, float deltaY) {
144 mX += deltaX;
145 if (mX < mMinX) mX = mMinX;
146 if (mX > mMaxX) mX = mMaxX;
147 mY += deltaY;
148 if (mY < mMinY) mY = mMinY;
149 if (mY > mMaxY) mY = mMaxY;
150 }
151
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100152 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 }
154
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100155 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 }
157
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100158 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159 }
160
Arthur Hung7c645402019-01-25 17:45:42 +0800161 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
162 int32_t displayId) {
163 std::vector<int32_t> newSpots;
164 // Add spots for fingers that are down.
165 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
166 uint32_t id = idBits.clearFirstMarkedBit();
167 newSpots.push_back(id);
168 }
169
170 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 }
172
173 virtual void clearSpots() {
174 }
Arthur Hung7c645402019-01-25 17:45:42 +0800175
176 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177};
178
179
180// --- FakeInputReaderPolicy ---
181
182class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700183 std::mutex mLock;
184 std::condition_variable mDevicesChangedCondition;
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100187 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700188 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
189 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100190 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700191 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192
193protected:
194 virtual ~FakeInputReaderPolicy() { }
195
196public:
197 FakeInputReaderPolicy() {
198 }
199
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700200 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800201 waitForInputDevices([](bool devicesChanged) {
202 if (!devicesChanged) {
203 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
204 }
205 });
206 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700207
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800208 void assertInputDevicesNotChanged() {
209 waitForInputDevices([](bool devicesChanged) {
210 if (devicesChanged) {
211 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
212 }
213 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700214 }
215
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700216 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100217 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100218 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700219 }
220
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700221 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
222 return mConfig.getDisplayViewportByUniqueId(uniqueId);
223 }
224 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
225 return mConfig.getDisplayViewportByType(type);
226 }
227
228 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
229 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700230 }
231
232 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700233 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
234 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700235 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700236 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 }
240
Arthur Hung6cd19a42019-08-30 19:04:12 +0800241 bool updateViewport(const DisplayViewport& viewport) {
242 size_t count = mViewports.size();
243 for (size_t i = 0; i < count; i++) {
244 const DisplayViewport& currentViewport = mViewports[i];
245 if (currentViewport.displayId == viewport.displayId) {
246 mViewports[i] = viewport;
247 mConfig.setDisplayViewports(mViewports);
248 return true;
249 }
250 }
251 // no viewport found.
252 return false;
253 }
254
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100255 void addExcludedDeviceName(const std::string& deviceName) {
256 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 }
258
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700259 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
260 mConfig.portAssociations.insert({inputPort, displayPort});
261 }
262
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000263 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700264
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000265 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700266
Michael Wright17db18e2020-06-26 20:51:44 +0100267 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
268 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 }
270
271 const InputReaderConfiguration* getReaderConfiguration() const {
272 return &mConfig;
273 }
274
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800275 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 return mInputDevices;
277 }
278
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100279 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700280 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700281 return transform;
282 }
283
284 void setTouchAffineTransformation(const TouchAffineTransformation t) {
285 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800286 }
287
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800288 void setPointerCapture(bool enabled) {
289 mConfig.pointerCapture = enabled;
290 }
291
Arthur Hung7c645402019-01-25 17:45:42 +0800292 void setShowTouches(bool enabled) {
293 mConfig.showTouches = enabled;
294 }
295
Garfield Tan888a6a42020-01-09 11:39:16 -0800296 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
297 mConfig.defaultPointerDisplayId = pointerDisplayId;
298 }
299
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700301 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700302 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
303 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700304 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
305 || orientation == DISPLAY_ORIENTATION_270);
306 DisplayViewport v;
307 v.displayId = displayId;
308 v.orientation = orientation;
309 v.logicalLeft = 0;
310 v.logicalTop = 0;
311 v.logicalRight = isRotated ? height : width;
312 v.logicalBottom = isRotated ? width : height;
313 v.physicalLeft = 0;
314 v.physicalTop = 0;
315 v.physicalRight = isRotated ? height : width;
316 v.physicalBottom = isRotated ? width : height;
317 v.deviceWidth = isRotated ? height : width;
318 v.deviceHeight = isRotated ? width : height;
319 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700320 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100321 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700322 return v;
323 }
324
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
326 *outConfig = mConfig;
327 }
328
Michael Wright17db18e2020-06-26 20:51:44 +0100329 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
330 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 }
332
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800333 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700334 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700336 mInputDevicesChanged = true;
337 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
339
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100340 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700341 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342 }
343
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100344 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
345 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800347
348 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
349 std::unique_lock<std::mutex> lock(mLock);
350 base::ScopedLockAssertion assumeLocked(mLock);
351
352 const bool devicesChanged =
353 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
354 return mInputDevicesChanged;
355 });
356 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
357 mInputDevicesChanged = false;
358 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359};
360
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361// --- FakeEventHub ---
362
363class FakeEventHub : public EventHubInterface {
364 struct KeyInfo {
365 int32_t keyCode;
366 uint32_t flags;
367 };
368
369 struct Device {
370 InputDeviceIdentifier identifier;
371 uint32_t classes;
372 PropertyMap configuration;
373 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
374 KeyedVector<int, bool> relativeAxes;
375 KeyedVector<int32_t, int32_t> keyCodeStates;
376 KeyedVector<int32_t, int32_t> scanCodeStates;
377 KeyedVector<int32_t, int32_t> switchStates;
378 KeyedVector<int32_t, int32_t> absoluteAxisValue;
379 KeyedVector<int32_t, KeyInfo> keysByScanCode;
380 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
381 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800382 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700383 bool enabled;
384
385 status_t enable() {
386 enabled = true;
387 return OK;
388 }
389
390 status_t disable() {
391 enabled = false;
392 return OK;
393 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700395 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700396 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 }
398 };
399
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700400 std::mutex mLock;
401 std::condition_variable mEventsCondition;
402
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100404 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700405 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600406 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700408public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 virtual ~FakeEventHub() {
410 for (size_t i = 0; i < mDevices.size(); i++) {
411 delete mDevices.valueAt(i);
412 }
413 }
414
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 FakeEventHub() { }
416
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100417 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418 Device* device = new Device(classes);
419 device->identifier.name = name;
420 mDevices.add(deviceId, device);
421
422 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
423 }
424
425 void removeDevice(int32_t deviceId) {
426 delete mDevices.valueFor(deviceId);
427 mDevices.removeItem(deviceId);
428
429 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
430 }
431
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700432 bool isDeviceEnabled(int32_t deviceId) {
433 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700434 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700435 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
436 return false;
437 }
438 return device->enabled;
439 }
440
441 status_t enableDevice(int32_t deviceId) {
442 status_t result;
443 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700444 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700445 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
446 return BAD_VALUE;
447 }
448 if (device->enabled) {
449 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
450 return OK;
451 }
452 result = device->enable();
453 return result;
454 }
455
456 status_t disableDevice(int32_t deviceId) {
457 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700458 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700459 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
460 return BAD_VALUE;
461 }
462 if (!device->enabled) {
463 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
464 return OK;
465 }
466 return device->disable();
467 }
468
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469 void finishDeviceScan() {
470 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
471 }
472
473 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
474 Device* device = getDevice(deviceId);
475 device->configuration.addProperty(key, value);
476 }
477
478 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
479 Device* device = getDevice(deviceId);
480 device->configuration.addAll(configuration);
481 }
482
483 void addAbsoluteAxis(int32_t deviceId, int axis,
484 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
485 Device* device = getDevice(deviceId);
486
487 RawAbsoluteAxisInfo info;
488 info.valid = true;
489 info.minValue = minValue;
490 info.maxValue = maxValue;
491 info.flat = flat;
492 info.fuzz = fuzz;
493 info.resolution = resolution;
494 device->absoluteAxes.add(axis, info);
495 }
496
497 void addRelativeAxis(int32_t deviceId, int32_t axis) {
498 Device* device = getDevice(deviceId);
499 device->relativeAxes.add(axis, true);
500 }
501
502 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
503 Device* device = getDevice(deviceId);
504 device->keyCodeStates.replaceValueFor(keyCode, state);
505 }
506
507 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
508 Device* device = getDevice(deviceId);
509 device->scanCodeStates.replaceValueFor(scanCode, state);
510 }
511
512 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
513 Device* device = getDevice(deviceId);
514 device->switchStates.replaceValueFor(switchCode, state);
515 }
516
517 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
518 Device* device = getDevice(deviceId);
519 device->absoluteAxisValue.replaceValueFor(axis, value);
520 }
521
522 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
523 int32_t keyCode, uint32_t flags) {
524 Device* device = getDevice(deviceId);
525 KeyInfo info;
526 info.keyCode = keyCode;
527 info.flags = flags;
528 if (scanCode) {
529 device->keysByScanCode.add(scanCode, info);
530 }
531 if (usageCode) {
532 device->keysByUsageCode.add(usageCode, info);
533 }
534 }
535
536 void addLed(int32_t deviceId, int32_t led, bool initialState) {
537 Device* device = getDevice(deviceId);
538 device->leds.add(led, initialState);
539 }
540
541 bool getLedState(int32_t deviceId, int32_t led) {
542 Device* device = getDevice(deviceId);
543 return device->leds.valueFor(led);
544 }
545
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100546 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 return mExcludedDevices;
548 }
549
550 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
551 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800552 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 }
554
555 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
556 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700557 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 RawEvent event;
559 event.when = when;
560 event.deviceId = deviceId;
561 event.type = type;
562 event.code = code;
563 event.value = value;
564 mEvents.push_back(event);
565
566 if (type == EV_ABS) {
567 setAbsoluteAxisValue(deviceId, code, value);
568 }
569 }
570
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600571 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
572 std::vector<TouchVideoFrame>> videoFrames) {
573 mVideoFrames = std::move(videoFrames);
574 }
575
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700577 std::unique_lock<std::mutex> lock(mLock);
578 base::ScopedLockAssertion assumeLocked(mLock);
579 const bool queueIsEmpty =
580 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
581 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
582 if (!queueIsEmpty) {
583 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
584 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 }
586
587private:
588 Device* getDevice(int32_t deviceId) const {
589 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100590 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 }
592
593 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
594 Device* device = getDevice(deviceId);
595 return device ? device->classes : 0;
596 }
597
598 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
599 Device* device = getDevice(deviceId);
600 return device ? device->identifier : InputDeviceIdentifier();
601 }
602
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100603 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604 return 0;
605 }
606
607 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
608 Device* device = getDevice(deviceId);
609 if (device) {
610 *outConfiguration = device->configuration;
611 }
612 }
613
614 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
615 RawAbsoluteAxisInfo* outAxisInfo) const {
616 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800617 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 ssize_t index = device->absoluteAxes.indexOfKey(axis);
619 if (index >= 0) {
620 *outAxisInfo = device->absoluteAxes.valueAt(index);
621 return OK;
622 }
623 }
624 outAxisInfo->clear();
625 return -1;
626 }
627
628 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
629 Device* device = getDevice(deviceId);
630 if (device) {
631 return device->relativeAxes.indexOfKey(axis) >= 0;
632 }
633 return false;
634 }
635
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100636 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 return false;
638 }
639
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700640 virtual status_t mapKey(int32_t deviceId,
641 int32_t scanCode, int32_t usageCode, int32_t metaState,
642 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 Device* device = getDevice(deviceId);
644 if (device) {
645 const KeyInfo* key = getKey(device, scanCode, usageCode);
646 if (key) {
647 if (outKeycode) {
648 *outKeycode = key->keyCode;
649 }
650 if (outFlags) {
651 *outFlags = key->flags;
652 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700653 if (outMetaState) {
654 *outMetaState = metaState;
655 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 return OK;
657 }
658 }
659 return NAME_NOT_FOUND;
660 }
661
662 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
663 if (usageCode) {
664 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
665 if (index >= 0) {
666 return &device->keysByUsageCode.valueAt(index);
667 }
668 }
669 if (scanCode) {
670 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
671 if (index >= 0) {
672 return &device->keysByScanCode.valueAt(index);
673 }
674 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700675 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 }
677
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100678 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 return NAME_NOT_FOUND;
680 }
681
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100682 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 mExcludedDevices = devices;
684 }
685
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100686 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700687 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 if (mEvents.empty()) {
689 return 0;
690 }
691
692 *buffer = *mEvents.begin();
693 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700694 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695 return 1;
696 }
697
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800698 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600699 auto it = mVideoFrames.find(deviceId);
700 if (it != mVideoFrames.end()) {
701 std::vector<TouchVideoFrame> frames = std::move(it->second);
702 mVideoFrames.erase(deviceId);
703 return frames;
704 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800705 return {};
706 }
707
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
709 Device* device = getDevice(deviceId);
710 if (device) {
711 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
712 if (index >= 0) {
713 return device->scanCodeStates.valueAt(index);
714 }
715 }
716 return AKEY_STATE_UNKNOWN;
717 }
718
719 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
720 Device* device = getDevice(deviceId);
721 if (device) {
722 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
723 if (index >= 0) {
724 return device->keyCodeStates.valueAt(index);
725 }
726 }
727 return AKEY_STATE_UNKNOWN;
728 }
729
730 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
731 Device* device = getDevice(deviceId);
732 if (device) {
733 ssize_t index = device->switchStates.indexOfKey(sw);
734 if (index >= 0) {
735 return device->switchStates.valueAt(index);
736 }
737 }
738 return AKEY_STATE_UNKNOWN;
739 }
740
741 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
742 int32_t* outValue) const {
743 Device* device = getDevice(deviceId);
744 if (device) {
745 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
746 if (index >= 0) {
747 *outValue = device->absoluteAxisValue.valueAt(index);
748 return OK;
749 }
750 }
751 *outValue = 0;
752 return -1;
753 }
754
755 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
756 uint8_t* outFlags) const {
757 bool result = false;
758 Device* device = getDevice(deviceId);
759 if (device) {
760 for (size_t i = 0; i < numCodes; i++) {
761 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
762 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
763 outFlags[i] = 1;
764 result = true;
765 }
766 }
767 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
768 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
769 outFlags[i] = 1;
770 result = true;
771 }
772 }
773 }
774 }
775 return result;
776 }
777
778 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
779 Device* device = getDevice(deviceId);
780 if (device) {
781 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
782 return index >= 0;
783 }
784 return false;
785 }
786
787 virtual bool hasLed(int32_t deviceId, int32_t led) const {
788 Device* device = getDevice(deviceId);
789 return device && device->leds.indexOfKey(led) >= 0;
790 }
791
792 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
793 Device* device = getDevice(deviceId);
794 if (device) {
795 ssize_t index = device->leds.indexOfKey(led);
796 if (index >= 0) {
797 device->leds.replaceValueAt(led, on);
798 } else {
799 ADD_FAILURE()
800 << "Attempted to set the state of an LED that the EventHub declared "
801 "was not present. led=" << led;
802 }
803 }
804 }
805
806 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800807 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 outVirtualKeys.clear();
809
810 Device* device = getDevice(deviceId);
811 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800812 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 }
814 }
815
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100816 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700817 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 }
819
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100820 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 return false;
822 }
823
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000824 virtual void vibrate(int32_t, const VibrationElement&) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100826 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
828
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100829 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800830 return false;
831 }
832
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800833 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 }
835
836 virtual void monitor() {
837 }
838
839 virtual void requestReopenDevices() {
840 }
841
842 virtual void wake() {
843 }
844};
845
846
847// --- FakeInputReaderContext ---
848
849class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700850 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 sp<InputReaderPolicyInterface> mPolicy;
852 sp<InputListenerInterface> mListener;
853 int32_t mGlobalMetaState;
854 bool mUpdateGlobalMetaStateWasCalled;
855 int32_t mGeneration;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800856 int32_t mNextId;
Michael Wright17db18e2020-06-26 20:51:44 +0100857 std::weak_ptr<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858
859public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700860 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
861 const sp<InputReaderPolicyInterface>& policy,
862 const sp<InputListenerInterface>& listener)
863 : mEventHub(eventHub),
864 mPolicy(policy),
865 mListener(listener),
866 mGlobalMetaState(0),
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800867 mNextId(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
869 virtual ~FakeInputReaderContext() { }
870
871 void assertUpdateGlobalMetaStateWasCalled() {
872 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
873 << "Expected updateGlobalMetaState() to have been called.";
874 mUpdateGlobalMetaStateWasCalled = false;
875 }
876
877 void setGlobalMetaState(int32_t state) {
878 mGlobalMetaState = state;
879 }
880
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800881 uint32_t getGeneration() {
882 return mGeneration;
883 }
884
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800885 void updatePointerDisplay() {
Michael Wright17db18e2020-06-26 20:51:44 +0100886 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800887 if (controller != nullptr) {
888 InputReaderConfiguration config;
889 mPolicy->getReaderConfiguration(&config);
890 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
891 if (viewport) {
892 controller->setDisplayViewport(*viewport);
893 }
894 }
895 }
896
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897private:
898 virtual void updateGlobalMetaState() {
899 mUpdateGlobalMetaStateWasCalled = true;
900 }
901
902 virtual int32_t getGlobalMetaState() {
903 return mGlobalMetaState;
904 }
905
906 virtual EventHubInterface* getEventHub() {
907 return mEventHub.get();
908 }
909
910 virtual InputReaderPolicyInterface* getPolicy() {
911 return mPolicy.get();
912 }
913
914 virtual InputListenerInterface* getListener() {
915 return mListener.get();
916 }
917
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100918 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919 }
920
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800921 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
Michael Wright17db18e2020-06-26 20:51:44 +0100923 virtual std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) {
924 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800925 if (controller == nullptr) {
926 controller = mPolicy->obtainPointerController(deviceId);
927 mPointerController = controller;
928 updatePointerDisplay();
929 }
930 return controller;
931 }
932
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 virtual void fadePointer() {
934 }
935
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100936 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 }
938
939 virtual int32_t bumpGeneration() {
940 return ++mGeneration;
941 }
Michael Wright842500e2015-03-13 17:32:02 -0700942
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800943 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700944
945 }
946
947 virtual void dispatchExternalStylusState(const StylusState&) {
948
949 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800950
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800951 virtual int32_t getNextId() { return mNextId++; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952};
953
954
955// --- FakeInputMapper ---
956
957class FakeInputMapper : public InputMapper {
958 uint32_t mSources;
959 int32_t mKeyboardType;
960 int32_t mMetaState;
961 KeyedVector<int32_t, int32_t> mKeyCodeStates;
962 KeyedVector<int32_t, int32_t> mScanCodeStates;
963 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800964 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700966 std::mutex mLock;
967 std::condition_variable mStateChangedCondition;
968 bool mConfigureWasCalled GUARDED_BY(mLock);
969 bool mResetWasCalled GUARDED_BY(mLock);
970 bool mProcessWasCalled GUARDED_BY(mLock);
971 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972
Arthur Hungc23540e2018-11-29 20:42:11 +0800973 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800975 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
976 : InputMapper(deviceContext),
977 mSources(sources),
978 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800980 mConfigureWasCalled(false),
981 mResetWasCalled(false),
982 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
984 virtual ~FakeInputMapper() { }
985
986 void setKeyboardType(int32_t keyboardType) {
987 mKeyboardType = keyboardType;
988 }
989
990 void setMetaState(int32_t metaState) {
991 mMetaState = metaState;
992 }
993
994 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700995 std::unique_lock<std::mutex> lock(mLock);
996 base::ScopedLockAssertion assumeLocked(mLock);
997 const bool configureCalled =
998 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
999 return mConfigureWasCalled;
1000 });
1001 if (!configureCalled) {
1002 FAIL() << "Expected configure() to have been called.";
1003 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 mConfigureWasCalled = false;
1005 }
1006
1007 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001008 std::unique_lock<std::mutex> lock(mLock);
1009 base::ScopedLockAssertion assumeLocked(mLock);
1010 const bool resetCalled =
1011 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1012 return mResetWasCalled;
1013 });
1014 if (!resetCalled) {
1015 FAIL() << "Expected reset() to have been called.";
1016 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017 mResetWasCalled = false;
1018 }
1019
Yi Kong9b14ac62018-07-17 13:48:38 -07001020 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001021 std::unique_lock<std::mutex> lock(mLock);
1022 base::ScopedLockAssertion assumeLocked(mLock);
1023 const bool processCalled =
1024 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1025 return mProcessWasCalled;
1026 });
1027 if (!processCalled) {
1028 FAIL() << "Expected process() to have been called.";
1029 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030 if (outLastEvent) {
1031 *outLastEvent = mLastEvent;
1032 }
1033 mProcessWasCalled = false;
1034 }
1035
1036 void setKeyCodeState(int32_t keyCode, int32_t state) {
1037 mKeyCodeStates.replaceValueFor(keyCode, state);
1038 }
1039
1040 void setScanCodeState(int32_t scanCode, int32_t state) {
1041 mScanCodeStates.replaceValueFor(scanCode, state);
1042 }
1043
1044 void setSwitchState(int32_t switchCode, int32_t state) {
1045 mSwitchStates.replaceValueFor(switchCode, state);
1046 }
1047
1048 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001049 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050 }
1051
1052private:
1053 virtual uint32_t getSources() {
1054 return mSources;
1055 }
1056
1057 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1058 InputMapper::populateDeviceInfo(deviceInfo);
1059
1060 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1061 deviceInfo->setKeyboardType(mKeyboardType);
1062 }
1063 }
1064
Arthur Hungc23540e2018-11-29 20:42:11 +08001065 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001066 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001068
1069 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001070 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001071 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1072 mViewport = config->getDisplayViewportByPort(*displayPort);
1073 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001074
1075 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076 }
1077
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001078 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001079 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 }
1083
1084 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001085 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 mLastEvent = *rawEvent;
1087 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001088 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089 }
1090
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001091 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1093 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1094 }
1095
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001096 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1098 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1099 }
1100
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001101 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1103 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1104 }
1105
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001106 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 const int32_t* keyCodes, uint8_t* outFlags) {
1108 bool result = false;
1109 for (size_t i = 0; i < numCodes; i++) {
1110 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1111 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1112 outFlags[i] = 1;
1113 result = true;
1114 }
1115 }
1116 }
1117 return result;
1118 }
1119
1120 virtual int32_t getMetaState() {
1121 return mMetaState;
1122 }
1123
1124 virtual void fadePointer() {
1125 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001126
1127 virtual std::optional<int32_t> getAssociatedDisplay() {
1128 if (mViewport) {
1129 return std::make_optional(mViewport->displayId);
1130 }
1131 return std::nullopt;
1132 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133};
1134
1135
1136// --- InstrumentedInputReader ---
1137
1138class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001139 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140
1141public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001142 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1143 const sp<InputReaderPolicyInterface>& policy,
1144 const sp<InputListenerInterface>& listener)
1145 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001147 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001149 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001151 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001152 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 InputDeviceIdentifier identifier;
1154 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001155 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001157 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 }
1159
Prabir Pradhan28efc192019-11-05 01:10:04 +00001160 // Make the protected loopOnce method accessible to tests.
1161 using InputReader::loopOnce;
1162
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001164 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1165 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001166 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001167 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001168 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001169 return device;
1170 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001171 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172 }
1173
1174 friend class InputReaderTest;
1175};
1176
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177// --- InputReaderPolicyTest ---
1178class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001179protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001180 sp<FakeInputReaderPolicy> mFakePolicy;
1181
Prabir Pradhan28efc192019-11-05 01:10:04 +00001182 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1183 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001184};
1185
1186/**
1187 * Check that empty set of viewports is an acceptable configuration.
1188 * Also try to get internal viewport two different ways - by type and by uniqueId.
1189 *
1190 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1191 * Such configuration is not currently allowed.
1192 */
1193TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001194 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001195
1196 // We didn't add any viewports yet, so there shouldn't be any.
1197 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001198 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001199 ASSERT_FALSE(internalViewport);
1200
1201 // Add an internal viewport, then clear it
1202 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001203 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1204 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001205
1206 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001207 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001208 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001209 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210
1211 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001212 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215
1216 mFakePolicy->clearViewports();
1217 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001218 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001219 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001220 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221 ASSERT_FALSE(internalViewport);
1222}
1223
1224TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1225 const std::string internalUniqueId = "local:0";
1226 const std::string externalUniqueId = "local:1";
1227 const std::string virtualUniqueId1 = "virtual:2";
1228 const std::string virtualUniqueId2 = "virtual:3";
1229 constexpr int32_t virtualDisplayId1 = 2;
1230 constexpr int32_t virtualDisplayId2 = 3;
1231
1232 // Add an internal viewport
1233 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001234 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1235 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001236 // Add an external viewport
1237 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001238 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1239 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 // Add an virtual viewport
1241 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001242 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1243 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001244 // Add another virtual viewport
1245 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001246 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1247 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001248
1249 // Check matching by type for internal
1250 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001251 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252 ASSERT_TRUE(internalViewport);
1253 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1254
1255 // Check matching by type for external
1256 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001257 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001258 ASSERT_TRUE(externalViewport);
1259 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1260
1261 // Check matching by uniqueId for virtual viewport #1
1262 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001263 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001265 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001266 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1267 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1268
1269 // Check matching by uniqueId for virtual viewport #2
1270 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001271 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001273 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001274 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1275 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1276}
1277
1278
1279/**
1280 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1281 * that lookup works by checking display id.
1282 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1283 */
1284TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1285 const std::string uniqueId1 = "uniqueId1";
1286 const std::string uniqueId2 = "uniqueId2";
1287 constexpr int32_t displayId1 = 2;
1288 constexpr int32_t displayId2 = 3;
1289
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001290 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1291 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292 for (const ViewportType& type : types) {
1293 mFakePolicy->clearViewports();
1294 // Add a viewport
1295 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001296 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001297 // Add another viewport
1298 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001299 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001300
1301 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001302 std::optional<DisplayViewport> viewport1 =
1303 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001304 ASSERT_TRUE(viewport1);
1305 ASSERT_EQ(displayId1, viewport1->displayId);
1306 ASSERT_EQ(type, viewport1->type);
1307
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001308 std::optional<DisplayViewport> viewport2 =
1309 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001310 ASSERT_TRUE(viewport2);
1311 ASSERT_EQ(displayId2, viewport2->displayId);
1312 ASSERT_EQ(type, viewport2->type);
1313
1314 // When there are multiple viewports of the same kind, and uniqueId is not specified
1315 // in the call to getDisplayViewport, then that situation is not supported.
1316 // The viewports can be stored in any order, so we cannot rely on the order, since that
1317 // is just implementation detail.
1318 // However, we can check that it still returns *a* viewport, we just cannot assert
1319 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001320 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001321 ASSERT_TRUE(someViewport);
1322 }
1323}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001325/**
1326 * Check getDisplayViewportByPort
1327 */
1328TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001329 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001330 const std::string uniqueId1 = "uniqueId1";
1331 const std::string uniqueId2 = "uniqueId2";
1332 constexpr int32_t displayId1 = 1;
1333 constexpr int32_t displayId2 = 2;
1334 const uint8_t hdmi1 = 0;
1335 const uint8_t hdmi2 = 1;
1336 const uint8_t hdmi3 = 2;
1337
1338 mFakePolicy->clearViewports();
1339 // Add a viewport that's associated with some display port that's not of interest.
1340 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1341 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1342 // Add another viewport, connected to HDMI1 port
1343 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1344 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1345
1346 // Check that correct display viewport was returned by comparing the display ports.
1347 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1348 ASSERT_TRUE(hdmi1Viewport);
1349 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1350 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1351
1352 // Check that we can still get the same viewport using the uniqueId
1353 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1354 ASSERT_TRUE(hdmi1Viewport);
1355 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1356 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1357 ASSERT_EQ(type, hdmi1Viewport->type);
1358
1359 // Check that we cannot find a port with "HDMI2", because we never added one
1360 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1361 ASSERT_FALSE(hdmi2Viewport);
1362}
1363
Michael Wrightd02c5b62014-02-10 15:10:22 -08001364// --- InputReaderTest ---
1365
1366class InputReaderTest : public testing::Test {
1367protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001368 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001369 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001370 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001371 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001372
Prabir Pradhan28efc192019-11-05 01:10:04 +00001373 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001374 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001375 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001376 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001377
Prabir Pradhan28efc192019-11-05 01:10:04 +00001378 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1379 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380 }
1381
Prabir Pradhan28efc192019-11-05 01:10:04 +00001382 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383 mFakeListener.clear();
1384 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001385 }
1386
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001387 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1388 const PropertyMap* configuration) {
1389 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001390
1391 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001392 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001393 }
1394 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001395 mReader->loopOnce();
1396 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1398 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399 }
1400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001401 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001402 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001403 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001404 }
1405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001406 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001407 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001408 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001409 }
1410
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001411 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001412 const std::string& name, uint32_t classes,
1413 uint32_t sources,
1414 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001415 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1416 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001417 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001418 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001419 return mapper;
1420 }
1421};
1422
1423TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001424 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001425 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001426 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001427 0, 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;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001450 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_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;
1487 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1488 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;
1520 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1521 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;
1553 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1554 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;
1586 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1587 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;
1628 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_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;
1638 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1639 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;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001659 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_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;
1692 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1693 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;
Arthur Hungc23540e2018-11-29 20:42:11 +08001707 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_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;
2027 static const uint32_t 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
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002043 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 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;
2064const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2065 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_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());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002071 ASSERT_EQ(0U, 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;
2266 static const uint32_t 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
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002275 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002276 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002278 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2280 InputDeviceIdentifier identifier;
2281 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002282 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002283 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002285 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 }
2287
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002288 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2289
Prabir Pradhan28efc192019-11-05 01:10:04 +00002290 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291 delete mDevice;
2292 delete mFakeContext;
2293 mFakeListener.clear();
2294 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002295 }
2296
2297 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002298 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002299 }
2300
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002301 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002302 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2303 mFakeContext->updatePointerDisplay();
2304 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002305 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2306 }
2307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002308 template <class T, typename... Args>
2309 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002310 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002311 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002313 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314 }
2315
2316 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002317 int32_t orientation, const std::string& uniqueId,
2318 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002319 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002320 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002321 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2322 }
2323
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002324 void clearViewports() {
2325 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326 }
2327
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002328 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2329 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002330 RawEvent event;
2331 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002332 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333 event.type = type;
2334 event.code = code;
2335 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002336 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337 }
2338
2339 static void assertMotionRange(const InputDeviceInfo& info,
2340 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2341 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002342 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2344 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2345 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2346 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2347 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2348 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2349 }
2350
2351 static void assertPointerCoords(const PointerCoords& coords,
2352 float x, float y, float pressure, float size,
2353 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2354 float orientation, float distance) {
2355 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2356 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2357 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2358 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2359 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2360 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2361 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2362 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2363 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2364 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2365 }
2366
Michael Wright17db18e2020-06-26 20:51:44 +01002367 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002369 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370 ASSERT_NEAR(x, actualX, 1);
2371 ASSERT_NEAR(y, actualY, 1);
2372 }
2373};
2374
2375const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002376const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002377const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2379const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2380const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002381const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382
2383// --- SwitchInputMapperTest ---
2384
2385class SwitchInputMapperTest : public InputMapperTest {
2386protected:
2387};
2388
2389TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002390 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002392 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393}
2394
2395TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002396 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002398 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002399 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002401 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002402 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403}
2404
2405TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002406 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002408 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2409 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2410 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2411 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412
2413 NotifySwitchArgs args;
2414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2415 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002416 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2417 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418 args.switchMask);
2419 ASSERT_EQ(uint32_t(0), args.policyFlags);
2420}
2421
2422
2423// --- KeyboardInputMapperTest ---
2424
2425class KeyboardInputMapperTest : public InputMapperTest {
2426protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002427 const std::string UNIQUE_ID = "local:0";
2428
2429 void prepareDisplay(int32_t orientation);
2430
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002431 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002432 int32_t originalKeyCode, int32_t rotatedKeyCode,
2433 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434};
2435
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002436/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2437 * orientation.
2438 */
2439void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002440 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2441 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002442}
2443
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002444void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002445 int32_t originalScanCode, int32_t originalKeyCode,
2446 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 NotifyKeyArgs args;
2448
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002449 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2452 ASSERT_EQ(originalScanCode, args.scanCode);
2453 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002454 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002456 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2458 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2459 ASSERT_EQ(originalScanCode, args.scanCode);
2460 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002461 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002462}
2463
Michael Wrightd02c5b62014-02-10 15:10:22 -08002464TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002465 KeyboardInputMapper& mapper =
2466 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2467 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002469 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470}
2471
2472TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2473 const int32_t USAGE_A = 0x070004;
2474 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2476 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002477
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002478 KeyboardInputMapper& mapper =
2479 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481
2482 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002483 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002484 NotifyKeyArgs args;
2485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2486 ASSERT_EQ(DEVICE_ID, args.deviceId);
2487 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2488 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2489 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2490 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2491 ASSERT_EQ(KEY_HOME, args.scanCode);
2492 ASSERT_EQ(AMETA_NONE, args.metaState);
2493 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2494 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2495 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2496
2497 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002498 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2500 ASSERT_EQ(DEVICE_ID, args.deviceId);
2501 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2502 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2503 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2504 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2505 ASSERT_EQ(KEY_HOME, args.scanCode);
2506 ASSERT_EQ(AMETA_NONE, args.metaState);
2507 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2508 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2509 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2510
2511 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002512 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2513 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2515 ASSERT_EQ(DEVICE_ID, args.deviceId);
2516 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2517 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2518 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2519 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2520 ASSERT_EQ(0, args.scanCode);
2521 ASSERT_EQ(AMETA_NONE, args.metaState);
2522 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2523 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2524 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2525
2526 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002527 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2528 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2530 ASSERT_EQ(DEVICE_ID, args.deviceId);
2531 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2532 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2533 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2534 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2535 ASSERT_EQ(0, args.scanCode);
2536 ASSERT_EQ(AMETA_NONE, args.metaState);
2537 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2538 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2539 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2540
2541 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002542 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2543 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2545 ASSERT_EQ(DEVICE_ID, args.deviceId);
2546 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2547 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2548 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2549 ASSERT_EQ(0, args.keyCode);
2550 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2551 ASSERT_EQ(AMETA_NONE, args.metaState);
2552 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2553 ASSERT_EQ(0U, args.policyFlags);
2554 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2555
2556 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002557 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2558 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2560 ASSERT_EQ(DEVICE_ID, args.deviceId);
2561 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2562 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2563 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2564 ASSERT_EQ(0, args.keyCode);
2565 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2566 ASSERT_EQ(AMETA_NONE, args.metaState);
2567 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2568 ASSERT_EQ(0U, args.policyFlags);
2569 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2570}
2571
2572TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002576 KeyboardInputMapper& mapper =
2577 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2578 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579
2580 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002581 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582
2583 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002584 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585 NotifyKeyArgs args;
2586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2590
2591 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002592 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2594 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002595 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596
2597 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002598 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2600 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002601 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602
2603 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002604 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2606 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002607 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2609}
2610
2611TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002617 KeyboardInputMapper& mapper =
2618 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2619 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002621 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2623 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2624 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2625 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2626 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2627 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2628 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2629 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2630}
2631
2632TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002639 KeyboardInputMapper& mapper =
2640 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2641 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002643 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002644 ASSERT_NO_FATAL_FAILURE(
2645 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2646 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2647 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2649 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2650 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2651 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002653 clearViewports();
2654 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002655 ASSERT_NO_FATAL_FAILURE(
2656 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2657 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2658 AKEYCODE_DPAD_UP, DISPLAY_ID));
2659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2660 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2661 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2662 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002664 clearViewports();
2665 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002666 ASSERT_NO_FATAL_FAILURE(
2667 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2668 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2669 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2670 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2671 AKEYCODE_DPAD_UP, DISPLAY_ID));
2672 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2673 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002675 clearViewports();
2676 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002677 ASSERT_NO_FATAL_FAILURE(
2678 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2679 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2680 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2681 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2682 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2683 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2684 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685
2686 // Special case: if orientation changes while key is down, we still emit the same keycode
2687 // in the key up as we did in the key down.
2688 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002689 clearViewports();
2690 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002691 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2693 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2694 ASSERT_EQ(KEY_UP, args.scanCode);
2695 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2696
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002697 clearViewports();
2698 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002699 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2701 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2702 ASSERT_EQ(KEY_UP, args.scanCode);
2703 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2704}
2705
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002706TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2707 // If the keyboard is not orientation aware,
2708 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002709 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002711 KeyboardInputMapper& mapper =
2712 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2713 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002714 NotifyKeyArgs args;
2715
2716 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002717 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002719 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2721 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2722
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002723 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002724 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002726 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2728 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2729}
2730
2731TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2732 // If the keyboard is orientation aware,
2733 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002735
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002736 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002737 KeyboardInputMapper& mapper =
2738 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2739 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002740 NotifyKeyArgs args;
2741
2742 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2743 // ^--- already checked by the previous test
2744
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002745 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002746 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002747 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002749 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2751 ASSERT_EQ(DISPLAY_ID, args.displayId);
2752
2753 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002754 clearViewports();
2755 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002756 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002757 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002759 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2761 ASSERT_EQ(newDisplayId, args.displayId);
2762}
2763
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002765 KeyboardInputMapper& mapper =
2766 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2767 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002769 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002770 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002772 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002773 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774}
2775
2776TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002777 KeyboardInputMapper& mapper =
2778 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2779 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002781 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002784 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786}
2787
2788TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002789 KeyboardInputMapper& mapper =
2790 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2791 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794
2795 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2796 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002797 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798 ASSERT_TRUE(flags[0]);
2799 ASSERT_FALSE(flags[1]);
2800}
2801
2802TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002803 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2804 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2805 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2806 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2808 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002810 KeyboardInputMapper& mapper =
2811 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2812 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813
2814 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002815 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
2819 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002820 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2821 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002822 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2823 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002825 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826
2827 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002828 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2829 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002830 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2832 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002833 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834
2835 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002836 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2837 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002838 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2839 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2840 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002841 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842
2843 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002844 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2845 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002846 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2847 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2848 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002849 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850
2851 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002852 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2853 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002854 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2856 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858
2859 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002860 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2861 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002862 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002865 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866}
2867
Arthur Hung2c9a3342019-07-23 14:18:59 +08002868TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2869 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002870 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002874
2875 // keyboard 2.
2876 const std::string USB2 = "USB2";
2877 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002878 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002879 InputDeviceIdentifier identifier;
2880 identifier.name = "KEYBOARD2";
2881 identifier.location = USB2;
2882 std::unique_ptr<InputDevice> device2 =
2883 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002884 identifier);
2885 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2886 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2887 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2888 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2889 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002890
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002891 KeyboardInputMapper& mapper =
2892 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2893 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002895 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002896 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002897 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002898 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2899 device2->reset(ARBITRARY_TIME);
2900
2901 // Prepared displays and associated info.
2902 constexpr uint8_t hdmi1 = 0;
2903 constexpr uint8_t hdmi2 = 1;
2904 const std::string SECONDARY_UNIQUE_ID = "local:1";
2905
2906 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2907 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2908
2909 // No associated display viewport found, should disable the device.
2910 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2911 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2912 ASSERT_FALSE(device2->isEnabled());
2913
2914 // Prepare second display.
2915 constexpr int32_t newDisplayId = 2;
2916 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002917 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002918 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002919 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002920 // Default device will reconfigure above, need additional reconfiguration for another device.
2921 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2922 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2923
2924 // Device should be enabled after the associated display is found.
2925 ASSERT_TRUE(mDevice->isEnabled());
2926 ASSERT_TRUE(device2->isEnabled());
2927
2928 // Test pad key events
2929 ASSERT_NO_FATAL_FAILURE(
2930 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2931 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2932 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2933 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2934 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2935 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2936 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2937
2938 ASSERT_NO_FATAL_FAILURE(
2939 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2940 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2941 AKEYCODE_DPAD_RIGHT, newDisplayId));
2942 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2943 AKEYCODE_DPAD_DOWN, newDisplayId));
2944 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2945 AKEYCODE_DPAD_LEFT, newDisplayId));
2946}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002948// --- KeyboardInputMapperTest_ExternalDevice ---
2949
2950class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2951protected:
2952 virtual void SetUp() override {
2953 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2954 }
2955};
2956
2957TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002958 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2959 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002960
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002961 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2962 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2964 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002966 KeyboardInputMapper& mapper =
2967 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2968 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002969
2970 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2971 NotifyKeyArgs args;
2972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2973 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2974
2975 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2977 ASSERT_EQ(uint32_t(0), args.policyFlags);
2978
2979 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2981 ASSERT_EQ(uint32_t(0), args.policyFlags);
2982
2983 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2985 ASSERT_EQ(uint32_t(0), args.policyFlags);
2986
2987 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2990
2991 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2993 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2994}
2995
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002996TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002997 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002998
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002999 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003002
Powei Fengd041c5d2019-05-03 17:11:33 -07003003 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003004 KeyboardInputMapper& mapper =
3005 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3006 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003007
3008 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3009 NotifyKeyArgs args;
3010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3011 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3012
3013 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3015 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3016
3017 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3019 ASSERT_EQ(uint32_t(0), args.policyFlags);
3020
3021 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3023 ASSERT_EQ(uint32_t(0), args.policyFlags);
3024
3025 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3027 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3028
3029 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3031 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3032}
3033
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034// --- CursorInputMapperTest ---
3035
3036class CursorInputMapperTest : public InputMapperTest {
3037protected:
3038 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3039
Michael Wright17db18e2020-06-26 20:51:44 +01003040 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041
Prabir Pradhan28efc192019-11-05 01:10:04 +00003042 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 InputMapperTest::SetUp();
3044
Michael Wright17db18e2020-06-26 20:51:44 +01003045 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003046 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 }
3048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003049 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3050 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003051
3052 void prepareDisplay(int32_t orientation) {
3053 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003054 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003055 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3056 orientation, uniqueId, NO_PORT, viewportType);
3057 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058};
3059
3060const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003062void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3063 int32_t originalY, int32_t rotatedX,
3064 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 NotifyMotionArgs args;
3066
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003067 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3068 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3069 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3073 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3074 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3075 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3076}
3077
3078TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003080 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003082 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083}
3084
3085TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090}
3091
3092TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003094 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095
3096 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
3099 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003100 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3101 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3103 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3104
3105 // When the bounds are set, then there should be a valid motion range.
3106 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3107
3108 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003109 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
3111 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3112 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3113 1, 800 - 1, 0.0f, 0.0f));
3114 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3115 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3116 2, 480 - 1, 0.0f, 0.0f));
3117 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3118 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3119 0.0f, 1.0f, 0.0f, 0.0f));
3120}
3121
3122TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
3126 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128
3129 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3130 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3131 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3132 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3133 AINPUT_MOTION_RANGE_Y, 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_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3137 0.0f, 1.0f, 0.0f, 0.0f));
3138}
3139
3140TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003142 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143
3144 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3145
3146 NotifyMotionArgs args;
3147
3148 // Button press.
3149 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003150 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3151 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3153 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3154 ASSERT_EQ(DEVICE_ID, args.deviceId);
3155 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3156 ASSERT_EQ(uint32_t(0), args.policyFlags);
3157 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3158 ASSERT_EQ(0, args.flags);
3159 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3160 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3161 ASSERT_EQ(0, args.edgeFlags);
3162 ASSERT_EQ(uint32_t(1), args.pointerCount);
3163 ASSERT_EQ(0, args.pointerProperties[0].id);
3164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3166 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3167 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3168 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3169 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3170
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3172 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3173 ASSERT_EQ(DEVICE_ID, args.deviceId);
3174 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3175 ASSERT_EQ(uint32_t(0), args.policyFlags);
3176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3177 ASSERT_EQ(0, args.flags);
3178 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3179 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3180 ASSERT_EQ(0, args.edgeFlags);
3181 ASSERT_EQ(uint32_t(1), args.pointerCount);
3182 ASSERT_EQ(0, args.pointerProperties[0].id);
3183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3185 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3186 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3187 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3188 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3189
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003191 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3192 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3194 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3195 ASSERT_EQ(DEVICE_ID, args.deviceId);
3196 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3197 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3199 ASSERT_EQ(0, args.flags);
3200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3201 ASSERT_EQ(0, args.buttonState);
3202 ASSERT_EQ(0, args.edgeFlags);
3203 ASSERT_EQ(uint32_t(1), args.pointerCount);
3204 ASSERT_EQ(0, args.pointerProperties[0].id);
3205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3207 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3208 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3209 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3210 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3211
3212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3213 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3214 ASSERT_EQ(DEVICE_ID, args.deviceId);
3215 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3216 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3218 ASSERT_EQ(0, args.flags);
3219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3220 ASSERT_EQ(0, args.buttonState);
3221 ASSERT_EQ(0, args.edgeFlags);
3222 ASSERT_EQ(uint32_t(1), args.pointerCount);
3223 ASSERT_EQ(0, args.pointerProperties[0].id);
3224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3226 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3227 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3228 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3229 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3230}
3231
3232TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003234 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235
3236 NotifyMotionArgs args;
3237
3238 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003239 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3240 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3244 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3245
3246 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003247 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3248 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3252 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3253}
3254
3255TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258
3259 NotifyMotionArgs args;
3260
3261 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003262 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3263 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3267 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3268
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3272 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3273
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003275 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3276 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3280 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3281
3282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3285 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3286}
3287
3288TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
3292 NotifyMotionArgs args;
3293
3294 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003295 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3296 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3297 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3298 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3300 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3302 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3303 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3304
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3308 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3309 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3310
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003312 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3313 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3314 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3318 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3319 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3320
3321 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003322 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3323 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3327 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3328
3329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3332 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3333}
3334
3335TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003337 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003339 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3345 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3346 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3348}
3349
3350TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351 addConfigurationProperty("cursor.mode", "navigation");
3352 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003353 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003355 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3357 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3358 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3364
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003365 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3367 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3374
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003375 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3377 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3384
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003385 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3387 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3394}
3395
3396TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003398 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399
3400 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3401 mFakePointerController->setPosition(100, 200);
3402 mFakePointerController->setButtonState(0);
3403
3404 NotifyMotionArgs motionArgs;
3405 NotifyKeyArgs keyArgs;
3406
3407 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003408 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3409 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3411 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3412 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3413 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3415 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3416
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3419 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3420 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3422 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3423
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003424 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3425 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_EQ(0, motionArgs.buttonState);
3429 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3431 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3432
3433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003434 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 ASSERT_EQ(0, motionArgs.buttonState);
3436 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3438 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3439
3440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003442 ASSERT_EQ(0, motionArgs.buttonState);
3443 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3445 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3446
3447 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003448 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3449 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3450 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3452 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3453 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3454 motionArgs.buttonState);
3455 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3456 mFakePointerController->getButtonState());
3457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3458 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3459
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3461 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3462 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3463 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3464 mFakePointerController->getButtonState());
3465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3466 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3467
3468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3470 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3471 motionArgs.buttonState);
3472 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3473 mFakePointerController->getButtonState());
3474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3475 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3476
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003477 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3478 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3482 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3484 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3485
3486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003488 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3489 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3491 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3492
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003493 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3494 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3497 ASSERT_EQ(0, motionArgs.buttonState);
3498 ASSERT_EQ(0, mFakePointerController->getButtonState());
3499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3500 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003501 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3502 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003503
3504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505 ASSERT_EQ(0, motionArgs.buttonState);
3506 ASSERT_EQ(0, mFakePointerController->getButtonState());
3507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3509 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 -08003510
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3512 ASSERT_EQ(0, motionArgs.buttonState);
3513 ASSERT_EQ(0, mFakePointerController->getButtonState());
3514 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3516 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3517
3518 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003519 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3520 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3522 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3523 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003524
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003526 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3528 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3530 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3531
3532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3534 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3535 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3537 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3538
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003539 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3540 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003542 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 ASSERT_EQ(0, motionArgs.buttonState);
3544 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3546 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3547
3548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003550 ASSERT_EQ(0, motionArgs.buttonState);
3551 ASSERT_EQ(0, mFakePointerController->getButtonState());
3552
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3554 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3556 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3557 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3558
3559 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003560 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3561 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3563 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3564 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003565
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3569 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3571 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3572
3573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3575 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3576 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3578 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3579
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003580 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3581 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584 ASSERT_EQ(0, motionArgs.buttonState);
3585 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3587 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 -08003588
3589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3591 ASSERT_EQ(0, motionArgs.buttonState);
3592 ASSERT_EQ(0, mFakePointerController->getButtonState());
3593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3594 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3595
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3597 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3598 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3599
3600 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003601 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3602 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3604 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3605 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003606
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3610 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3612 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3613
3614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3616 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3617 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3619 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3620
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003621 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3622 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003624 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_EQ(0, motionArgs.buttonState);
3626 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3628 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 -08003629
3630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3632 ASSERT_EQ(0, motionArgs.buttonState);
3633 ASSERT_EQ(0, mFakePointerController->getButtonState());
3634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3635 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3636
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3638 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3639 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3640
3641 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003642 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3643 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3645 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3646 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003647
Michael Wrightd02c5b62014-02-10 15:10:22 -08003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003649 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3651 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3653 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3654
3655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3657 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3658 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3660 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3661
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003662 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3663 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_EQ(0, motionArgs.buttonState);
3667 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3669 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 -08003670
3671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3672 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3673 ASSERT_EQ(0, motionArgs.buttonState);
3674 ASSERT_EQ(0, mFakePointerController->getButtonState());
3675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3676 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3677
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3679 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3680 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3681}
3682
3683TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003685 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686
3687 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3688 mFakePointerController->setPosition(100, 200);
3689 mFakePointerController->setButtonState(0);
3690
3691 NotifyMotionArgs args;
3692
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003693 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3694 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3695 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003697 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3698 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3700 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 +01003701 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003702}
3703
3704TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003705 addConfigurationProperty("cursor.mode", "pointer");
3706 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003708
3709 NotifyDeviceResetArgs resetArgs;
3710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3711 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3712 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3713
3714 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3715 mFakePointerController->setPosition(100, 200);
3716 mFakePointerController->setButtonState(0);
3717
3718 NotifyMotionArgs args;
3719
3720 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003721 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3722 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3723 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3725 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3728 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 +01003729 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003730
3731 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003732 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3733 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3735 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3736 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3738 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3740 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3741 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3743 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3744
3745 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003746 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3747 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3749 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3752 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3754 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3755 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3757 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3758
3759 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003760 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3761 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3762 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3764 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3767 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 +01003768 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003769
3770 // Disable pointer capture and check that the device generation got bumped
3771 // and events are generated the usual way.
3772 const uint32_t generation = mFakeContext->getGeneration();
3773 mFakePolicy->setPointerCapture(false);
3774 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3775 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3776
3777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3778 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3779 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3780
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003781 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3782 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3783 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3785 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3788 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 +01003789 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790}
3791
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003792TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003794
Garfield Tan888a6a42020-01-09 11:39:16 -08003795 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003796 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003797 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3798 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003799 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003800 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3801 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3802
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003803 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3804 mFakePointerController->setPosition(100, 200);
3805 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003806
3807 NotifyMotionArgs args;
3808 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3809 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3810 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3812 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3813 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3815 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 +01003816 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003817 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3818}
3819
Michael Wrightd02c5b62014-02-10 15:10:22 -08003820// --- TouchInputMapperTest ---
3821
3822class TouchInputMapperTest : public InputMapperTest {
3823protected:
3824 static const int32_t RAW_X_MIN;
3825 static const int32_t RAW_X_MAX;
3826 static const int32_t RAW_Y_MIN;
3827 static const int32_t RAW_Y_MAX;
3828 static const int32_t RAW_TOUCH_MIN;
3829 static const int32_t RAW_TOUCH_MAX;
3830 static const int32_t RAW_TOOL_MIN;
3831 static const int32_t RAW_TOOL_MAX;
3832 static const int32_t RAW_PRESSURE_MIN;
3833 static const int32_t RAW_PRESSURE_MAX;
3834 static const int32_t RAW_ORIENTATION_MIN;
3835 static const int32_t RAW_ORIENTATION_MAX;
3836 static const int32_t RAW_DISTANCE_MIN;
3837 static const int32_t RAW_DISTANCE_MAX;
3838 static const int32_t RAW_TILT_MIN;
3839 static const int32_t RAW_TILT_MAX;
3840 static const int32_t RAW_ID_MIN;
3841 static const int32_t RAW_ID_MAX;
3842 static const int32_t RAW_SLOT_MIN;
3843 static const int32_t RAW_SLOT_MAX;
3844 static const float X_PRECISION;
3845 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003846 static const float X_PRECISION_VIRTUAL;
3847 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848
3849 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003850 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851
3852 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3853
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003854 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003855 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003856
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857 enum Axes {
3858 POSITION = 1 << 0,
3859 TOUCH = 1 << 1,
3860 TOOL = 1 << 2,
3861 PRESSURE = 1 << 3,
3862 ORIENTATION = 1 << 4,
3863 MINOR = 1 << 5,
3864 ID = 1 << 6,
3865 DISTANCE = 1 << 7,
3866 TILT = 1 << 8,
3867 SLOT = 1 << 9,
3868 TOOL_TYPE = 1 << 10,
3869 };
3870
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003871 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3872 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003873 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003875 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 int32_t toRawX(float displayX);
3877 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003878 float toCookedX(float rawX, float rawY);
3879 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003881 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003883 float toDisplayY(int32_t rawY, int32_t displayHeight);
3884
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885};
3886
3887const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3888const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3889const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3890const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3891const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3892const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3893const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3894const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003895const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3896const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3898const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3899const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3900const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3901const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3902const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3903const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3904const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3905const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3906const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3907const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3908const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003909const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3910 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3911const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3912 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003913const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3914 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915
3916const float TouchInputMapperTest::GEOMETRIC_SCALE =
3917 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3918 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3919
3920const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3921 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3922 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3923};
3924
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003925void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003926 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3927 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003928}
3929
3930void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3931 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3932 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933}
3934
Santos Cordonfa5cf462017-04-05 10:37:00 -07003935void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003936 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
3937 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
3938 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003939}
3940
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003942 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3943 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3944 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3945 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946}
3947
Jason Gerecke489fda82012-09-07 17:19:40 -07003948void TouchInputMapperTest::prepareLocationCalibration() {
3949 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3950}
3951
Michael Wrightd02c5b62014-02-10 15:10:22 -08003952int32_t TouchInputMapperTest::toRawX(float displayX) {
3953 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3954}
3955
3956int32_t TouchInputMapperTest::toRawY(float displayY) {
3957 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3958}
3959
Jason Gerecke489fda82012-09-07 17:19:40 -07003960float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3961 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3962 return rawX;
3963}
3964
3965float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3966 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3967 return rawY;
3968}
3969
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003971 return toDisplayX(rawX, DISPLAY_WIDTH);
3972}
3973
3974float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3975 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976}
3977
3978float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003979 return toDisplayY(rawY, DISPLAY_HEIGHT);
3980}
3981
3982float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3983 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984}
3985
3986
3987// --- SingleTouchInputMapperTest ---
3988
3989class SingleTouchInputMapperTest : public TouchInputMapperTest {
3990protected:
3991 void prepareButtons();
3992 void prepareAxes(int axes);
3993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003994 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3995 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3996 void processUp(SingleTouchInputMapper& mappery);
3997 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3998 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3999 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4000 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4001 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4002 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003};
4004
4005void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004006 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007}
4008
4009void SingleTouchInputMapperTest::prepareAxes(int axes) {
4010 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004011 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4012 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 }
4014 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004015 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4016 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 }
4018 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004019 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4020 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 }
4022 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004023 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4024 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 }
4026 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004027 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4028 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029 }
4030}
4031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004032void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004033 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4034 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4035 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036}
4037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004038void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004039 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4040 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041}
4042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004043void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004044 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045}
4046
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004047void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004048 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049}
4050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004051void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4052 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004053 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054}
4055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004056void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004057 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058}
4059
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004060void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4061 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004062 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4063 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064}
4065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004066void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4067 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004068 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069}
4070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004072 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073}
4074
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 prepareButtons();
4077 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004078 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004080 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081}
4082
4083TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004084 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4085 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086 prepareButtons();
4087 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004088 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004090 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091}
4092
4093TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 prepareButtons();
4095 prepareAxes(POSITION);
4096 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004097 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004099 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100}
4101
4102TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 prepareButtons();
4104 prepareAxes(POSITION);
4105 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004106 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109}
4110
4111TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 addConfigurationProperty("touch.deviceType", "touchScreen");
4113 prepareDisplay(DISPLAY_ORIENTATION_0);
4114 prepareButtons();
4115 prepareAxes(POSITION);
4116 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004117 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118
4119 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004120 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
4122 // Virtual key is down.
4123 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4124 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4125 processDown(mapper, x, y);
4126 processSync(mapper);
4127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004129 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
4131 // Virtual key is up.
4132 processUp(mapper);
4133 processSync(mapper);
4134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004136 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137}
4138
4139TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 addConfigurationProperty("touch.deviceType", "touchScreen");
4141 prepareDisplay(DISPLAY_ORIENTATION_0);
4142 prepareButtons();
4143 prepareAxes(POSITION);
4144 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004145 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146
4147 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004148 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149
4150 // Virtual key is down.
4151 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4152 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4153 processDown(mapper, x, y);
4154 processSync(mapper);
4155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004157 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158
4159 // Virtual key is up.
4160 processUp(mapper);
4161 processSync(mapper);
4162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004164 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165}
4166
4167TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 addConfigurationProperty("touch.deviceType", "touchScreen");
4169 prepareDisplay(DISPLAY_ORIENTATION_0);
4170 prepareButtons();
4171 prepareAxes(POSITION);
4172 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004173 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174
4175 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4176 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004177 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 ASSERT_TRUE(flags[0]);
4179 ASSERT_FALSE(flags[1]);
4180}
4181
4182TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 addConfigurationProperty("touch.deviceType", "touchScreen");
4184 prepareDisplay(DISPLAY_ORIENTATION_0);
4185 prepareButtons();
4186 prepareAxes(POSITION);
4187 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004188 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189
4190 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4191
4192 NotifyKeyArgs args;
4193
4194 // Press virtual key.
4195 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4196 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4197 processDown(mapper, x, y);
4198 processSync(mapper);
4199
4200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4201 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4202 ASSERT_EQ(DEVICE_ID, args.deviceId);
4203 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4204 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4206 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4207 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4208 ASSERT_EQ(KEY_HOME, args.scanCode);
4209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4210 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4211
4212 // Release virtual key.
4213 processUp(mapper);
4214 processSync(mapper);
4215
4216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4217 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4218 ASSERT_EQ(DEVICE_ID, args.deviceId);
4219 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4220 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4221 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4222 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4223 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4224 ASSERT_EQ(KEY_HOME, args.scanCode);
4225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4226 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4227
4228 // Should not have sent any motions.
4229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4230}
4231
4232TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 addConfigurationProperty("touch.deviceType", "touchScreen");
4234 prepareDisplay(DISPLAY_ORIENTATION_0);
4235 prepareButtons();
4236 prepareAxes(POSITION);
4237 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004238 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239
4240 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4241
4242 NotifyKeyArgs keyArgs;
4243
4244 // Press virtual key.
4245 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4246 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4247 processDown(mapper, x, y);
4248 processSync(mapper);
4249
4250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4251 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4252 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4253 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4254 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4255 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4256 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4257 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4258 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4259 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4260 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4261
4262 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4263 // into the display area.
4264 y -= 100;
4265 processMove(mapper, x, y);
4266 processSync(mapper);
4267
4268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4269 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4270 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4271 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4272 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4273 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4274 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4275 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4276 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4277 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4279 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4280
4281 NotifyMotionArgs motionArgs;
4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4285 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4286 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4287 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4288 ASSERT_EQ(0, motionArgs.flags);
4289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4290 ASSERT_EQ(0, motionArgs.buttonState);
4291 ASSERT_EQ(0, motionArgs.edgeFlags);
4292 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4293 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4296 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4297 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4298 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4299 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4300
4301 // Keep moving out of bounds. Should generate a pointer move.
4302 y -= 50;
4303 processMove(mapper, x, y);
4304 processSync(mapper);
4305
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4308 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4309 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4310 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4312 ASSERT_EQ(0, motionArgs.flags);
4313 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4314 ASSERT_EQ(0, motionArgs.buttonState);
4315 ASSERT_EQ(0, motionArgs.edgeFlags);
4316 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4317 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4320 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4321 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4322 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4324
4325 // Release out of bounds. Should generate a pointer up.
4326 processUp(mapper);
4327 processSync(mapper);
4328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4331 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4332 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4333 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4334 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4335 ASSERT_EQ(0, motionArgs.flags);
4336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4337 ASSERT_EQ(0, motionArgs.buttonState);
4338 ASSERT_EQ(0, motionArgs.edgeFlags);
4339 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4343 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4344 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4345 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4347
4348 // Should not have sent any more keys or motions.
4349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4351}
4352
4353TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 addConfigurationProperty("touch.deviceType", "touchScreen");
4355 prepareDisplay(DISPLAY_ORIENTATION_0);
4356 prepareButtons();
4357 prepareAxes(POSITION);
4358 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004359 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
4361 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4362
4363 NotifyMotionArgs motionArgs;
4364
4365 // Initially go down out of bounds.
4366 int32_t x = -10;
4367 int32_t y = -10;
4368 processDown(mapper, x, y);
4369 processSync(mapper);
4370
4371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4372
4373 // Move into the display area. Should generate a pointer down.
4374 x = 50;
4375 y = 75;
4376 processMove(mapper, x, y);
4377 processSync(mapper);
4378
4379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4381 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4382 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4383 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4385 ASSERT_EQ(0, motionArgs.flags);
4386 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4387 ASSERT_EQ(0, motionArgs.buttonState);
4388 ASSERT_EQ(0, motionArgs.edgeFlags);
4389 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4390 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4393 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4394 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4395 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4397
4398 // Release. Should generate a pointer up.
4399 processUp(mapper);
4400 processSync(mapper);
4401
4402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4407 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4408 ASSERT_EQ(0, motionArgs.flags);
4409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4410 ASSERT_EQ(0, motionArgs.buttonState);
4411 ASSERT_EQ(0, motionArgs.edgeFlags);
4412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4416 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4417 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4418 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4420
4421 // Should not have sent any more keys or motions.
4422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4424}
4425
Santos Cordonfa5cf462017-04-05 10:37:00 -07004426TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004427 addConfigurationProperty("touch.deviceType", "touchScreen");
4428 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4429
4430 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4431 prepareButtons();
4432 prepareAxes(POSITION);
4433 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004435
4436 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4437
4438 NotifyMotionArgs motionArgs;
4439
4440 // Down.
4441 int32_t x = 100;
4442 int32_t y = 125;
4443 processDown(mapper, x, y);
4444 processSync(mapper);
4445
4446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4447 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4448 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4449 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4450 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4451 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4453 ASSERT_EQ(0, motionArgs.flags);
4454 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4455 ASSERT_EQ(0, motionArgs.buttonState);
4456 ASSERT_EQ(0, motionArgs.edgeFlags);
4457 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4458 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4461 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4462 1, 0, 0, 0, 0, 0, 0, 0));
4463 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4464 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4466
4467 // Move.
4468 x += 50;
4469 y += 75;
4470 processMove(mapper, x, y);
4471 processSync(mapper);
4472
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4475 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4476 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4480 ASSERT_EQ(0, motionArgs.flags);
4481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4482 ASSERT_EQ(0, motionArgs.buttonState);
4483 ASSERT_EQ(0, motionArgs.edgeFlags);
4484 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4488 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4489 1, 0, 0, 0, 0, 0, 0, 0));
4490 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4491 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4492 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4493
4494 // Up.
4495 processUp(mapper);
4496 processSync(mapper);
4497
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4500 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4501 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4502 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4503 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4504 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4505 ASSERT_EQ(0, motionArgs.flags);
4506 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4507 ASSERT_EQ(0, motionArgs.buttonState);
4508 ASSERT_EQ(0, motionArgs.edgeFlags);
4509 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4510 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4513 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4514 1, 0, 0, 0, 0, 0, 0, 0));
4515 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4516 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4517 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4518
4519 // Should not have sent any more keys or motions.
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4522}
4523
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 addConfigurationProperty("touch.deviceType", "touchScreen");
4526 prepareDisplay(DISPLAY_ORIENTATION_0);
4527 prepareButtons();
4528 prepareAxes(POSITION);
4529 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004530 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531
4532 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4533
4534 NotifyMotionArgs motionArgs;
4535
4536 // Down.
4537 int32_t x = 100;
4538 int32_t y = 125;
4539 processDown(mapper, x, y);
4540 processSync(mapper);
4541
4542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4544 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4545 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4546 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4547 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4548 ASSERT_EQ(0, motionArgs.flags);
4549 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4550 ASSERT_EQ(0, motionArgs.buttonState);
4551 ASSERT_EQ(0, motionArgs.edgeFlags);
4552 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4556 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4557 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4558 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4560
4561 // Move.
4562 x += 50;
4563 y += 75;
4564 processMove(mapper, x, y);
4565 processSync(mapper);
4566
4567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4569 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4570 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4571 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4573 ASSERT_EQ(0, motionArgs.flags);
4574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4575 ASSERT_EQ(0, motionArgs.buttonState);
4576 ASSERT_EQ(0, motionArgs.edgeFlags);
4577 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4578 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4581 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4582 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4583 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4585
4586 // Up.
4587 processUp(mapper);
4588 processSync(mapper);
4589
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4592 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4593 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4594 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4596 ASSERT_EQ(0, motionArgs.flags);
4597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4598 ASSERT_EQ(0, motionArgs.buttonState);
4599 ASSERT_EQ(0, motionArgs.edgeFlags);
4600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4604 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4605 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4606 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4608
4609 // Should not have sent any more keys or motions.
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4612}
4613
4614TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 addConfigurationProperty("touch.deviceType", "touchScreen");
4616 prepareButtons();
4617 prepareAxes(POSITION);
4618 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004619 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620
4621 NotifyMotionArgs args;
4622
4623 // Rotation 90.
4624 prepareDisplay(DISPLAY_ORIENTATION_90);
4625 processDown(mapper, toRawX(50), toRawY(75));
4626 processSync(mapper);
4627
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4629 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4630 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4631
4632 processUp(mapper);
4633 processSync(mapper);
4634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4635}
4636
4637TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 addConfigurationProperty("touch.deviceType", "touchScreen");
4639 prepareButtons();
4640 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004641 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642
4643 NotifyMotionArgs args;
4644
4645 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004646 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647 prepareDisplay(DISPLAY_ORIENTATION_0);
4648 processDown(mapper, toRawX(50), toRawY(75));
4649 processSync(mapper);
4650
4651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4652 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4653 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4654
4655 processUp(mapper);
4656 processSync(mapper);
4657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4658
4659 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004660 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 prepareDisplay(DISPLAY_ORIENTATION_90);
4662 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4663 processSync(mapper);
4664
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4666 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4667 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4668
4669 processUp(mapper);
4670 processSync(mapper);
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4672
4673 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004674 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 prepareDisplay(DISPLAY_ORIENTATION_180);
4676 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4677 processSync(mapper);
4678
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4680 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4681 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4682
4683 processUp(mapper);
4684 processSync(mapper);
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4686
4687 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004688 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 prepareDisplay(DISPLAY_ORIENTATION_270);
4690 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4691 processSync(mapper);
4692
4693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4694 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4695 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4696
4697 processUp(mapper);
4698 processSync(mapper);
4699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4700}
4701
4702TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 addConfigurationProperty("touch.deviceType", "touchScreen");
4704 prepareDisplay(DISPLAY_ORIENTATION_0);
4705 prepareButtons();
4706 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004707 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708
4709 // These calculations are based on the input device calibration documentation.
4710 int32_t rawX = 100;
4711 int32_t rawY = 200;
4712 int32_t rawPressure = 10;
4713 int32_t rawToolMajor = 12;
4714 int32_t rawDistance = 2;
4715 int32_t rawTiltX = 30;
4716 int32_t rawTiltY = 110;
4717
4718 float x = toDisplayX(rawX);
4719 float y = toDisplayY(rawY);
4720 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4721 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4722 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4723 float distance = float(rawDistance);
4724
4725 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4726 float tiltScale = M_PI / 180;
4727 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4728 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4729 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4730 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4731
4732 processDown(mapper, rawX, rawY);
4733 processPressure(mapper, rawPressure);
4734 processToolMajor(mapper, rawToolMajor);
4735 processDistance(mapper, rawDistance);
4736 processTilt(mapper, rawTiltX, rawTiltY);
4737 processSync(mapper);
4738
4739 NotifyMotionArgs args;
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4742 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4743 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4744}
4745
Jason Gerecke489fda82012-09-07 17:19:40 -07004746TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004747 addConfigurationProperty("touch.deviceType", "touchScreen");
4748 prepareDisplay(DISPLAY_ORIENTATION_0);
4749 prepareLocationCalibration();
4750 prepareButtons();
4751 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004752 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004753
4754 int32_t rawX = 100;
4755 int32_t rawY = 200;
4756
4757 float x = toDisplayX(toCookedX(rawX, rawY));
4758 float y = toDisplayY(toCookedY(rawX, rawY));
4759
4760 processDown(mapper, rawX, rawY);
4761 processSync(mapper);
4762
4763 NotifyMotionArgs args;
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4766 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4767}
4768
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 addConfigurationProperty("touch.deviceType", "touchScreen");
4771 prepareDisplay(DISPLAY_ORIENTATION_0);
4772 prepareButtons();
4773 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004774 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775
4776 NotifyMotionArgs motionArgs;
4777 NotifyKeyArgs keyArgs;
4778
4779 processDown(mapper, 100, 200);
4780 processSync(mapper);
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4783 ASSERT_EQ(0, motionArgs.buttonState);
4784
4785 // press BTN_LEFT, release BTN_LEFT
4786 processKey(mapper, BTN_LEFT, 1);
4787 processSync(mapper);
4788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4790 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4791
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4794 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4795
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 processKey(mapper, BTN_LEFT, 0);
4797 processSync(mapper);
4798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004804 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805
4806 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4807 processKey(mapper, BTN_RIGHT, 1);
4808 processKey(mapper, BTN_MIDDLE, 1);
4809 processSync(mapper);
4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4812 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4813 motionArgs.buttonState);
4814
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4817 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4818
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4821 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4822 motionArgs.buttonState);
4823
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824 processKey(mapper, BTN_RIGHT, 0);
4825 processSync(mapper);
4826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004827 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004829
4830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833
4834 processKey(mapper, BTN_MIDDLE, 0);
4835 processSync(mapper);
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004837 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004839
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004842 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843
4844 // press BTN_BACK, release BTN_BACK
4845 processKey(mapper, BTN_BACK, 1);
4846 processSync(mapper);
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4848 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4849 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004850
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004853 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4854
4855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4856 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4857 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858
4859 processKey(mapper, BTN_BACK, 0);
4860 processSync(mapper);
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004864
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004867 ASSERT_EQ(0, motionArgs.buttonState);
4868
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4870 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4871 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4872
4873 // press BTN_SIDE, release BTN_SIDE
4874 processKey(mapper, BTN_SIDE, 1);
4875 processSync(mapper);
4876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4877 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4878 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004879
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004882 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4883
4884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4886 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887
4888 processKey(mapper, BTN_SIDE, 0);
4889 processSync(mapper);
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004893
4894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004896 ASSERT_EQ(0, motionArgs.buttonState);
4897
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4899 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4900 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4901
4902 // press BTN_FORWARD, release BTN_FORWARD
4903 processKey(mapper, BTN_FORWARD, 1);
4904 processSync(mapper);
4905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4906 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4907 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004908
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004911 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4912
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4915 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916
4917 processKey(mapper, BTN_FORWARD, 0);
4918 processSync(mapper);
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004925 ASSERT_EQ(0, motionArgs.buttonState);
4926
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4928 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4929 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4930
4931 // press BTN_EXTRA, release BTN_EXTRA
4932 processKey(mapper, BTN_EXTRA, 1);
4933 processSync(mapper);
4934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4935 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4936 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004937
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004940 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4941
4942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4943 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4944 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945
4946 processKey(mapper, BTN_EXTRA, 0);
4947 processSync(mapper);
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004949 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004951
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004954 ASSERT_EQ(0, motionArgs.buttonState);
4955
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4957 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4958 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4959
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4961
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 // press BTN_STYLUS, release BTN_STYLUS
4963 processKey(mapper, BTN_STYLUS, 1);
4964 processSync(mapper);
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004967 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4968
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4970 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4971 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972
4973 processKey(mapper, BTN_STYLUS, 0);
4974 processSync(mapper);
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004978
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004981 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982
4983 // press BTN_STYLUS2, release BTN_STYLUS2
4984 processKey(mapper, BTN_STYLUS2, 1);
4985 processSync(mapper);
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004988 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4989
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4992 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
4994 processKey(mapper, BTN_STYLUS2, 0);
4995 processSync(mapper);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004999
5000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005002 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003
5004 // release touch
5005 processUp(mapper);
5006 processSync(mapper);
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5009 ASSERT_EQ(0, motionArgs.buttonState);
5010}
5011
5012TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013 addConfigurationProperty("touch.deviceType", "touchScreen");
5014 prepareDisplay(DISPLAY_ORIENTATION_0);
5015 prepareButtons();
5016 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018
5019 NotifyMotionArgs motionArgs;
5020
5021 // default tool type is finger
5022 processDown(mapper, 100, 200);
5023 processSync(mapper);
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5025 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5027
5028 // eraser
5029 processKey(mapper, BTN_TOOL_RUBBER, 1);
5030 processSync(mapper);
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5034
5035 // stylus
5036 processKey(mapper, BTN_TOOL_RUBBER, 0);
5037 processKey(mapper, BTN_TOOL_PEN, 1);
5038 processSync(mapper);
5039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5042
5043 // brush
5044 processKey(mapper, BTN_TOOL_PEN, 0);
5045 processKey(mapper, BTN_TOOL_BRUSH, 1);
5046 processSync(mapper);
5047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5050
5051 // pencil
5052 processKey(mapper, BTN_TOOL_BRUSH, 0);
5053 processKey(mapper, BTN_TOOL_PENCIL, 1);
5054 processSync(mapper);
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5058
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005059 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 processKey(mapper, BTN_TOOL_PENCIL, 0);
5061 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5062 processSync(mapper);
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5065 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5066
5067 // mouse
5068 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5069 processKey(mapper, BTN_TOOL_MOUSE, 1);
5070 processSync(mapper);
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5074
5075 // lens
5076 processKey(mapper, BTN_TOOL_MOUSE, 0);
5077 processKey(mapper, BTN_TOOL_LENS, 1);
5078 processSync(mapper);
5079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5082
5083 // double-tap
5084 processKey(mapper, BTN_TOOL_LENS, 0);
5085 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5086 processSync(mapper);
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5090
5091 // triple-tap
5092 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5093 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5094 processSync(mapper);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5098
5099 // quad-tap
5100 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5101 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5102 processSync(mapper);
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5106
5107 // finger
5108 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5109 processKey(mapper, BTN_TOOL_FINGER, 1);
5110 processSync(mapper);
5111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5114
5115 // stylus trumps finger
5116 processKey(mapper, BTN_TOOL_PEN, 1);
5117 processSync(mapper);
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5121
5122 // eraser trumps stylus
5123 processKey(mapper, BTN_TOOL_RUBBER, 1);
5124 processSync(mapper);
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5128
5129 // mouse trumps eraser
5130 processKey(mapper, BTN_TOOL_MOUSE, 1);
5131 processSync(mapper);
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5135
5136 // back to default tool type
5137 processKey(mapper, BTN_TOOL_MOUSE, 0);
5138 processKey(mapper, BTN_TOOL_RUBBER, 0);
5139 processKey(mapper, BTN_TOOL_PEN, 0);
5140 processKey(mapper, BTN_TOOL_FINGER, 0);
5141 processSync(mapper);
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5145}
5146
5147TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 addConfigurationProperty("touch.deviceType", "touchScreen");
5149 prepareDisplay(DISPLAY_ORIENTATION_0);
5150 prepareButtons();
5151 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005152 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005153 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005154
5155 NotifyMotionArgs motionArgs;
5156
5157 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5158 processKey(mapper, BTN_TOOL_FINGER, 1);
5159 processMove(mapper, 100, 200);
5160 processSync(mapper);
5161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5162 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5164 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5165
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5167 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5169 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5170
5171 // move a little
5172 processMove(mapper, 150, 250);
5173 processSync(mapper);
5174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5175 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5177 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5178
5179 // down when BTN_TOUCH is pressed, pressure defaults to 1
5180 processKey(mapper, BTN_TOUCH, 1);
5181 processSync(mapper);
5182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5185 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5186
5187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5188 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5190 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5191
5192 // up when BTN_TOUCH is released, hover restored
5193 processKey(mapper, BTN_TOUCH, 0);
5194 processSync(mapper);
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5198 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5199
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5201 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5203 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5208 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5209
5210 // exit hover when pointer goes away
5211 processKey(mapper, BTN_TOOL_FINGER, 0);
5212 processSync(mapper);
5213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5214 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5216 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5217}
5218
5219TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220 addConfigurationProperty("touch.deviceType", "touchScreen");
5221 prepareDisplay(DISPLAY_ORIENTATION_0);
5222 prepareButtons();
5223 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005224 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225
5226 NotifyMotionArgs motionArgs;
5227
5228 // initially hovering because pressure is 0
5229 processDown(mapper, 100, 200);
5230 processPressure(mapper, 0);
5231 processSync(mapper);
5232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5233 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5235 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5236
5237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5240 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5241
5242 // move a little
5243 processMove(mapper, 150, 250);
5244 processSync(mapper);
5245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5246 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5248 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5249
5250 // down when pressure is non-zero
5251 processPressure(mapper, RAW_PRESSURE_MAX);
5252 processSync(mapper);
5253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5256 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5257
5258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5261 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5262
5263 // up when pressure becomes 0, hover restored
5264 processPressure(mapper, 0);
5265 processSync(mapper);
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5269 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5270
5271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5274 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5275
5276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5277 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5279 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5280
5281 // exit hover when pointer goes away
5282 processUp(mapper);
5283 processSync(mapper);
5284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5287 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5288}
5289
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290// --- MultiTouchInputMapperTest ---
5291
5292class MultiTouchInputMapperTest : public TouchInputMapperTest {
5293protected:
5294 void prepareAxes(int axes);
5295
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005296 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5297 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5298 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5299 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5300 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5301 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5302 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5303 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5304 void processId(MultiTouchInputMapper& mapper, int32_t id);
5305 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5306 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5307 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5308 void processMTSync(MultiTouchInputMapper& mapper);
5309 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310};
5311
5312void MultiTouchInputMapperTest::prepareAxes(int axes) {
5313 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005314 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5315 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316 }
5317 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005318 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5319 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005321 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5322 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 }
5324 }
5325 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005326 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5327 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5330 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 }
5332 }
5333 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005334 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5335 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 }
5337 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005338 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5339 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 }
5341 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005342 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5343 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344 }
5345 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005346 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5347 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 }
5349 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005350 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5351 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352 }
5353 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005354 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355 }
5356}
5357
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005358void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5359 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005360 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5361 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362}
5363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5365 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005366 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367}
5368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005369void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5370 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005371 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372}
5373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005375 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376}
5377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005378void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005379 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380}
5381
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005382void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5383 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005384 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385}
5386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005388 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389}
5390
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005391void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005392 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393}
5394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005396 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397}
5398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005399void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005400 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401}
5402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005403void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005404 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405}
5406
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005407void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5408 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005409 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410}
5411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005412void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005413 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414}
5415
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005417 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418}
5419
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421 addConfigurationProperty("touch.deviceType", "touchScreen");
5422 prepareDisplay(DISPLAY_ORIENTATION_0);
5423 prepareAxes(POSITION);
5424 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426
5427 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5428
5429 NotifyMotionArgs motionArgs;
5430
5431 // Two fingers down at once.
5432 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5433 processPosition(mapper, x1, y1);
5434 processMTSync(mapper);
5435 processPosition(mapper, x2, y2);
5436 processMTSync(mapper);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5441 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5445 ASSERT_EQ(0, motionArgs.flags);
5446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5447 ASSERT_EQ(0, motionArgs.buttonState);
5448 ASSERT_EQ(0, motionArgs.edgeFlags);
5449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5454 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5455 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5456 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5460 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5461 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5462 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5463 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5464 motionArgs.action);
5465 ASSERT_EQ(0, motionArgs.flags);
5466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5467 ASSERT_EQ(0, motionArgs.buttonState);
5468 ASSERT_EQ(0, motionArgs.edgeFlags);
5469 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5472 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5475 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5477 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5478 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5479 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5481
5482 // Move.
5483 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5484 processPosition(mapper, x1, y1);
5485 processMTSync(mapper);
5486 processPosition(mapper, x2, y2);
5487 processMTSync(mapper);
5488 processSync(mapper);
5489
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5491 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5492 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5493 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5494 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5496 ASSERT_EQ(0, motionArgs.flags);
5497 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5498 ASSERT_EQ(0, motionArgs.buttonState);
5499 ASSERT_EQ(0, motionArgs.edgeFlags);
5500 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5501 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5503 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5506 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5508 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5509 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5510 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5511 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5512
5513 // First finger up.
5514 x2 += 15; y2 -= 20;
5515 processPosition(mapper, x2, y2);
5516 processMTSync(mapper);
5517 processSync(mapper);
5518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5521 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5522 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5523 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5525 motionArgs.action);
5526 ASSERT_EQ(0, motionArgs.flags);
5527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5528 ASSERT_EQ(0, motionArgs.buttonState);
5529 ASSERT_EQ(0, motionArgs.edgeFlags);
5530 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5531 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5533 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5536 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5538 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5539 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5540 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5541 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5545 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5546 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5547 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5549 ASSERT_EQ(0, motionArgs.flags);
5550 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5551 ASSERT_EQ(0, motionArgs.buttonState);
5552 ASSERT_EQ(0, motionArgs.edgeFlags);
5553 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5554 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5558 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5559 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5560 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5561
5562 // Move.
5563 x2 += 20; y2 -= 25;
5564 processPosition(mapper, x2, y2);
5565 processMTSync(mapper);
5566 processSync(mapper);
5567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5570 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5571 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5572 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5574 ASSERT_EQ(0, motionArgs.flags);
5575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5576 ASSERT_EQ(0, motionArgs.buttonState);
5577 ASSERT_EQ(0, motionArgs.edgeFlags);
5578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5579 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5582 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5583 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5584 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5586
5587 // New finger down.
5588 int32_t x3 = 700, y3 = 300;
5589 processPosition(mapper, x2, y2);
5590 processMTSync(mapper);
5591 processPosition(mapper, x3, y3);
5592 processMTSync(mapper);
5593 processSync(mapper);
5594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5596 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5597 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5598 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5599 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5601 motionArgs.action);
5602 ASSERT_EQ(0, motionArgs.flags);
5603 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5604 ASSERT_EQ(0, motionArgs.buttonState);
5605 ASSERT_EQ(0, motionArgs.edgeFlags);
5606 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5607 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5609 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5612 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5614 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5615 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5616 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5618
5619 // Second finger up.
5620 x3 += 30; y3 -= 20;
5621 processPosition(mapper, x3, y3);
5622 processMTSync(mapper);
5623 processSync(mapper);
5624
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5626 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5627 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5628 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5629 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5631 motionArgs.action);
5632 ASSERT_EQ(0, motionArgs.flags);
5633 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5634 ASSERT_EQ(0, motionArgs.buttonState);
5635 ASSERT_EQ(0, motionArgs.edgeFlags);
5636 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5639 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5642 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5644 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5645 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5646 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5648
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5650 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5651 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5652 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5653 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5655 ASSERT_EQ(0, motionArgs.flags);
5656 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5657 ASSERT_EQ(0, motionArgs.buttonState);
5658 ASSERT_EQ(0, motionArgs.edgeFlags);
5659 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5660 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5663 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5664 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5665 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5667
5668 // Last finger up.
5669 processMTSync(mapper);
5670 processSync(mapper);
5671
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5673 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5674 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5675 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5676 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5678 ASSERT_EQ(0, motionArgs.flags);
5679 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5680 ASSERT_EQ(0, motionArgs.buttonState);
5681 ASSERT_EQ(0, motionArgs.edgeFlags);
5682 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5683 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5686 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5687 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5688 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5689 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5690
5691 // Should not have sent any more keys or motions.
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5694}
5695
5696TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 addConfigurationProperty("touch.deviceType", "touchScreen");
5698 prepareDisplay(DISPLAY_ORIENTATION_0);
5699 prepareAxes(POSITION | ID);
5700 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005701 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702
5703 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5704
5705 NotifyMotionArgs motionArgs;
5706
5707 // Two fingers down at once.
5708 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5709 processPosition(mapper, x1, y1);
5710 processId(mapper, 1);
5711 processMTSync(mapper);
5712 processPosition(mapper, x2, y2);
5713 processId(mapper, 2);
5714 processMTSync(mapper);
5715 processSync(mapper);
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5719 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5723 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5724
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5726 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5727 motionArgs.action);
5728 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5729 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5731 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5734 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5736 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5737
5738 // Move.
5739 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5740 processPosition(mapper, x1, y1);
5741 processId(mapper, 1);
5742 processMTSync(mapper);
5743 processPosition(mapper, x2, y2);
5744 processId(mapper, 2);
5745 processMTSync(mapper);
5746 processSync(mapper);
5747
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5750 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5751 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5753 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5756 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5758 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5759
5760 // First finger up.
5761 x2 += 15; y2 -= 20;
5762 processPosition(mapper, x2, y2);
5763 processId(mapper, 2);
5764 processMTSync(mapper);
5765 processSync(mapper);
5766
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5768 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5769 motionArgs.action);
5770 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5771 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5773 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5776 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5778 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5779
5780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5782 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5783 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5787
5788 // Move.
5789 x2 += 20; y2 -= 25;
5790 processPosition(mapper, x2, y2);
5791 processId(mapper, 2);
5792 processMTSync(mapper);
5793 processSync(mapper);
5794
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5797 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5798 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5801 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5802
5803 // New finger down.
5804 int32_t x3 = 700, y3 = 300;
5805 processPosition(mapper, x2, y2);
5806 processId(mapper, 2);
5807 processMTSync(mapper);
5808 processPosition(mapper, x3, y3);
5809 processId(mapper, 3);
5810 processMTSync(mapper);
5811 processSync(mapper);
5812
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5814 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5815 motionArgs.action);
5816 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5817 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5819 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5822 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5824 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5825
5826 // Second finger up.
5827 x3 += 30; y3 -= 20;
5828 processPosition(mapper, x3, y3);
5829 processId(mapper, 3);
5830 processMTSync(mapper);
5831 processSync(mapper);
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5835 motionArgs.action);
5836 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5837 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5839 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5842 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5844 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5845
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5848 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5849 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5852 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5853
5854 // Last finger up.
5855 processMTSync(mapper);
5856 processSync(mapper);
5857
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5859 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5860 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5861 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5864 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5865
5866 // Should not have sent any more keys or motions.
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5869}
5870
5871TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872 addConfigurationProperty("touch.deviceType", "touchScreen");
5873 prepareDisplay(DISPLAY_ORIENTATION_0);
5874 prepareAxes(POSITION | ID | SLOT);
5875 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005876 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877
5878 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5879
5880 NotifyMotionArgs motionArgs;
5881
5882 // Two fingers down at once.
5883 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5884 processPosition(mapper, x1, y1);
5885 processId(mapper, 1);
5886 processSlot(mapper, 1);
5887 processPosition(mapper, x2, y2);
5888 processId(mapper, 2);
5889 processSync(mapper);
5890
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5893 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5897 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5898
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5901 motionArgs.action);
5902 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5903 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5905 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5908 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5910 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5911
5912 // Move.
5913 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5914 processSlot(mapper, 0);
5915 processPosition(mapper, x1, y1);
5916 processSlot(mapper, 1);
5917 processPosition(mapper, x2, y2);
5918 processSync(mapper);
5919
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5922 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5923 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5925 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5928 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5930 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5931
5932 // First finger up.
5933 x2 += 15; y2 -= 20;
5934 processSlot(mapper, 0);
5935 processId(mapper, -1);
5936 processSlot(mapper, 1);
5937 processPosition(mapper, x2, y2);
5938 processSync(mapper);
5939
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5942 motionArgs.action);
5943 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5944 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5946 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5949 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5951 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5952
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5955 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5956 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5959 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5960
5961 // Move.
5962 x2 += 20; y2 -= 25;
5963 processPosition(mapper, x2, y2);
5964 processSync(mapper);
5965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5968 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5969 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5972 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5973
5974 // New finger down.
5975 int32_t x3 = 700, y3 = 300;
5976 processPosition(mapper, x2, y2);
5977 processSlot(mapper, 0);
5978 processId(mapper, 3);
5979 processPosition(mapper, x3, y3);
5980 processSync(mapper);
5981
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5984 motionArgs.action);
5985 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5986 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5988 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5991 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5993 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5994
5995 // Second finger up.
5996 x3 += 30; y3 -= 20;
5997 processSlot(mapper, 1);
5998 processId(mapper, -1);
5999 processSlot(mapper, 0);
6000 processPosition(mapper, x3, y3);
6001 processSync(mapper);
6002
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6004 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6005 motionArgs.action);
6006 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6009 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6012 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6014 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6015
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6018 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6019 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6022 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6023
6024 // Last finger up.
6025 processId(mapper, -1);
6026 processSync(mapper);
6027
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6030 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6031 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6034 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6035
6036 // Should not have sent any more keys or motions.
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6039}
6040
6041TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042 addConfigurationProperty("touch.deviceType", "touchScreen");
6043 prepareDisplay(DISPLAY_ORIENTATION_0);
6044 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006045 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046
6047 // These calculations are based on the input device calibration documentation.
6048 int32_t rawX = 100;
6049 int32_t rawY = 200;
6050 int32_t rawTouchMajor = 7;
6051 int32_t rawTouchMinor = 6;
6052 int32_t rawToolMajor = 9;
6053 int32_t rawToolMinor = 8;
6054 int32_t rawPressure = 11;
6055 int32_t rawDistance = 0;
6056 int32_t rawOrientation = 3;
6057 int32_t id = 5;
6058
6059 float x = toDisplayX(rawX);
6060 float y = toDisplayY(rawY);
6061 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6062 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6063 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6064 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6065 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6066 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6067 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6068 float distance = float(rawDistance);
6069
6070 processPosition(mapper, rawX, rawY);
6071 processTouchMajor(mapper, rawTouchMajor);
6072 processTouchMinor(mapper, rawTouchMinor);
6073 processToolMajor(mapper, rawToolMajor);
6074 processToolMinor(mapper, rawToolMinor);
6075 processPressure(mapper, rawPressure);
6076 processOrientation(mapper, rawOrientation);
6077 processDistance(mapper, rawDistance);
6078 processId(mapper, id);
6079 processMTSync(mapper);
6080 processSync(mapper);
6081
6082 NotifyMotionArgs args;
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6084 ASSERT_EQ(0, args.pointerProperties[0].id);
6085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6086 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6087 orientation, distance));
6088}
6089
6090TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 addConfigurationProperty("touch.deviceType", "touchScreen");
6092 prepareDisplay(DISPLAY_ORIENTATION_0);
6093 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6094 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006095 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096
6097 // These calculations are based on the input device calibration documentation.
6098 int32_t rawX = 100;
6099 int32_t rawY = 200;
6100 int32_t rawTouchMajor = 140;
6101 int32_t rawTouchMinor = 120;
6102 int32_t rawToolMajor = 180;
6103 int32_t rawToolMinor = 160;
6104
6105 float x = toDisplayX(rawX);
6106 float y = toDisplayY(rawY);
6107 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6108 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6109 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6110 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6111 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6112
6113 processPosition(mapper, rawX, rawY);
6114 processTouchMajor(mapper, rawTouchMajor);
6115 processTouchMinor(mapper, rawTouchMinor);
6116 processToolMajor(mapper, rawToolMajor);
6117 processToolMinor(mapper, rawToolMinor);
6118 processMTSync(mapper);
6119 processSync(mapper);
6120
6121 NotifyMotionArgs args;
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6124 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6125}
6126
6127TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128 addConfigurationProperty("touch.deviceType", "touchScreen");
6129 prepareDisplay(DISPLAY_ORIENTATION_0);
6130 prepareAxes(POSITION | TOUCH | TOOL);
6131 addConfigurationProperty("touch.size.calibration", "diameter");
6132 addConfigurationProperty("touch.size.scale", "10");
6133 addConfigurationProperty("touch.size.bias", "160");
6134 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006135 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136
6137 // These calculations are based on the input device calibration documentation.
6138 // Note: We only provide a single common touch/tool value because the device is assumed
6139 // not to emit separate values for each pointer (isSummed = 1).
6140 int32_t rawX = 100;
6141 int32_t rawY = 200;
6142 int32_t rawX2 = 150;
6143 int32_t rawY2 = 250;
6144 int32_t rawTouchMajor = 5;
6145 int32_t rawToolMajor = 8;
6146
6147 float x = toDisplayX(rawX);
6148 float y = toDisplayY(rawY);
6149 float x2 = toDisplayX(rawX2);
6150 float y2 = toDisplayY(rawY2);
6151 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6152 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6153 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6154
6155 processPosition(mapper, rawX, rawY);
6156 processTouchMajor(mapper, rawTouchMajor);
6157 processToolMajor(mapper, rawToolMajor);
6158 processMTSync(mapper);
6159 processPosition(mapper, rawX2, rawY2);
6160 processTouchMajor(mapper, rawTouchMajor);
6161 processToolMajor(mapper, rawToolMajor);
6162 processMTSync(mapper);
6163 processSync(mapper);
6164
6165 NotifyMotionArgs args;
6166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6171 args.action);
6172 ASSERT_EQ(size_t(2), args.pointerCount);
6173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6174 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6176 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6177}
6178
6179TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 addConfigurationProperty("touch.deviceType", "touchScreen");
6181 prepareDisplay(DISPLAY_ORIENTATION_0);
6182 prepareAxes(POSITION | TOUCH | TOOL);
6183 addConfigurationProperty("touch.size.calibration", "area");
6184 addConfigurationProperty("touch.size.scale", "43");
6185 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006186 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187
6188 // These calculations are based on the input device calibration documentation.
6189 int32_t rawX = 100;
6190 int32_t rawY = 200;
6191 int32_t rawTouchMajor = 5;
6192 int32_t rawToolMajor = 8;
6193
6194 float x = toDisplayX(rawX);
6195 float y = toDisplayY(rawY);
6196 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6197 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6198 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6199
6200 processPosition(mapper, rawX, rawY);
6201 processTouchMajor(mapper, rawTouchMajor);
6202 processToolMajor(mapper, rawToolMajor);
6203 processMTSync(mapper);
6204 processSync(mapper);
6205
6206 NotifyMotionArgs args;
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6209 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6210}
6211
6212TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213 addConfigurationProperty("touch.deviceType", "touchScreen");
6214 prepareDisplay(DISPLAY_ORIENTATION_0);
6215 prepareAxes(POSITION | PRESSURE);
6216 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6217 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006218 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219
Michael Wrightaa449c92017-12-13 21:21:43 +00006220 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006221 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006222 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6223 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6224 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6225
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 // These calculations are based on the input device calibration documentation.
6227 int32_t rawX = 100;
6228 int32_t rawY = 200;
6229 int32_t rawPressure = 60;
6230
6231 float x = toDisplayX(rawX);
6232 float y = toDisplayY(rawY);
6233 float pressure = float(rawPressure) * 0.01f;
6234
6235 processPosition(mapper, rawX, rawY);
6236 processPressure(mapper, rawPressure);
6237 processMTSync(mapper);
6238 processSync(mapper);
6239
6240 NotifyMotionArgs args;
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6243 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6244}
6245
6246TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 addConfigurationProperty("touch.deviceType", "touchScreen");
6248 prepareDisplay(DISPLAY_ORIENTATION_0);
6249 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251
6252 NotifyMotionArgs motionArgs;
6253 NotifyKeyArgs keyArgs;
6254
6255 processId(mapper, 1);
6256 processPosition(mapper, 100, 200);
6257 processSync(mapper);
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6260 ASSERT_EQ(0, motionArgs.buttonState);
6261
6262 // press BTN_LEFT, release BTN_LEFT
6263 processKey(mapper, BTN_LEFT, 1);
6264 processSync(mapper);
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6267 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6268
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6271 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6272
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273 processKey(mapper, BTN_LEFT, 0);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006278
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006281 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6284 processKey(mapper, BTN_RIGHT, 1);
6285 processKey(mapper, BTN_MIDDLE, 1);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6289 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6290 motionArgs.buttonState);
6291
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6294 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6295
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6298 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6299 motionArgs.buttonState);
6300
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 processKey(mapper, BTN_RIGHT, 0);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310
6311 processKey(mapper, BTN_MIDDLE, 0);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 // press BTN_BACK, release BTN_BACK
6322 processKey(mapper, BTN_BACK, 1);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6325 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6326 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006327
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6334 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335
6336 processKey(mapper, BTN_BACK, 0);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344 ASSERT_EQ(0, motionArgs.buttonState);
6345
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6347 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6348 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6349
6350 // press BTN_SIDE, release BTN_SIDE
6351 processKey(mapper, BTN_SIDE, 1);
6352 processSync(mapper);
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6354 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6355 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006356
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6363 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364
6365 processKey(mapper, BTN_SIDE, 0);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006369 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373 ASSERT_EQ(0, motionArgs.buttonState);
6374
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6376 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6377 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6378
6379 // press BTN_FORWARD, release BTN_FORWARD
6380 processKey(mapper, BTN_FORWARD, 1);
6381 processSync(mapper);
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6383 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6384 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006385
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6389
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6392 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393
6394 processKey(mapper, BTN_FORWARD, 0);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402 ASSERT_EQ(0, motionArgs.buttonState);
6403
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6405 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6406 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6407
6408 // press BTN_EXTRA, release BTN_EXTRA
6409 processKey(mapper, BTN_EXTRA, 1);
6410 processSync(mapper);
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6412 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6413 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006414
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6418
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6421 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422
6423 processKey(mapper, BTN_EXTRA, 0);
6424 processSync(mapper);
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006426 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006428
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006431 ASSERT_EQ(0, motionArgs.buttonState);
6432
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6434 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6435 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6436
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6438
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 // press BTN_STYLUS, release BTN_STYLUS
6440 processKey(mapper, BTN_STYLUS, 1);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6445
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6448 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449
6450 processKey(mapper, BTN_STYLUS, 0);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006455
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006459
6460 // press BTN_STYLUS2, release BTN_STYLUS2
6461 processKey(mapper, BTN_STYLUS2, 1);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006465 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6466
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6469 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470
6471 processKey(mapper, BTN_STYLUS2, 0);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006476
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
6481 // release touch
6482 processId(mapper, -1);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6486 ASSERT_EQ(0, motionArgs.buttonState);
6487}
6488
6489TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 addConfigurationProperty("touch.deviceType", "touchScreen");
6491 prepareDisplay(DISPLAY_ORIENTATION_0);
6492 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006494
6495 NotifyMotionArgs motionArgs;
6496
6497 // default tool type is finger
6498 processId(mapper, 1);
6499 processPosition(mapper, 100, 200);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6504
6505 // eraser
6506 processKey(mapper, BTN_TOOL_RUBBER, 1);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6511
6512 // stylus
6513 processKey(mapper, BTN_TOOL_RUBBER, 0);
6514 processKey(mapper, BTN_TOOL_PEN, 1);
6515 processSync(mapper);
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6519
6520 // brush
6521 processKey(mapper, BTN_TOOL_PEN, 0);
6522 processKey(mapper, BTN_TOOL_BRUSH, 1);
6523 processSync(mapper);
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6527
6528 // pencil
6529 processKey(mapper, BTN_TOOL_BRUSH, 0);
6530 processKey(mapper, BTN_TOOL_PENCIL, 1);
6531 processSync(mapper);
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6535
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006536 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537 processKey(mapper, BTN_TOOL_PENCIL, 0);
6538 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6543
6544 // mouse
6545 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6546 processKey(mapper, BTN_TOOL_MOUSE, 1);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6551
6552 // lens
6553 processKey(mapper, BTN_TOOL_MOUSE, 0);
6554 processKey(mapper, BTN_TOOL_LENS, 1);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6559
6560 // double-tap
6561 processKey(mapper, BTN_TOOL_LENS, 0);
6562 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6567
6568 // triple-tap
6569 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6570 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6575
6576 // quad-tap
6577 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6578 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6583
6584 // finger
6585 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6586 processKey(mapper, BTN_TOOL_FINGER, 1);
6587 processSync(mapper);
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6591
6592 // stylus trumps finger
6593 processKey(mapper, BTN_TOOL_PEN, 1);
6594 processSync(mapper);
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6598
6599 // eraser trumps stylus
6600 processKey(mapper, BTN_TOOL_RUBBER, 1);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6605
6606 // mouse trumps eraser
6607 processKey(mapper, BTN_TOOL_MOUSE, 1);
6608 processSync(mapper);
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6612
6613 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6614 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6619
6620 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6621 processToolType(mapper, MT_TOOL_PEN);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6626
6627 // back to default tool type
6628 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6629 processKey(mapper, BTN_TOOL_MOUSE, 0);
6630 processKey(mapper, BTN_TOOL_RUBBER, 0);
6631 processKey(mapper, BTN_TOOL_PEN, 0);
6632 processKey(mapper, BTN_TOOL_FINGER, 0);
6633 processSync(mapper);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6637}
6638
6639TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640 addConfigurationProperty("touch.deviceType", "touchScreen");
6641 prepareDisplay(DISPLAY_ORIENTATION_0);
6642 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006643 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006645
6646 NotifyMotionArgs motionArgs;
6647
6648 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6649 processId(mapper, 1);
6650 processPosition(mapper, 100, 200);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6655 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6656
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6660 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6661
6662 // move a little
6663 processPosition(mapper, 150, 250);
6664 processSync(mapper);
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6666 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6668 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6669
6670 // down when BTN_TOUCH is pressed, pressure defaults to 1
6671 processKey(mapper, BTN_TOUCH, 1);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6676 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6677
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6679 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6681 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6682
6683 // up when BTN_TOUCH is released, hover restored
6684 processKey(mapper, BTN_TOUCH, 0);
6685 processSync(mapper);
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6689 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6690
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6694 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6695
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6699 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6700
6701 // exit hover when pointer goes away
6702 processId(mapper, -1);
6703 processSync(mapper);
6704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6707 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6708}
6709
6710TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006711 addConfigurationProperty("touch.deviceType", "touchScreen");
6712 prepareDisplay(DISPLAY_ORIENTATION_0);
6713 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006714 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006715
6716 NotifyMotionArgs motionArgs;
6717
6718 // initially hovering because pressure is 0
6719 processId(mapper, 1);
6720 processPosition(mapper, 100, 200);
6721 processPressure(mapper, 0);
6722 processSync(mapper);
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6726 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6727
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6731 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6732
6733 // move a little
6734 processPosition(mapper, 150, 250);
6735 processSync(mapper);
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6737 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6739 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6740
6741 // down when pressure becomes non-zero
6742 processPressure(mapper, RAW_PRESSURE_MAX);
6743 processSync(mapper);
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6745 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6747 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6748
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6750 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6752 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6753
6754 // up when pressure becomes 0, hover restored
6755 processPressure(mapper, 0);
6756 processSync(mapper);
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6758 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6760 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6761
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6765 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6766
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6770 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6771
6772 // exit hover when pointer goes away
6773 processId(mapper, -1);
6774 processSync(mapper);
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6778 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6779}
6780
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006781/**
6782 * Set the input device port <--> display port associations, and check that the
6783 * events are routed to the display that matches the display port.
6784 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6785 */
6786TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006787 const std::string usb2 = "USB2";
6788 const uint8_t hdmi1 = 0;
6789 const uint8_t hdmi2 = 1;
6790 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006791 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006792
6793 addConfigurationProperty("touch.deviceType", "touchScreen");
6794 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006795 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006796
6797 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6798 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6799
6800 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6801 // for this input device is specified, and the matching viewport is not present,
6802 // the input device should be disabled (at the mapper level).
6803
6804 // Add viewport for display 2 on hdmi2
6805 prepareSecondaryDisplay(type, hdmi2);
6806 // Send a touch event
6807 processPosition(mapper, 100, 100);
6808 processSync(mapper);
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6810
6811 // Add viewport for display 1 on hdmi1
6812 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6813 // Send a touch event again
6814 processPosition(mapper, 100, 100);
6815 processSync(mapper);
6816
6817 NotifyMotionArgs args;
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6819 ASSERT_EQ(DISPLAY_ID, args.displayId);
6820}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006822TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006823 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006824 std::shared_ptr<FakePointerController> fakePointerController =
6825 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006826 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006827 fakePointerController->setPosition(100, 200);
6828 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006829 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6830
Garfield Tan888a6a42020-01-09 11:39:16 -08006831 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006832 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006833
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006834 prepareDisplay(DISPLAY_ORIENTATION_0);
6835 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006836 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006837
6838 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006839 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006840
6841 NotifyMotionArgs motionArgs;
6842 processPosition(mapper, 100, 100);
6843 processSync(mapper);
6844
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6846 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6847 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6848}
6849
Arthur Hung7c645402019-01-25 17:45:42 +08006850TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6851 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006852 prepareAxes(POSITION | ID | SLOT);
6853 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006854 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006855
6856 // Create the second touch screen device, and enable multi fingers.
6857 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006858 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006859 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006860 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006861 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006862 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006863 std::unique_ptr<InputDevice> device2 =
6864 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006865 identifier);
6866 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6867 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6868 0 /*flat*/, 0 /*fuzz*/);
6869 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6870 0 /*flat*/, 0 /*fuzz*/);
6871 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6872 0 /*flat*/, 0 /*fuzz*/);
6873 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6874 0 /*flat*/, 0 /*fuzz*/);
6875 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6876 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6877 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006878
6879 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006880 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006881 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6882 device2->reset(ARBITRARY_TIME);
6883
6884 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006885 std::shared_ptr<FakePointerController> fakePointerController =
6886 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006887 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6888 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6889
6890 // Setup policy for associated displays and show touches.
6891 const uint8_t hdmi1 = 0;
6892 const uint8_t hdmi2 = 1;
6893 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6894 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6895 mFakePolicy->setShowTouches(true);
6896
6897 // Create displays.
6898 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006899 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006900
6901 // Default device will reconfigure above, need additional reconfiguration for another device.
6902 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006903 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006904
6905 // Two fingers down at default display.
6906 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6907 processPosition(mapper, x1, y1);
6908 processId(mapper, 1);
6909 processSlot(mapper, 1);
6910 processPosition(mapper, x2, y2);
6911 processId(mapper, 2);
6912 processSync(mapper);
6913
6914 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6915 fakePointerController->getSpots().find(DISPLAY_ID);
6916 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6917 ASSERT_EQ(size_t(2), iter->second.size());
6918
6919 // Two fingers down at second display.
6920 processPosition(mapper2, x1, y1);
6921 processId(mapper2, 1);
6922 processSlot(mapper2, 1);
6923 processPosition(mapper2, x2, y2);
6924 processId(mapper2, 2);
6925 processSync(mapper2);
6926
6927 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6928 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6929 ASSERT_EQ(size_t(2), iter->second.size());
6930}
6931
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006932TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006933 prepareAxes(POSITION);
6934 addConfigurationProperty("touch.deviceType", "touchScreen");
6935 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006936 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006937
6938 NotifyMotionArgs motionArgs;
6939 // Unrotated video frame
6940 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6941 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006942 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006943 processPosition(mapper, 100, 200);
6944 processSync(mapper);
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6946 ASSERT_EQ(frames, motionArgs.videoFrames);
6947
6948 // Subsequent touch events should not have any videoframes
6949 // This is implemented separately in FakeEventHub,
6950 // but that should match the behaviour of TouchVideoDevice.
6951 processPosition(mapper, 200, 200);
6952 processSync(mapper);
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6955}
6956
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006957TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006958 prepareAxes(POSITION);
6959 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006961 // Unrotated video frame
6962 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6963 NotifyMotionArgs motionArgs;
6964
6965 // Test all 4 orientations
6966 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6967 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6968 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6969 clearViewports();
6970 prepareDisplay(orientation);
6971 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006972 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006973 processPosition(mapper, 100, 200);
6974 processSync(mapper);
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6976 frames[0].rotate(orientation);
6977 ASSERT_EQ(frames, motionArgs.videoFrames);
6978 }
6979}
6980
6981TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006982 prepareAxes(POSITION);
6983 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006984 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006985 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6986 // so mix these.
6987 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6988 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6989 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6990 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6991 NotifyMotionArgs motionArgs;
6992
6993 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006994 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006995 processPosition(mapper, 100, 200);
6996 processSync(mapper);
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6998 std::for_each(frames.begin(), frames.end(),
6999 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7000 ASSERT_EQ(frames, motionArgs.videoFrames);
7001}
7002
Arthur Hung9da14732019-09-02 16:16:58 +08007003/**
7004 * If we had defined port associations, but the viewport is not ready, the touch device would be
7005 * expected to be disabled, and it should be enabled after the viewport has found.
7006 */
7007TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007008 constexpr uint8_t hdmi2 = 1;
7009 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007010 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007011
7012 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7013
7014 addConfigurationProperty("touch.deviceType", "touchScreen");
7015 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007016 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007017
7018 ASSERT_EQ(mDevice->isEnabled(), false);
7019
7020 // Add display on hdmi2, the device should be enabled and can receive touch event.
7021 prepareSecondaryDisplay(type, hdmi2);
7022 ASSERT_EQ(mDevice->isEnabled(), true);
7023
7024 // Send a touch event.
7025 processPosition(mapper, 100, 100);
7026 processSync(mapper);
7027
7028 NotifyMotionArgs args;
7029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7030 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7031}
7032
Arthur Hung6cd19a42019-08-30 19:04:12 +08007033
Arthur Hung6cd19a42019-08-30 19:04:12 +08007034
Arthur Hung421eb1c2020-01-16 00:09:42 +08007035TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007036 addConfigurationProperty("touch.deviceType", "touchScreen");
7037 prepareDisplay(DISPLAY_ORIENTATION_0);
7038 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007039 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007040
7041 NotifyMotionArgs motionArgs;
7042
7043 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7044 // finger down
7045 processId(mapper, 1);
7046 processPosition(mapper, x1, y1);
7047 processSync(mapper);
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7049 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7051
7052 // finger move
7053 processId(mapper, 1);
7054 processPosition(mapper, x2, y2);
7055 processSync(mapper);
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7059
7060 // finger up.
7061 processId(mapper, -1);
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7064 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7065 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7066
7067 // new finger down
7068 processId(mapper, 1);
7069 processPosition(mapper, x3, y3);
7070 processSync(mapper);
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7072 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7074}
7075
7076/**
arthurhungcc7f9802020-04-30 17:55:40 +08007077 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7078 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007079 */
arthurhungcc7f9802020-04-30 17:55:40 +08007080TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007081 addConfigurationProperty("touch.deviceType", "touchScreen");
7082 prepareDisplay(DISPLAY_ORIENTATION_0);
7083 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007084 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007085
7086 NotifyMotionArgs motionArgs;
7087
7088 // default tool type is finger
7089 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007090 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007091 processPosition(mapper, x1, y1);
7092 processSync(mapper);
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7094 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7096
7097 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7098 processToolType(mapper, MT_TOOL_PALM);
7099 processSync(mapper);
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7101 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7102
7103 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007104 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007105 processPosition(mapper, x2, y2);
7106 processSync(mapper);
7107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7108
7109 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007110 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7113
7114 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007115 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007116 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007117 processPosition(mapper, x3, y3);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7122}
7123
arthurhungbf89a482020-04-17 17:37:55 +08007124/**
arthurhungcc7f9802020-04-30 17:55:40 +08007125 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7126 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007127 */
arthurhungcc7f9802020-04-30 17:55:40 +08007128TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007129 addConfigurationProperty("touch.deviceType", "touchScreen");
7130 prepareDisplay(DISPLAY_ORIENTATION_0);
7131 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7132 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7133
7134 NotifyMotionArgs motionArgs;
7135
7136 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007137 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7138 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007139 processPosition(mapper, x1, y1);
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7144
7145 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007146 processSlot(mapper, SECOND_SLOT);
7147 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007148 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007149 processSync(mapper);
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7151 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7152 motionArgs.action);
7153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7154
7155 // If the tool type of the first finger changes to MT_TOOL_PALM,
7156 // we expect to receive ACTION_POINTER_UP with cancel flag.
7157 processSlot(mapper, FIRST_SLOT);
7158 processId(mapper, FIRST_TRACKING_ID);
7159 processToolType(mapper, MT_TOOL_PALM);
7160 processSync(mapper);
7161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7162 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7163 motionArgs.action);
7164 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7165
7166 // The following MOVE events of second finger should be processed.
7167 processSlot(mapper, SECOND_SLOT);
7168 processId(mapper, SECOND_TRACKING_ID);
7169 processPosition(mapper, x2 + 1, y2 + 1);
7170 processSync(mapper);
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7173 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7174
7175 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7176 // it. Second finger receive move.
7177 processSlot(mapper, FIRST_SLOT);
7178 processId(mapper, INVALID_TRACKING_ID);
7179 processSync(mapper);
7180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7182 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7183
7184 // Second finger keeps moving.
7185 processSlot(mapper, SECOND_SLOT);
7186 processId(mapper, SECOND_TRACKING_ID);
7187 processPosition(mapper, x2 + 2, y2 + 2);
7188 processSync(mapper);
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7191 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7192
7193 // Second finger up.
7194 processId(mapper, INVALID_TRACKING_ID);
7195 processSync(mapper);
7196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7197 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7198 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7199}
7200
7201/**
7202 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7203 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7204 */
7205TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7206 addConfigurationProperty("touch.deviceType", "touchScreen");
7207 prepareDisplay(DISPLAY_ORIENTATION_0);
7208 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7209 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7210
7211 NotifyMotionArgs motionArgs;
7212
7213 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7214 // First finger down.
7215 processId(mapper, FIRST_TRACKING_ID);
7216 processPosition(mapper, x1, y1);
7217 processSync(mapper);
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7221
7222 // Second finger down.
7223 processSlot(mapper, SECOND_SLOT);
7224 processId(mapper, SECOND_TRACKING_ID);
7225 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007226 processSync(mapper);
7227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7228 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7229 motionArgs.action);
7230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7231
arthurhungcc7f9802020-04-30 17:55:40 +08007232 // If the tool type of the first finger changes to MT_TOOL_PALM,
7233 // we expect to receive ACTION_POINTER_UP with cancel flag.
7234 processSlot(mapper, FIRST_SLOT);
7235 processId(mapper, FIRST_TRACKING_ID);
7236 processToolType(mapper, MT_TOOL_PALM);
7237 processSync(mapper);
7238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7239 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7240 motionArgs.action);
7241 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7242
7243 // Second finger keeps moving.
7244 processSlot(mapper, SECOND_SLOT);
7245 processId(mapper, SECOND_TRACKING_ID);
7246 processPosition(mapper, x2 + 1, y2 + 1);
7247 processSync(mapper);
7248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7250
7251 // second finger becomes palm, receive cancel due to only 1 finger is active.
7252 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007253 processToolType(mapper, MT_TOOL_PALM);
7254 processSync(mapper);
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7256 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7257
arthurhungcc7f9802020-04-30 17:55:40 +08007258 // third finger down.
7259 processSlot(mapper, THIRD_SLOT);
7260 processId(mapper, THIRD_TRACKING_ID);
7261 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007262 processPosition(mapper, x3, y3);
7263 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007267 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7268
7269 // third finger move
7270 processId(mapper, THIRD_TRACKING_ID);
7271 processPosition(mapper, x3 + 1, y3 + 1);
7272 processSync(mapper);
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7275
7276 // first finger up, third finger receive move.
7277 processSlot(mapper, FIRST_SLOT);
7278 processId(mapper, INVALID_TRACKING_ID);
7279 processSync(mapper);
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7282 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7283
7284 // second finger up, third finger receive move.
7285 processSlot(mapper, SECOND_SLOT);
7286 processId(mapper, INVALID_TRACKING_ID);
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7290 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7291
7292 // third finger up.
7293 processSlot(mapper, THIRD_SLOT);
7294 processId(mapper, INVALID_TRACKING_ID);
7295 processSync(mapper);
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7298 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7299}
7300
7301/**
7302 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7303 * and the active finger could still be allowed to receive the events
7304 */
7305TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7306 addConfigurationProperty("touch.deviceType", "touchScreen");
7307 prepareDisplay(DISPLAY_ORIENTATION_0);
7308 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7309 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7310
7311 NotifyMotionArgs motionArgs;
7312
7313 // default tool type is finger
7314 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7315 processId(mapper, FIRST_TRACKING_ID);
7316 processPosition(mapper, x1, y1);
7317 processSync(mapper);
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7321
7322 // Second finger down.
7323 processSlot(mapper, SECOND_SLOT);
7324 processId(mapper, SECOND_TRACKING_ID);
7325 processPosition(mapper, x2, y2);
7326 processSync(mapper);
7327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7328 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7329 motionArgs.action);
7330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7331
7332 // If the tool type of the second finger changes to MT_TOOL_PALM,
7333 // we expect to receive ACTION_POINTER_UP with cancel flag.
7334 processId(mapper, SECOND_TRACKING_ID);
7335 processToolType(mapper, MT_TOOL_PALM);
7336 processSync(mapper);
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7338 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7339 motionArgs.action);
7340 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7341
7342 // The following MOVE event should be processed.
7343 processSlot(mapper, FIRST_SLOT);
7344 processId(mapper, FIRST_TRACKING_ID);
7345 processPosition(mapper, x1 + 1, y1 + 1);
7346 processSync(mapper);
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7349 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7350
7351 // second finger up.
7352 processSlot(mapper, SECOND_SLOT);
7353 processId(mapper, INVALID_TRACKING_ID);
7354 processSync(mapper);
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7357
7358 // first finger keep moving
7359 processSlot(mapper, FIRST_SLOT);
7360 processId(mapper, FIRST_TRACKING_ID);
7361 processPosition(mapper, x1 + 2, y1 + 2);
7362 processSync(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7365
7366 // first finger up.
7367 processId(mapper, INVALID_TRACKING_ID);
7368 processSync(mapper);
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7371 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007372}
7373
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007374// --- MultiTouchInputMapperTest_ExternalDevice ---
7375
7376class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7377protected:
7378 virtual void SetUp() override {
7379 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7380 }
7381};
7382
7383/**
7384 * Expect fallback to internal viewport if device is external and external viewport is not present.
7385 */
7386TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7387 prepareAxes(POSITION);
7388 addConfigurationProperty("touch.deviceType", "touchScreen");
7389 prepareDisplay(DISPLAY_ORIENTATION_0);
7390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7391
7392 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7393
7394 NotifyMotionArgs motionArgs;
7395
7396 // Expect the event to be sent to the internal viewport,
7397 // because an external viewport is not present.
7398 processPosition(mapper, 100, 100);
7399 processSync(mapper);
7400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7401 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7402
7403 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007404 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007405 processPosition(mapper, 100, 100);
7406 processSync(mapper);
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7408 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7409}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007410
7411/**
7412 * Test touch should not work if outside of surface.
7413 */
7414class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7415protected:
7416 void halfDisplayToCenterHorizontal(int32_t orientation) {
7417 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007418 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007419
7420 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7421 internalViewport->orientation = orientation;
7422 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7423 internalViewport->logicalLeft = 0;
7424 internalViewport->logicalTop = 0;
7425 internalViewport->logicalRight = DISPLAY_HEIGHT;
7426 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7427
7428 internalViewport->physicalLeft = 0;
7429 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7430 internalViewport->physicalRight = DISPLAY_HEIGHT;
7431 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7432
7433 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7434 internalViewport->deviceHeight = DISPLAY_WIDTH;
7435 } else {
7436 internalViewport->logicalLeft = 0;
7437 internalViewport->logicalTop = 0;
7438 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7439 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7440
7441 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7442 internalViewport->physicalTop = 0;
7443 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7444 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7445
7446 internalViewport->deviceWidth = DISPLAY_WIDTH;
7447 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7448 }
7449
7450 mFakePolicy->updateViewport(internalViewport.value());
7451 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7452 }
7453
7454 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7455 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7456 int32_t yExpected) {
7457 // touch on outside area should not work.
7458 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7459 processSync(mapper);
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7461
7462 // touch on inside area should receive the event.
7463 NotifyMotionArgs args;
7464 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7465 processSync(mapper);
7466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7467 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7468 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7469
7470 // Reset.
7471 mapper.reset(ARBITRARY_TIME);
7472 }
7473};
7474
7475TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7476 addConfigurationProperty("touch.deviceType", "touchScreen");
7477 prepareDisplay(DISPLAY_ORIENTATION_0);
7478 prepareAxes(POSITION);
7479 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7480
7481 // Touch on center of normal display should work.
7482 const int32_t x = DISPLAY_WIDTH / 4;
7483 const int32_t y = DISPLAY_HEIGHT / 2;
7484 processPosition(mapper, toRawX(x), toRawY(y));
7485 processSync(mapper);
7486 NotifyMotionArgs args;
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7489 0.0f, 0.0f, 0.0f, 0.0f));
7490 // Reset.
7491 mapper.reset(ARBITRARY_TIME);
7492
7493 // Let physical display be different to device, and make surface and physical could be 1:1.
7494 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7495
7496 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7497 const int32_t yExpected = y;
7498 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7499}
7500
7501TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7502 addConfigurationProperty("touch.deviceType", "touchScreen");
7503 prepareDisplay(DISPLAY_ORIENTATION_0);
7504 prepareAxes(POSITION);
7505 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7506
7507 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7508 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7509
7510 const int32_t x = DISPLAY_WIDTH / 4;
7511 const int32_t y = DISPLAY_HEIGHT / 2;
7512
7513 // expect x/y = swap x/y then reverse y.
7514 const int32_t xExpected = y;
7515 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7516 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7517}
7518
7519TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7520 addConfigurationProperty("touch.deviceType", "touchScreen");
7521 prepareDisplay(DISPLAY_ORIENTATION_0);
7522 prepareAxes(POSITION);
7523 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7524
7525 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7526 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7527
7528 const int32_t x = DISPLAY_WIDTH / 4;
7529 const int32_t y = DISPLAY_HEIGHT / 2;
7530
7531 // expect x/y = swap x/y then reverse x.
7532 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7533 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7534 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7535}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536} // namespace android