blob: bea99324e08fb51cf19634dd6d544176375c0d9a [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
Kim Low03ea0352020-11-06 12:45:07 -080017#include <BatteryInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070018#include <CursorInputMapper.h>
19#include <InputDevice.h>
20#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.h>
Chris Yef59a2f42020-10-16 12:55:26 -070026#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <SingleTouchInputMapper.h>
28#include <SwitchInputMapper.h>
29#include <TestInputListener.h>
30#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080031#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000032#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070033#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080035#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036#include <math.h>
37
Michael Wright17db18e2020-06-26 20:51:44 +010038#include <memory>
Michael Wrightdde67b82020-10-27 16:09:22 +000039#include "input/DisplayViewport.h"
40#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010041
Michael Wrightd02c5b62014-02-10 15:10:22 -080042namespace android {
43
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070044using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070045using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070046
47// Timeout for waiting for an expected event
48static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
49
Michael Wrightd02c5b62014-02-10 15:10:22 -080050// An arbitrary time value.
51static const nsecs_t ARBITRARY_TIME = 1234;
52
53// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080054static constexpr int32_t DISPLAY_ID = 0;
55static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
56static constexpr int32_t DISPLAY_WIDTH = 480;
57static constexpr int32_t DISPLAY_HEIGHT = 800;
58static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
59static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
60static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070061static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070062static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080063
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t FIRST_SLOT = 0;
65static constexpr int32_t SECOND_SLOT = 1;
66static constexpr int32_t THIRD_SLOT = 2;
67static constexpr int32_t INVALID_TRACKING_ID = -1;
68static constexpr int32_t FIRST_TRACKING_ID = 0;
69static constexpr int32_t SECOND_TRACKING_ID = 1;
70static constexpr int32_t THIRD_TRACKING_ID = 2;
Kim Low03ea0352020-11-06 12:45:07 -080071static constexpr int32_t BATTERY_STATUS = 4;
72static constexpr int32_t BATTERY_CAPACITY = 66;
arthurhungcc7f9802020-04-30 17:55:40 +080073
Michael Wrightd02c5b62014-02-10 15:10:22 -080074// Error tolerance for floating point assertions.
75static const float EPSILON = 0.001f;
76
77template<typename T>
78static inline T min(T a, T b) {
79 return a < b ? a : b;
80}
81
82static inline float avg(float x, float y) {
83 return (x + y) / 2;
84}
85
86
87// --- FakePointerController ---
88
89class FakePointerController : public PointerControllerInterface {
90 bool mHaveBounds;
91 float mMinX, mMinY, mMaxX, mMaxY;
92 float mX, mY;
93 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080094 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
Michael Wrightd02c5b62014-02-10 15:10:22 -080096public:
97 FakePointerController() :
98 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080099 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100 }
101
Michael Wright17db18e2020-06-26 20:51:44 +0100102 virtual ~FakePointerController() {}
103
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104 void setBounds(float minX, float minY, float maxX, float maxY) {
105 mHaveBounds = true;
106 mMinX = minX;
107 mMinY = minY;
108 mMaxX = maxX;
109 mMaxY = maxY;
110 }
111
Chris Yea52ade12020-08-27 16:49:20 -0700112 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113 mX = x;
114 mY = y;
115 }
116
Chris Yea52ade12020-08-27 16:49:20 -0700117 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Chris Yea52ade12020-08-27 16:49:20 -0700119 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120
Chris Yea52ade12020-08-27 16:49:20 -0700121 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122 *outX = mX;
123 *outY = mY;
124 }
125
Chris Yea52ade12020-08-27 16:49:20 -0700126 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800127
Chris Yea52ade12020-08-27 16:49:20 -0700128 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800129 mDisplayId = viewport.displayId;
130 }
131
Arthur Hung7c645402019-01-25 17:45:42 +0800132 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
133 return mSpotsByDisplay;
134 }
135
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136private:
Chris Yea52ade12020-08-27 16:49:20 -0700137 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138 *outMinX = mMinX;
139 *outMinY = mMinY;
140 *outMaxX = mMaxX;
141 *outMaxY = mMaxY;
142 return mHaveBounds;
143 }
144
Chris Yea52ade12020-08-27 16:49:20 -0700145 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146 mX += deltaX;
147 if (mX < mMinX) mX = mMinX;
148 if (mX > mMaxX) mX = mMaxX;
149 mY += deltaY;
150 if (mY < mMinY) mY = mMinY;
151 if (mY > mMaxY) mY = mMaxY;
152 }
153
Chris Yea52ade12020-08-27 16:49:20 -0700154 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155
Chris Yea52ade12020-08-27 16:49:20 -0700156 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157
Chris Yea52ade12020-08-27 16:49:20 -0700158 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159
Chris Yea52ade12020-08-27 16:49:20 -0700160 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
161 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800162 std::vector<int32_t> newSpots;
163 // Add spots for fingers that are down.
164 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
165 uint32_t id = idBits.clearFirstMarkedBit();
166 newSpots.push_back(id);
167 }
168
169 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 }
171
Chris Yea52ade12020-08-27 16:49:20 -0700172 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800173
174 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175};
176
177
178// --- FakeInputReaderPolicy ---
179
180class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700181 std::mutex mLock;
182 std::condition_variable mDevicesChangedCondition;
183
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100185 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700186 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
187 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100188 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700189 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191protected:
Chris Yea52ade12020-08-27 16:49:20 -0700192 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193
194public:
195 FakeInputReaderPolicy() {
196 }
197
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700198 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800199 waitForInputDevices([](bool devicesChanged) {
200 if (!devicesChanged) {
201 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
202 }
203 });
204 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700205
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800206 void assertInputDevicesNotChanged() {
207 waitForInputDevices([](bool devicesChanged) {
208 if (devicesChanged) {
209 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
210 }
211 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700212 }
213
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700214 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100215 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100216 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700217 }
218
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700219 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
220 return mConfig.getDisplayViewportByUniqueId(uniqueId);
221 }
222 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
223 return mConfig.getDisplayViewportByType(type);
224 }
225
226 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
227 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700228 }
229
230 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000231 bool isActive, const std::string& uniqueId,
232 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
233 const DisplayViewport viewport =
234 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
235 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700236 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100237 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 }
239
Arthur Hung6cd19a42019-08-30 19:04:12 +0800240 bool updateViewport(const DisplayViewport& viewport) {
241 size_t count = mViewports.size();
242 for (size_t i = 0; i < count; i++) {
243 const DisplayViewport& currentViewport = mViewports[i];
244 if (currentViewport.displayId == viewport.displayId) {
245 mViewports[i] = viewport;
246 mConfig.setDisplayViewports(mViewports);
247 return true;
248 }
249 }
250 // no viewport found.
251 return false;
252 }
253
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100254 void addExcludedDeviceName(const std::string& deviceName) {
255 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 }
257
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700258 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
259 mConfig.portAssociations.insert({inputPort, displayPort});
260 }
261
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000262 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700263
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000264 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700265
Michael Wright17db18e2020-06-26 20:51:44 +0100266 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
267 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 }
269
270 const InputReaderConfiguration* getReaderConfiguration() const {
271 return &mConfig;
272 }
273
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800274 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 return mInputDevices;
276 }
277
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100278 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700279 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700280 return transform;
281 }
282
283 void setTouchAffineTransformation(const TouchAffineTransformation t) {
284 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800285 }
286
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800287 void setPointerCapture(bool enabled) {
288 mConfig.pointerCapture = enabled;
289 }
290
Arthur Hung7c645402019-01-25 17:45:42 +0800291 void setShowTouches(bool enabled) {
292 mConfig.showTouches = enabled;
293 }
294
Garfield Tan888a6a42020-01-09 11:39:16 -0800295 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
296 mConfig.defaultPointerDisplayId = pointerDisplayId;
297 }
298
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800299 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
300
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700302 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000303 int32_t orientation, bool isActive,
304 const std::string& uniqueId,
305 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700306 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
307 || orientation == DISPLAY_ORIENTATION_270);
308 DisplayViewport v;
309 v.displayId = displayId;
310 v.orientation = orientation;
311 v.logicalLeft = 0;
312 v.logicalTop = 0;
313 v.logicalRight = isRotated ? height : width;
314 v.logicalBottom = isRotated ? width : height;
315 v.physicalLeft = 0;
316 v.physicalTop = 0;
317 v.physicalRight = isRotated ? height : width;
318 v.physicalBottom = isRotated ? width : height;
319 v.deviceWidth = isRotated ? height : width;
320 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000321 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700322 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700323 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100324 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700325 return v;
326 }
327
Chris Yea52ade12020-08-27 16:49:20 -0700328 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 *outConfig = mConfig;
330 }
331
Chris Yea52ade12020-08-27 16:49:20 -0700332 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100333 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Chris Yea52ade12020-08-27 16:49:20 -0700336 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700337 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700339 mInputDevicesChanged = true;
340 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 }
342
Chris Yea52ade12020-08-27 16:49:20 -0700343 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
344 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700345 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 }
347
Chris Yea52ade12020-08-27 16:49:20 -0700348 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800349
350 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
351 std::unique_lock<std::mutex> lock(mLock);
352 base::ScopedLockAssertion assumeLocked(mLock);
353
354 const bool devicesChanged =
355 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
356 return mInputDevicesChanged;
357 });
358 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
359 mInputDevicesChanged = false;
360 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361};
362
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363// --- FakeEventHub ---
364
365class FakeEventHub : public EventHubInterface {
366 struct KeyInfo {
367 int32_t keyCode;
368 uint32_t flags;
369 };
370
Chris Yef59a2f42020-10-16 12:55:26 -0700371 struct SensorInfo {
372 InputDeviceSensorType sensorType;
373 int32_t sensorDataIndex;
374 };
375
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 struct Device {
377 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700378 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379 PropertyMap configuration;
380 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
381 KeyedVector<int, bool> relativeAxes;
382 KeyedVector<int32_t, int32_t> keyCodeStates;
383 KeyedVector<int32_t, int32_t> scanCodeStates;
384 KeyedVector<int32_t, int32_t> switchStates;
385 KeyedVector<int32_t, int32_t> absoluteAxisValue;
386 KeyedVector<int32_t, KeyInfo> keysByScanCode;
387 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
388 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700389 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
390 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800391 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700392 bool enabled;
393
394 status_t enable() {
395 enabled = true;
396 return OK;
397 }
398
399 status_t disable() {
400 enabled = false;
401 return OK;
402 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403
Chris Ye1b0c7342020-07-28 21:57:03 -0700404 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 };
406
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700407 std::mutex mLock;
408 std::condition_variable mEventsCondition;
409
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100411 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700412 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600413 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000414 std::vector<int32_t> mVibrators = {0, 1};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700416public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 virtual ~FakeEventHub() {
418 for (size_t i = 0; i < mDevices.size(); i++) {
419 delete mDevices.valueAt(i);
420 }
421 }
422
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 FakeEventHub() { }
424
Chris Ye1b0c7342020-07-28 21:57:03 -0700425 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426 Device* device = new Device(classes);
427 device->identifier.name = name;
428 mDevices.add(deviceId, device);
429
430 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
431 }
432
433 void removeDevice(int32_t deviceId) {
434 delete mDevices.valueFor(deviceId);
435 mDevices.removeItem(deviceId);
436
437 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
438 }
439
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700440 bool isDeviceEnabled(int32_t deviceId) {
441 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700442 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700443 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
444 return false;
445 }
446 return device->enabled;
447 }
448
449 status_t enableDevice(int32_t deviceId) {
450 status_t result;
451 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700452 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
454 return BAD_VALUE;
455 }
456 if (device->enabled) {
457 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
458 return OK;
459 }
460 result = device->enable();
461 return result;
462 }
463
464 status_t disableDevice(int32_t deviceId) {
465 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700466 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700467 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
468 return BAD_VALUE;
469 }
470 if (!device->enabled) {
471 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
472 return OK;
473 }
474 return device->disable();
475 }
476
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477 void finishDeviceScan() {
478 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
479 }
480
481 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
482 Device* device = getDevice(deviceId);
483 device->configuration.addProperty(key, value);
484 }
485
486 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
487 Device* device = getDevice(deviceId);
488 device->configuration.addAll(configuration);
489 }
490
491 void addAbsoluteAxis(int32_t deviceId, int axis,
492 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
493 Device* device = getDevice(deviceId);
494
495 RawAbsoluteAxisInfo info;
496 info.valid = true;
497 info.minValue = minValue;
498 info.maxValue = maxValue;
499 info.flat = flat;
500 info.fuzz = fuzz;
501 info.resolution = resolution;
502 device->absoluteAxes.add(axis, info);
503 }
504
505 void addRelativeAxis(int32_t deviceId, int32_t axis) {
506 Device* device = getDevice(deviceId);
507 device->relativeAxes.add(axis, true);
508 }
509
510 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
511 Device* device = getDevice(deviceId);
512 device->keyCodeStates.replaceValueFor(keyCode, state);
513 }
514
515 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
516 Device* device = getDevice(deviceId);
517 device->scanCodeStates.replaceValueFor(scanCode, state);
518 }
519
520 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
521 Device* device = getDevice(deviceId);
522 device->switchStates.replaceValueFor(switchCode, state);
523 }
524
525 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
526 Device* device = getDevice(deviceId);
527 device->absoluteAxisValue.replaceValueFor(axis, value);
528 }
529
530 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
531 int32_t keyCode, uint32_t flags) {
532 Device* device = getDevice(deviceId);
533 KeyInfo info;
534 info.keyCode = keyCode;
535 info.flags = flags;
536 if (scanCode) {
537 device->keysByScanCode.add(scanCode, info);
538 }
539 if (usageCode) {
540 device->keysByUsageCode.add(usageCode, info);
541 }
542 }
543
544 void addLed(int32_t deviceId, int32_t led, bool initialState) {
545 Device* device = getDevice(deviceId);
546 device->leds.add(led, initialState);
547 }
548
Chris Yef59a2f42020-10-16 12:55:26 -0700549 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
550 int32_t sensorDataIndex) {
551 Device* device = getDevice(deviceId);
552 SensorInfo info;
553 info.sensorType = sensorType;
554 info.sensorDataIndex = sensorDataIndex;
555 device->sensorsByAbsCode.emplace(absCode, info);
556 }
557
558 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
559 Device* device = getDevice(deviceId);
560 typename BitArray<MSC_MAX>::Buffer buffer;
561 buffer[mscEvent / 32] = 1 << mscEvent % 32;
562 device->mscBitmask.loadFromBuffer(buffer);
563 }
564
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 bool getLedState(int32_t deviceId, int32_t led) {
566 Device* device = getDevice(deviceId);
567 return device->leds.valueFor(led);
568 }
569
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100570 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 return mExcludedDevices;
572 }
573
574 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
575 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800576 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
579 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
580 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700581 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582 RawEvent event;
583 event.when = when;
584 event.deviceId = deviceId;
585 event.type = type;
586 event.code = code;
587 event.value = value;
588 mEvents.push_back(event);
589
590 if (type == EV_ABS) {
591 setAbsoluteAxisValue(deviceId, code, value);
592 }
593 }
594
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600595 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
596 std::vector<TouchVideoFrame>> videoFrames) {
597 mVideoFrames = std::move(videoFrames);
598 }
599
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700601 std::unique_lock<std::mutex> lock(mLock);
602 base::ScopedLockAssertion assumeLocked(mLock);
603 const bool queueIsEmpty =
604 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
605 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
606 if (!queueIsEmpty) {
607 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
608 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
611private:
612 Device* getDevice(int32_t deviceId) const {
613 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100614 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 }
616
Chris Yea52ade12020-08-27 16:49:20 -0700617 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700619 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 }
621
Chris Yea52ade12020-08-27 16:49:20 -0700622 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 Device* device = getDevice(deviceId);
624 return device ? device->identifier : InputDeviceIdentifier();
625 }
626
Chris Yea52ade12020-08-27 16:49:20 -0700627 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628
Chris Yea52ade12020-08-27 16:49:20 -0700629 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 Device* device = getDevice(deviceId);
631 if (device) {
632 *outConfiguration = device->configuration;
633 }
634 }
635
Chris Yea52ade12020-08-27 16:49:20 -0700636 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
637 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800639 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 ssize_t index = device->absoluteAxes.indexOfKey(axis);
641 if (index >= 0) {
642 *outAxisInfo = device->absoluteAxes.valueAt(index);
643 return OK;
644 }
645 }
646 outAxisInfo->clear();
647 return -1;
648 }
649
Chris Yea52ade12020-08-27 16:49:20 -0700650 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 Device* device = getDevice(deviceId);
652 if (device) {
653 return device->relativeAxes.indexOfKey(axis) >= 0;
654 }
655 return false;
656 }
657
Chris Yea52ade12020-08-27 16:49:20 -0700658 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659
Chris Yef59a2f42020-10-16 12:55:26 -0700660 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
661 Device* device = getDevice(deviceId);
662 if (device) {
663 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
664 }
665 return false;
666 }
667
Chris Yea52ade12020-08-27 16:49:20 -0700668 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
669 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 Device* device = getDevice(deviceId);
671 if (device) {
672 const KeyInfo* key = getKey(device, scanCode, usageCode);
673 if (key) {
674 if (outKeycode) {
675 *outKeycode = key->keyCode;
676 }
677 if (outFlags) {
678 *outFlags = key->flags;
679 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700680 if (outMetaState) {
681 *outMetaState = metaState;
682 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 return OK;
684 }
685 }
686 return NAME_NOT_FOUND;
687 }
688
689 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
690 if (usageCode) {
691 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
692 if (index >= 0) {
693 return &device->keysByUsageCode.valueAt(index);
694 }
695 }
696 if (scanCode) {
697 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
698 if (index >= 0) {
699 return &device->keysByScanCode.valueAt(index);
700 }
701 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700702 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 }
704
Chris Yea52ade12020-08-27 16:49:20 -0700705 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706
Chris Yef59a2f42020-10-16 12:55:26 -0700707 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
708 int32_t absCode) {
709 Device* device = getDevice(deviceId);
710 if (!device) {
711 return Errorf("Sensor device not found.");
712 }
713 auto it = device->sensorsByAbsCode.find(absCode);
714 if (it == device->sensorsByAbsCode.end()) {
715 return Errorf("Sensor map not found.");
716 }
717 const SensorInfo& info = it->second;
718 return std::make_pair(info.sensorType, info.sensorDataIndex);
719 }
720
Chris Yea52ade12020-08-27 16:49:20 -0700721 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 mExcludedDevices = devices;
723 }
724
Chris Yea52ade12020-08-27 16:49:20 -0700725 size_t getEvents(int, RawEvent* buffer, size_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700726 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 if (mEvents.empty()) {
728 return 0;
729 }
730
731 *buffer = *mEvents.begin();
732 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700733 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 return 1;
735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600738 auto it = mVideoFrames.find(deviceId);
739 if (it != mVideoFrames.end()) {
740 std::vector<TouchVideoFrame> frames = std::move(it->second);
741 mVideoFrames.erase(deviceId);
742 return frames;
743 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800744 return {};
745 }
746
Chris Yea52ade12020-08-27 16:49:20 -0700747 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 Device* device = getDevice(deviceId);
749 if (device) {
750 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
751 if (index >= 0) {
752 return device->scanCodeStates.valueAt(index);
753 }
754 }
755 return AKEY_STATE_UNKNOWN;
756 }
757
Chris Yea52ade12020-08-27 16:49:20 -0700758 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759 Device* device = getDevice(deviceId);
760 if (device) {
761 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
762 if (index >= 0) {
763 return device->keyCodeStates.valueAt(index);
764 }
765 }
766 return AKEY_STATE_UNKNOWN;
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 Device* device = getDevice(deviceId);
771 if (device) {
772 ssize_t index = device->switchStates.indexOfKey(sw);
773 if (index >= 0) {
774 return device->switchStates.valueAt(index);
775 }
776 }
777 return AKEY_STATE_UNKNOWN;
778 }
779
Chris Yea52ade12020-08-27 16:49:20 -0700780 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
781 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782 Device* device = getDevice(deviceId);
783 if (device) {
784 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
785 if (index >= 0) {
786 *outValue = device->absoluteAxisValue.valueAt(index);
787 return OK;
788 }
789 }
790 *outValue = 0;
791 return -1;
792 }
793
Chris Yea52ade12020-08-27 16:49:20 -0700794 // Return true if the device has non-empty key layout.
795 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
796 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797 bool result = false;
798 Device* device = getDevice(deviceId);
799 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700800 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801 for (size_t i = 0; i < numCodes; i++) {
802 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
803 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
804 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 }
806 }
807 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
808 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
809 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811 }
812 }
813 }
814 return result;
815 }
816
Chris Yea52ade12020-08-27 16:49:20 -0700817 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 Device* device = getDevice(deviceId);
819 if (device) {
820 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
821 return index >= 0;
822 }
823 return false;
824 }
825
Chris Yea52ade12020-08-27 16:49:20 -0700826 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 Device* device = getDevice(deviceId);
828 return device && device->leds.indexOfKey(led) >= 0;
829 }
830
Chris Yea52ade12020-08-27 16:49:20 -0700831 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 Device* device = getDevice(deviceId);
833 if (device) {
834 ssize_t index = device->leds.indexOfKey(led);
835 if (index >= 0) {
836 device->leds.replaceValueAt(led, on);
837 } else {
838 ADD_FAILURE()
839 << "Attempted to set the state of an LED that the EventHub declared "
840 "was not present. led=" << led;
841 }
842 }
843 }
844
Chris Yea52ade12020-08-27 16:49:20 -0700845 void getVirtualKeyDefinitions(
846 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 outVirtualKeys.clear();
848
849 Device* device = getDevice(deviceId);
850 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800851 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 }
853 }
854
Chris Yea52ade12020-08-27 16:49:20 -0700855 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700856 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 }
858
Chris Yea52ade12020-08-27 16:49:20 -0700859 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860 return false;
861 }
862
Chris Yea52ade12020-08-27 16:49:20 -0700863 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864
Chris Yea52ade12020-08-27 16:49:20 -0700865 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866
Chris Ye87143712020-11-10 05:05:58 +0000867 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
868
Kim Low03ea0352020-11-06 12:45:07 -0800869 std::optional<int32_t> getBatteryCapacity(int32_t) const override { return BATTERY_CAPACITY; }
870
871 std::optional<int32_t> getBatteryStatus(int32_t) const override { return BATTERY_STATUS; }
872
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100873 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874 return false;
875 }
876
Chris Yea52ade12020-08-27 16:49:20 -0700877 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Chris Yea52ade12020-08-27 16:49:20 -0700879 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880
Chris Yea52ade12020-08-27 16:49:20 -0700881 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882
Chris Yea52ade12020-08-27 16:49:20 -0700883 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884};
885
Michael Wrightd02c5b62014-02-10 15:10:22 -0800886// --- FakeInputMapper ---
887
888class FakeInputMapper : public InputMapper {
889 uint32_t mSources;
890 int32_t mKeyboardType;
891 int32_t mMetaState;
892 KeyedVector<int32_t, int32_t> mKeyCodeStates;
893 KeyedVector<int32_t, int32_t> mScanCodeStates;
894 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800895 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700897 std::mutex mLock;
898 std::condition_variable mStateChangedCondition;
899 bool mConfigureWasCalled GUARDED_BY(mLock);
900 bool mResetWasCalled GUARDED_BY(mLock);
901 bool mProcessWasCalled GUARDED_BY(mLock);
902 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903
Arthur Hungc23540e2018-11-29 20:42:11 +0800904 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800906 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
907 : InputMapper(deviceContext),
908 mSources(sources),
909 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800911 mConfigureWasCalled(false),
912 mResetWasCalled(false),
913 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914
Chris Yea52ade12020-08-27 16:49:20 -0700915 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916
917 void setKeyboardType(int32_t keyboardType) {
918 mKeyboardType = keyboardType;
919 }
920
921 void setMetaState(int32_t metaState) {
922 mMetaState = metaState;
923 }
924
925 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700926 std::unique_lock<std::mutex> lock(mLock);
927 base::ScopedLockAssertion assumeLocked(mLock);
928 const bool configureCalled =
929 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
930 return mConfigureWasCalled;
931 });
932 if (!configureCalled) {
933 FAIL() << "Expected configure() to have been called.";
934 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 mConfigureWasCalled = false;
936 }
937
938 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700939 std::unique_lock<std::mutex> lock(mLock);
940 base::ScopedLockAssertion assumeLocked(mLock);
941 const bool resetCalled =
942 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
943 return mResetWasCalled;
944 });
945 if (!resetCalled) {
946 FAIL() << "Expected reset() to have been called.";
947 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948 mResetWasCalled = false;
949 }
950
Yi Kong9b14ac62018-07-17 13:48:38 -0700951 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700952 std::unique_lock<std::mutex> lock(mLock);
953 base::ScopedLockAssertion assumeLocked(mLock);
954 const bool processCalled =
955 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
956 return mProcessWasCalled;
957 });
958 if (!processCalled) {
959 FAIL() << "Expected process() to have been called.";
960 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 if (outLastEvent) {
962 *outLastEvent = mLastEvent;
963 }
964 mProcessWasCalled = false;
965 }
966
967 void setKeyCodeState(int32_t keyCode, int32_t state) {
968 mKeyCodeStates.replaceValueFor(keyCode, state);
969 }
970
971 void setScanCodeState(int32_t scanCode, int32_t state) {
972 mScanCodeStates.replaceValueFor(scanCode, state);
973 }
974
975 void setSwitchState(int32_t switchCode, int32_t state) {
976 mSwitchStates.replaceValueFor(switchCode, state);
977 }
978
979 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800980 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981 }
982
983private:
Chris Yea52ade12020-08-27 16:49:20 -0700984 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985
Chris Yea52ade12020-08-27 16:49:20 -0700986 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 InputMapper::populateDeviceInfo(deviceInfo);
988
989 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
990 deviceInfo->setKeyboardType(mKeyboardType);
991 }
992 }
993
Chris Yea52ade12020-08-27 16:49:20 -0700994 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700995 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800996 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800997
998 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800999 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001000 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1001 mViewport = config->getDisplayViewportByPort(*displayPort);
1002 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001003
1004 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 }
1006
Chris Yea52ade12020-08-27 16:49:20 -07001007 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001008 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001010 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 }
1012
Chris Yea52ade12020-08-27 16:49:20 -07001013 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015 mLastEvent = *rawEvent;
1016 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001017 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 }
1019
Chris Yea52ade12020-08-27 16:49:20 -07001020 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1022 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1023 }
1024
Chris Yea52ade12020-08-27 16:49:20 -07001025 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1027 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1028 }
1029
Chris Yea52ade12020-08-27 16:49:20 -07001030 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1032 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1033 }
1034
Chris Yea52ade12020-08-27 16:49:20 -07001035 // Return true if the device has non-empty key layout.
1036 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1037 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038 for (size_t i = 0; i < numCodes; i++) {
1039 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1040 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1041 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 }
1043 }
1044 }
Chris Yea52ade12020-08-27 16:49:20 -07001045 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046 return result;
1047 }
1048
1049 virtual int32_t getMetaState() {
1050 return mMetaState;
1051 }
1052
1053 virtual void fadePointer() {
1054 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001055
1056 virtual std::optional<int32_t> getAssociatedDisplay() {
1057 if (mViewport) {
1058 return std::make_optional(mViewport->displayId);
1059 }
1060 return std::nullopt;
1061 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062};
1063
1064
1065// --- InstrumentedInputReader ---
1066
1067class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069
1070public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001071 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1072 const sp<InputReaderPolicyInterface>& policy,
1073 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001074 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001076 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001080 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001081 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 InputDeviceIdentifier identifier;
1083 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001084 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001086 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087 }
1088
Prabir Pradhan28efc192019-11-05 01:10:04 +00001089 // Make the protected loopOnce method accessible to tests.
1090 using InputReader::loopOnce;
1091
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001093 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1094 const InputDeviceIdentifier& identifier)
1095 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001096 if (!mNextDevices.empty()) {
1097 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1098 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 return device;
1100 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001101 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 }
1103
arthurhungdcef2dc2020-08-11 14:47:50 +08001104 // --- FakeInputReaderContext ---
1105 class FakeInputReaderContext : public ContextImpl {
1106 int32_t mGlobalMetaState;
1107 bool mUpdateGlobalMetaStateWasCalled;
1108 int32_t mGeneration;
1109
1110 public:
1111 FakeInputReaderContext(InputReader* reader)
1112 : ContextImpl(reader),
1113 mGlobalMetaState(0),
1114 mUpdateGlobalMetaStateWasCalled(false),
1115 mGeneration(1) {}
1116
1117 virtual ~FakeInputReaderContext() {}
1118
1119 void assertUpdateGlobalMetaStateWasCalled() {
1120 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1121 << "Expected updateGlobalMetaState() to have been called.";
1122 mUpdateGlobalMetaStateWasCalled = false;
1123 }
1124
1125 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1126
1127 uint32_t getGeneration() { return mGeneration; }
1128
1129 void updateGlobalMetaState() override {
1130 mUpdateGlobalMetaStateWasCalled = true;
1131 ContextImpl::updateGlobalMetaState();
1132 }
1133
1134 int32_t getGlobalMetaState() override {
1135 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1136 }
1137
1138 int32_t bumpGeneration() override {
1139 mGeneration = ContextImpl::bumpGeneration();
1140 return mGeneration;
1141 }
1142 } mFakeContext;
1143
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001145
1146public:
1147 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148};
1149
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150// --- InputReaderPolicyTest ---
1151class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001152protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001153 sp<FakeInputReaderPolicy> mFakePolicy;
1154
Chris Yea52ade12020-08-27 16:49:20 -07001155 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1156 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001157};
1158
1159/**
1160 * Check that empty set of viewports is an acceptable configuration.
1161 * Also try to get internal viewport two different ways - by type and by uniqueId.
1162 *
1163 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1164 * Such configuration is not currently allowed.
1165 */
1166TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001167 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168
1169 // We didn't add any viewports yet, so there shouldn't be any.
1170 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001171 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001172 ASSERT_FALSE(internalViewport);
1173
1174 // Add an internal viewport, then clear it
1175 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001176 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001177 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001178
1179 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001182 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001183
1184 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001185 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001186 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001187 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001188
1189 mFakePolicy->clearViewports();
1190 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001193 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001194 ASSERT_FALSE(internalViewport);
1195}
1196
1197TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1198 const std::string internalUniqueId = "local:0";
1199 const std::string externalUniqueId = "local:1";
1200 const std::string virtualUniqueId1 = "virtual:2";
1201 const std::string virtualUniqueId2 = "virtual:3";
1202 constexpr int32_t virtualDisplayId1 = 2;
1203 constexpr int32_t virtualDisplayId2 = 3;
1204
1205 // Add an internal viewport
1206 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001207 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1208 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209 // Add an external viewport
1210 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001211 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1212 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 // Add an virtual viewport
1214 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001215 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1216 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001217 // Add another virtual viewport
1218 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001219 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1220 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221
1222 // Check matching by type for internal
1223 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001224 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001225 ASSERT_TRUE(internalViewport);
1226 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1227
1228 // Check matching by type for external
1229 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001230 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001231 ASSERT_TRUE(externalViewport);
1232 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1233
1234 // Check matching by uniqueId for virtual viewport #1
1235 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001236 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001238 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1240 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1241
1242 // Check matching by uniqueId for virtual viewport #2
1243 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001244 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001246 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001247 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1248 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1249}
1250
1251
1252/**
1253 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1254 * that lookup works by checking display id.
1255 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1256 */
1257TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1258 const std::string uniqueId1 = "uniqueId1";
1259 const std::string uniqueId2 = "uniqueId2";
1260 constexpr int32_t displayId1 = 2;
1261 constexpr int32_t displayId2 = 3;
1262
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001263 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1264 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001265 for (const ViewportType& type : types) {
1266 mFakePolicy->clearViewports();
1267 // Add a viewport
1268 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001269 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1270 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271 // Add another viewport
1272 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001273 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1274 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275
1276 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001277 std::optional<DisplayViewport> viewport1 =
1278 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001279 ASSERT_TRUE(viewport1);
1280 ASSERT_EQ(displayId1, viewport1->displayId);
1281 ASSERT_EQ(type, viewport1->type);
1282
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001283 std::optional<DisplayViewport> viewport2 =
1284 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285 ASSERT_TRUE(viewport2);
1286 ASSERT_EQ(displayId2, viewport2->displayId);
1287 ASSERT_EQ(type, viewport2->type);
1288
1289 // When there are multiple viewports of the same kind, and uniqueId is not specified
1290 // in the call to getDisplayViewport, then that situation is not supported.
1291 // The viewports can be stored in any order, so we cannot rely on the order, since that
1292 // is just implementation detail.
1293 // However, we can check that it still returns *a* viewport, we just cannot assert
1294 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001295 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001296 ASSERT_TRUE(someViewport);
1297 }
1298}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001300/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001301 * When we have multiple internal displays make sure we always return the default display when
1302 * querying by type.
1303 */
1304TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1305 const std::string uniqueId1 = "uniqueId1";
1306 const std::string uniqueId2 = "uniqueId2";
1307 constexpr int32_t nonDefaultDisplayId = 2;
1308 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1309 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1310
1311 // Add the default display first and ensure it gets returned.
1312 mFakePolicy->clearViewports();
1313 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001314 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001315 ViewportType::INTERNAL);
1316 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001317 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001318 ViewportType::INTERNAL);
1319
1320 std::optional<DisplayViewport> viewport =
1321 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1322 ASSERT_TRUE(viewport);
1323 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1324 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1325
1326 // Add the default display second to make sure order doesn't matter.
1327 mFakePolicy->clearViewports();
1328 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001329 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001330 ViewportType::INTERNAL);
1331 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001332 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001333 ViewportType::INTERNAL);
1334
1335 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1336 ASSERT_TRUE(viewport);
1337 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1338 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1339}
1340
1341/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001342 * Check getDisplayViewportByPort
1343 */
1344TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001345 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001346 const std::string uniqueId1 = "uniqueId1";
1347 const std::string uniqueId2 = "uniqueId2";
1348 constexpr int32_t displayId1 = 1;
1349 constexpr int32_t displayId2 = 2;
1350 const uint8_t hdmi1 = 0;
1351 const uint8_t hdmi2 = 1;
1352 const uint8_t hdmi3 = 2;
1353
1354 mFakePolicy->clearViewports();
1355 // Add a viewport that's associated with some display port that's not of interest.
1356 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001357 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1358 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001359 // Add another viewport, connected to HDMI1 port
1360 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001361 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1362 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001363
1364 // Check that correct display viewport was returned by comparing the display ports.
1365 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1366 ASSERT_TRUE(hdmi1Viewport);
1367 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1368 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1369
1370 // Check that we can still get the same viewport using the uniqueId
1371 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1372 ASSERT_TRUE(hdmi1Viewport);
1373 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1374 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1375 ASSERT_EQ(type, hdmi1Viewport->type);
1376
1377 // Check that we cannot find a port with "HDMI2", because we never added one
1378 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1379 ASSERT_FALSE(hdmi2Viewport);
1380}
1381
Michael Wrightd02c5b62014-02-10 15:10:22 -08001382// --- InputReaderTest ---
1383
1384class InputReaderTest : public testing::Test {
1385protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001386 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001388 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001389 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001390
Chris Yea52ade12020-08-27 16:49:20 -07001391 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001392 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001393 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001394 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001395
Prabir Pradhan28efc192019-11-05 01:10:04 +00001396 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1397 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001398 }
1399
Chris Yea52ade12020-08-27 16:49:20 -07001400 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401 mFakeListener.clear();
1402 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001403 }
1404
Chris Ye1b0c7342020-07-28 21:57:03 -07001405 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001406 const PropertyMap* configuration) {
1407 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001408
1409 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001410 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001411 }
1412 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001413 mReader->loopOnce();
1414 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1416 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001417 }
1418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001419 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001420 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001421 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001422 }
1423
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001424 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001425 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001426 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001427 }
1428
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001430 const std::string& name,
1431 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001432 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001433 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1434 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001435 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001436 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001437 return mapper;
1438 }
1439};
1440
Chris Ye98d3f532020-10-01 21:48:59 -07001441TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001442 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1443 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1444 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001445
Chris Ye98d3f532020-10-01 21:48:59 -07001446 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001447 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001448 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001449 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001450 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1451 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1452 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001453}
1454
1455TEST_F(InputReaderTest, PolicyGetInputDevices) {
1456 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1457 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1458 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001459
1460 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001461 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001462 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001463 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001464 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001465 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1466 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1467 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1468}
1469
Chris Yee7310032020-09-22 15:36:28 -07001470TEST_F(InputReaderTest, GetMergedInputDevices) {
1471 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1472 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1473 // Add two subdevices to device
1474 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1475 // Must add at least one mapper or the device will be ignored!
1476 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1477 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1478
1479 // Push same device instance for next device to be added, so they'll have same identifier.
1480 mReader->pushNextDevice(device);
1481 mReader->pushNextDevice(device);
1482 ASSERT_NO_FATAL_FAILURE(
1483 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1484 ASSERT_NO_FATAL_FAILURE(
1485 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1486
1487 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001488 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001489}
1490
Chris Yee14523a2020-12-19 13:46:00 -08001491TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1492 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1493 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1494 // Add two subdevices to device
1495 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1496 // Must add at least one mapper or the device will be ignored!
1497 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1498 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1499
1500 // Push same device instance for next device to be added, so they'll have same identifier.
1501 mReader->pushNextDevice(device);
1502 mReader->pushNextDevice(device);
1503 // Sensor device is initially disabled
1504 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1505 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1506 nullptr));
1507 // Device is disabled because the only sub device is a sensor device and disabled initially.
1508 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1509 ASSERT_FALSE(device->isEnabled());
1510 ASSERT_NO_FATAL_FAILURE(
1511 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1512 // The merged device is enabled if any sub device is enabled
1513 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1514 ASSERT_TRUE(device->isEnabled());
1515}
1516
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001517TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001518 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001519 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001520 constexpr int32_t eventHubId = 1;
1521 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001522 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001523 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001524 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001525 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001526
Yi Kong9b14ac62018-07-17 13:48:38 -07001527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001528
1529 NotifyDeviceResetArgs resetArgs;
1530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001531 ASSERT_EQ(deviceId, resetArgs.deviceId);
1532
1533 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001534 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001535 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001536
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001538 ASSERT_EQ(deviceId, resetArgs.deviceId);
1539 ASSERT_EQ(device->isEnabled(), false);
1540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001541 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001542 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001545 ASSERT_EQ(device->isEnabled(), false);
1546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001547 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001548 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001550 ASSERT_EQ(deviceId, resetArgs.deviceId);
1551 ASSERT_EQ(device->isEnabled(), true);
1552}
1553
Michael Wrightd02c5b62014-02-10 15:10:22 -08001554TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001555 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001556 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001558 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001559 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001560 AINPUT_SOURCE_KEYBOARD, nullptr);
1561 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562
1563 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1564 AINPUT_SOURCE_ANY, AKEYCODE_A))
1565 << "Should return unknown when the device id is >= 0 but unknown.";
1566
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001567 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1568 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1569 << "Should return unknown when the device id is valid but the sources are not "
1570 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001572 ASSERT_EQ(AKEY_STATE_DOWN,
1573 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1574 AKEYCODE_A))
1575 << "Should return value provided by mapper when device id is valid and the device "
1576 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577
1578 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1579 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1580 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1581
1582 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1583 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1584 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1585}
1586
1587TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001588 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001589 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001591 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001592 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001593 AINPUT_SOURCE_KEYBOARD, nullptr);
1594 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595
1596 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1597 AINPUT_SOURCE_ANY, KEY_A))
1598 << "Should return unknown when the device id is >= 0 but unknown.";
1599
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001600 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1601 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1602 << "Should return unknown when the device id is valid but the sources are not "
1603 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 ASSERT_EQ(AKEY_STATE_DOWN,
1606 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1607 KEY_A))
1608 << "Should return value provided by mapper when device id is valid and the device "
1609 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001610
1611 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1612 AINPUT_SOURCE_TRACKBALL, KEY_A))
1613 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1614
1615 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1616 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1617 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1618}
1619
1620TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001621 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001622 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001623 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001624 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001626 AINPUT_SOURCE_KEYBOARD, nullptr);
1627 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628
1629 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1630 AINPUT_SOURCE_ANY, SW_LID))
1631 << "Should return unknown when the device id is >= 0 but unknown.";
1632
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1634 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1635 << "Should return unknown when the device id is valid but the sources are not "
1636 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001637
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 ASSERT_EQ(AKEY_STATE_DOWN,
1639 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1640 SW_LID))
1641 << "Should return value provided by mapper when device id is valid and the device "
1642 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643
1644 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1645 AINPUT_SOURCE_TRACKBALL, SW_LID))
1646 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1647
1648 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1649 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1650 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1651}
1652
1653TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001654 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001655 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001656 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001657 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001659 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001661 mapper.addSupportedKeyCode(AKEYCODE_A);
1662 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663
1664 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1665 uint8_t flags[4] = { 0, 0, 0, 1 };
1666
1667 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1668 << "Should return false when device id is >= 0 but unknown.";
1669 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1670
1671 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001672 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1673 << "Should return false when device id is valid but the sources are not supported by "
1674 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1676
1677 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1679 keyCodes, flags))
1680 << "Should return value provided by mapper when device id is valid and the device "
1681 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1683
1684 flags[3] = 1;
1685 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1686 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1687 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1688
1689 flags[3] = 1;
1690 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1691 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1692 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1693}
1694
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001695TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001697 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001698
1699 NotifyConfigurationChangedArgs args;
1700
1701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1702 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1703}
1704
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001705TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001707 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001709 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001711 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001714 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001715 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1716
1717 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001718 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721 ASSERT_EQ(EV_KEY, event.type);
1722 ASSERT_EQ(KEY_A, event.code);
1723 ASSERT_EQ(1, event.value);
1724}
1725
Garfield Tan1c7bc862020-01-28 13:24:04 -08001726TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001728 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001729 constexpr int32_t eventHubId = 1;
1730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001731 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001732 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001733 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001734 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001735
1736 NotifyDeviceResetArgs resetArgs;
1737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001738 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001739
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001740 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001741 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001743 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001744 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001746 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001747 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001749 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001750 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001752 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001753 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001755 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001756 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001757}
1758
Garfield Tan1c7bc862020-01-28 13:24:04 -08001759TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1760 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001761 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001762 constexpr int32_t eventHubId = 1;
1763 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1764 // Must add at least one mapper or the device will be ignored!
1765 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001766 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001767 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1768
1769 NotifyDeviceResetArgs resetArgs;
1770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1771 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1772}
1773
Arthur Hungc23540e2018-11-29 20:42:11 +08001774TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001775 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001776 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001778 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1780 FakeInputMapper& mapper =
1781 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001782 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001783
1784 const uint8_t hdmi1 = 1;
1785
1786 // Associated touch screen with second display.
1787 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1788
1789 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001790 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001791 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001792 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001793 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001794 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001795 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001796 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001797 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001798 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001799
1800 // Add the device, and make sure all of the callbacks are triggered.
1801 // The device is added after the input port associations are processed since
1802 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001806 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001807
Arthur Hung2c9a3342019-07-23 14:18:59 +08001808 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001809 ASSERT_EQ(deviceId, device->getId());
1810 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1811 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001812
1813 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001815 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001816 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001817}
1818
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001819TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1820 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1821 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1822 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1823 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1824 // Must add at least one mapper or the device will be ignored!
1825 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1826 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1827 mReader->pushNextDevice(device);
1828 mReader->pushNextDevice(device);
1829 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1830 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1831
1832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1833
1834 NotifyDeviceResetArgs resetArgs;
1835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1836 ASSERT_EQ(deviceId, resetArgs.deviceId);
1837 ASSERT_TRUE(device->isEnabled());
1838 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1839 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1840
1841 disableDevice(deviceId);
1842 mReader->loopOnce();
1843
1844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1845 ASSERT_EQ(deviceId, resetArgs.deviceId);
1846 ASSERT_FALSE(device->isEnabled());
1847 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1848 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1849
1850 enableDevice(deviceId);
1851 mReader->loopOnce();
1852
1853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1854 ASSERT_EQ(deviceId, resetArgs.deviceId);
1855 ASSERT_TRUE(device->isEnabled());
1856 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1857 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1858}
1859
1860TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1861 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1862 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1863 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1864 // Add two subdevices to device
1865 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1866 FakeInputMapper& mapperDevice1 =
1867 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1868 FakeInputMapper& mapperDevice2 =
1869 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1870 mReader->pushNextDevice(device);
1871 mReader->pushNextDevice(device);
1872 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1873 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1874
1875 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1876 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1877
1878 ASSERT_EQ(AKEY_STATE_DOWN,
1879 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1880 ASSERT_EQ(AKEY_STATE_DOWN,
1881 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1882 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1883 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1884}
1885
Prabir Pradhan7e186182020-11-10 13:56:45 -08001886TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1887 NotifyPointerCaptureChangedArgs args;
1888
1889 mFakePolicy->setPointerCapture(true);
1890 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1891 mReader->loopOnce();
1892 mFakeListener->assertNotifyCaptureWasCalled(&args);
1893 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1894
1895 mFakePolicy->setPointerCapture(false);
1896 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1897 mReader->loopOnce();
1898 mFakeListener->assertNotifyCaptureWasCalled(&args);
1899 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1900
1901 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1902 // does not change.
1903 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1904 mReader->loopOnce();
1905 mFakeListener->assertNotifyCaptureWasCalled(&args);
1906 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1907}
1908
Chris Ye87143712020-11-10 05:05:58 +00001909class FakeVibratorInputMapper : public FakeInputMapper {
1910public:
1911 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1912 : FakeInputMapper(deviceContext, sources) {}
1913
1914 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1915};
1916
1917TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1918 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1919 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1920 constexpr int32_t eventHubId = 1;
1921 const char* DEVICE_LOCATION = "BLUETOOTH";
1922 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1923 FakeVibratorInputMapper& mapper =
1924 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1925 mReader->pushNextDevice(device);
1926
1927 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1928 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1929
1930 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1931 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1932}
1933
Kim Low03ea0352020-11-06 12:45:07 -08001934class FakeBatteryInputMapper : public FakeInputMapper {
1935public:
1936 FakeBatteryInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1937 : FakeInputMapper(deviceContext, sources) {}
1938
1939 std::optional<int32_t> getBatteryCapacity() override {
1940 return getDeviceContext().getBatteryCapacity();
1941 }
1942
1943 std::optional<int32_t> getBatteryStatus() override {
1944 return getDeviceContext().getBatteryStatus();
1945 }
1946};
1947
1948TEST_F(InputReaderTest, BatteryGetCapacity) {
1949 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1950 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1951 constexpr int32_t eventHubId = 1;
1952 const char* DEVICE_LOCATION = "BLUETOOTH";
1953 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1954 FakeBatteryInputMapper& mapper =
1955 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1956 mReader->pushNextDevice(device);
1957
1958 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1959 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1960
1961 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
1962}
1963
1964TEST_F(InputReaderTest, BatteryGetStatus) {
1965 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1966 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1967 constexpr int32_t eventHubId = 1;
1968 const char* DEVICE_LOCATION = "BLUETOOTH";
1969 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1970 FakeBatteryInputMapper& mapper =
1971 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1972 mReader->pushNextDevice(device);
1973
1974 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1975 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1976
1977 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
1978}
1979
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001980// --- InputReaderIntegrationTest ---
1981
1982// These tests create and interact with the InputReader only through its interface.
1983// The InputReader is started during SetUp(), which starts its processing in its own
1984// thread. The tests use linux uinput to emulate input devices.
1985// NOTE: Interacting with the physical device while these tests are running may cause
1986// the tests to fail.
1987class InputReaderIntegrationTest : public testing::Test {
1988protected:
1989 sp<TestInputListener> mTestListener;
1990 sp<FakeInputReaderPolicy> mFakePolicy;
1991 sp<InputReaderInterface> mReader;
1992
Chris Yea52ade12020-08-27 16:49:20 -07001993 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001994 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001995 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1996 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001997
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001998 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001999 ASSERT_EQ(mReader->start(), OK);
2000
2001 // Since this test is run on a real device, all the input devices connected
2002 // to the test device will show up in mReader. We wait for those input devices to
2003 // show up before beginning the tests.
2004 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2005 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2006 }
2007
Chris Yea52ade12020-08-27 16:49:20 -07002008 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002009 ASSERT_EQ(mReader->stop(), OK);
2010 mTestListener.clear();
2011 mFakePolicy.clear();
2012 }
2013};
2014
2015TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2016 // An invalid input device that is only used for this test.
2017 class InvalidUinputDevice : public UinputDevice {
2018 public:
2019 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2020
2021 private:
2022 void configureDevice(int fd, uinput_user_dev* device) override {}
2023 };
2024
2025 const size_t numDevices = mFakePolicy->getInputDevices().size();
2026
2027 // UinputDevice does not set any event or key bits, so InputReader should not
2028 // consider it as a valid device.
2029 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2030 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2031 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2032 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2033
2034 invalidDevice.reset();
2035 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2036 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2037 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2038}
2039
2040TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2041 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2042
2043 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2044 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2045 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2046 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2047
2048 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002049 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2050 const auto& it =
2051 std::find_if(inputDevices.begin(), inputDevices.end(),
2052 [&keyboard](const InputDeviceInfo& info) {
2053 return info.getIdentifier().name == keyboard->getName();
2054 });
2055
2056 ASSERT_NE(it, inputDevices.end());
2057 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2058 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2059 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002060
2061 keyboard.reset();
2062 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2063 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2064 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2065}
2066
2067TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2068 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2069 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2070
2071 NotifyConfigurationChangedArgs configChangedArgs;
2072 ASSERT_NO_FATAL_FAILURE(
2073 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002074 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002075 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2076
2077 NotifyKeyArgs keyArgs;
2078 keyboard->pressAndReleaseHomeKey();
2079 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2080 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002081 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002082 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002083 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2084 prevTimestamp = keyArgs.eventTime;
2085
2086 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2087 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002088 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002089 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2090}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002091
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002092/**
2093 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2094 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2095 * are passed to the listener.
2096 */
2097static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2098TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2099 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2100 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2101 NotifyKeyArgs keyArgs;
2102
2103 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2104 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2105 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2106 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2107
2108 controller->pressAndReleaseKey(BTN_GEAR_UP);
2109 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2110 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2111 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2112}
2113
Arthur Hungaab25622020-01-16 11:22:11 +08002114// --- TouchProcessTest ---
2115class TouchIntegrationTest : public InputReaderIntegrationTest {
2116protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002117 const std::string UNIQUE_ID = "local:0";
2118
Chris Yea52ade12020-08-27 16:49:20 -07002119 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002120 InputReaderIntegrationTest::SetUp();
2121 // At least add an internal display.
2122 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2123 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002124 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002125
2126 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2127 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2128 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2129 }
2130
2131 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2132 int32_t orientation, const std::string& uniqueId,
2133 std::optional<uint8_t> physicalPort,
2134 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002135 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2136 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002137 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2138 }
2139
2140 std::unique_ptr<UinputTouchScreen> mDevice;
2141};
2142
2143TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2144 NotifyMotionArgs args;
2145 const Point centerPoint = mDevice->getCenterPoint();
2146
2147 // ACTION_DOWN
2148 mDevice->sendDown(centerPoint);
2149 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2151
2152 // ACTION_MOVE
2153 mDevice->sendMove(centerPoint + Point(1, 1));
2154 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2156
2157 // ACTION_UP
2158 mDevice->sendUp();
2159 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2161}
2162
2163TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2164 NotifyMotionArgs args;
2165 const Point centerPoint = mDevice->getCenterPoint();
2166
2167 // ACTION_DOWN
2168 mDevice->sendDown(centerPoint);
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2171
2172 // ACTION_POINTER_DOWN (Second slot)
2173 const Point secondPoint = centerPoint + Point(100, 100);
2174 mDevice->sendSlot(SECOND_SLOT);
2175 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2176 mDevice->sendDown(secondPoint + Point(1, 1));
2177 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2178 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2179 args.action);
2180
2181 // ACTION_MOVE (Second slot)
2182 mDevice->sendMove(secondPoint);
2183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2185
2186 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002187 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002188 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002189 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002190 args.action);
2191
2192 // ACTION_UP
2193 mDevice->sendSlot(FIRST_SLOT);
2194 mDevice->sendUp();
2195 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2197}
2198
2199TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2200 NotifyMotionArgs args;
2201 const Point centerPoint = mDevice->getCenterPoint();
2202
2203 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002204 mDevice->sendSlot(FIRST_SLOT);
2205 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002206 mDevice->sendDown(centerPoint);
2207 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2209
arthurhungcc7f9802020-04-30 17:55:40 +08002210 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002211 const Point secondPoint = centerPoint + Point(100, 100);
2212 mDevice->sendSlot(SECOND_SLOT);
2213 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2214 mDevice->sendDown(secondPoint);
2215 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2216 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2217 args.action);
2218
arthurhungcc7f9802020-04-30 17:55:40 +08002219 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002220 mDevice->sendMove(secondPoint + Point(1, 1));
2221 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2223
arthurhungcc7f9802020-04-30 17:55:40 +08002224 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2225 // a palm event.
2226 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002227 mDevice->sendToolType(MT_TOOL_PALM);
2228 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002229 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2230 args.action);
2231 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002232
arthurhungcc7f9802020-04-30 17:55:40 +08002233 // Send up to second slot, expect first slot send moving.
2234 mDevice->sendPointerUp();
2235 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002237
arthurhungcc7f9802020-04-30 17:55:40 +08002238 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002239 mDevice->sendSlot(FIRST_SLOT);
2240 mDevice->sendUp();
2241
arthurhungcc7f9802020-04-30 17:55:40 +08002242 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2243 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002244}
2245
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002247class InputDeviceTest : public testing::Test {
2248protected:
2249 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002250 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251 static const int32_t DEVICE_ID;
2252 static const int32_t DEVICE_GENERATION;
2253 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002254 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002255 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002257 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002259 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002260 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002261 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262
Chris Yea52ade12020-08-27 16:49:20 -07002263 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002264 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002265 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002266 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002267 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2268 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269 InputDeviceIdentifier identifier;
2270 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002271 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002272 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002273 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002274 mReader->pushNextDevice(mDevice);
2275 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2276 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277 }
2278
Chris Yea52ade12020-08-27 16:49:20 -07002279 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280 mFakeListener.clear();
2281 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282 }
2283};
2284
2285const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002286const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002287const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2289const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002290const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2291 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002292const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002293
2294TEST_F(InputDeviceTest, ImmutableProperties) {
2295 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002296 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002297 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298}
2299
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002300TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2301 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002302}
2303
Michael Wrightd02c5b62014-02-10 15:10:22 -08002304TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2305 // Configuration.
2306 InputReaderConfiguration config;
2307 mDevice->configure(ARBITRARY_TIME, &config, 0);
2308
2309 // Reset.
2310 mDevice->reset(ARBITRARY_TIME);
2311
2312 NotifyDeviceResetArgs resetArgs;
2313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2314 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2315 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2316
2317 // Metadata.
2318 ASSERT_TRUE(mDevice->isIgnored());
2319 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2320
2321 InputDeviceInfo info;
2322 mDevice->getDeviceInfo(&info);
2323 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002324 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2326 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2327
2328 // State queries.
2329 ASSERT_EQ(0, mDevice->getMetaState());
2330
2331 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2332 << "Ignored device should return unknown key code state.";
2333 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2334 << "Ignored device should return unknown scan code state.";
2335 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2336 << "Ignored device should return unknown switch state.";
2337
2338 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2339 uint8_t flags[2] = { 0, 1 };
2340 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2341 << "Ignored device should never mark any key codes.";
2342 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2343 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2344}
2345
2346TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2347 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002348 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002349
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002350 FakeInputMapper& mapper1 =
2351 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002352 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2353 mapper1.setMetaState(AMETA_ALT_ON);
2354 mapper1.addSupportedKeyCode(AKEYCODE_A);
2355 mapper1.addSupportedKeyCode(AKEYCODE_B);
2356 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2357 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2358 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2359 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2360 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002362 FakeInputMapper& mapper2 =
2363 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002364 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365
2366 InputReaderConfiguration config;
2367 mDevice->configure(ARBITRARY_TIME, &config, 0);
2368
2369 String8 propertyValue;
2370 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2371 << "Device should have read configuration during configuration phase.";
2372 ASSERT_STREQ("value", propertyValue.string());
2373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002374 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2375 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376
2377 // Reset
2378 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002379 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2380 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381
2382 NotifyDeviceResetArgs resetArgs;
2383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2384 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2385 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2386
2387 // Metadata.
2388 ASSERT_FALSE(mDevice->isIgnored());
2389 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2390
2391 InputDeviceInfo info;
2392 mDevice->getDeviceInfo(&info);
2393 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002394 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2396 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2397
2398 // State queries.
2399 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2400 << "Should query mappers and combine meta states.";
2401
2402 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2403 << "Should return unknown key code state when source not supported.";
2404 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2405 << "Should return unknown scan code state when source not supported.";
2406 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2407 << "Should return unknown switch state when source not supported.";
2408
2409 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2410 << "Should query mapper when source is supported.";
2411 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2412 << "Should query mapper when source is supported.";
2413 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2414 << "Should query mapper when source is supported.";
2415
2416 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2417 uint8_t flags[4] = { 0, 0, 0, 1 };
2418 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2419 << "Should do nothing when source is unsupported.";
2420 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2421 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2422 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2423 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2424
2425 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2426 << "Should query mapper when source is supported.";
2427 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2428 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2429 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2430 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2431
2432 // Event handling.
2433 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002434 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435 mDevice->process(&event, 1);
2436
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002437 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2438 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439}
2440
Arthur Hung2c9a3342019-07-23 14:18:59 +08002441// A single input device is associated with a specific display. Check that:
2442// 1. Device is disabled if the viewport corresponding to the associated display is not found
2443// 2. Device is disabled when setEnabled API is called
2444TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002445 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002446
2447 // First Configuration.
2448 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2449
2450 // Device should be enabled by default.
2451 ASSERT_TRUE(mDevice->isEnabled());
2452
2453 // Prepare associated info.
2454 constexpr uint8_t hdmi = 1;
2455 const std::string UNIQUE_ID = "local:1";
2456
2457 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2458 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2459 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2460 // Device should be disabled because it is associated with a specific display via
2461 // input port <-> display port association, but the corresponding display is not found
2462 ASSERT_FALSE(mDevice->isEnabled());
2463
2464 // Prepare displays.
2465 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002466 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2467 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002468 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2469 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2470 ASSERT_TRUE(mDevice->isEnabled());
2471
2472 // Device should be disabled after set disable.
2473 mFakePolicy->addDisabledDevice(mDevice->getId());
2474 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2475 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2476 ASSERT_FALSE(mDevice->isEnabled());
2477
2478 // Device should still be disabled even found the associated display.
2479 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2480 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2481 ASSERT_FALSE(mDevice->isEnabled());
2482}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
2484// --- InputMapperTest ---
2485
2486class InputMapperTest : public testing::Test {
2487protected:
2488 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002489 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490 static const int32_t DEVICE_ID;
2491 static const int32_t DEVICE_GENERATION;
2492 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002493 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002494 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002496 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002498 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002499 std::unique_ptr<InstrumentedInputReader> mReader;
2500 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002501
Chris Ye1b0c7342020-07-28 21:57:03 -07002502 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002503 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002504 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002505 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002506 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2507 mFakeListener);
2508 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509 }
2510
Chris Yea52ade12020-08-27 16:49:20 -07002511 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002512
Chris Yea52ade12020-08-27 16:49:20 -07002513 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 mFakeListener.clear();
2515 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 }
2517
2518 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002519 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002520 }
2521
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002522 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002523 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002524 mReader->requestRefreshConfiguration(changes);
2525 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002526 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002527 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2528 }
2529
arthurhungdcef2dc2020-08-11 14:47:50 +08002530 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2531 const std::string& location, int32_t eventHubId,
2532 Flags<InputDeviceClass> classes) {
2533 InputDeviceIdentifier identifier;
2534 identifier.name = name;
2535 identifier.location = location;
2536 std::shared_ptr<InputDevice> device =
2537 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2538 identifier);
2539 mReader->pushNextDevice(device);
2540 mFakeEventHub->addDevice(eventHubId, name, classes);
2541 mReader->loopOnce();
2542 return device;
2543 }
2544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002545 template <class T, typename... Args>
2546 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002547 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002548 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002550 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002551 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552 }
2553
2554 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002555 int32_t orientation, const std::string& uniqueId,
2556 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002557 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2558 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002559 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2560 }
2561
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002562 void clearViewports() {
2563 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564 }
2565
arthurhungdcef2dc2020-08-11 14:47:50 +08002566 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 RawEvent event;
2568 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002569 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570 event.type = type;
2571 event.code = code;
2572 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002573 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002574 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 }
2576
2577 static void assertMotionRange(const InputDeviceInfo& info,
2578 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2579 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002580 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2582 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2583 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2584 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2585 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2586 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2587 }
2588
2589 static void assertPointerCoords(const PointerCoords& coords,
2590 float x, float y, float pressure, float size,
2591 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2592 float orientation, float distance) {
2593 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2594 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2595 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2596 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2597 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2598 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2599 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2600 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2601 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2602 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2603 }
2604
Michael Wright17db18e2020-06-26 20:51:44 +01002605 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002607 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608 ASSERT_NEAR(x, actualX, 1);
2609 ASSERT_NEAR(y, actualY, 1);
2610 }
2611};
2612
2613const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002614const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002615const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2617const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002618const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2619 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002620const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621
2622// --- SwitchInputMapperTest ---
2623
2624class SwitchInputMapperTest : public InputMapperTest {
2625protected:
2626};
2627
2628TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002629 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002631 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632}
2633
2634TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002635 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002637 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002638 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002640 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002641 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642}
2643
2644TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002645 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002647 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2648 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2649 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2650 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
2652 NotifySwitchArgs args;
2653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2654 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002655 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2656 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657 args.switchMask);
2658 ASSERT_EQ(uint32_t(0), args.policyFlags);
2659}
2660
Chris Ye87143712020-11-10 05:05:58 +00002661// --- VibratorInputMapperTest ---
2662class VibratorInputMapperTest : public InputMapperTest {
2663protected:
2664 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2665};
2666
2667TEST_F(VibratorInputMapperTest, GetSources) {
2668 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2669
2670 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2671}
2672
2673TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2674 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2675
2676 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2677}
2678
2679TEST_F(VibratorInputMapperTest, Vibrate) {
2680 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002681 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002682 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2683
2684 VibrationElement pattern(2);
2685 VibrationSequence sequence(2);
2686 pattern.duration = std::chrono::milliseconds(200);
2687 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2688 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2689 sequence.addElement(pattern);
2690 pattern.duration = std::chrono::milliseconds(500);
2691 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2692 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2693 sequence.addElement(pattern);
2694
2695 std::vector<int64_t> timings = {0, 1};
2696 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2697
2698 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002699 // Start vibrating
2700 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002701 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002702 // Verify vibrator state listener was notified.
2703 mReader->loopOnce();
2704 NotifyVibratorStateArgs args;
2705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2706 ASSERT_EQ(DEVICE_ID, args.deviceId);
2707 ASSERT_TRUE(args.isOn);
2708 // Stop vibrating
2709 mapper.cancelVibrate(VIBRATION_TOKEN);
2710 ASSERT_FALSE(mapper.isVibrating());
2711 // Verify vibrator state listener was notified.
2712 mReader->loopOnce();
2713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2714 ASSERT_EQ(DEVICE_ID, args.deviceId);
2715 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002716}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717
Chris Yef59a2f42020-10-16 12:55:26 -07002718// --- SensorInputMapperTest ---
2719
2720class SensorInputMapperTest : public InputMapperTest {
2721protected:
2722 static const int32_t ACCEL_RAW_MIN;
2723 static const int32_t ACCEL_RAW_MAX;
2724 static const int32_t ACCEL_RAW_FUZZ;
2725 static const int32_t ACCEL_RAW_FLAT;
2726 static const int32_t ACCEL_RAW_RESOLUTION;
2727
2728 static const int32_t GYRO_RAW_MIN;
2729 static const int32_t GYRO_RAW_MAX;
2730 static const int32_t GYRO_RAW_FUZZ;
2731 static const int32_t GYRO_RAW_FLAT;
2732 static const int32_t GYRO_RAW_RESOLUTION;
2733
2734 static const float GRAVITY_MS2_UNIT;
2735 static const float DEGREE_RADIAN_UNIT;
2736
2737 void prepareAccelAxes();
2738 void prepareGyroAxes();
2739 void setAccelProperties();
2740 void setGyroProperties();
2741 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2742};
2743
2744const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2745const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2746const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2747const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2748const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2749
2750const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2751const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2752const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2753const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2754const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2755
2756const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2757const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2758
2759void SensorInputMapperTest::prepareAccelAxes() {
2760 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2761 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2762 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2763 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2764 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2765 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2766}
2767
2768void SensorInputMapperTest::prepareGyroAxes() {
2769 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2770 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2772 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2773 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2774 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2775}
2776
2777void SensorInputMapperTest::setAccelProperties() {
2778 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2779 /* sensorDataIndex */ 0);
2780 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2781 /* sensorDataIndex */ 1);
2782 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2783 /* sensorDataIndex */ 2);
2784 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2785 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2786 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2787 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2788 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2789}
2790
2791void SensorInputMapperTest::setGyroProperties() {
2792 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2793 /* sensorDataIndex */ 0);
2794 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2795 /* sensorDataIndex */ 1);
2796 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2797 /* sensorDataIndex */ 2);
2798 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2799 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2800 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2801 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2802 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2803}
2804
2805TEST_F(SensorInputMapperTest, GetSources) {
2806 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2807
2808 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2809}
2810
2811TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2812 setAccelProperties();
2813 prepareAccelAxes();
2814 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2815
2816 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2817 std::chrono::microseconds(10000),
2818 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002819 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002820 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, 20000);
2821 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, -20000);
2822 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Z, 40000);
2823 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2824 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2825
2826 NotifySensorArgs args;
2827 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2828 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2829 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2830
2831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2832 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2833 ASSERT_EQ(args.deviceId, DEVICE_ID);
2834 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2835 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2836 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2837 ASSERT_EQ(args.values, values);
2838 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2839}
2840
2841TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2842 setGyroProperties();
2843 prepareGyroAxes();
2844 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2845
2846 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2847 std::chrono::microseconds(10000),
2848 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002849 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002850 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RX, 20000);
2851 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RY, -20000);
2852 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RZ, 40000);
2853 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2854 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2855
2856 NotifySensorArgs args;
2857 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2858 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2859 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2860
2861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2862 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2863 ASSERT_EQ(args.deviceId, DEVICE_ID);
2864 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2865 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2866 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2867 ASSERT_EQ(args.values, values);
2868 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2869}
2870
Kim Low03ea0352020-11-06 12:45:07 -08002871// --- BatteryInputMapperTest ---
2872class BatteryInputMapperTest : public InputMapperTest {
2873protected:
2874 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY); }
2875};
2876
2877TEST_F(BatteryInputMapperTest, GetSources) {
2878 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2879
2880 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2881}
2882
2883TEST_F(BatteryInputMapperTest, GetBatteryCapacity) {
2884 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2885
2886 ASSERT_TRUE(mapper.getBatteryCapacity());
2887 ASSERT_EQ(*mapper.getBatteryCapacity(), BATTERY_CAPACITY);
2888}
2889
2890TEST_F(BatteryInputMapperTest, GetBatteryStatus) {
2891 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2892
2893 ASSERT_TRUE(mapper.getBatteryStatus());
2894 ASSERT_EQ(*mapper.getBatteryStatus(), BATTERY_STATUS);
2895}
2896
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897// --- KeyboardInputMapperTest ---
2898
2899class KeyboardInputMapperTest : public InputMapperTest {
2900protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002901 const std::string UNIQUE_ID = "local:0";
2902
2903 void prepareDisplay(int32_t orientation);
2904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002905 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002906 int32_t originalKeyCode, int32_t rotatedKeyCode,
2907 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908};
2909
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002910/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2911 * orientation.
2912 */
2913void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002914 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2915 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002916}
2917
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002918void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002919 int32_t originalScanCode, int32_t originalKeyCode,
2920 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921 NotifyKeyArgs args;
2922
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002923 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2925 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2926 ASSERT_EQ(originalScanCode, args.scanCode);
2927 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002928 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002930 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2932 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2933 ASSERT_EQ(originalScanCode, args.scanCode);
2934 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002935 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936}
2937
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002939 KeyboardInputMapper& mapper =
2940 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2941 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002943 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944}
2945
2946TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2947 const int32_t USAGE_A = 0x070004;
2948 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002949 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2950 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002951 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2952 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2953 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002955 KeyboardInputMapper& mapper =
2956 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2957 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002958 // Initial metastate to AMETA_NONE.
2959 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2960 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961
2962 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002963 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964 NotifyKeyArgs args;
2965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2966 ASSERT_EQ(DEVICE_ID, args.deviceId);
2967 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2968 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2969 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2970 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2971 ASSERT_EQ(KEY_HOME, args.scanCode);
2972 ASSERT_EQ(AMETA_NONE, args.metaState);
2973 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2974 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2975 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2976
2977 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002978 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2980 ASSERT_EQ(DEVICE_ID, args.deviceId);
2981 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2982 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2983 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2984 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2985 ASSERT_EQ(KEY_HOME, args.scanCode);
2986 ASSERT_EQ(AMETA_NONE, args.metaState);
2987 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2988 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2989 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2990
2991 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002992 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2993 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(DEVICE_ID, args.deviceId);
2996 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2997 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2998 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2999 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3000 ASSERT_EQ(0, args.scanCode);
3001 ASSERT_EQ(AMETA_NONE, args.metaState);
3002 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3003 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3004 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3005
3006 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003007 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3008 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3010 ASSERT_EQ(DEVICE_ID, args.deviceId);
3011 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3012 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3014 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3015 ASSERT_EQ(0, args.scanCode);
3016 ASSERT_EQ(AMETA_NONE, args.metaState);
3017 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3018 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3019 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3020
3021 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003022 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3023 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3025 ASSERT_EQ(DEVICE_ID, args.deviceId);
3026 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3027 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3028 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3029 ASSERT_EQ(0, args.keyCode);
3030 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3031 ASSERT_EQ(AMETA_NONE, args.metaState);
3032 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3033 ASSERT_EQ(0U, args.policyFlags);
3034 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3035
3036 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003037 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3038 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3040 ASSERT_EQ(DEVICE_ID, args.deviceId);
3041 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3042 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3043 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3044 ASSERT_EQ(0, args.keyCode);
3045 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3046 ASSERT_EQ(AMETA_NONE, args.metaState);
3047 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3048 ASSERT_EQ(0U, args.policyFlags);
3049 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3050}
3051
3052TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003055 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3056 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3057 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 KeyboardInputMapper& mapper =
3060 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3061 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062
arthurhungc903df12020-08-11 15:08:42 +08003063 // Initial metastate to AMETA_NONE.
3064 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3065 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066
3067 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003068 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 NotifyKeyArgs args;
3070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003072 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003073 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
3075 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003076 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3078 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080
3081 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003082 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3084 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
3087 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003088 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3090 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003092 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093}
3094
3095TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3099 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003101 KeyboardInputMapper& mapper =
3102 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3103 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003105 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003106 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3107 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3108 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3109 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3110 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3111 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3112 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3113 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3114}
3115
3116TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003117 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3118 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3119 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3120 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003123 KeyboardInputMapper& mapper =
3124 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3125 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003127 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003128 ASSERT_NO_FATAL_FAILURE(
3129 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3130 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3131 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3132 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3133 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3134 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3135 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003137 clearViewports();
3138 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003139 ASSERT_NO_FATAL_FAILURE(
3140 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3141 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3142 AKEYCODE_DPAD_UP, DISPLAY_ID));
3143 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3144 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3145 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3146 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003148 clearViewports();
3149 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003150 ASSERT_NO_FATAL_FAILURE(
3151 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3152 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3153 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3154 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3155 AKEYCODE_DPAD_UP, DISPLAY_ID));
3156 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3157 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003159 clearViewports();
3160 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003161 ASSERT_NO_FATAL_FAILURE(
3162 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3163 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3164 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3165 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3166 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3167 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3168 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169
3170 // Special case: if orientation changes while key is down, we still emit the same keycode
3171 // in the key up as we did in the key down.
3172 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003173 clearViewports();
3174 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003175 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3177 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3178 ASSERT_EQ(KEY_UP, args.scanCode);
3179 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3180
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003181 clearViewports();
3182 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003183 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3185 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3186 ASSERT_EQ(KEY_UP, args.scanCode);
3187 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3188}
3189
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003190TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3191 // If the keyboard is not orientation aware,
3192 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003193 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003195 KeyboardInputMapper& mapper =
3196 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3197 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003198 NotifyKeyArgs args;
3199
3200 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003201 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003203 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3205 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3206
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003207 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003208 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003210 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3212 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3213}
3214
3215TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3216 // If the keyboard is orientation aware,
3217 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003218 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003219
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003220 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003221 KeyboardInputMapper& mapper =
3222 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3223 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003224 NotifyKeyArgs args;
3225
3226 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3227 // ^--- already checked by the previous test
3228
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003229 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003230 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003231 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003233 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3235 ASSERT_EQ(DISPLAY_ID, args.displayId);
3236
3237 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003238 clearViewports();
3239 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003240 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003241 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003243 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3245 ASSERT_EQ(newDisplayId, args.displayId);
3246}
3247
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 KeyboardInputMapper& mapper =
3250 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3251 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003253 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003256 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258}
3259
3260TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003261 KeyboardInputMapper& mapper =
3262 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3263 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003266 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003268 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003269 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270}
3271
3272TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 KeyboardInputMapper& mapper =
3274 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3275 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
3279 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3280 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282 ASSERT_TRUE(flags[0]);
3283 ASSERT_FALSE(flags[1]);
3284}
3285
3286TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003287 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3288 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3289 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3291 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3292 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003294 KeyboardInputMapper& mapper =
3295 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3296 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003297 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003298 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3299 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300
3301 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003302 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3303 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3304 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305
3306 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003307 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3308 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003309 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3310 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3311 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003312 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313
3314 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003315 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3316 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3318 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3319 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003320 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321
3322 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003323 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3324 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003325 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3326 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3327 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003328 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
3330 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003331 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3332 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003333 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3334 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3335 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003336 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337
3338 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003339 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3340 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003341 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3342 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3343 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003344 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
3346 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003347 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3348 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003349 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3350 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3351 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003352 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353}
3354
Chris Yea52ade12020-08-27 16:49:20 -07003355TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3356 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3357 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3358 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3359 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3360
3361 KeyboardInputMapper& mapper =
3362 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3363 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3364
3365 // Initial metastate should be AMETA_NONE as no meta keys added.
3366 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3367 // Meta state should be AMETA_NONE after reset
3368 mapper.reset(ARBITRARY_TIME);
3369 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3370 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3371 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3372 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3373
3374 NotifyKeyArgs args;
3375 // Press button "A"
3376 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
3377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3378 ASSERT_EQ(AMETA_NONE, args.metaState);
3379 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3380 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3381 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3382
3383 // Button up.
3384 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
3385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3386 ASSERT_EQ(AMETA_NONE, args.metaState);
3387 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3388 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3389 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3390}
3391
Arthur Hung2c9a3342019-07-23 14:18:59 +08003392TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3393 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003394 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3395 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3396 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3397 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003398
3399 // keyboard 2.
3400 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003401 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003402 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003403 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003404 std::shared_ptr<InputDevice> device2 =
3405 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3406 Flags<InputDeviceClass>(0));
3407
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003408 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3409 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3410 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3411 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003412
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003413 KeyboardInputMapper& mapper =
3414 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3415 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003416
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003417 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003418 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003420 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3421 device2->reset(ARBITRARY_TIME);
3422
3423 // Prepared displays and associated info.
3424 constexpr uint8_t hdmi1 = 0;
3425 constexpr uint8_t hdmi2 = 1;
3426 const std::string SECONDARY_UNIQUE_ID = "local:1";
3427
3428 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3429 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3430
3431 // No associated display viewport found, should disable the device.
3432 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3433 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3434 ASSERT_FALSE(device2->isEnabled());
3435
3436 // Prepare second display.
3437 constexpr int32_t newDisplayId = 2;
3438 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003439 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003440 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003441 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003442 // Default device will reconfigure above, need additional reconfiguration for another device.
3443 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3444 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3445
3446 // Device should be enabled after the associated display is found.
3447 ASSERT_TRUE(mDevice->isEnabled());
3448 ASSERT_TRUE(device2->isEnabled());
3449
3450 // Test pad key events
3451 ASSERT_NO_FATAL_FAILURE(
3452 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3453 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3454 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3455 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3456 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3457 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3458 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3459
3460 ASSERT_NO_FATAL_FAILURE(
3461 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3462 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3463 AKEYCODE_DPAD_RIGHT, newDisplayId));
3464 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3465 AKEYCODE_DPAD_DOWN, newDisplayId));
3466 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3467 AKEYCODE_DPAD_LEFT, newDisplayId));
3468}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
arthurhungc903df12020-08-11 15:08:42 +08003470TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3471 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3472 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3473 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3474 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3476 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3477
3478 KeyboardInputMapper& mapper =
3479 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3481 // Initial metastate to AMETA_NONE.
3482 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3483 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3484
3485 // Initialization should have turned all of the lights off.
3486 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3487 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3488 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3489
3490 // Toggle caps lock on.
3491 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3492 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3493 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3494 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3495
3496 // Toggle num lock on.
3497 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3498 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3499 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3500 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3501
3502 // Toggle scroll lock on.
3503 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3504 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3505 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3506 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3507
3508 mFakeEventHub->removeDevice(EVENTHUB_ID);
3509 mReader->loopOnce();
3510
3511 // keyboard 2 should default toggle keys.
3512 const std::string USB2 = "USB2";
3513 const std::string DEVICE_NAME2 = "KEYBOARD2";
3514 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3515 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3516 std::shared_ptr<InputDevice> device2 =
3517 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3518 Flags<InputDeviceClass>(0));
3519 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3520 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3521 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3522 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3523 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3524 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3525
arthurhung6fe95782020-10-05 22:41:16 +08003526 KeyboardInputMapper& mapper2 =
3527 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3528 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003529 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3530 device2->reset(ARBITRARY_TIME);
3531
3532 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3533 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3534 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003535 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3536 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003537}
3538
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003539// --- KeyboardInputMapperTest_ExternalDevice ---
3540
3541class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3542protected:
Chris Yea52ade12020-08-27 16:49:20 -07003543 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003544};
3545
3546TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003547 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3548 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003549
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3552 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3553 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 KeyboardInputMapper& mapper =
3556 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3557 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003558
3559 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3560 NotifyKeyArgs args;
3561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3562 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3563
3564 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3566 ASSERT_EQ(uint32_t(0), args.policyFlags);
3567
3568 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3570 ASSERT_EQ(uint32_t(0), args.policyFlags);
3571
3572 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3574 ASSERT_EQ(uint32_t(0), args.policyFlags);
3575
3576 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3578 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3579
3580 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3582 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3583}
3584
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003586 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003587
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003588 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3590 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003591
Powei Fengd041c5d2019-05-03 17:11:33 -07003592 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003593 KeyboardInputMapper& mapper =
3594 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3595 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003596
3597 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3598 NotifyKeyArgs args;
3599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3600 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3601
3602 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3604 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3605
3606 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3608 ASSERT_EQ(uint32_t(0), args.policyFlags);
3609
3610 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3612 ASSERT_EQ(uint32_t(0), args.policyFlags);
3613
3614 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3617
3618 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3620 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3621}
3622
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623// --- CursorInputMapperTest ---
3624
3625class CursorInputMapperTest : public InputMapperTest {
3626protected:
3627 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3628
Michael Wright17db18e2020-06-26 20:51:44 +01003629 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003630
Chris Yea52ade12020-08-27 16:49:20 -07003631 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632 InputMapperTest::SetUp();
3633
Michael Wright17db18e2020-06-26 20:51:44 +01003634 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003635 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636 }
3637
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003638 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3639 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003640
3641 void prepareDisplay(int32_t orientation) {
3642 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003643 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003644 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3645 orientation, uniqueId, NO_PORT, viewportType);
3646 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647};
3648
3649const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3650
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003651void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3652 int32_t originalY, int32_t rotatedX,
3653 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654 NotifyMotionArgs args;
3655
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003656 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3657 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3658 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3662 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3663 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3664 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3665}
3666
3667TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003671 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003672}
3673
3674TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003676 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003678 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679}
3680
3681TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003683 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684
3685 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003686 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687
3688 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003689 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3690 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3692 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3693
3694 // When the bounds are set, then there should be a valid motion range.
3695 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3696
3697 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699
3700 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3701 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3702 1, 800 - 1, 0.0f, 0.0f));
3703 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3704 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3705 2, 480 - 1, 0.0f, 0.0f));
3706 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3707 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3708 0.0f, 1.0f, 0.0f, 0.0f));
3709}
3710
3711TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
3715 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
3718 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3719 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3720 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3721 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3722 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3723 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3724 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3725 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3726 0.0f, 1.0f, 0.0f, 0.0f));
3727}
3728
3729TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003731 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732
arthurhungdcef2dc2020-08-11 14:47:50 +08003733 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734
3735 NotifyMotionArgs args;
3736
3737 // Button press.
3738 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003739 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3740 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3742 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3743 ASSERT_EQ(DEVICE_ID, args.deviceId);
3744 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3745 ASSERT_EQ(uint32_t(0), args.policyFlags);
3746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3747 ASSERT_EQ(0, args.flags);
3748 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3749 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3750 ASSERT_EQ(0, args.edgeFlags);
3751 ASSERT_EQ(uint32_t(1), args.pointerCount);
3752 ASSERT_EQ(0, args.pointerProperties[0].id);
3753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3755 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3756 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3757 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3758 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3759
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3761 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3762 ASSERT_EQ(DEVICE_ID, args.deviceId);
3763 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3764 ASSERT_EQ(uint32_t(0), args.policyFlags);
3765 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3766 ASSERT_EQ(0, args.flags);
3767 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3768 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3769 ASSERT_EQ(0, args.edgeFlags);
3770 ASSERT_EQ(uint32_t(1), args.pointerCount);
3771 ASSERT_EQ(0, args.pointerProperties[0].id);
3772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3774 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3775 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3776 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3777 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3778
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003780 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3781 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3783 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3784 ASSERT_EQ(DEVICE_ID, args.deviceId);
3785 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3786 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003787 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3788 ASSERT_EQ(0, args.flags);
3789 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3790 ASSERT_EQ(0, args.buttonState);
3791 ASSERT_EQ(0, args.edgeFlags);
3792 ASSERT_EQ(uint32_t(1), args.pointerCount);
3793 ASSERT_EQ(0, args.pointerProperties[0].id);
3794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3796 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3797 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3798 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3799 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3800
3801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3802 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3803 ASSERT_EQ(DEVICE_ID, args.deviceId);
3804 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3805 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3807 ASSERT_EQ(0, args.flags);
3808 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3809 ASSERT_EQ(0, args.buttonState);
3810 ASSERT_EQ(0, args.edgeFlags);
3811 ASSERT_EQ(uint32_t(1), args.pointerCount);
3812 ASSERT_EQ(0, args.pointerProperties[0].id);
3813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3815 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3816 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3817 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3818 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3819}
3820
3821TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824
3825 NotifyMotionArgs args;
3826
3827 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003828 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3829 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3833 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3834
3835 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003836 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3837 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3841 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3842}
3843
3844TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003846 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847
3848 NotifyMotionArgs args;
3849
3850 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003851 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3852 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3854 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3856 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3857
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3859 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3861 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3862
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003864 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3865 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003867 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3869 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3870
3871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3874 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3875}
3876
3877TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003879 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880
3881 NotifyMotionArgs args;
3882
3883 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003884 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3885 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3886 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3887 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3889 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3891 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3892 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3893
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3897 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3898 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3899
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003901 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3902 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3903 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3907 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3908 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3909
3910 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003911 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3912 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3916 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3917
3918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3921 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3922}
3923
3924TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003926 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003928 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3930 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3931 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3932 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3933 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3934 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3935 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3936 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3937}
3938
3939TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940 addConfigurationProperty("cursor.mode", "navigation");
3941 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003942 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003944 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3946 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3947 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3948 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3949 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3950 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3951 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3952 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3953
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003954 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3956 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3957 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3958 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3959 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3960 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3961 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3962 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3963
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003964 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3966 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3967 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3968 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3969 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3970 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3971 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3972 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3973
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003974 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3976 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3977 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3978 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3979 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3983}
3984
3985TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003987 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988
3989 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3990 mFakePointerController->setPosition(100, 200);
3991 mFakePointerController->setButtonState(0);
3992
3993 NotifyMotionArgs motionArgs;
3994 NotifyKeyArgs keyArgs;
3995
3996 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003997 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3998 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4000 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4001 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4002 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4004 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4005
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4007 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4008 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4009 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4011 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4012
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004013 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
4014 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 ASSERT_EQ(0, motionArgs.buttonState);
4018 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4020 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4021
4022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004023 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024 ASSERT_EQ(0, motionArgs.buttonState);
4025 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4027 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4028
4029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004031 ASSERT_EQ(0, motionArgs.buttonState);
4032 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4034 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4035
4036 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004037 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
4038 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
4039 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4042 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4043 motionArgs.buttonState);
4044 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4045 mFakePointerController->getButtonState());
4046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4047 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4048
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4051 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4052 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4053 mFakePointerController->getButtonState());
4054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4055 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4056
4057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4059 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4060 motionArgs.buttonState);
4061 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4062 mFakePointerController->getButtonState());
4063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4064 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4065
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004066 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
4067 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4071 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4073 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4074
4075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004077 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4078 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4080 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4081
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004082 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
4083 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4086 ASSERT_EQ(0, motionArgs.buttonState);
4087 ASSERT_EQ(0, mFakePointerController->getButtonState());
4088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4089 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 -08004090 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
4091 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004092
4093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_EQ(0, motionArgs.buttonState);
4095 ASSERT_EQ(0, mFakePointerController->getButtonState());
4096 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4098 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 -08004099
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4101 ASSERT_EQ(0, motionArgs.buttonState);
4102 ASSERT_EQ(0, mFakePointerController->getButtonState());
4103 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4105 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4106
4107 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004108 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
4109 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4111 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4112 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4117 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4119 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4120
4121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4123 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4124 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4126 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4127
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004128 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
4129 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 ASSERT_EQ(0, motionArgs.buttonState);
4133 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4135 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4136
4137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004139 ASSERT_EQ(0, motionArgs.buttonState);
4140 ASSERT_EQ(0, mFakePointerController->getButtonState());
4141
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4143 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4147
4148 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004149 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
4150 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4152 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4153 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004154
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4158 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4160 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4161
4162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4164 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4165 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4167 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4168
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004169 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
4170 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 ASSERT_EQ(0, motionArgs.buttonState);
4174 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4176 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 -08004177
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4179 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4180 ASSERT_EQ(0, motionArgs.buttonState);
4181 ASSERT_EQ(0, mFakePointerController->getButtonState());
4182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4183 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4184
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4186 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4187 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4188
4189 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004190 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
4191 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4193 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4194 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4199 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4201 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4202
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4205 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4206 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4208 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4209
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004210 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
4211 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_EQ(0, motionArgs.buttonState);
4215 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4217 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 -08004218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4220 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4221 ASSERT_EQ(0, motionArgs.buttonState);
4222 ASSERT_EQ(0, mFakePointerController->getButtonState());
4223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4224 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4225
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4228 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4229
4230 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004231 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
4232 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4234 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4235 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004236
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4240 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4242 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4243
4244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4246 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4247 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4249 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4250
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004251 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
4252 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_EQ(0, motionArgs.buttonState);
4256 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4258 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 -08004259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4261 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4262 ASSERT_EQ(0, motionArgs.buttonState);
4263 ASSERT_EQ(0, mFakePointerController->getButtonState());
4264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4265 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4266
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4268 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4269 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4270}
4271
4272TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004274 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
4276 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4277 mFakePointerController->setPosition(100, 200);
4278 mFakePointerController->setButtonState(0);
4279
4280 NotifyMotionArgs args;
4281
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004282 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4283 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4284 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004286 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4289 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 +01004290 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004291}
4292
4293TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004294 addConfigurationProperty("cursor.mode", "pointer");
4295 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004296 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004297
4298 NotifyDeviceResetArgs resetArgs;
4299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4300 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4301 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4302
4303 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4304 mFakePointerController->setPosition(100, 200);
4305 mFakePointerController->setButtonState(0);
4306
4307 NotifyMotionArgs args;
4308
4309 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004310 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4311 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4312 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4314 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4317 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 +01004318 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004319
4320 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004321 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4322 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4324 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4327 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4329 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4332 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4333
4334 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004335 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
4336 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4338 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4341 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4343 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4346 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4347
4348 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004349 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
4350 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
4351 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4353 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4356 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 +01004357 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004358
4359 // Disable pointer capture and check that the device generation got bumped
4360 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004361 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004362 mFakePolicy->setPointerCapture(false);
4363 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004364 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004365
4366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4367 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4368 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4369
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004370 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4371 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4372 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4374 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4377 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 +01004378 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379}
4380
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004381TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004382 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004383
Garfield Tan888a6a42020-01-09 11:39:16 -08004384 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004385 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004386 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4387 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004388 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4389 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004390 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4391 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4392
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004393 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4394 mFakePointerController->setPosition(100, 200);
4395 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004396
4397 NotifyMotionArgs args;
4398 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4399 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4400 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4402 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4405 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 +01004406 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004407 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4408}
4409
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410// --- TouchInputMapperTest ---
4411
4412class TouchInputMapperTest : public InputMapperTest {
4413protected:
4414 static const int32_t RAW_X_MIN;
4415 static const int32_t RAW_X_MAX;
4416 static const int32_t RAW_Y_MIN;
4417 static const int32_t RAW_Y_MAX;
4418 static const int32_t RAW_TOUCH_MIN;
4419 static const int32_t RAW_TOUCH_MAX;
4420 static const int32_t RAW_TOOL_MIN;
4421 static const int32_t RAW_TOOL_MAX;
4422 static const int32_t RAW_PRESSURE_MIN;
4423 static const int32_t RAW_PRESSURE_MAX;
4424 static const int32_t RAW_ORIENTATION_MIN;
4425 static const int32_t RAW_ORIENTATION_MAX;
4426 static const int32_t RAW_DISTANCE_MIN;
4427 static const int32_t RAW_DISTANCE_MAX;
4428 static const int32_t RAW_TILT_MIN;
4429 static const int32_t RAW_TILT_MAX;
4430 static const int32_t RAW_ID_MIN;
4431 static const int32_t RAW_ID_MAX;
4432 static const int32_t RAW_SLOT_MIN;
4433 static const int32_t RAW_SLOT_MAX;
4434 static const float X_PRECISION;
4435 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004436 static const float X_PRECISION_VIRTUAL;
4437 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438
4439 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004440 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441
4442 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4443
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004444 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004445 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004446
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 enum Axes {
4448 POSITION = 1 << 0,
4449 TOUCH = 1 << 1,
4450 TOOL = 1 << 2,
4451 PRESSURE = 1 << 3,
4452 ORIENTATION = 1 << 4,
4453 MINOR = 1 << 5,
4454 ID = 1 << 6,
4455 DISTANCE = 1 << 7,
4456 TILT = 1 << 8,
4457 SLOT = 1 << 9,
4458 TOOL_TYPE = 1 << 10,
4459 };
4460
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004461 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4462 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004463 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004465 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 int32_t toRawX(float displayX);
4467 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004468 float toCookedX(float rawX, float rawY);
4469 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004471 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004473 float toDisplayY(int32_t rawY, int32_t displayHeight);
4474
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475};
4476
4477const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4478const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4479const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4480const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4481const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4482const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4483const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4484const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004485const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4486const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4488const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4489const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4490const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4491const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4492const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4493const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4494const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4495const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4496const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4497const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4498const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004499const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4500 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4501const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4502 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004503const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4504 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505
4506const float TouchInputMapperTest::GEOMETRIC_SCALE =
4507 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4508 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4509
4510const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4511 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4512 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4513};
4514
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004515void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004516 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4517 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004518}
4519
4520void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4521 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4522 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523}
4524
Santos Cordonfa5cf462017-04-05 10:37:00 -07004525void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004526 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4527 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4528 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004529}
4530
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004532 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4533 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536}
4537
Jason Gerecke489fda82012-09-07 17:19:40 -07004538void TouchInputMapperTest::prepareLocationCalibration() {
4539 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4540}
4541
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542int32_t TouchInputMapperTest::toRawX(float displayX) {
4543 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4544}
4545
4546int32_t TouchInputMapperTest::toRawY(float displayY) {
4547 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4548}
4549
Jason Gerecke489fda82012-09-07 17:19:40 -07004550float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4551 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4552 return rawX;
4553}
4554
4555float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4556 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4557 return rawY;
4558}
4559
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004561 return toDisplayX(rawX, DISPLAY_WIDTH);
4562}
4563
4564float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4565 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566}
4567
4568float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004569 return toDisplayY(rawY, DISPLAY_HEIGHT);
4570}
4571
4572float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4573 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574}
4575
4576
4577// --- SingleTouchInputMapperTest ---
4578
4579class SingleTouchInputMapperTest : public TouchInputMapperTest {
4580protected:
4581 void prepareButtons();
4582 void prepareAxes(int axes);
4583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004584 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4585 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4586 void processUp(SingleTouchInputMapper& mappery);
4587 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4588 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4589 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4590 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4591 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4592 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593};
4594
4595void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004596 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597}
4598
4599void SingleTouchInputMapperTest::prepareAxes(int axes) {
4600 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004601 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4602 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 }
4604 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004605 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4606 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607 }
4608 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004609 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4610 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 }
4612 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004613 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4614 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 }
4616 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004617 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4618 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 }
4620}
4621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004622void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004623 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4624 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4625 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626}
4627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004628void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004629 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4630 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631}
4632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004633void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004634 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635}
4636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004637void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004638 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639}
4640
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004641void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4642 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004643 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644}
4645
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004646void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004647 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648}
4649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004650void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4651 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004652 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4653 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654}
4655
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004656void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4657 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004658 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659}
4660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004661void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004662 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663}
4664
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 prepareButtons();
4667 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004668 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004670 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671}
4672
4673TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004674 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4675 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676 prepareButtons();
4677 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004678 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004680 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681}
4682
4683TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 prepareButtons();
4685 prepareAxes(POSITION);
4686 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004687 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004689 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690}
4691
4692TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 prepareButtons();
4694 prepareAxes(POSITION);
4695 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004696 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004698 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699}
4700
4701TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702 addConfigurationProperty("touch.deviceType", "touchScreen");
4703 prepareDisplay(DISPLAY_ORIENTATION_0);
4704 prepareButtons();
4705 prepareAxes(POSITION);
4706 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004707 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708
4709 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004710 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
4712 // Virtual key is down.
4713 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4714 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4715 processDown(mapper, x, y);
4716 processSync(mapper);
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4718
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004719 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720
4721 // Virtual key is up.
4722 processUp(mapper);
4723 processSync(mapper);
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004726 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727}
4728
4729TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730 addConfigurationProperty("touch.deviceType", "touchScreen");
4731 prepareDisplay(DISPLAY_ORIENTATION_0);
4732 prepareButtons();
4733 prepareAxes(POSITION);
4734 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004735 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736
4737 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004738 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739
4740 // Virtual key is down.
4741 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4742 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4743 processDown(mapper, x, y);
4744 processSync(mapper);
4745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004747 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748
4749 // Virtual key is up.
4750 processUp(mapper);
4751 processSync(mapper);
4752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4753
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004754 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755}
4756
4757TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758 addConfigurationProperty("touch.deviceType", "touchScreen");
4759 prepareDisplay(DISPLAY_ORIENTATION_0);
4760 prepareButtons();
4761 prepareAxes(POSITION);
4762 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004763 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764
4765 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4766 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004767 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 ASSERT_TRUE(flags[0]);
4769 ASSERT_FALSE(flags[1]);
4770}
4771
4772TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 addConfigurationProperty("touch.deviceType", "touchScreen");
4774 prepareDisplay(DISPLAY_ORIENTATION_0);
4775 prepareButtons();
4776 prepareAxes(POSITION);
4777 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004778 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779
arthurhungdcef2dc2020-08-11 14:47:50 +08004780 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781
4782 NotifyKeyArgs args;
4783
4784 // Press virtual key.
4785 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4786 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4787 processDown(mapper, x, y);
4788 processSync(mapper);
4789
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4791 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4792 ASSERT_EQ(DEVICE_ID, args.deviceId);
4793 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4794 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4795 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4796 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4797 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4798 ASSERT_EQ(KEY_HOME, args.scanCode);
4799 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4800 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4801
4802 // Release virtual key.
4803 processUp(mapper);
4804 processSync(mapper);
4805
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4807 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4808 ASSERT_EQ(DEVICE_ID, args.deviceId);
4809 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4810 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4811 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4812 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4813 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4814 ASSERT_EQ(KEY_HOME, args.scanCode);
4815 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4816 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4817
4818 // Should not have sent any motions.
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4820}
4821
4822TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 addConfigurationProperty("touch.deviceType", "touchScreen");
4824 prepareDisplay(DISPLAY_ORIENTATION_0);
4825 prepareButtons();
4826 prepareAxes(POSITION);
4827 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004828 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829
arthurhungdcef2dc2020-08-11 14:47:50 +08004830 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831
4832 NotifyKeyArgs keyArgs;
4833
4834 // Press virtual key.
4835 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4836 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4837 processDown(mapper, x, y);
4838 processSync(mapper);
4839
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4841 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4842 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4843 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4844 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4845 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4846 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4847 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4848 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4849 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4850 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4851
4852 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4853 // into the display area.
4854 y -= 100;
4855 processMove(mapper, x, y);
4856 processSync(mapper);
4857
4858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4859 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4860 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4861 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4862 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4863 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4864 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4865 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4866 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4867 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4868 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4869 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4870
4871 NotifyMotionArgs motionArgs;
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4874 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4875 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4876 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4878 ASSERT_EQ(0, motionArgs.flags);
4879 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4880 ASSERT_EQ(0, motionArgs.buttonState);
4881 ASSERT_EQ(0, motionArgs.edgeFlags);
4882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4883 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4886 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4887 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4888 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4890
4891 // Keep moving out of bounds. Should generate a pointer move.
4892 y -= 50;
4893 processMove(mapper, x, y);
4894 processSync(mapper);
4895
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4897 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4898 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4899 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4900 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4902 ASSERT_EQ(0, motionArgs.flags);
4903 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4904 ASSERT_EQ(0, motionArgs.buttonState);
4905 ASSERT_EQ(0, motionArgs.edgeFlags);
4906 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4907 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4910 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4911 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4912 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4913 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4914
4915 // Release out of bounds. Should generate a pointer up.
4916 processUp(mapper);
4917 processSync(mapper);
4918
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4920 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4921 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4922 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4923 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4924 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4925 ASSERT_EQ(0, motionArgs.flags);
4926 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4927 ASSERT_EQ(0, motionArgs.buttonState);
4928 ASSERT_EQ(0, motionArgs.edgeFlags);
4929 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4933 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4934 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4935 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4936 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4937
4938 // Should not have sent any more keys or motions.
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4941}
4942
4943TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944 addConfigurationProperty("touch.deviceType", "touchScreen");
4945 prepareDisplay(DISPLAY_ORIENTATION_0);
4946 prepareButtons();
4947 prepareAxes(POSITION);
4948 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004949 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950
arthurhungdcef2dc2020-08-11 14:47:50 +08004951 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952
4953 NotifyMotionArgs motionArgs;
4954
4955 // Initially go down out of bounds.
4956 int32_t x = -10;
4957 int32_t y = -10;
4958 processDown(mapper, x, y);
4959 processSync(mapper);
4960
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4962
4963 // Move into the display area. Should generate a pointer down.
4964 x = 50;
4965 y = 75;
4966 processMove(mapper, x, y);
4967 processSync(mapper);
4968
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4970 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4971 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4972 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4973 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4974 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4975 ASSERT_EQ(0, motionArgs.flags);
4976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4977 ASSERT_EQ(0, motionArgs.buttonState);
4978 ASSERT_EQ(0, motionArgs.edgeFlags);
4979 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4980 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4983 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4984 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4985 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4986 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4987
4988 // Release. Should generate a pointer up.
4989 processUp(mapper);
4990 processSync(mapper);
4991
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4993 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4994 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4995 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4996 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4997 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4998 ASSERT_EQ(0, motionArgs.flags);
4999 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5000 ASSERT_EQ(0, motionArgs.buttonState);
5001 ASSERT_EQ(0, motionArgs.edgeFlags);
5002 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5003 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5006 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5007 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5008 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5010
5011 // Should not have sent any more keys or motions.
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5014}
5015
Santos Cordonfa5cf462017-04-05 10:37:00 -07005016TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005017 addConfigurationProperty("touch.deviceType", "touchScreen");
5018 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5019
5020 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5021 prepareButtons();
5022 prepareAxes(POSITION);
5023 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005024 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005025
arthurhungdcef2dc2020-08-11 14:47:50 +08005026 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005027
5028 NotifyMotionArgs motionArgs;
5029
5030 // Down.
5031 int32_t x = 100;
5032 int32_t y = 125;
5033 processDown(mapper, x, y);
5034 processSync(mapper);
5035
5036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5037 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5038 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5039 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5040 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5041 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5043 ASSERT_EQ(0, motionArgs.flags);
5044 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5045 ASSERT_EQ(0, motionArgs.buttonState);
5046 ASSERT_EQ(0, motionArgs.edgeFlags);
5047 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5048 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5051 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5052 1, 0, 0, 0, 0, 0, 0, 0));
5053 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5054 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5055 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5056
5057 // Move.
5058 x += 50;
5059 y += 75;
5060 processMove(mapper, x, y);
5061 processSync(mapper);
5062
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5064 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5065 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5066 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5067 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5068 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5070 ASSERT_EQ(0, motionArgs.flags);
5071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5072 ASSERT_EQ(0, motionArgs.buttonState);
5073 ASSERT_EQ(0, motionArgs.edgeFlags);
5074 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5078 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5079 1, 0, 0, 0, 0, 0, 0, 0));
5080 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5081 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5083
5084 // Up.
5085 processUp(mapper);
5086 processSync(mapper);
5087
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5090 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5091 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5092 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5093 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5094 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5095 ASSERT_EQ(0, motionArgs.flags);
5096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5097 ASSERT_EQ(0, motionArgs.buttonState);
5098 ASSERT_EQ(0, motionArgs.edgeFlags);
5099 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5103 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5104 1, 0, 0, 0, 0, 0, 0, 0));
5105 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5106 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5107 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5108
5109 // Should not have sent any more keys or motions.
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5112}
5113
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 addConfigurationProperty("touch.deviceType", "touchScreen");
5116 prepareDisplay(DISPLAY_ORIENTATION_0);
5117 prepareButtons();
5118 prepareAxes(POSITION);
5119 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005120 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121
arthurhungdcef2dc2020-08-11 14:47:50 +08005122 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123
5124 NotifyMotionArgs motionArgs;
5125
5126 // Down.
5127 int32_t x = 100;
5128 int32_t y = 125;
5129 processDown(mapper, x, y);
5130 processSync(mapper);
5131
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5134 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5135 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5136 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5137 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5138 ASSERT_EQ(0, motionArgs.flags);
5139 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5140 ASSERT_EQ(0, motionArgs.buttonState);
5141 ASSERT_EQ(0, motionArgs.edgeFlags);
5142 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5143 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5146 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5147 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5148 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5149 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5150
5151 // Move.
5152 x += 50;
5153 y += 75;
5154 processMove(mapper, x, y);
5155 processSync(mapper);
5156
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5158 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5159 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5160 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5161 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5163 ASSERT_EQ(0, motionArgs.flags);
5164 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5165 ASSERT_EQ(0, motionArgs.buttonState);
5166 ASSERT_EQ(0, motionArgs.edgeFlags);
5167 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5168 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5171 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5172 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5173 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5174 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5175
5176 // Up.
5177 processUp(mapper);
5178 processSync(mapper);
5179
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5182 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5183 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5184 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5185 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5186 ASSERT_EQ(0, motionArgs.flags);
5187 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5188 ASSERT_EQ(0, motionArgs.buttonState);
5189 ASSERT_EQ(0, motionArgs.edgeFlags);
5190 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5191 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5194 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5195 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5196 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5198
5199 // Should not have sent any more keys or motions.
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5202}
5203
5204TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205 addConfigurationProperty("touch.deviceType", "touchScreen");
5206 prepareButtons();
5207 prepareAxes(POSITION);
5208 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005209 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210
5211 NotifyMotionArgs args;
5212
5213 // Rotation 90.
5214 prepareDisplay(DISPLAY_ORIENTATION_90);
5215 processDown(mapper, toRawX(50), toRawY(75));
5216 processSync(mapper);
5217
5218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5219 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5220 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5221
5222 processUp(mapper);
5223 processSync(mapper);
5224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5225}
5226
5227TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228 addConfigurationProperty("touch.deviceType", "touchScreen");
5229 prepareButtons();
5230 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232
5233 NotifyMotionArgs args;
5234
5235 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005236 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237 prepareDisplay(DISPLAY_ORIENTATION_0);
5238 processDown(mapper, toRawX(50), toRawY(75));
5239 processSync(mapper);
5240
5241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5242 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5243 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5244
5245 processUp(mapper);
5246 processSync(mapper);
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5248
5249 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005250 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005251 prepareDisplay(DISPLAY_ORIENTATION_90);
5252 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5253 processSync(mapper);
5254
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5256 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5257 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5258
5259 processUp(mapper);
5260 processSync(mapper);
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5262
5263 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005264 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265 prepareDisplay(DISPLAY_ORIENTATION_180);
5266 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5267 processSync(mapper);
5268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5270 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5271 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5272
5273 processUp(mapper);
5274 processSync(mapper);
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5276
5277 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005278 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279 prepareDisplay(DISPLAY_ORIENTATION_270);
5280 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5281 processSync(mapper);
5282
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5284 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5285 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5286
5287 processUp(mapper);
5288 processSync(mapper);
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5290}
5291
5292TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293 addConfigurationProperty("touch.deviceType", "touchScreen");
5294 prepareDisplay(DISPLAY_ORIENTATION_0);
5295 prepareButtons();
5296 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005297 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298
5299 // These calculations are based on the input device calibration documentation.
5300 int32_t rawX = 100;
5301 int32_t rawY = 200;
5302 int32_t rawPressure = 10;
5303 int32_t rawToolMajor = 12;
5304 int32_t rawDistance = 2;
5305 int32_t rawTiltX = 30;
5306 int32_t rawTiltY = 110;
5307
5308 float x = toDisplayX(rawX);
5309 float y = toDisplayY(rawY);
5310 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5311 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5312 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5313 float distance = float(rawDistance);
5314
5315 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5316 float tiltScale = M_PI / 180;
5317 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5318 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5319 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5320 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5321
5322 processDown(mapper, rawX, rawY);
5323 processPressure(mapper, rawPressure);
5324 processToolMajor(mapper, rawToolMajor);
5325 processDistance(mapper, rawDistance);
5326 processTilt(mapper, rawTiltX, rawTiltY);
5327 processSync(mapper);
5328
5329 NotifyMotionArgs args;
5330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5332 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5333 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5334}
5335
Jason Gerecke489fda82012-09-07 17:19:40 -07005336TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005337 addConfigurationProperty("touch.deviceType", "touchScreen");
5338 prepareDisplay(DISPLAY_ORIENTATION_0);
5339 prepareLocationCalibration();
5340 prepareButtons();
5341 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005342 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005343
5344 int32_t rawX = 100;
5345 int32_t rawY = 200;
5346
5347 float x = toDisplayX(toCookedX(rawX, rawY));
5348 float y = toDisplayY(toCookedY(rawX, rawY));
5349
5350 processDown(mapper, rawX, rawY);
5351 processSync(mapper);
5352
5353 NotifyMotionArgs args;
5354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5356 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5357}
5358
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360 addConfigurationProperty("touch.deviceType", "touchScreen");
5361 prepareDisplay(DISPLAY_ORIENTATION_0);
5362 prepareButtons();
5363 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365
5366 NotifyMotionArgs motionArgs;
5367 NotifyKeyArgs keyArgs;
5368
5369 processDown(mapper, 100, 200);
5370 processSync(mapper);
5371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5373 ASSERT_EQ(0, motionArgs.buttonState);
5374
5375 // press BTN_LEFT, release BTN_LEFT
5376 processKey(mapper, BTN_LEFT, 1);
5377 processSync(mapper);
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5380 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5381
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5384 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5385
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386 processKey(mapper, BTN_LEFT, 0);
5387 processSync(mapper);
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005391
5392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005394 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395
5396 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5397 processKey(mapper, BTN_RIGHT, 1);
5398 processKey(mapper, BTN_MIDDLE, 1);
5399 processSync(mapper);
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5402 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5403 motionArgs.buttonState);
5404
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5407 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5408
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5411 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5412 motionArgs.buttonState);
5413
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 processKey(mapper, BTN_RIGHT, 0);
5415 processSync(mapper);
5416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005419
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005422 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423
5424 processKey(mapper, BTN_MIDDLE, 0);
5425 processSync(mapper);
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005429
5430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005432 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433
5434 // press BTN_BACK, release BTN_BACK
5435 processKey(mapper, BTN_BACK, 1);
5436 processSync(mapper);
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5438 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5439 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005440
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005443 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5447 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448
5449 processKey(mapper, BTN_BACK, 0);
5450 processSync(mapper);
5451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005452 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005457 ASSERT_EQ(0, motionArgs.buttonState);
5458
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5460 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5461 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5462
5463 // press BTN_SIDE, release BTN_SIDE
5464 processKey(mapper, BTN_SIDE, 1);
5465 processSync(mapper);
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5467 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5468 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005469
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005472 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5473
5474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5476 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477
5478 processKey(mapper, BTN_SIDE, 0);
5479 processSync(mapper);
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005483
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005486 ASSERT_EQ(0, motionArgs.buttonState);
5487
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5489 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5490 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5491
5492 // press BTN_FORWARD, release BTN_FORWARD
5493 processKey(mapper, BTN_FORWARD, 1);
5494 processSync(mapper);
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5496 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5497 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005498
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005501 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5505 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506
5507 processKey(mapper, BTN_FORWARD, 0);
5508 processSync(mapper);
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005512
5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005515 ASSERT_EQ(0, motionArgs.buttonState);
5516
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5518 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5519 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5520
5521 // press BTN_EXTRA, release BTN_EXTRA
5522 processKey(mapper, BTN_EXTRA, 1);
5523 processSync(mapper);
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5525 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5526 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005527
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005530 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5531
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5534 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005535
5536 processKey(mapper, BTN_EXTRA, 0);
5537 processSync(mapper);
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005541
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005544 ASSERT_EQ(0, motionArgs.buttonState);
5545
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5547 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5548 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5549
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5551
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552 // press BTN_STYLUS, release BTN_STYLUS
5553 processKey(mapper, BTN_STYLUS, 1);
5554 processSync(mapper);
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5558
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5560 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5561 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562
5563 processKey(mapper, BTN_STYLUS, 0);
5564 processSync(mapper);
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005566 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005568
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005571 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572
5573 // press BTN_STYLUS2, release BTN_STYLUS2
5574 processKey(mapper, BTN_STYLUS2, 1);
5575 processSync(mapper);
5576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005578 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5582 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583
5584 processKey(mapper, BTN_STYLUS2, 0);
5585 processSync(mapper);
5586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005589
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005592 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593
5594 // release touch
5595 processUp(mapper);
5596 processSync(mapper);
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5598 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5599 ASSERT_EQ(0, motionArgs.buttonState);
5600}
5601
5602TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 addConfigurationProperty("touch.deviceType", "touchScreen");
5604 prepareDisplay(DISPLAY_ORIENTATION_0);
5605 prepareButtons();
5606 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005607 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608
5609 NotifyMotionArgs motionArgs;
5610
5611 // default tool type is finger
5612 processDown(mapper, 100, 200);
5613 processSync(mapper);
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5617
5618 // eraser
5619 processKey(mapper, BTN_TOOL_RUBBER, 1);
5620 processSync(mapper);
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5624
5625 // stylus
5626 processKey(mapper, BTN_TOOL_RUBBER, 0);
5627 processKey(mapper, BTN_TOOL_PEN, 1);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5632
5633 // brush
5634 processKey(mapper, BTN_TOOL_PEN, 0);
5635 processKey(mapper, BTN_TOOL_BRUSH, 1);
5636 processSync(mapper);
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5640
5641 // pencil
5642 processKey(mapper, BTN_TOOL_BRUSH, 0);
5643 processKey(mapper, BTN_TOOL_PENCIL, 1);
5644 processSync(mapper);
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5648
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005649 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650 processKey(mapper, BTN_TOOL_PENCIL, 0);
5651 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5652 processSync(mapper);
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5656
5657 // mouse
5658 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5659 processKey(mapper, BTN_TOOL_MOUSE, 1);
5660 processSync(mapper);
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5664
5665 // lens
5666 processKey(mapper, BTN_TOOL_MOUSE, 0);
5667 processKey(mapper, BTN_TOOL_LENS, 1);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5672
5673 // double-tap
5674 processKey(mapper, BTN_TOOL_LENS, 0);
5675 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5676 processSync(mapper);
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5680
5681 // triple-tap
5682 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5683 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5684 processSync(mapper);
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5688
5689 // quad-tap
5690 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5691 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5692 processSync(mapper);
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5696
5697 // finger
5698 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5699 processKey(mapper, BTN_TOOL_FINGER, 1);
5700 processSync(mapper);
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5704
5705 // stylus trumps finger
5706 processKey(mapper, BTN_TOOL_PEN, 1);
5707 processSync(mapper);
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5711
5712 // eraser trumps stylus
5713 processKey(mapper, BTN_TOOL_RUBBER, 1);
5714 processSync(mapper);
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5718
5719 // mouse trumps eraser
5720 processKey(mapper, BTN_TOOL_MOUSE, 1);
5721 processSync(mapper);
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5725
5726 // back to default tool type
5727 processKey(mapper, BTN_TOOL_MOUSE, 0);
5728 processKey(mapper, BTN_TOOL_RUBBER, 0);
5729 processKey(mapper, BTN_TOOL_PEN, 0);
5730 processKey(mapper, BTN_TOOL_FINGER, 0);
5731 processSync(mapper);
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5735}
5736
5737TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 addConfigurationProperty("touch.deviceType", "touchScreen");
5739 prepareDisplay(DISPLAY_ORIENTATION_0);
5740 prepareButtons();
5741 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005742 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005743 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744
5745 NotifyMotionArgs motionArgs;
5746
5747 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5748 processKey(mapper, BTN_TOOL_FINGER, 1);
5749 processMove(mapper, 100, 200);
5750 processSync(mapper);
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5754 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5755
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5759 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5760
5761 // move a little
5762 processMove(mapper, 150, 250);
5763 processSync(mapper);
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5767 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5768
5769 // down when BTN_TOUCH is pressed, pressure defaults to 1
5770 processKey(mapper, BTN_TOUCH, 1);
5771 processSync(mapper);
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5775 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5776
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5780 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5781
5782 // up when BTN_TOUCH is released, hover restored
5783 processKey(mapper, BTN_TOUCH, 0);
5784 processSync(mapper);
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5788 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5789
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5793 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5794
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5798 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5799
5800 // exit hover when pointer goes away
5801 processKey(mapper, BTN_TOOL_FINGER, 0);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5806 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5807}
5808
5809TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810 addConfigurationProperty("touch.deviceType", "touchScreen");
5811 prepareDisplay(DISPLAY_ORIENTATION_0);
5812 prepareButtons();
5813 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005814 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815
5816 NotifyMotionArgs motionArgs;
5817
5818 // initially hovering because pressure is 0
5819 processDown(mapper, 100, 200);
5820 processPressure(mapper, 0);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5825 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5826
5827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5830 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5831
5832 // move a little
5833 processMove(mapper, 150, 250);
5834 processSync(mapper);
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5838 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5839
5840 // down when pressure is non-zero
5841 processPressure(mapper, RAW_PRESSURE_MAX);
5842 processSync(mapper);
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5844 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5846 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5851 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5852
5853 // up when pressure becomes 0, hover restored
5854 processPressure(mapper, 0);
5855 processSync(mapper);
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5859 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5864 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5865
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5867 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5869 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5870
5871 // exit hover when pointer goes away
5872 processUp(mapper);
5873 processSync(mapper);
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5875 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5877 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5878}
5879
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880// --- MultiTouchInputMapperTest ---
5881
5882class MultiTouchInputMapperTest : public TouchInputMapperTest {
5883protected:
5884 void prepareAxes(int axes);
5885
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005886 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5887 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5888 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5889 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5890 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5891 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5892 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5893 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5894 void processId(MultiTouchInputMapper& mapper, int32_t id);
5895 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5896 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5897 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5898 void processMTSync(MultiTouchInputMapper& mapper);
5899 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900};
5901
5902void MultiTouchInputMapperTest::prepareAxes(int axes) {
5903 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005904 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5905 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 }
5907 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5909 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005911 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5912 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 }
5914 }
5915 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5917 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005919 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5920 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921 }
5922 }
5923 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5925 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 }
5927 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005928 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5929 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930 }
5931 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005932 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5933 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 }
5935 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005936 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5937 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 }
5939 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005940 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5941 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 }
5943 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005944 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 }
5946}
5947
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005948void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5949 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005950 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5951 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952}
5953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005954void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5955 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005956 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957}
5958
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005959void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5960 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005961 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962}
5963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005964void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005965 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966}
5967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005968void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005969 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970}
5971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005972void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5973 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005974 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975}
5976
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005977void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005978 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979}
5980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005981void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005982 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983}
5984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005985void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005986 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987}
5988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005989void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005990 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991}
5992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005993void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005994 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995}
5996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005997void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5998 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005999 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000}
6001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006002void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006003 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004}
6005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006006void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006007 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008}
6009
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006011 addConfigurationProperty("touch.deviceType", "touchScreen");
6012 prepareDisplay(DISPLAY_ORIENTATION_0);
6013 prepareAxes(POSITION);
6014 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006015 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016
arthurhungdcef2dc2020-08-11 14:47:50 +08006017 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018
6019 NotifyMotionArgs motionArgs;
6020
6021 // Two fingers down at once.
6022 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6023 processPosition(mapper, x1, y1);
6024 processMTSync(mapper);
6025 processPosition(mapper, x2, y2);
6026 processMTSync(mapper);
6027 processSync(mapper);
6028
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6031 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6032 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6033 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6035 ASSERT_EQ(0, motionArgs.flags);
6036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6037 ASSERT_EQ(0, motionArgs.buttonState);
6038 ASSERT_EQ(0, motionArgs.edgeFlags);
6039 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6040 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6043 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6044 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6045 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6046 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6050 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6051 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6052 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6053 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6054 motionArgs.action);
6055 ASSERT_EQ(0, motionArgs.flags);
6056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6057 ASSERT_EQ(0, motionArgs.buttonState);
6058 ASSERT_EQ(0, motionArgs.edgeFlags);
6059 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6060 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6062 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6065 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6067 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6068 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6069 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6070 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6071
6072 // Move.
6073 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6074 processPosition(mapper, x1, y1);
6075 processMTSync(mapper);
6076 processPosition(mapper, x2, y2);
6077 processMTSync(mapper);
6078 processSync(mapper);
6079
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6082 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6083 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6084 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6086 ASSERT_EQ(0, motionArgs.flags);
6087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6088 ASSERT_EQ(0, motionArgs.buttonState);
6089 ASSERT_EQ(0, motionArgs.edgeFlags);
6090 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6093 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6096 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6098 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6099 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6100 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6102
6103 // First finger up.
6104 x2 += 15; y2 -= 20;
6105 processPosition(mapper, x2, y2);
6106 processMTSync(mapper);
6107 processSync(mapper);
6108
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6111 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6112 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6113 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6114 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6115 motionArgs.action);
6116 ASSERT_EQ(0, motionArgs.flags);
6117 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6118 ASSERT_EQ(0, motionArgs.buttonState);
6119 ASSERT_EQ(0, motionArgs.edgeFlags);
6120 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6121 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6123 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6126 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6128 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6129 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6130 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6131 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6135 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6136 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6137 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6139 ASSERT_EQ(0, motionArgs.flags);
6140 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6141 ASSERT_EQ(0, motionArgs.buttonState);
6142 ASSERT_EQ(0, motionArgs.edgeFlags);
6143 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6144 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6147 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6148 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6149 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6150 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6151
6152 // Move.
6153 x2 += 20; y2 -= 25;
6154 processPosition(mapper, x2, y2);
6155 processMTSync(mapper);
6156 processSync(mapper);
6157
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6160 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6161 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6162 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6164 ASSERT_EQ(0, motionArgs.flags);
6165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6166 ASSERT_EQ(0, motionArgs.buttonState);
6167 ASSERT_EQ(0, motionArgs.edgeFlags);
6168 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6169 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6172 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6173 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6174 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6175 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6176
6177 // New finger down.
6178 int32_t x3 = 700, y3 = 300;
6179 processPosition(mapper, x2, y2);
6180 processMTSync(mapper);
6181 processPosition(mapper, x3, y3);
6182 processMTSync(mapper);
6183 processSync(mapper);
6184
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6187 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6188 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6189 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6190 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6191 motionArgs.action);
6192 ASSERT_EQ(0, motionArgs.flags);
6193 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6194 ASSERT_EQ(0, motionArgs.buttonState);
6195 ASSERT_EQ(0, motionArgs.edgeFlags);
6196 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6197 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6199 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6202 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6204 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6205 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6206 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6207 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6208
6209 // Second finger up.
6210 x3 += 30; y3 -= 20;
6211 processPosition(mapper, x3, y3);
6212 processMTSync(mapper);
6213 processSync(mapper);
6214
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6221 motionArgs.action);
6222 ASSERT_EQ(0, motionArgs.flags);
6223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6224 ASSERT_EQ(0, motionArgs.buttonState);
6225 ASSERT_EQ(0, motionArgs.edgeFlags);
6226 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6232 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6241 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6242 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6243 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6245 ASSERT_EQ(0, motionArgs.flags);
6246 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6247 ASSERT_EQ(0, motionArgs.buttonState);
6248 ASSERT_EQ(0, motionArgs.edgeFlags);
6249 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6250 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6253 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6254 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6255 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6257
6258 // Last finger up.
6259 processMTSync(mapper);
6260 processSync(mapper);
6261
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6264 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6265 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6266 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6268 ASSERT_EQ(0, motionArgs.flags);
6269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6270 ASSERT_EQ(0, motionArgs.buttonState);
6271 ASSERT_EQ(0, motionArgs.edgeFlags);
6272 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6273 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6276 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6277 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6278 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6280
6281 // Should not have sent any more keys or motions.
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6284}
6285
6286TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 addConfigurationProperty("touch.deviceType", "touchScreen");
6288 prepareDisplay(DISPLAY_ORIENTATION_0);
6289 prepareAxes(POSITION | ID);
6290 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006291 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292
arthurhungdcef2dc2020-08-11 14:47:50 +08006293 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006294
6295 NotifyMotionArgs motionArgs;
6296
6297 // Two fingers down at once.
6298 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6299 processPosition(mapper, x1, y1);
6300 processId(mapper, 1);
6301 processMTSync(mapper);
6302 processPosition(mapper, x2, y2);
6303 processId(mapper, 2);
6304 processMTSync(mapper);
6305 processSync(mapper);
6306
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6308 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6309 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6310 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6313 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6317 motionArgs.action);
6318 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6321 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6324 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6326 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6327
6328 // Move.
6329 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6330 processPosition(mapper, x1, y1);
6331 processId(mapper, 1);
6332 processMTSync(mapper);
6333 processPosition(mapper, x2, y2);
6334 processId(mapper, 2);
6335 processMTSync(mapper);
6336 processSync(mapper);
6337
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6340 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6341 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6343 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6346 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6348 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6349
6350 // First finger up.
6351 x2 += 15; y2 -= 20;
6352 processPosition(mapper, x2, y2);
6353 processId(mapper, 2);
6354 processMTSync(mapper);
6355 processSync(mapper);
6356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6359 motionArgs.action);
6360 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6361 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6363 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6366 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6368 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6369
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6373 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6376 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6377
6378 // Move.
6379 x2 += 20; y2 -= 25;
6380 processPosition(mapper, x2, y2);
6381 processId(mapper, 2);
6382 processMTSync(mapper);
6383 processSync(mapper);
6384
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6387 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6388 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6391 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6392
6393 // New finger down.
6394 int32_t x3 = 700, y3 = 300;
6395 processPosition(mapper, x2, y2);
6396 processId(mapper, 2);
6397 processMTSync(mapper);
6398 processPosition(mapper, x3, y3);
6399 processId(mapper, 3);
6400 processMTSync(mapper);
6401 processSync(mapper);
6402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6405 motionArgs.action);
6406 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6407 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6409 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6412 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6414 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6415
6416 // Second finger up.
6417 x3 += 30; y3 -= 20;
6418 processPosition(mapper, x3, y3);
6419 processId(mapper, 3);
6420 processMTSync(mapper);
6421 processSync(mapper);
6422
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6425 motionArgs.action);
6426 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6427 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6429 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6432 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6434 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6435
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6438 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6443
6444 // Last finger up.
6445 processMTSync(mapper);
6446 processSync(mapper);
6447
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6454 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6455
6456 // Should not have sent any more keys or motions.
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6459}
6460
6461TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462 addConfigurationProperty("touch.deviceType", "touchScreen");
6463 prepareDisplay(DISPLAY_ORIENTATION_0);
6464 prepareAxes(POSITION | ID | SLOT);
6465 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006467
arthurhungdcef2dc2020-08-11 14:47:50 +08006468 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469
6470 NotifyMotionArgs motionArgs;
6471
6472 // Two fingers down at once.
6473 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6474 processPosition(mapper, x1, y1);
6475 processId(mapper, 1);
6476 processSlot(mapper, 1);
6477 processPosition(mapper, x2, y2);
6478 processId(mapper, 2);
6479 processSync(mapper);
6480
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6482 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6483 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6484 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6487 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6488
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6490 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6491 motionArgs.action);
6492 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6495 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6498 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6500 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6501
6502 // Move.
6503 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6504 processSlot(mapper, 0);
6505 processPosition(mapper, x1, y1);
6506 processSlot(mapper, 1);
6507 processPosition(mapper, x2, y2);
6508 processSync(mapper);
6509
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6512 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6515 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6518 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6520 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6521
6522 // First finger up.
6523 x2 += 15; y2 -= 20;
6524 processSlot(mapper, 0);
6525 processId(mapper, -1);
6526 processSlot(mapper, 1);
6527 processPosition(mapper, x2, y2);
6528 processSync(mapper);
6529
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6532 motionArgs.action);
6533 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6536 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6539 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6541 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6542
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6546 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6549 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6550
6551 // Move.
6552 x2 += 20; y2 -= 25;
6553 processPosition(mapper, x2, y2);
6554 processSync(mapper);
6555
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6558 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6559 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6563
6564 // New finger down.
6565 int32_t x3 = 700, y3 = 300;
6566 processPosition(mapper, x2, y2);
6567 processSlot(mapper, 0);
6568 processId(mapper, 3);
6569 processPosition(mapper, x3, y3);
6570 processSync(mapper);
6571
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6574 motionArgs.action);
6575 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6578 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6581 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6583 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6584
6585 // Second finger up.
6586 x3 += 30; y3 -= 20;
6587 processSlot(mapper, 1);
6588 processId(mapper, -1);
6589 processSlot(mapper, 0);
6590 processPosition(mapper, x3, y3);
6591 processSync(mapper);
6592
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6595 motionArgs.action);
6596 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6597 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6599 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6602 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6605
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6608 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6612 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6613
6614 // Last finger up.
6615 processId(mapper, -1);
6616 processSync(mapper);
6617
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6620 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6624 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6625
6626 // Should not have sent any more keys or motions.
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6629}
6630
6631TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006632 addConfigurationProperty("touch.deviceType", "touchScreen");
6633 prepareDisplay(DISPLAY_ORIENTATION_0);
6634 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006635 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006636
6637 // These calculations are based on the input device calibration documentation.
6638 int32_t rawX = 100;
6639 int32_t rawY = 200;
6640 int32_t rawTouchMajor = 7;
6641 int32_t rawTouchMinor = 6;
6642 int32_t rawToolMajor = 9;
6643 int32_t rawToolMinor = 8;
6644 int32_t rawPressure = 11;
6645 int32_t rawDistance = 0;
6646 int32_t rawOrientation = 3;
6647 int32_t id = 5;
6648
6649 float x = toDisplayX(rawX);
6650 float y = toDisplayY(rawY);
6651 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6652 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6653 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6654 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6655 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6656 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6657 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6658 float distance = float(rawDistance);
6659
6660 processPosition(mapper, rawX, rawY);
6661 processTouchMajor(mapper, rawTouchMajor);
6662 processTouchMinor(mapper, rawTouchMinor);
6663 processToolMajor(mapper, rawToolMajor);
6664 processToolMinor(mapper, rawToolMinor);
6665 processPressure(mapper, rawPressure);
6666 processOrientation(mapper, rawOrientation);
6667 processDistance(mapper, rawDistance);
6668 processId(mapper, id);
6669 processMTSync(mapper);
6670 processSync(mapper);
6671
6672 NotifyMotionArgs args;
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6674 ASSERT_EQ(0, args.pointerProperties[0].id);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6676 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6677 orientation, distance));
6678}
6679
6680TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006681 addConfigurationProperty("touch.deviceType", "touchScreen");
6682 prepareDisplay(DISPLAY_ORIENTATION_0);
6683 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6684 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006685 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006686
6687 // These calculations are based on the input device calibration documentation.
6688 int32_t rawX = 100;
6689 int32_t rawY = 200;
6690 int32_t rawTouchMajor = 140;
6691 int32_t rawTouchMinor = 120;
6692 int32_t rawToolMajor = 180;
6693 int32_t rawToolMinor = 160;
6694
6695 float x = toDisplayX(rawX);
6696 float y = toDisplayY(rawY);
6697 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6698 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6699 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6700 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6701 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6702
6703 processPosition(mapper, rawX, rawY);
6704 processTouchMajor(mapper, rawTouchMajor);
6705 processTouchMinor(mapper, rawTouchMinor);
6706 processToolMajor(mapper, rawToolMajor);
6707 processToolMinor(mapper, rawToolMinor);
6708 processMTSync(mapper);
6709 processSync(mapper);
6710
6711 NotifyMotionArgs args;
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6714 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6715}
6716
6717TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718 addConfigurationProperty("touch.deviceType", "touchScreen");
6719 prepareDisplay(DISPLAY_ORIENTATION_0);
6720 prepareAxes(POSITION | TOUCH | TOOL);
6721 addConfigurationProperty("touch.size.calibration", "diameter");
6722 addConfigurationProperty("touch.size.scale", "10");
6723 addConfigurationProperty("touch.size.bias", "160");
6724 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006725 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006726
6727 // These calculations are based on the input device calibration documentation.
6728 // Note: We only provide a single common touch/tool value because the device is assumed
6729 // not to emit separate values for each pointer (isSummed = 1).
6730 int32_t rawX = 100;
6731 int32_t rawY = 200;
6732 int32_t rawX2 = 150;
6733 int32_t rawY2 = 250;
6734 int32_t rawTouchMajor = 5;
6735 int32_t rawToolMajor = 8;
6736
6737 float x = toDisplayX(rawX);
6738 float y = toDisplayY(rawY);
6739 float x2 = toDisplayX(rawX2);
6740 float y2 = toDisplayY(rawY2);
6741 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6742 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6743 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6744
6745 processPosition(mapper, rawX, rawY);
6746 processTouchMajor(mapper, rawTouchMajor);
6747 processToolMajor(mapper, rawToolMajor);
6748 processMTSync(mapper);
6749 processPosition(mapper, rawX2, rawY2);
6750 processTouchMajor(mapper, rawTouchMajor);
6751 processToolMajor(mapper, rawToolMajor);
6752 processMTSync(mapper);
6753 processSync(mapper);
6754
6755 NotifyMotionArgs args;
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6758
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6761 args.action);
6762 ASSERT_EQ(size_t(2), args.pointerCount);
6763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6764 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6766 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6767}
6768
6769TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006770 addConfigurationProperty("touch.deviceType", "touchScreen");
6771 prepareDisplay(DISPLAY_ORIENTATION_0);
6772 prepareAxes(POSITION | TOUCH | TOOL);
6773 addConfigurationProperty("touch.size.calibration", "area");
6774 addConfigurationProperty("touch.size.scale", "43");
6775 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006776 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006777
6778 // These calculations are based on the input device calibration documentation.
6779 int32_t rawX = 100;
6780 int32_t rawY = 200;
6781 int32_t rawTouchMajor = 5;
6782 int32_t rawToolMajor = 8;
6783
6784 float x = toDisplayX(rawX);
6785 float y = toDisplayY(rawY);
6786 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6787 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6788 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6789
6790 processPosition(mapper, rawX, rawY);
6791 processTouchMajor(mapper, rawTouchMajor);
6792 processToolMajor(mapper, rawToolMajor);
6793 processMTSync(mapper);
6794 processSync(mapper);
6795
6796 NotifyMotionArgs args;
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6799 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6800}
6801
6802TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006803 addConfigurationProperty("touch.deviceType", "touchScreen");
6804 prepareDisplay(DISPLAY_ORIENTATION_0);
6805 prepareAxes(POSITION | PRESSURE);
6806 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6807 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006808 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006809
Michael Wrightaa449c92017-12-13 21:21:43 +00006810 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006811 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006812 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6813 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6814 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6815
Michael Wrightd02c5b62014-02-10 15:10:22 -08006816 // These calculations are based on the input device calibration documentation.
6817 int32_t rawX = 100;
6818 int32_t rawY = 200;
6819 int32_t rawPressure = 60;
6820
6821 float x = toDisplayX(rawX);
6822 float y = toDisplayY(rawY);
6823 float pressure = float(rawPressure) * 0.01f;
6824
6825 processPosition(mapper, rawX, rawY);
6826 processPressure(mapper, rawPressure);
6827 processMTSync(mapper);
6828 processSync(mapper);
6829
6830 NotifyMotionArgs args;
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6833 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6834}
6835
6836TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006837 addConfigurationProperty("touch.deviceType", "touchScreen");
6838 prepareDisplay(DISPLAY_ORIENTATION_0);
6839 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006840 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006841
6842 NotifyMotionArgs motionArgs;
6843 NotifyKeyArgs keyArgs;
6844
6845 processId(mapper, 1);
6846 processPosition(mapper, 100, 200);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6850 ASSERT_EQ(0, motionArgs.buttonState);
6851
6852 // press BTN_LEFT, release BTN_LEFT
6853 processKey(mapper, BTN_LEFT, 1);
6854 processSync(mapper);
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6857 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6858
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6861 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6862
Michael Wrightd02c5b62014-02-10 15:10:22 -08006863 processKey(mapper, BTN_LEFT, 0);
6864 processSync(mapper);
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006868
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006871 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006872
6873 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6874 processKey(mapper, BTN_RIGHT, 1);
6875 processKey(mapper, BTN_MIDDLE, 1);
6876 processSync(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6879 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6880 motionArgs.buttonState);
6881
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6884 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6885
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6888 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6889 motionArgs.buttonState);
6890
Michael Wrightd02c5b62014-02-10 15:10:22 -08006891 processKey(mapper, BTN_RIGHT, 0);
6892 processSync(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006895 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006896
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006899 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006900
6901 processKey(mapper, BTN_MIDDLE, 0);
6902 processSync(mapper);
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006906
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006909 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006910
6911 // press BTN_BACK, release BTN_BACK
6912 processKey(mapper, BTN_BACK, 1);
6913 processSync(mapper);
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6915 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6916 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006917
Michael Wrightd02c5b62014-02-10 15:10:22 -08006918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006920 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6921
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6923 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6924 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925
6926 processKey(mapper, BTN_BACK, 0);
6927 processSync(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006930 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006931
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006934 ASSERT_EQ(0, motionArgs.buttonState);
6935
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6937 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6938 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6939
6940 // press BTN_SIDE, release BTN_SIDE
6941 processKey(mapper, BTN_SIDE, 1);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6944 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6945 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006946
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006949 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6950
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6953 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006954
6955 processKey(mapper, BTN_SIDE, 0);
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006958 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006959 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006960
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006963 ASSERT_EQ(0, motionArgs.buttonState);
6964
Michael Wrightd02c5b62014-02-10 15:10:22 -08006965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6966 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6967 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6968
6969 // press BTN_FORWARD, release BTN_FORWARD
6970 processKey(mapper, BTN_FORWARD, 1);
6971 processSync(mapper);
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6973 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6974 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006975
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006978 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6979
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6982 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983
6984 processKey(mapper, BTN_FORWARD, 0);
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006988 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006989
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006992 ASSERT_EQ(0, motionArgs.buttonState);
6993
Michael Wrightd02c5b62014-02-10 15:10:22 -08006994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6995 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6996 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6997
6998 // press BTN_EXTRA, release BTN_EXTRA
6999 processKey(mapper, BTN_EXTRA, 1);
7000 processSync(mapper);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7002 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7003 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007004
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007007 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7008
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7011 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007012
7013 processKey(mapper, BTN_EXTRA, 0);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007017 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007018
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007021 ASSERT_EQ(0, motionArgs.buttonState);
7022
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7024 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7025 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7026
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7028
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029 // press BTN_STYLUS, release BTN_STYLUS
7030 processKey(mapper, BTN_STYLUS, 1);
7031 processSync(mapper);
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007034 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7035
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7038 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039
7040 processKey(mapper, BTN_STYLUS, 0);
7041 processSync(mapper);
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007045
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007048 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049
7050 // press BTN_STYLUS2, release BTN_STYLUS2
7051 processKey(mapper, BTN_STYLUS2, 1);
7052 processSync(mapper);
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007055 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7056
7057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7059 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060
7061 processKey(mapper, BTN_STYLUS2, 0);
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007066
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007069 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070
7071 // release touch
7072 processId(mapper, -1);
7073 processSync(mapper);
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7075 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7076 ASSERT_EQ(0, motionArgs.buttonState);
7077}
7078
7079TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007080 addConfigurationProperty("touch.deviceType", "touchScreen");
7081 prepareDisplay(DISPLAY_ORIENTATION_0);
7082 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007083 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084
7085 NotifyMotionArgs motionArgs;
7086
7087 // default tool type is finger
7088 processId(mapper, 1);
7089 processPosition(mapper, 100, 200);
7090 processSync(mapper);
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7094
7095 // eraser
7096 processKey(mapper, BTN_TOOL_RUBBER, 1);
7097 processSync(mapper);
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7101
7102 // stylus
7103 processKey(mapper, BTN_TOOL_RUBBER, 0);
7104 processKey(mapper, BTN_TOOL_PEN, 1);
7105 processSync(mapper);
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7109
7110 // brush
7111 processKey(mapper, BTN_TOOL_PEN, 0);
7112 processKey(mapper, BTN_TOOL_BRUSH, 1);
7113 processSync(mapper);
7114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7117
7118 // pencil
7119 processKey(mapper, BTN_TOOL_BRUSH, 0);
7120 processKey(mapper, BTN_TOOL_PENCIL, 1);
7121 processSync(mapper);
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7125
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007126 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 processKey(mapper, BTN_TOOL_PENCIL, 0);
7128 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7129 processSync(mapper);
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7133
7134 // mouse
7135 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7136 processKey(mapper, BTN_TOOL_MOUSE, 1);
7137 processSync(mapper);
7138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7141
7142 // lens
7143 processKey(mapper, BTN_TOOL_MOUSE, 0);
7144 processKey(mapper, BTN_TOOL_LENS, 1);
7145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7149
7150 // double-tap
7151 processKey(mapper, BTN_TOOL_LENS, 0);
7152 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7153 processSync(mapper);
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7157
7158 // triple-tap
7159 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7160 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7161 processSync(mapper);
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7165
7166 // quad-tap
7167 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7168 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7173
7174 // finger
7175 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7176 processKey(mapper, BTN_TOOL_FINGER, 1);
7177 processSync(mapper);
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7181
7182 // stylus trumps finger
7183 processKey(mapper, BTN_TOOL_PEN, 1);
7184 processSync(mapper);
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7188
7189 // eraser trumps stylus
7190 processKey(mapper, BTN_TOOL_RUBBER, 1);
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7195
7196 // mouse trumps eraser
7197 processKey(mapper, BTN_TOOL_MOUSE, 1);
7198 processSync(mapper);
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7202
7203 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7204 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7205 processSync(mapper);
7206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7209
7210 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7211 processToolType(mapper, MT_TOOL_PEN);
7212 processSync(mapper);
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7216
7217 // back to default tool type
7218 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7219 processKey(mapper, BTN_TOOL_MOUSE, 0);
7220 processKey(mapper, BTN_TOOL_RUBBER, 0);
7221 processKey(mapper, BTN_TOOL_PEN, 0);
7222 processKey(mapper, BTN_TOOL_FINGER, 0);
7223 processSync(mapper);
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7227}
7228
7229TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 addConfigurationProperty("touch.deviceType", "touchScreen");
7231 prepareDisplay(DISPLAY_ORIENTATION_0);
7232 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007233 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007234 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007235
7236 NotifyMotionArgs motionArgs;
7237
7238 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7239 processId(mapper, 1);
7240 processPosition(mapper, 100, 200);
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7245 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7246
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7250 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7251
7252 // move a little
7253 processPosition(mapper, 150, 250);
7254 processSync(mapper);
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7258 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7259
7260 // down when BTN_TOUCH is pressed, pressure defaults to 1
7261 processKey(mapper, BTN_TOUCH, 1);
7262 processSync(mapper);
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7266 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7267
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7271 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7272
7273 // up when BTN_TOUCH is released, hover restored
7274 processKey(mapper, BTN_TOUCH, 0);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7279 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7280
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7284 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7285
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7289 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7290
7291 // exit hover when pointer goes away
7292 processId(mapper, -1);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7297 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7298}
7299
7300TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007301 addConfigurationProperty("touch.deviceType", "touchScreen");
7302 prepareDisplay(DISPLAY_ORIENTATION_0);
7303 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305
7306 NotifyMotionArgs motionArgs;
7307
7308 // initially hovering because pressure is 0
7309 processId(mapper, 1);
7310 processPosition(mapper, 100, 200);
7311 processPressure(mapper, 0);
7312 processSync(mapper);
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7316 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7317
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7321 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7322
7323 // move a little
7324 processPosition(mapper, 150, 250);
7325 processSync(mapper);
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7327 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7329 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7330
7331 // down when pressure becomes non-zero
7332 processPressure(mapper, RAW_PRESSURE_MAX);
7333 processSync(mapper);
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7337 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7338
7339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7342 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7343
7344 // up when pressure becomes 0, hover restored
7345 processPressure(mapper, 0);
7346 processSync(mapper);
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7348 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7350 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7351
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7355 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7356
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7360 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7361
7362 // exit hover when pointer goes away
7363 processId(mapper, -1);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7368 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7369}
7370
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007371/**
7372 * Set the input device port <--> display port associations, and check that the
7373 * events are routed to the display that matches the display port.
7374 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7375 */
7376TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007377 const std::string usb2 = "USB2";
7378 const uint8_t hdmi1 = 0;
7379 const uint8_t hdmi2 = 1;
7380 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007381 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007382
7383 addConfigurationProperty("touch.deviceType", "touchScreen");
7384 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007385 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007386
7387 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7388 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7389
7390 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7391 // for this input device is specified, and the matching viewport is not present,
7392 // the input device should be disabled (at the mapper level).
7393
7394 // Add viewport for display 2 on hdmi2
7395 prepareSecondaryDisplay(type, hdmi2);
7396 // Send a touch event
7397 processPosition(mapper, 100, 100);
7398 processSync(mapper);
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7400
7401 // Add viewport for display 1 on hdmi1
7402 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7403 // Send a touch event again
7404 processPosition(mapper, 100, 100);
7405 processSync(mapper);
7406
7407 NotifyMotionArgs args;
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7409 ASSERT_EQ(DISPLAY_ID, args.displayId);
7410}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007411
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007412TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007413 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007414 std::shared_ptr<FakePointerController> fakePointerController =
7415 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007416 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007417 fakePointerController->setPosition(100, 200);
7418 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007419 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7420
Garfield Tan888a6a42020-01-09 11:39:16 -08007421 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007422 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007423
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007424 prepareDisplay(DISPLAY_ORIENTATION_0);
7425 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007426 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007427
7428 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007429 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007430
7431 NotifyMotionArgs motionArgs;
7432 processPosition(mapper, 100, 100);
7433 processSync(mapper);
7434
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7437 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7438}
7439
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007440/**
7441 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7442 * events should not be delivered to the listener.
7443 */
7444TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7445 addConfigurationProperty("touch.deviceType", "touchScreen");
7446 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7447 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7448 ViewportType::INTERNAL);
7449 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7450 prepareAxes(POSITION);
7451 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7452
7453 NotifyMotionArgs motionArgs;
7454 processPosition(mapper, 100, 100);
7455 processSync(mapper);
7456
7457 mFakeListener->assertNotifyMotionWasNotCalled();
7458}
7459
Garfield Tanc734e4f2021-01-15 20:01:39 -08007460TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7461 addConfigurationProperty("touch.deviceType", "touchScreen");
7462 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7463 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7464 ViewportType::INTERNAL);
7465 std::optional<DisplayViewport> optionalDisplayViewport =
7466 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7467 ASSERT_TRUE(optionalDisplayViewport.has_value());
7468 DisplayViewport displayViewport = *optionalDisplayViewport;
7469
7470 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7471 prepareAxes(POSITION);
7472 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7473
7474 // Finger down
7475 int32_t x = 100, y = 100;
7476 processPosition(mapper, x, y);
7477 processSync(mapper);
7478
7479 NotifyMotionArgs motionArgs;
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7481 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7482
7483 // Deactivate display viewport
7484 displayViewport.isActive = false;
7485 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7486 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7487
7488 // Finger move
7489 x += 10, y += 10;
7490 processPosition(mapper, x, y);
7491 processSync(mapper);
7492
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7494 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7495
7496 // Reactivate display viewport
7497 displayViewport.isActive = true;
7498 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7499 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7500
7501 // Finger move again
7502 x += 10, y += 10;
7503 processPosition(mapper, x, y);
7504 processSync(mapper);
7505
7506 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7507 // no pointer on the touch device.
7508 mFakeListener->assertNotifyMotionWasNotCalled();
7509}
7510
Arthur Hung7c645402019-01-25 17:45:42 +08007511TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7512 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007513 prepareAxes(POSITION | ID | SLOT);
7514 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007515 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007516
7517 // Create the second touch screen device, and enable multi fingers.
7518 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007519 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007520 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007521 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007522 std::shared_ptr<InputDevice> device2 =
7523 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7524 Flags<InputDeviceClass>(0));
7525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007526 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7527 0 /*flat*/, 0 /*fuzz*/);
7528 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7529 0 /*flat*/, 0 /*fuzz*/);
7530 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7531 0 /*flat*/, 0 /*fuzz*/);
7532 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7533 0 /*flat*/, 0 /*fuzz*/);
7534 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7535 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7536 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007537
7538 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007539 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007540 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7541 device2->reset(ARBITRARY_TIME);
7542
7543 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007544 std::shared_ptr<FakePointerController> fakePointerController =
7545 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007546 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7547 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7548
7549 // Setup policy for associated displays and show touches.
7550 const uint8_t hdmi1 = 0;
7551 const uint8_t hdmi2 = 1;
7552 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7553 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7554 mFakePolicy->setShowTouches(true);
7555
7556 // Create displays.
7557 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007558 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007559
7560 // Default device will reconfigure above, need additional reconfiguration for another device.
7561 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007562 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007563
7564 // Two fingers down at default display.
7565 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7566 processPosition(mapper, x1, y1);
7567 processId(mapper, 1);
7568 processSlot(mapper, 1);
7569 processPosition(mapper, x2, y2);
7570 processId(mapper, 2);
7571 processSync(mapper);
7572
7573 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7574 fakePointerController->getSpots().find(DISPLAY_ID);
7575 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7576 ASSERT_EQ(size_t(2), iter->second.size());
7577
7578 // Two fingers down at second display.
7579 processPosition(mapper2, x1, y1);
7580 processId(mapper2, 1);
7581 processSlot(mapper2, 1);
7582 processPosition(mapper2, x2, y2);
7583 processId(mapper2, 2);
7584 processSync(mapper2);
7585
7586 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7587 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7588 ASSERT_EQ(size_t(2), iter->second.size());
7589}
7590
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007591TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007592 prepareAxes(POSITION);
7593 addConfigurationProperty("touch.deviceType", "touchScreen");
7594 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007595 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007596
7597 NotifyMotionArgs motionArgs;
7598 // Unrotated video frame
7599 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7600 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007601 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007602 processPosition(mapper, 100, 200);
7603 processSync(mapper);
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(frames, motionArgs.videoFrames);
7606
7607 // Subsequent touch events should not have any videoframes
7608 // This is implemented separately in FakeEventHub,
7609 // but that should match the behaviour of TouchVideoDevice.
7610 processPosition(mapper, 200, 200);
7611 processSync(mapper);
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7613 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7614}
7615
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007616TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007617 prepareAxes(POSITION);
7618 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007620 // Unrotated video frame
7621 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7622 NotifyMotionArgs motionArgs;
7623
7624 // Test all 4 orientations
7625 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7626 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7627 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7628 clearViewports();
7629 prepareDisplay(orientation);
7630 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007631 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007632 processPosition(mapper, 100, 200);
7633 processSync(mapper);
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7635 frames[0].rotate(orientation);
7636 ASSERT_EQ(frames, motionArgs.videoFrames);
7637 }
7638}
7639
7640TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007641 prepareAxes(POSITION);
7642 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007643 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007644 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7645 // so mix these.
7646 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7647 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7648 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7649 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7650 NotifyMotionArgs motionArgs;
7651
7652 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007653 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007654 processPosition(mapper, 100, 200);
7655 processSync(mapper);
7656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7657 std::for_each(frames.begin(), frames.end(),
7658 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7659 ASSERT_EQ(frames, motionArgs.videoFrames);
7660}
7661
Arthur Hung9da14732019-09-02 16:16:58 +08007662/**
7663 * If we had defined port associations, but the viewport is not ready, the touch device would be
7664 * expected to be disabled, and it should be enabled after the viewport has found.
7665 */
7666TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007667 constexpr uint8_t hdmi2 = 1;
7668 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007669 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007670
7671 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7672
7673 addConfigurationProperty("touch.deviceType", "touchScreen");
7674 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007675 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007676
7677 ASSERT_EQ(mDevice->isEnabled(), false);
7678
7679 // Add display on hdmi2, the device should be enabled and can receive touch event.
7680 prepareSecondaryDisplay(type, hdmi2);
7681 ASSERT_EQ(mDevice->isEnabled(), true);
7682
7683 // Send a touch event.
7684 processPosition(mapper, 100, 100);
7685 processSync(mapper);
7686
7687 NotifyMotionArgs args;
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7689 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7690}
7691
Arthur Hung6cd19a42019-08-30 19:04:12 +08007692
Arthur Hung6cd19a42019-08-30 19:04:12 +08007693
Arthur Hung421eb1c2020-01-16 00:09:42 +08007694TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007695 addConfigurationProperty("touch.deviceType", "touchScreen");
7696 prepareDisplay(DISPLAY_ORIENTATION_0);
7697 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007698 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007699
7700 NotifyMotionArgs motionArgs;
7701
7702 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7703 // finger down
7704 processId(mapper, 1);
7705 processPosition(mapper, x1, y1);
7706 processSync(mapper);
7707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7708 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7710
7711 // finger move
7712 processId(mapper, 1);
7713 processPosition(mapper, x2, y2);
7714 processSync(mapper);
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7718
7719 // finger up.
7720 processId(mapper, -1);
7721 processSync(mapper);
7722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7723 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7725
7726 // new finger down
7727 processId(mapper, 1);
7728 processPosition(mapper, x3, y3);
7729 processSync(mapper);
7730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7731 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7733}
7734
7735/**
arthurhungcc7f9802020-04-30 17:55:40 +08007736 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7737 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007738 */
arthurhungcc7f9802020-04-30 17:55:40 +08007739TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007740 addConfigurationProperty("touch.deviceType", "touchScreen");
7741 prepareDisplay(DISPLAY_ORIENTATION_0);
7742 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007743 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007744
7745 NotifyMotionArgs motionArgs;
7746
7747 // default tool type is finger
7748 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007749 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007750 processPosition(mapper, x1, y1);
7751 processSync(mapper);
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7753 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7755
7756 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7757 processToolType(mapper, MT_TOOL_PALM);
7758 processSync(mapper);
7759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7760 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7761
7762 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007763 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007764 processPosition(mapper, x2, y2);
7765 processSync(mapper);
7766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7767
7768 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007769 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007770 processSync(mapper);
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7772
7773 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007774 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007775 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007776 processPosition(mapper, x3, y3);
7777 processSync(mapper);
7778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7779 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7781}
7782
arthurhungbf89a482020-04-17 17:37:55 +08007783/**
arthurhungcc7f9802020-04-30 17:55:40 +08007784 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7785 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007786 */
arthurhungcc7f9802020-04-30 17:55:40 +08007787TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007788 addConfigurationProperty("touch.deviceType", "touchScreen");
7789 prepareDisplay(DISPLAY_ORIENTATION_0);
7790 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7791 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7792
7793 NotifyMotionArgs motionArgs;
7794
7795 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007796 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7797 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007798 processPosition(mapper, x1, y1);
7799 processSync(mapper);
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7801 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7803
7804 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007805 processSlot(mapper, SECOND_SLOT);
7806 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007807 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007808 processSync(mapper);
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7810 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7811 motionArgs.action);
7812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7813
7814 // If the tool type of the first finger changes to MT_TOOL_PALM,
7815 // we expect to receive ACTION_POINTER_UP with cancel flag.
7816 processSlot(mapper, FIRST_SLOT);
7817 processId(mapper, FIRST_TRACKING_ID);
7818 processToolType(mapper, MT_TOOL_PALM);
7819 processSync(mapper);
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7821 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7822 motionArgs.action);
7823 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7824
7825 // The following MOVE events of second finger should be processed.
7826 processSlot(mapper, SECOND_SLOT);
7827 processId(mapper, SECOND_TRACKING_ID);
7828 processPosition(mapper, x2 + 1, y2 + 1);
7829 processSync(mapper);
7830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7832 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7833
7834 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7835 // it. Second finger receive move.
7836 processSlot(mapper, FIRST_SLOT);
7837 processId(mapper, INVALID_TRACKING_ID);
7838 processSync(mapper);
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7841 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7842
7843 // Second finger keeps moving.
7844 processSlot(mapper, SECOND_SLOT);
7845 processId(mapper, SECOND_TRACKING_ID);
7846 processPosition(mapper, x2 + 2, y2 + 2);
7847 processSync(mapper);
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7850 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7851
7852 // Second finger up.
7853 processId(mapper, INVALID_TRACKING_ID);
7854 processSync(mapper);
7855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7856 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7857 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7858}
7859
7860/**
7861 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7862 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7863 */
7864TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7865 addConfigurationProperty("touch.deviceType", "touchScreen");
7866 prepareDisplay(DISPLAY_ORIENTATION_0);
7867 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7868 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7869
7870 NotifyMotionArgs motionArgs;
7871
7872 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7873 // First finger down.
7874 processId(mapper, FIRST_TRACKING_ID);
7875 processPosition(mapper, x1, y1);
7876 processSync(mapper);
7877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7878 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7880
7881 // Second finger down.
7882 processSlot(mapper, SECOND_SLOT);
7883 processId(mapper, SECOND_TRACKING_ID);
7884 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007885 processSync(mapper);
7886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7887 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7888 motionArgs.action);
7889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7890
arthurhungcc7f9802020-04-30 17:55:40 +08007891 // If the tool type of the first finger changes to MT_TOOL_PALM,
7892 // we expect to receive ACTION_POINTER_UP with cancel flag.
7893 processSlot(mapper, FIRST_SLOT);
7894 processId(mapper, FIRST_TRACKING_ID);
7895 processToolType(mapper, MT_TOOL_PALM);
7896 processSync(mapper);
7897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7898 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7899 motionArgs.action);
7900 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7901
7902 // Second finger keeps moving.
7903 processSlot(mapper, SECOND_SLOT);
7904 processId(mapper, SECOND_TRACKING_ID);
7905 processPosition(mapper, x2 + 1, y2 + 1);
7906 processSync(mapper);
7907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7909
7910 // second finger becomes palm, receive cancel due to only 1 finger is active.
7911 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007912 processToolType(mapper, MT_TOOL_PALM);
7913 processSync(mapper);
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7915 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7916
arthurhungcc7f9802020-04-30 17:55:40 +08007917 // third finger down.
7918 processSlot(mapper, THIRD_SLOT);
7919 processId(mapper, THIRD_TRACKING_ID);
7920 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007921 processPosition(mapper, x3, y3);
7922 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007926 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7927
7928 // third finger move
7929 processId(mapper, THIRD_TRACKING_ID);
7930 processPosition(mapper, x3 + 1, y3 + 1);
7931 processSync(mapper);
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7934
7935 // first finger up, third finger receive move.
7936 processSlot(mapper, FIRST_SLOT);
7937 processId(mapper, INVALID_TRACKING_ID);
7938 processSync(mapper);
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7941 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7942
7943 // second finger up, third finger receive move.
7944 processSlot(mapper, SECOND_SLOT);
7945 processId(mapper, INVALID_TRACKING_ID);
7946 processSync(mapper);
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7949 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7950
7951 // third finger up.
7952 processSlot(mapper, THIRD_SLOT);
7953 processId(mapper, INVALID_TRACKING_ID);
7954 processSync(mapper);
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7956 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7957 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7958}
7959
7960/**
7961 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7962 * and the active finger could still be allowed to receive the events
7963 */
7964TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7965 addConfigurationProperty("touch.deviceType", "touchScreen");
7966 prepareDisplay(DISPLAY_ORIENTATION_0);
7967 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7968 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7969
7970 NotifyMotionArgs motionArgs;
7971
7972 // default tool type is finger
7973 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7974 processId(mapper, FIRST_TRACKING_ID);
7975 processPosition(mapper, x1, y1);
7976 processSync(mapper);
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7980
7981 // Second finger down.
7982 processSlot(mapper, SECOND_SLOT);
7983 processId(mapper, SECOND_TRACKING_ID);
7984 processPosition(mapper, x2, y2);
7985 processSync(mapper);
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7987 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7988 motionArgs.action);
7989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7990
7991 // If the tool type of the second finger changes to MT_TOOL_PALM,
7992 // we expect to receive ACTION_POINTER_UP with cancel flag.
7993 processId(mapper, SECOND_TRACKING_ID);
7994 processToolType(mapper, MT_TOOL_PALM);
7995 processSync(mapper);
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7997 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7998 motionArgs.action);
7999 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8000
8001 // The following MOVE event should be processed.
8002 processSlot(mapper, FIRST_SLOT);
8003 processId(mapper, FIRST_TRACKING_ID);
8004 processPosition(mapper, x1 + 1, y1 + 1);
8005 processSync(mapper);
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8008 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8009
8010 // second finger up.
8011 processSlot(mapper, SECOND_SLOT);
8012 processId(mapper, INVALID_TRACKING_ID);
8013 processSync(mapper);
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8016
8017 // first finger keep moving
8018 processSlot(mapper, FIRST_SLOT);
8019 processId(mapper, FIRST_TRACKING_ID);
8020 processPosition(mapper, x1 + 2, y1 + 2);
8021 processSync(mapper);
8022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8024
8025 // first finger up.
8026 processId(mapper, INVALID_TRACKING_ID);
8027 processSync(mapper);
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8030 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008031}
8032
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008033// --- MultiTouchInputMapperTest_ExternalDevice ---
8034
8035class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8036protected:
Chris Yea52ade12020-08-27 16:49:20 -07008037 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008038};
8039
8040/**
8041 * Expect fallback to internal viewport if device is external and external viewport is not present.
8042 */
8043TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8044 prepareAxes(POSITION);
8045 addConfigurationProperty("touch.deviceType", "touchScreen");
8046 prepareDisplay(DISPLAY_ORIENTATION_0);
8047 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8048
8049 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8050
8051 NotifyMotionArgs motionArgs;
8052
8053 // Expect the event to be sent to the internal viewport,
8054 // because an external viewport is not present.
8055 processPosition(mapper, 100, 100);
8056 processSync(mapper);
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8058 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8059
8060 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008061 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008062 processPosition(mapper, 100, 100);
8063 processSync(mapper);
8064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8065 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8066}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008067
8068/**
8069 * Test touch should not work if outside of surface.
8070 */
8071class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8072protected:
8073 void halfDisplayToCenterHorizontal(int32_t orientation) {
8074 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008075 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008076
8077 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8078 internalViewport->orientation = orientation;
8079 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8080 internalViewport->logicalLeft = 0;
8081 internalViewport->logicalTop = 0;
8082 internalViewport->logicalRight = DISPLAY_HEIGHT;
8083 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8084
8085 internalViewport->physicalLeft = 0;
8086 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8087 internalViewport->physicalRight = DISPLAY_HEIGHT;
8088 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8089
8090 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8091 internalViewport->deviceHeight = DISPLAY_WIDTH;
8092 } else {
8093 internalViewport->logicalLeft = 0;
8094 internalViewport->logicalTop = 0;
8095 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8096 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8097
8098 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8099 internalViewport->physicalTop = 0;
8100 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8101 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8102
8103 internalViewport->deviceWidth = DISPLAY_WIDTH;
8104 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8105 }
8106
8107 mFakePolicy->updateViewport(internalViewport.value());
8108 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8109 }
8110
arthurhung5d547942020-12-14 17:04:45 +08008111 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8112 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008113 int32_t yExpected) {
8114 // touch on outside area should not work.
8115 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8116 processSync(mapper);
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8118
8119 // touch on inside area should receive the event.
8120 NotifyMotionArgs args;
8121 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8124 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8125 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8126
8127 // Reset.
8128 mapper.reset(ARBITRARY_TIME);
8129 }
8130};
8131
arthurhung5d547942020-12-14 17:04:45 +08008132TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008133 addConfigurationProperty("touch.deviceType", "touchScreen");
8134 prepareDisplay(DISPLAY_ORIENTATION_0);
8135 prepareAxes(POSITION);
8136 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8137
8138 // Touch on center of normal display should work.
8139 const int32_t x = DISPLAY_WIDTH / 4;
8140 const int32_t y = DISPLAY_HEIGHT / 2;
8141 processPosition(mapper, toRawX(x), toRawY(y));
8142 processSync(mapper);
8143 NotifyMotionArgs args;
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8146 0.0f, 0.0f, 0.0f, 0.0f));
8147 // Reset.
8148 mapper.reset(ARBITRARY_TIME);
8149
8150 // Let physical display be different to device, and make surface and physical could be 1:1.
8151 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8152
8153 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8154 const int32_t yExpected = y;
8155 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8156}
8157
arthurhung5d547942020-12-14 17:04:45 +08008158TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008159 addConfigurationProperty("touch.deviceType", "touchScreen");
8160 prepareDisplay(DISPLAY_ORIENTATION_0);
8161 prepareAxes(POSITION);
8162 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8163
8164 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8165 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8166
8167 const int32_t x = DISPLAY_WIDTH / 4;
8168 const int32_t y = DISPLAY_HEIGHT / 2;
8169
8170 // expect x/y = swap x/y then reverse y.
8171 const int32_t xExpected = y;
8172 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8173 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8174}
8175
arthurhung5d547942020-12-14 17:04:45 +08008176TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008177 addConfigurationProperty("touch.deviceType", "touchScreen");
8178 prepareDisplay(DISPLAY_ORIENTATION_0);
8179 prepareAxes(POSITION);
8180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8181
8182 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8183 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8184
8185 const int32_t x = DISPLAY_WIDTH / 4;
8186 const int32_t y = DISPLAY_HEIGHT / 2;
8187
8188 // expect x/y = swap x/y then reverse x.
8189 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8190 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8191 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8192}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008193
arthurhunga36b28e2020-12-29 20:28:15 +08008194TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8195 addConfigurationProperty("touch.deviceType", "touchScreen");
8196 prepareDisplay(DISPLAY_ORIENTATION_0);
8197 prepareAxes(POSITION);
8198 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8199
8200 const int32_t x = 0;
8201 const int32_t y = 0;
8202
8203 const int32_t xExpected = x;
8204 const int32_t yExpected = y;
8205 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8206
8207 clearViewports();
8208 prepareDisplay(DISPLAY_ORIENTATION_90);
8209 // expect x/y = swap x/y then reverse y.
8210 const int32_t xExpected90 = y;
8211 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8212 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8213
8214 clearViewports();
8215 prepareDisplay(DISPLAY_ORIENTATION_270);
8216 // expect x/y = swap x/y then reverse x.
8217 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8218 const int32_t yExpected270 = x;
8219 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8220}
8221
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008222TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8223 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8224 std::shared_ptr<FakePointerController> fakePointerController =
8225 std::make_shared<FakePointerController>();
8226 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8227 fakePointerController->setPosition(0, 0);
8228 fakePointerController->setButtonState(0);
8229
8230 // prepare device and capture
8231 prepareDisplay(DISPLAY_ORIENTATION_0);
8232 prepareAxes(POSITION | ID | SLOT);
8233 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8234 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8235 mFakePolicy->setPointerCapture(true);
8236 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8237 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8238
8239 // captured touchpad should be a touchpad source
8240 NotifyDeviceResetArgs resetArgs;
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8242 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8243
Chris Yef74dc422020-09-02 22:41:50 -07008244 InputDeviceInfo deviceInfo;
8245 mDevice->getDeviceInfo(&deviceInfo);
8246
8247 const InputDeviceInfo::MotionRange* relRangeX =
8248 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8249 ASSERT_NE(relRangeX, nullptr);
8250 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8251 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8252 const InputDeviceInfo::MotionRange* relRangeY =
8253 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8254 ASSERT_NE(relRangeY, nullptr);
8255 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8256 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8257
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008258 // run captured pointer tests - note that this is unscaled, so input listener events should be
8259 // identical to what the hardware sends (accounting for any
8260 // calibration).
8261 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008262 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008263 processId(mapper, 1);
8264 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8265 processKey(mapper, BTN_TOUCH, 1);
8266 processSync(mapper);
8267
8268 // expect coord[0] to contain initial location of touch 0
8269 NotifyMotionArgs args;
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8271 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8272 ASSERT_EQ(1U, args.pointerCount);
8273 ASSERT_EQ(0, args.pointerProperties[0].id);
8274 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8275 ASSERT_NO_FATAL_FAILURE(
8276 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8277
8278 // FINGER 1 DOWN
8279 processSlot(mapper, 1);
8280 processId(mapper, 2);
8281 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8282 processSync(mapper);
8283
8284 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008286 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8287 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008288 ASSERT_EQ(2U, args.pointerCount);
8289 ASSERT_EQ(0, args.pointerProperties[0].id);
8290 ASSERT_EQ(1, args.pointerProperties[1].id);
8291 ASSERT_NO_FATAL_FAILURE(
8292 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8293 ASSERT_NO_FATAL_FAILURE(
8294 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8295
8296 // FINGER 1 MOVE
8297 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8298 processSync(mapper);
8299
8300 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8301 // from move
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8304 ASSERT_NO_FATAL_FAILURE(
8305 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8306 ASSERT_NO_FATAL_FAILURE(
8307 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8308
8309 // FINGER 0 MOVE
8310 processSlot(mapper, 0);
8311 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8312 processSync(mapper);
8313
8314 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8317 ASSERT_NO_FATAL_FAILURE(
8318 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8319 ASSERT_NO_FATAL_FAILURE(
8320 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8321
8322 // BUTTON DOWN
8323 processKey(mapper, BTN_LEFT, 1);
8324 processSync(mapper);
8325
8326 // touchinputmapper design sends a move before button press
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8331
8332 // BUTTON UP
8333 processKey(mapper, BTN_LEFT, 0);
8334 processSync(mapper);
8335
8336 // touchinputmapper design sends a move after button release
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8341
8342 // FINGER 0 UP
8343 processId(mapper, -1);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8347
8348 // FINGER 1 MOVE
8349 processSlot(mapper, 1);
8350 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8351 processSync(mapper);
8352
8353 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8356 ASSERT_EQ(1U, args.pointerCount);
8357 ASSERT_EQ(1, args.pointerProperties[0].id);
8358 ASSERT_NO_FATAL_FAILURE(
8359 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8360
8361 // FINGER 1 UP
8362 processId(mapper, -1);
8363 processKey(mapper, BTN_TOUCH, 0);
8364 processSync(mapper);
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8367
8368 // non captured touchpad should be a mouse source
8369 mFakePolicy->setPointerCapture(false);
8370 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8372 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8373}
8374
8375TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8376 std::shared_ptr<FakePointerController> fakePointerController =
8377 std::make_shared<FakePointerController>();
8378 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8379 fakePointerController->setPosition(0, 0);
8380 fakePointerController->setButtonState(0);
8381
8382 // prepare device and capture
8383 prepareDisplay(DISPLAY_ORIENTATION_0);
8384 prepareAxes(POSITION | ID | SLOT);
8385 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8386 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8387 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8388 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8389 // run uncaptured pointer tests - pushes out generic events
8390 // FINGER 0 DOWN
8391 processId(mapper, 3);
8392 processPosition(mapper, 100, 100);
8393 processKey(mapper, BTN_TOUCH, 1);
8394 processSync(mapper);
8395
8396 // start at (100,100), cursor should be at (0,0) * scale
8397 NotifyMotionArgs args;
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8400 ASSERT_NO_FATAL_FAILURE(
8401 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8402
8403 // FINGER 0 MOVE
8404 processPosition(mapper, 200, 200);
8405 processSync(mapper);
8406
8407 // compute scaling to help with touch position checking
8408 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8409 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8410 float scale =
8411 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8412
8413 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8417 0, 0, 0, 0, 0, 0, 0));
8418}
8419
8420TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8421 std::shared_ptr<FakePointerController> fakePointerController =
8422 std::make_shared<FakePointerController>();
8423
8424 prepareDisplay(DISPLAY_ORIENTATION_0);
8425 prepareAxes(POSITION | ID | SLOT);
8426 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8427 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8428 mFakePolicy->setPointerCapture(false);
8429 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8430
8431 // uncaptured touchpad should be a pointer device
8432 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8433
8434 // captured touchpad should be a touchpad device
8435 mFakePolicy->setPointerCapture(true);
8436 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8437 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8438}
8439
Michael Wrightd02c5b62014-02-10 15:10:22 -08008440} // namespace android