blob: 336afc67fc6a2df48daf0d572111d0e3906fcaf4 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070025#include <PeripheralController.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>
chaviw3277faf2021-05-19 16:45:23 -050035#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080036#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <math.h>
38
Michael Wright17db18e2020-06-26 20:51:44 +010039#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080040#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070046using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070047using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048
49// Timeout for waiting for an expected event
50static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
51
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000053static constexpr nsecs_t ARBITRARY_TIME = 1234;
54static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
56// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080057static constexpr int32_t DISPLAY_ID = 0;
58static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
59static constexpr int32_t DISPLAY_WIDTH = 480;
60static constexpr int32_t DISPLAY_HEIGHT = 800;
61static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
62static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
63static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070064static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070065static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080066
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t FIRST_SLOT = 0;
68static constexpr int32_t SECOND_SLOT = 1;
69static constexpr int32_t THIRD_SLOT = 2;
70static constexpr int32_t INVALID_TRACKING_ID = -1;
71static constexpr int32_t FIRST_TRACKING_ID = 0;
72static constexpr int32_t SECOND_TRACKING_ID = 1;
73static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080074static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080075static constexpr int32_t BATTERY_STATUS = 4;
76static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080077static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
78static constexpr int32_t LIGHT_COLOR = 0x7F448866;
79static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080080
Michael Wrightd02c5b62014-02-10 15:10:22 -080081// Error tolerance for floating point assertions.
82static const float EPSILON = 0.001f;
83
84template<typename T>
85static inline T min(T a, T b) {
86 return a < b ? a : b;
87}
88
89static inline float avg(float x, float y) {
90 return (x + y) / 2;
91}
92
Chris Ye3fdbfef2021-01-06 18:45:18 -080093// Mapping for light color name and the light color
94const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
95 {"green", LightColor::GREEN},
96 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
Prabir Pradhanc14266f2021-05-12 15:56:24 -070098static int32_t getInverseRotation(int32_t orientation) {
99 switch (orientation) {
100 case DISPLAY_ORIENTATION_90:
101 return DISPLAY_ORIENTATION_270;
102 case DISPLAY_ORIENTATION_270:
103 return DISPLAY_ORIENTATION_90;
104 default:
105 return orientation;
106 }
107}
108
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109// --- FakePointerController ---
110
111class FakePointerController : public PointerControllerInterface {
112 bool mHaveBounds;
113 float mMinX, mMinY, mMaxX, mMaxY;
114 float mX, mY;
115 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800116 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118public:
119 FakePointerController() :
120 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800121 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122 }
123
Michael Wright17db18e2020-06-26 20:51:44 +0100124 virtual ~FakePointerController() {}
125
Michael Wrightd02c5b62014-02-10 15:10:22 -0800126 void setBounds(float minX, float minY, float maxX, float maxY) {
127 mHaveBounds = true;
128 mMinX = minX;
129 mMinY = minY;
130 mMaxX = maxX;
131 mMaxY = maxY;
132 }
133
Chris Yea52ade12020-08-27 16:49:20 -0700134 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 mX = x;
136 mY = y;
137 }
138
Chris Yea52ade12020-08-27 16:49:20 -0700139 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140
Chris Yea52ade12020-08-27 16:49:20 -0700141 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142
Chris Yea52ade12020-08-27 16:49:20 -0700143 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 *outX = mX;
145 *outY = mY;
146 }
147
Chris Yea52ade12020-08-27 16:49:20 -0700148 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149
Chris Yea52ade12020-08-27 16:49:20 -0700150 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800151 mDisplayId = viewport.displayId;
152 }
153
Arthur Hung7c645402019-01-25 17:45:42 +0800154 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
155 return mSpotsByDisplay;
156 }
157
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158private:
Chris Yea52ade12020-08-27 16:49:20 -0700159 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160 *outMinX = mMinX;
161 *outMinY = mMinY;
162 *outMaxX = mMaxX;
163 *outMaxY = mMaxY;
164 return mHaveBounds;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 mX += deltaX;
169 if (mX < mMinX) mX = mMinX;
170 if (mX > mMaxX) mX = mMaxX;
171 mY += deltaY;
172 if (mY < mMinY) mY = mMinY;
173 if (mY > mMaxY) mY = mMaxY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179
Chris Yea52ade12020-08-27 16:49:20 -0700180 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
Chris Yea52ade12020-08-27 16:49:20 -0700182 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
183 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800184 std::vector<int32_t> newSpots;
185 // Add spots for fingers that are down.
186 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
187 uint32_t id = idBits.clearFirstMarkedBit();
188 newSpots.push_back(id);
189 }
190
191 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800195
196 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197};
198
199
200// --- FakeInputReaderPolicy ---
201
202class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700203 std::mutex mLock;
204 std::condition_variable mDevicesChangedCondition;
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000207 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
209 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100210 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700211 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
213protected:
Chris Yea52ade12020-08-27 16:49:20 -0700214 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215
216public:
217 FakeInputReaderPolicy() {
218 }
219
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700220 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800221 waitForInputDevices([](bool devicesChanged) {
222 if (!devicesChanged) {
223 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
224 }
225 });
226 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700227
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800228 void assertInputDevicesNotChanged() {
229 waitForInputDevices([](bool devicesChanged) {
230 if (devicesChanged) {
231 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
232 }
233 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700234 }
235
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700236 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100237 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700239 }
240
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700241 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
242 return mConfig.getDisplayViewportByUniqueId(uniqueId);
243 }
244 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
245 return mConfig.getDisplayViewportByType(type);
246 }
247
248 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
249 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700250 }
251
Prabir Pradhan5632d622021-09-06 07:57:20 -0700252 void addDisplayViewport(DisplayViewport viewport) {
253 mViewports.push_back(std::move(viewport));
254 mConfig.setDisplayViewports(mViewports);
255 }
256
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700257 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000258 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700259 std::optional<uint8_t> physicalPort, ViewportType type) {
260 const bool isRotated =
261 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
262 DisplayViewport v;
263 v.displayId = displayId;
264 v.orientation = orientation;
265 v.logicalLeft = 0;
266 v.logicalTop = 0;
267 v.logicalRight = isRotated ? height : width;
268 v.logicalBottom = isRotated ? width : height;
269 v.physicalLeft = 0;
270 v.physicalTop = 0;
271 v.physicalRight = isRotated ? height : width;
272 v.physicalBottom = isRotated ? width : height;
273 v.deviceWidth = isRotated ? height : width;
274 v.deviceHeight = isRotated ? width : height;
275 v.isActive = isActive;
276 v.uniqueId = uniqueId;
277 v.physicalPort = physicalPort;
278 v.type = type;
279
280 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282
Arthur Hung6cd19a42019-08-30 19:04:12 +0800283 bool updateViewport(const DisplayViewport& viewport) {
284 size_t count = mViewports.size();
285 for (size_t i = 0; i < count; i++) {
286 const DisplayViewport& currentViewport = mViewports[i];
287 if (currentViewport.displayId == viewport.displayId) {
288 mViewports[i] = viewport;
289 mConfig.setDisplayViewports(mViewports);
290 return true;
291 }
292 }
293 // no viewport found.
294 return false;
295 }
296
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100297 void addExcludedDeviceName(const std::string& deviceName) {
298 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 }
300
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700301 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
302 mConfig.portAssociations.insert({inputPort, displayPort});
303 }
304
Christine Franks1ba71cc2021-04-07 14:37:42 -0700305 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
306 const std::string& displayUniqueId) {
307 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
308 }
309
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000310 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700311
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000312 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700313
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000314 void setPointerController(std::shared_ptr<FakePointerController> controller) {
315 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 }
317
318 const InputReaderConfiguration* getReaderConfiguration() const {
319 return &mConfig;
320 }
321
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800322 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 return mInputDevices;
324 }
325
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100326 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700327 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700328 return transform;
329 }
330
331 void setTouchAffineTransformation(const TouchAffineTransformation t) {
332 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800333 }
334
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000335 PointerCaptureRequest setPointerCapture(bool enabled) {
336 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
337 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800338 }
339
Arthur Hung7c645402019-01-25 17:45:42 +0800340 void setShowTouches(bool enabled) {
341 mConfig.showTouches = enabled;
342 }
343
Garfield Tan888a6a42020-01-09 11:39:16 -0800344 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
345 mConfig.defaultPointerDisplayId = pointerDisplayId;
346 }
347
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800348 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
349
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000351 uint32_t mNextPointerCaptureSequenceNumber = 0;
352
Chris Yea52ade12020-08-27 16:49:20 -0700353 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354 *outConfig = mConfig;
355 }
356
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000357 std::shared_ptr<PointerControllerInterface> obtainPointerController(
358 int32_t /*deviceId*/) override {
359 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360 }
361
Chris Yea52ade12020-08-27 16:49:20 -0700362 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700363 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700365 mInputDevicesChanged = true;
366 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367 }
368
Chris Yea52ade12020-08-27 16:49:20 -0700369 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
370 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700371 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 }
373
Chris Yea52ade12020-08-27 16:49:20 -0700374 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800375
376 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
377 std::unique_lock<std::mutex> lock(mLock);
378 base::ScopedLockAssertion assumeLocked(mLock);
379
380 const bool devicesChanged =
381 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
382 return mInputDevicesChanged;
383 });
384 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
385 mInputDevicesChanged = false;
386 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387};
388
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389// --- FakeEventHub ---
390
391class FakeEventHub : public EventHubInterface {
392 struct KeyInfo {
393 int32_t keyCode;
394 uint32_t flags;
395 };
396
Chris Yef59a2f42020-10-16 12:55:26 -0700397 struct SensorInfo {
398 InputDeviceSensorType sensorType;
399 int32_t sensorDataIndex;
400 };
401
Michael Wrightd02c5b62014-02-10 15:10:22 -0800402 struct Device {
403 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700404 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 PropertyMap configuration;
406 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
407 KeyedVector<int, bool> relativeAxes;
408 KeyedVector<int32_t, int32_t> keyCodeStates;
409 KeyedVector<int32_t, int32_t> scanCodeStates;
410 KeyedVector<int32_t, int32_t> switchStates;
411 KeyedVector<int32_t, int32_t> absoluteAxisValue;
412 KeyedVector<int32_t, KeyInfo> keysByScanCode;
413 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
414 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700415 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
416 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800417 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700418 bool enabled;
419
420 status_t enable() {
421 enabled = true;
422 return OK;
423 }
424
425 status_t disable() {
426 enabled = false;
427 return OK;
428 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429
Chris Ye1b0c7342020-07-28 21:57:03 -0700430 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431 };
432
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700433 std::mutex mLock;
434 std::condition_variable mEventsCondition;
435
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100437 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000438 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600439 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000440 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800441 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
442 // Simulates a device light brightness, from light id to light brightness.
443 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
444 // Simulates a device light intensities, from light id to light intensities map.
445 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
446 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700448public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449 virtual ~FakeEventHub() {
450 for (size_t i = 0; i < mDevices.size(); i++) {
451 delete mDevices.valueAt(i);
452 }
453 }
454
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 FakeEventHub() { }
456
Chris Ye1b0c7342020-07-28 21:57:03 -0700457 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 Device* device = new Device(classes);
459 device->identifier.name = name;
460 mDevices.add(deviceId, device);
461
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000462 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 }
464
465 void removeDevice(int32_t deviceId) {
466 delete mDevices.valueFor(deviceId);
467 mDevices.removeItem(deviceId);
468
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000469 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470 }
471
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700472 bool isDeviceEnabled(int32_t deviceId) {
473 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700474 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700475 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
476 return false;
477 }
478 return device->enabled;
479 }
480
481 status_t enableDevice(int32_t deviceId) {
482 status_t result;
483 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700484 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700485 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
486 return BAD_VALUE;
487 }
488 if (device->enabled) {
489 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
490 return OK;
491 }
492 result = device->enable();
493 return result;
494 }
495
496 status_t disableDevice(int32_t deviceId) {
497 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700498 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700499 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
500 return BAD_VALUE;
501 }
502 if (!device->enabled) {
503 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
504 return OK;
505 }
506 return device->disable();
507 }
508
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000510 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 }
512
513 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
514 Device* device = getDevice(deviceId);
515 device->configuration.addProperty(key, value);
516 }
517
518 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
519 Device* device = getDevice(deviceId);
520 device->configuration.addAll(configuration);
521 }
522
523 void addAbsoluteAxis(int32_t deviceId, int axis,
524 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
525 Device* device = getDevice(deviceId);
526
527 RawAbsoluteAxisInfo info;
528 info.valid = true;
529 info.minValue = minValue;
530 info.maxValue = maxValue;
531 info.flat = flat;
532 info.fuzz = fuzz;
533 info.resolution = resolution;
534 device->absoluteAxes.add(axis, info);
535 }
536
537 void addRelativeAxis(int32_t deviceId, int32_t axis) {
538 Device* device = getDevice(deviceId);
539 device->relativeAxes.add(axis, true);
540 }
541
542 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
543 Device* device = getDevice(deviceId);
544 device->keyCodeStates.replaceValueFor(keyCode, state);
545 }
546
547 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
548 Device* device = getDevice(deviceId);
549 device->scanCodeStates.replaceValueFor(scanCode, state);
550 }
551
552 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
553 Device* device = getDevice(deviceId);
554 device->switchStates.replaceValueFor(switchCode, state);
555 }
556
557 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
558 Device* device = getDevice(deviceId);
559 device->absoluteAxisValue.replaceValueFor(axis, value);
560 }
561
562 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
563 int32_t keyCode, uint32_t flags) {
564 Device* device = getDevice(deviceId);
565 KeyInfo info;
566 info.keyCode = keyCode;
567 info.flags = flags;
568 if (scanCode) {
569 device->keysByScanCode.add(scanCode, info);
570 }
571 if (usageCode) {
572 device->keysByUsageCode.add(usageCode, info);
573 }
574 }
575
576 void addLed(int32_t deviceId, int32_t led, bool initialState) {
577 Device* device = getDevice(deviceId);
578 device->leds.add(led, initialState);
579 }
580
Chris Yef59a2f42020-10-16 12:55:26 -0700581 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
582 int32_t sensorDataIndex) {
583 Device* device = getDevice(deviceId);
584 SensorInfo info;
585 info.sensorType = sensorType;
586 info.sensorDataIndex = sensorDataIndex;
587 device->sensorsByAbsCode.emplace(absCode, info);
588 }
589
590 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
591 Device* device = getDevice(deviceId);
592 typename BitArray<MSC_MAX>::Buffer buffer;
593 buffer[mscEvent / 32] = 1 << mscEvent % 32;
594 device->mscBitmask.loadFromBuffer(buffer);
595 }
596
Chris Ye3fdbfef2021-01-06 18:45:18 -0800597 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
598 mRawLightInfos.emplace(rawId, std::move(info));
599 }
600
601 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
602 mLightBrightness.emplace(rawId, brightness);
603 }
604
605 void fakeLightIntensities(int32_t rawId,
606 const std::unordered_map<LightColor, int32_t> intensities) {
607 mLightIntensities.emplace(rawId, std::move(intensities));
608 }
609
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 bool getLedState(int32_t deviceId, int32_t led) {
611 Device* device = getDevice(deviceId);
612 return device->leds.valueFor(led);
613 }
614
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100615 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 return mExcludedDevices;
617 }
618
619 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
620 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800621 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 }
623
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000624 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
625 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700626 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 RawEvent event;
628 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000629 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 event.deviceId = deviceId;
631 event.type = type;
632 event.code = code;
633 event.value = value;
634 mEvents.push_back(event);
635
636 if (type == EV_ABS) {
637 setAbsoluteAxisValue(deviceId, code, value);
638 }
639 }
640
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600641 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
642 std::vector<TouchVideoFrame>> videoFrames) {
643 mVideoFrames = std::move(videoFrames);
644 }
645
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700647 std::unique_lock<std::mutex> lock(mLock);
648 base::ScopedLockAssertion assumeLocked(mLock);
649 const bool queueIsEmpty =
650 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
651 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
652 if (!queueIsEmpty) {
653 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
654 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 }
656
657private:
658 Device* getDevice(int32_t deviceId) const {
659 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100660 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 }
662
Chris Yea52ade12020-08-27 16:49:20 -0700663 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700665 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 }
667
Chris Yea52ade12020-08-27 16:49:20 -0700668 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 Device* device = getDevice(deviceId);
670 return device ? device->identifier : InputDeviceIdentifier();
671 }
672
Chris Yea52ade12020-08-27 16:49:20 -0700673 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674
Chris Yea52ade12020-08-27 16:49:20 -0700675 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 Device* device = getDevice(deviceId);
677 if (device) {
678 *outConfiguration = device->configuration;
679 }
680 }
681
Chris Yea52ade12020-08-27 16:49:20 -0700682 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
683 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800685 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 ssize_t index = device->absoluteAxes.indexOfKey(axis);
687 if (index >= 0) {
688 *outAxisInfo = device->absoluteAxes.valueAt(index);
689 return OK;
690 }
691 }
692 outAxisInfo->clear();
693 return -1;
694 }
695
Chris Yea52ade12020-08-27 16:49:20 -0700696 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 Device* device = getDevice(deviceId);
698 if (device) {
699 return device->relativeAxes.indexOfKey(axis) >= 0;
700 }
701 return false;
702 }
703
Chris Yea52ade12020-08-27 16:49:20 -0700704 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705
Chris Yef59a2f42020-10-16 12:55:26 -0700706 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
707 Device* device = getDevice(deviceId);
708 if (device) {
709 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
710 }
711 return false;
712 }
713
Chris Yea52ade12020-08-27 16:49:20 -0700714 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
715 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 Device* device = getDevice(deviceId);
717 if (device) {
718 const KeyInfo* key = getKey(device, scanCode, usageCode);
719 if (key) {
720 if (outKeycode) {
721 *outKeycode = key->keyCode;
722 }
723 if (outFlags) {
724 *outFlags = key->flags;
725 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700726 if (outMetaState) {
727 *outMetaState = metaState;
728 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 return OK;
730 }
731 }
732 return NAME_NOT_FOUND;
733 }
734
735 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
736 if (usageCode) {
737 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
738 if (index >= 0) {
739 return &device->keysByUsageCode.valueAt(index);
740 }
741 }
742 if (scanCode) {
743 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
744 if (index >= 0) {
745 return &device->keysByScanCode.valueAt(index);
746 }
747 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700748 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 }
750
Chris Yea52ade12020-08-27 16:49:20 -0700751 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752
Chris Yef59a2f42020-10-16 12:55:26 -0700753 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
754 int32_t absCode) {
755 Device* device = getDevice(deviceId);
756 if (!device) {
757 return Errorf("Sensor device not found.");
758 }
759 auto it = device->sensorsByAbsCode.find(absCode);
760 if (it == device->sensorsByAbsCode.end()) {
761 return Errorf("Sensor map not found.");
762 }
763 const SensorInfo& info = it->second;
764 return std::make_pair(info.sensorType, info.sensorDataIndex);
765 }
766
Chris Yea52ade12020-08-27 16:49:20 -0700767 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800768 mExcludedDevices = devices;
769 }
770
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000771 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
772 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000774 const size_t filledSize = std::min(mEvents.size(), bufferSize);
775 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
776
777 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700778 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000779 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800780 }
781
Chris Yea52ade12020-08-27 16:49:20 -0700782 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600783 auto it = mVideoFrames.find(deviceId);
784 if (it != mVideoFrames.end()) {
785 std::vector<TouchVideoFrame> frames = std::move(it->second);
786 mVideoFrames.erase(deviceId);
787 return frames;
788 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800789 return {};
790 }
791
Chris Yea52ade12020-08-27 16:49:20 -0700792 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793 Device* device = getDevice(deviceId);
794 if (device) {
795 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
796 if (index >= 0) {
797 return device->scanCodeStates.valueAt(index);
798 }
799 }
800 return AKEY_STATE_UNKNOWN;
801 }
802
Chris Yea52ade12020-08-27 16:49:20 -0700803 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 Device* device = getDevice(deviceId);
805 if (device) {
806 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
807 if (index >= 0) {
808 return device->keyCodeStates.valueAt(index);
809 }
810 }
811 return AKEY_STATE_UNKNOWN;
812 }
813
Chris Yea52ade12020-08-27 16:49:20 -0700814 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815 Device* device = getDevice(deviceId);
816 if (device) {
817 ssize_t index = device->switchStates.indexOfKey(sw);
818 if (index >= 0) {
819 return device->switchStates.valueAt(index);
820 }
821 }
822 return AKEY_STATE_UNKNOWN;
823 }
824
Chris Yea52ade12020-08-27 16:49:20 -0700825 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
826 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 Device* device = getDevice(deviceId);
828 if (device) {
829 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
830 if (index >= 0) {
831 *outValue = device->absoluteAxisValue.valueAt(index);
832 return OK;
833 }
834 }
835 *outValue = 0;
836 return -1;
837 }
838
Chris Yea52ade12020-08-27 16:49:20 -0700839 // Return true if the device has non-empty key layout.
840 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
841 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842 bool result = false;
843 Device* device = getDevice(deviceId);
844 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700845 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846 for (size_t i = 0; i < numCodes; i++) {
847 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
848 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
849 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 }
851 }
852 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
853 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
854 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 }
856 }
857 }
858 }
859 return result;
860 }
861
Chris Yea52ade12020-08-27 16:49:20 -0700862 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863 Device* device = getDevice(deviceId);
864 if (device) {
865 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
866 return index >= 0;
867 }
868 return false;
869 }
870
Arthur Hungcb40a002021-08-03 14:31:01 +0000871 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
872 Device* device = getDevice(deviceId);
873 if (!device) {
874 return false;
875 }
876 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
877 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
878 return true;
879 }
880 }
881 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
882 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
883 return true;
884 }
885 }
886 return false;
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 Device* device = getDevice(deviceId);
891 return device && device->leds.indexOfKey(led) >= 0;
892 }
893
Chris Yea52ade12020-08-27 16:49:20 -0700894 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 Device* device = getDevice(deviceId);
896 if (device) {
897 ssize_t index = device->leds.indexOfKey(led);
898 if (index >= 0) {
899 device->leds.replaceValueAt(led, on);
900 } else {
901 ADD_FAILURE()
902 << "Attempted to set the state of an LED that the EventHub declared "
903 "was not present. led=" << led;
904 }
905 }
906 }
907
Chris Yea52ade12020-08-27 16:49:20 -0700908 void getVirtualKeyDefinitions(
909 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 outVirtualKeys.clear();
911
912 Device* device = getDevice(deviceId);
913 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800914 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915 }
916 }
917
Chris Yea52ade12020-08-27 16:49:20 -0700918 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700919 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 }
921
Chris Yea52ade12020-08-27 16:49:20 -0700922 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 return false;
924 }
925
Chris Yea52ade12020-08-27 16:49:20 -0700926 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Chris Yea52ade12020-08-27 16:49:20 -0700928 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Chris Ye87143712020-11-10 05:05:58 +0000930 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
931
Chris Yee2b1e5c2021-03-10 22:45:12 -0800932 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
933 return BATTERY_CAPACITY;
934 }
Kim Low03ea0352020-11-06 12:45:07 -0800935
Chris Yee2b1e5c2021-03-10 22:45:12 -0800936 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
937 return BATTERY_STATUS;
938 }
939
940 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
941
942 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
943 return std::nullopt;
944 }
Kim Low03ea0352020-11-06 12:45:07 -0800945
Chris Ye3fdbfef2021-01-06 18:45:18 -0800946 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
947 std::vector<int32_t> ids;
948 for (const auto& [rawId, info] : mRawLightInfos) {
949 ids.push_back(rawId);
950 }
951 return ids;
952 }
953
954 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
955 auto it = mRawLightInfos.find(lightId);
956 if (it == mRawLightInfos.end()) {
957 return std::nullopt;
958 }
959 return it->second;
960 }
961
962 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
963 mLightBrightness.emplace(lightId, brightness);
964 }
965
966 void setLightIntensities(int32_t deviceId, int32_t lightId,
967 std::unordered_map<LightColor, int32_t> intensities) override {
968 mLightIntensities.emplace(lightId, intensities);
969 };
970
971 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
972 auto lightIt = mLightBrightness.find(lightId);
973 if (lightIt == mLightBrightness.end()) {
974 return std::nullopt;
975 }
976 return lightIt->second;
977 }
978
979 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
980 int32_t deviceId, int32_t lightId) override {
981 auto lightIt = mLightIntensities.find(lightId);
982 if (lightIt == mLightIntensities.end()) {
983 return std::nullopt;
984 }
985 return lightIt->second;
986 };
987
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100988 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 return false;
990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993
Chris Yea52ade12020-08-27 16:49:20 -0700994 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995
Chris Yea52ade12020-08-27 16:49:20 -0700996 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997
Chris Yea52ade12020-08-27 16:49:20 -0700998 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999};
1000
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001// --- FakeInputMapper ---
1002
1003class FakeInputMapper : public InputMapper {
1004 uint32_t mSources;
1005 int32_t mKeyboardType;
1006 int32_t mMetaState;
1007 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1008 KeyedVector<int32_t, int32_t> mScanCodeStates;
1009 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001010 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001012 std::mutex mLock;
1013 std::condition_variable mStateChangedCondition;
1014 bool mConfigureWasCalled GUARDED_BY(mLock);
1015 bool mResetWasCalled GUARDED_BY(mLock);
1016 bool mProcessWasCalled GUARDED_BY(mLock);
1017 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018
Arthur Hungc23540e2018-11-29 20:42:11 +08001019 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001021 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1022 : InputMapper(deviceContext),
1023 mSources(sources),
1024 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001026 mConfigureWasCalled(false),
1027 mResetWasCalled(false),
1028 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029
Chris Yea52ade12020-08-27 16:49:20 -07001030 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031
1032 void setKeyboardType(int32_t keyboardType) {
1033 mKeyboardType = keyboardType;
1034 }
1035
1036 void setMetaState(int32_t metaState) {
1037 mMetaState = metaState;
1038 }
1039
1040 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001041 std::unique_lock<std::mutex> lock(mLock);
1042 base::ScopedLockAssertion assumeLocked(mLock);
1043 const bool configureCalled =
1044 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1045 return mConfigureWasCalled;
1046 });
1047 if (!configureCalled) {
1048 FAIL() << "Expected configure() to have been called.";
1049 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050 mConfigureWasCalled = false;
1051 }
1052
1053 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001054 std::unique_lock<std::mutex> lock(mLock);
1055 base::ScopedLockAssertion assumeLocked(mLock);
1056 const bool resetCalled =
1057 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1058 return mResetWasCalled;
1059 });
1060 if (!resetCalled) {
1061 FAIL() << "Expected reset() to have been called.";
1062 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063 mResetWasCalled = false;
1064 }
1065
Yi Kong9b14ac62018-07-17 13:48:38 -07001066 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001067 std::unique_lock<std::mutex> lock(mLock);
1068 base::ScopedLockAssertion assumeLocked(mLock);
1069 const bool processCalled =
1070 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1071 return mProcessWasCalled;
1072 });
1073 if (!processCalled) {
1074 FAIL() << "Expected process() to have been called.";
1075 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076 if (outLastEvent) {
1077 *outLastEvent = mLastEvent;
1078 }
1079 mProcessWasCalled = false;
1080 }
1081
1082 void setKeyCodeState(int32_t keyCode, int32_t state) {
1083 mKeyCodeStates.replaceValueFor(keyCode, state);
1084 }
1085
1086 void setScanCodeState(int32_t scanCode, int32_t state) {
1087 mScanCodeStates.replaceValueFor(scanCode, state);
1088 }
1089
1090 void setSwitchState(int32_t switchCode, int32_t state) {
1091 mSwitchStates.replaceValueFor(switchCode, state);
1092 }
1093
1094 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001095 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096 }
1097
1098private:
Chris Yea52ade12020-08-27 16:49:20 -07001099 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100
Chris Yea52ade12020-08-27 16:49:20 -07001101 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 InputMapper::populateDeviceInfo(deviceInfo);
1103
1104 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1105 deviceInfo->setKeyboardType(mKeyboardType);
1106 }
1107 }
1108
Chris Yea52ade12020-08-27 16:49:20 -07001109 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001112
1113 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001114 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001115 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1116 mViewport = config->getDisplayViewportByPort(*displayPort);
1117 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001118
1119 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 }
1121
Chris Yea52ade12020-08-27 16:49:20 -07001122 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001123 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001124 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001125 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 }
1127
Chris Yea52ade12020-08-27 16:49:20 -07001128 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001129 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 mLastEvent = *rawEvent;
1131 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001132 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133 }
1134
Chris Yea52ade12020-08-27 16:49:20 -07001135 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001136 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1137 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1138 }
1139
Chris Yea52ade12020-08-27 16:49:20 -07001140 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1142 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1143 }
1144
Chris Yea52ade12020-08-27 16:49:20 -07001145 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1147 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1148 }
1149
Chris Yea52ade12020-08-27 16:49:20 -07001150 // Return true if the device has non-empty key layout.
1151 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1152 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 for (size_t i = 0; i < numCodes; i++) {
1154 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1155 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1156 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157 }
1158 }
1159 }
Chris Yea52ade12020-08-27 16:49:20 -07001160 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 return result;
1162 }
1163
1164 virtual int32_t getMetaState() {
1165 return mMetaState;
1166 }
1167
1168 virtual void fadePointer() {
1169 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001170
1171 virtual std::optional<int32_t> getAssociatedDisplay() {
1172 if (mViewport) {
1173 return std::make_optional(mViewport->displayId);
1174 }
1175 return std::nullopt;
1176 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177};
1178
1179
1180// --- InstrumentedInputReader ---
1181
1182class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001183 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184
1185public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001186 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1187 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001188 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001189 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001191 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001193 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001195 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001196 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197 InputDeviceIdentifier identifier;
1198 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001199 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001201 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 }
1203
Prabir Pradhan28efc192019-11-05 01:10:04 +00001204 // Make the protected loopOnce method accessible to tests.
1205 using InputReader::loopOnce;
1206
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001208 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1209 const InputDeviceIdentifier& identifier)
1210 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001211 if (!mNextDevices.empty()) {
1212 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1213 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 return device;
1215 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001216 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 }
1218
arthurhungdcef2dc2020-08-11 14:47:50 +08001219 // --- FakeInputReaderContext ---
1220 class FakeInputReaderContext : public ContextImpl {
1221 int32_t mGlobalMetaState;
1222 bool mUpdateGlobalMetaStateWasCalled;
1223 int32_t mGeneration;
1224
1225 public:
1226 FakeInputReaderContext(InputReader* reader)
1227 : ContextImpl(reader),
1228 mGlobalMetaState(0),
1229 mUpdateGlobalMetaStateWasCalled(false),
1230 mGeneration(1) {}
1231
1232 virtual ~FakeInputReaderContext() {}
1233
1234 void assertUpdateGlobalMetaStateWasCalled() {
1235 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1236 << "Expected updateGlobalMetaState() to have been called.";
1237 mUpdateGlobalMetaStateWasCalled = false;
1238 }
1239
1240 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1241
1242 uint32_t getGeneration() { return mGeneration; }
1243
1244 void updateGlobalMetaState() override {
1245 mUpdateGlobalMetaStateWasCalled = true;
1246 ContextImpl::updateGlobalMetaState();
1247 }
1248
1249 int32_t getGlobalMetaState() override {
1250 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1251 }
1252
1253 int32_t bumpGeneration() override {
1254 mGeneration = ContextImpl::bumpGeneration();
1255 return mGeneration;
1256 }
1257 } mFakeContext;
1258
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001260
1261public:
1262 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263};
1264
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001265// --- InputReaderPolicyTest ---
1266class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001267protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268 sp<FakeInputReaderPolicy> mFakePolicy;
1269
Chris Yea52ade12020-08-27 16:49:20 -07001270 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1271 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272};
1273
1274/**
1275 * Check that empty set of viewports is an acceptable configuration.
1276 * Also try to get internal viewport two different ways - by type and by uniqueId.
1277 *
1278 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1279 * Such configuration is not currently allowed.
1280 */
1281TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001282 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001283
1284 // We didn't add any viewports yet, so there shouldn't be any.
1285 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001286 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001287 ASSERT_FALSE(internalViewport);
1288
1289 // Add an internal viewport, then clear it
1290 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001291 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001292 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001293
1294 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001295 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001296 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001297 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298
1299 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001300 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001301 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001302 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001303
1304 mFakePolicy->clearViewports();
1305 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001306 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001307 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001308 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001309 ASSERT_FALSE(internalViewport);
1310}
1311
1312TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1313 const std::string internalUniqueId = "local:0";
1314 const std::string externalUniqueId = "local:1";
1315 const std::string virtualUniqueId1 = "virtual:2";
1316 const std::string virtualUniqueId2 = "virtual:3";
1317 constexpr int32_t virtualDisplayId1 = 2;
1318 constexpr int32_t virtualDisplayId2 = 3;
1319
1320 // Add an internal viewport
1321 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001322 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1323 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001324 // Add an external viewport
1325 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001326 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1327 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328 // Add an virtual viewport
1329 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001330 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1331 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001332 // Add another virtual viewport
1333 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001334 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1335 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001336
1337 // Check matching by type for internal
1338 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001339 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001340 ASSERT_TRUE(internalViewport);
1341 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1342
1343 // Check matching by type for external
1344 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001345 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346 ASSERT_TRUE(externalViewport);
1347 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1348
1349 // Check matching by uniqueId for virtual viewport #1
1350 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001351 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001353 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1355 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1356
1357 // Check matching by uniqueId for virtual viewport #2
1358 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001359 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001360 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001361 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1363 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1364}
1365
1366
1367/**
1368 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1369 * that lookup works by checking display id.
1370 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1371 */
1372TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1373 const std::string uniqueId1 = "uniqueId1";
1374 const std::string uniqueId2 = "uniqueId2";
1375 constexpr int32_t displayId1 = 2;
1376 constexpr int32_t displayId2 = 3;
1377
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001378 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1379 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001380 for (const ViewportType& type : types) {
1381 mFakePolicy->clearViewports();
1382 // Add a viewport
1383 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001384 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1385 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 // Add another viewport
1387 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001388 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1389 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390
1391 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001392 std::optional<DisplayViewport> viewport1 =
1393 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394 ASSERT_TRUE(viewport1);
1395 ASSERT_EQ(displayId1, viewport1->displayId);
1396 ASSERT_EQ(type, viewport1->type);
1397
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001398 std::optional<DisplayViewport> viewport2 =
1399 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001400 ASSERT_TRUE(viewport2);
1401 ASSERT_EQ(displayId2, viewport2->displayId);
1402 ASSERT_EQ(type, viewport2->type);
1403
1404 // When there are multiple viewports of the same kind, and uniqueId is not specified
1405 // in the call to getDisplayViewport, then that situation is not supported.
1406 // The viewports can be stored in any order, so we cannot rely on the order, since that
1407 // is just implementation detail.
1408 // However, we can check that it still returns *a* viewport, we just cannot assert
1409 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001410 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 ASSERT_TRUE(someViewport);
1412 }
1413}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001414
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001415/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001416 * When we have multiple internal displays make sure we always return the default display when
1417 * querying by type.
1418 */
1419TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1420 const std::string uniqueId1 = "uniqueId1";
1421 const std::string uniqueId2 = "uniqueId2";
1422 constexpr int32_t nonDefaultDisplayId = 2;
1423 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1424 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1425
1426 // Add the default display first and ensure it gets returned.
1427 mFakePolicy->clearViewports();
1428 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001429 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001430 ViewportType::INTERNAL);
1431 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001432 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001433 ViewportType::INTERNAL);
1434
1435 std::optional<DisplayViewport> viewport =
1436 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1437 ASSERT_TRUE(viewport);
1438 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1439 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1440
1441 // Add the default display second to make sure order doesn't matter.
1442 mFakePolicy->clearViewports();
1443 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001444 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001445 ViewportType::INTERNAL);
1446 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001447 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001448 ViewportType::INTERNAL);
1449
1450 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1451 ASSERT_TRUE(viewport);
1452 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1453 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1454}
1455
1456/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001457 * Check getDisplayViewportByPort
1458 */
1459TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001460 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001461 const std::string uniqueId1 = "uniqueId1";
1462 const std::string uniqueId2 = "uniqueId2";
1463 constexpr int32_t displayId1 = 1;
1464 constexpr int32_t displayId2 = 2;
1465 const uint8_t hdmi1 = 0;
1466 const uint8_t hdmi2 = 1;
1467 const uint8_t hdmi3 = 2;
1468
1469 mFakePolicy->clearViewports();
1470 // Add a viewport that's associated with some display port that's not of interest.
1471 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001472 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1473 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001474 // Add another viewport, connected to HDMI1 port
1475 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001476 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1477 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001478
1479 // Check that correct display viewport was returned by comparing the display ports.
1480 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1481 ASSERT_TRUE(hdmi1Viewport);
1482 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1483 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1484
1485 // Check that we can still get the same viewport using the uniqueId
1486 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1487 ASSERT_TRUE(hdmi1Viewport);
1488 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1489 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1490 ASSERT_EQ(type, hdmi1Viewport->type);
1491
1492 // Check that we cannot find a port with "HDMI2", because we never added one
1493 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1494 ASSERT_FALSE(hdmi2Viewport);
1495}
1496
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497// --- InputReaderTest ---
1498
1499class InputReaderTest : public testing::Test {
1500protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001501 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001502 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001503 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001504 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001505
Chris Yea52ade12020-08-27 16:49:20 -07001506 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001507 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001509 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510
Prabir Pradhan28efc192019-11-05 01:10:04 +00001511 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001512 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001513 }
1514
Chris Yea52ade12020-08-27 16:49:20 -07001515 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001516 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001517 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518 }
1519
Chris Ye1b0c7342020-07-28 21:57:03 -07001520 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001521 const PropertyMap* configuration) {
1522 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001523
1524 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001525 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526 }
1527 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001528 mReader->loopOnce();
1529 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001530 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1531 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001532 }
1533
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001534 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001535 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001536 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001537 }
1538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001539 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001540 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001541 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001542 }
1543
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001544 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001545 const std::string& name,
1546 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001547 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1549 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001550 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001551 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001552 return mapper;
1553 }
1554};
1555
Chris Ye98d3f532020-10-01 21:48:59 -07001556TEST_F(InputReaderTest, PolicyGetInputDevices) {
1557 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1558 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1559 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560
1561 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001562 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001564 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001565 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1567 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001568 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569}
1570
Chris Yee7310032020-09-22 15:36:28 -07001571TEST_F(InputReaderTest, GetMergedInputDevices) {
1572 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1573 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1574 // Add two subdevices to device
1575 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1576 // Must add at least one mapper or the device will be ignored!
1577 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1578 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1579
1580 // Push same device instance for next device to be added, so they'll have same identifier.
1581 mReader->pushNextDevice(device);
1582 mReader->pushNextDevice(device);
1583 ASSERT_NO_FATAL_FAILURE(
1584 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1585 ASSERT_NO_FATAL_FAILURE(
1586 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1587
1588 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001589 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001590}
1591
Chris Yee14523a2020-12-19 13:46:00 -08001592TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1593 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1594 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1595 // Add two subdevices to device
1596 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1597 // Must add at least one mapper or the device will be ignored!
1598 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1599 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1600
1601 // Push same device instance for next device to be added, so they'll have same identifier.
1602 mReader->pushNextDevice(device);
1603 mReader->pushNextDevice(device);
1604 // Sensor device is initially disabled
1605 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1606 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1607 nullptr));
1608 // Device is disabled because the only sub device is a sensor device and disabled initially.
1609 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1610 ASSERT_FALSE(device->isEnabled());
1611 ASSERT_NO_FATAL_FAILURE(
1612 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1613 // The merged device is enabled if any sub device is enabled
1614 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1615 ASSERT_TRUE(device->isEnabled());
1616}
1617
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001619 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001620 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001621 constexpr int32_t eventHubId = 1;
1622 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001624 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001625 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001627
Yi Kong9b14ac62018-07-17 13:48:38 -07001628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001629
1630 NotifyDeviceResetArgs resetArgs;
1631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001632 ASSERT_EQ(deviceId, resetArgs.deviceId);
1633
1634 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001635 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001636 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001637
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001639 ASSERT_EQ(deviceId, resetArgs.deviceId);
1640 ASSERT_EQ(device->isEnabled(), false);
1641
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001642 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001643 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001646 ASSERT_EQ(device->isEnabled(), false);
1647
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001648 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001649 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001651 ASSERT_EQ(deviceId, resetArgs.deviceId);
1652 ASSERT_EQ(device->isEnabled(), true);
1653}
1654
Michael Wrightd02c5b62014-02-10 15:10:22 -08001655TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001656 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001657 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001659 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001661 AINPUT_SOURCE_KEYBOARD, nullptr);
1662 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663
1664 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1665 AINPUT_SOURCE_ANY, AKEYCODE_A))
1666 << "Should return unknown when the device id is >= 0 but unknown.";
1667
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001668 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1669 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1670 << "Should return unknown when the device id is valid but the sources are not "
1671 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001673 ASSERT_EQ(AKEY_STATE_DOWN,
1674 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1675 AKEYCODE_A))
1676 << "Should return value provided by mapper when device id is valid and the device "
1677 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001678
1679 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1680 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1681 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1682
1683 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1684 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1685 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1686}
1687
1688TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001689 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001690 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001692 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001693 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001694 AINPUT_SOURCE_KEYBOARD, nullptr);
1695 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696
1697 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1698 AINPUT_SOURCE_ANY, KEY_A))
1699 << "Should return unknown when the device id is >= 0 but unknown.";
1700
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1702 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1703 << "Should return unknown when the device id is valid but the sources are not "
1704 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001705
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 ASSERT_EQ(AKEY_STATE_DOWN,
1707 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1708 KEY_A))
1709 << "Should return value provided by mapper when device id is valid and the device "
1710 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711
1712 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1713 AINPUT_SOURCE_TRACKBALL, KEY_A))
1714 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1715
1716 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1717 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1718 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1719}
1720
1721TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001723 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001727 AINPUT_SOURCE_KEYBOARD, nullptr);
1728 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
1730 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1731 AINPUT_SOURCE_ANY, SW_LID))
1732 << "Should return unknown when the device id is >= 0 but unknown.";
1733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001734 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1735 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1736 << "Should return unknown when the device id is valid but the sources are not "
1737 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739 ASSERT_EQ(AKEY_STATE_DOWN,
1740 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1741 SW_LID))
1742 << "Should return value provided by mapper when device id is valid and the device "
1743 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1746 AINPUT_SOURCE_TRACKBALL, SW_LID))
1747 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1748
1749 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1750 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1751 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1752}
1753
1754TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001755 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001756 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001757 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001758 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001759 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001760 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001761
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001762 mapper.addSupportedKeyCode(AKEYCODE_A);
1763 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001764
1765 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1766 uint8_t flags[4] = { 0, 0, 0, 1 };
1767
1768 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1769 << "Should return false when device id is >= 0 but unknown.";
1770 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1771
1772 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001773 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1774 << "Should return false when device id is valid but the sources are not supported by "
1775 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001776 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1777
1778 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1780 keyCodes, flags))
1781 << "Should return value provided by mapper when device id is valid and the device "
1782 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001783 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1784
1785 flags[3] = 1;
1786 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1787 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1788 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1789
1790 flags[3] = 1;
1791 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1792 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1793 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1794}
1795
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001796TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001797 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001798 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799
1800 NotifyConfigurationChangedArgs args;
1801
1802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1803 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1804}
1805
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001806TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001807 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001808 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001809 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001811 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001812 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001813 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001815
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001816 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001817 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1819
1820 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001821 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001822 ASSERT_EQ(when, event.when);
1823 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001824 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001825 ASSERT_EQ(EV_KEY, event.type);
1826 ASSERT_EQ(KEY_A, event.code);
1827 ASSERT_EQ(1, event.value);
1828}
1829
Garfield Tan1c7bc862020-01-28 13:24:04 -08001830TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001831 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001832 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001833 constexpr int32_t eventHubId = 1;
1834 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001835 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001836 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001837 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001838 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001839
1840 NotifyDeviceResetArgs resetArgs;
1841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001842 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001844 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001845 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001847 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001848 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001850 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001851 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001853 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001854 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001856 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001857 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001859 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001860 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001861}
1862
Garfield Tan1c7bc862020-01-28 13:24:04 -08001863TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1864 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001865 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001866 constexpr int32_t eventHubId = 1;
1867 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1868 // Must add at least one mapper or the device will be ignored!
1869 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001870 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001871 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1872
1873 NotifyDeviceResetArgs resetArgs;
1874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1875 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1876}
1877
Arthur Hungc23540e2018-11-29 20:42:11 +08001878TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001879 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001880 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001881 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001882 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001883 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1884 FakeInputMapper& mapper =
1885 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001886 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001887
1888 const uint8_t hdmi1 = 1;
1889
1890 // Associated touch screen with second display.
1891 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1892
1893 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001894 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001895 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001896 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001897 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001898 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001899 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001900 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001901 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001902 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001903
1904 // Add the device, and make sure all of the callbacks are triggered.
1905 // The device is added after the input port associations are processed since
1906 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001910 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001911
Arthur Hung2c9a3342019-07-23 14:18:59 +08001912 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001913 ASSERT_EQ(deviceId, device->getId());
1914 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1915 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001916
1917 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001918 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001919 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001920 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001921}
1922
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001923TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1924 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1925 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1926 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1927 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1928 // Must add at least one mapper or the device will be ignored!
1929 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1930 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1931 mReader->pushNextDevice(device);
1932 mReader->pushNextDevice(device);
1933 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1934 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1935
1936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1937
1938 NotifyDeviceResetArgs resetArgs;
1939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1940 ASSERT_EQ(deviceId, resetArgs.deviceId);
1941 ASSERT_TRUE(device->isEnabled());
1942 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1943 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1944
1945 disableDevice(deviceId);
1946 mReader->loopOnce();
1947
1948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1949 ASSERT_EQ(deviceId, resetArgs.deviceId);
1950 ASSERT_FALSE(device->isEnabled());
1951 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1952 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1953
1954 enableDevice(deviceId);
1955 mReader->loopOnce();
1956
1957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1958 ASSERT_EQ(deviceId, resetArgs.deviceId);
1959 ASSERT_TRUE(device->isEnabled());
1960 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1961 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1962}
1963
1964TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1965 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1966 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1967 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1968 // Add two subdevices to device
1969 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1970 FakeInputMapper& mapperDevice1 =
1971 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1972 FakeInputMapper& mapperDevice2 =
1973 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1974 mReader->pushNextDevice(device);
1975 mReader->pushNextDevice(device);
1976 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1977 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1978
1979 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1980 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1981
1982 ASSERT_EQ(AKEY_STATE_DOWN,
1983 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1984 ASSERT_EQ(AKEY_STATE_DOWN,
1985 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1986 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1987 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1988}
1989
Prabir Pradhan7e186182020-11-10 13:56:45 -08001990TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1991 NotifyPointerCaptureChangedArgs args;
1992
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001993 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001994 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1995 mReader->loopOnce();
1996 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001997 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1998 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001999
2000 mFakePolicy->setPointerCapture(false);
2001 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2002 mReader->loopOnce();
2003 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002004 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002005
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002006 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002007 // does not change.
2008 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2009 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002010 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002011}
2012
Chris Ye87143712020-11-10 05:05:58 +00002013class FakeVibratorInputMapper : public FakeInputMapper {
2014public:
2015 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2016 : FakeInputMapper(deviceContext, sources) {}
2017
2018 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2019};
2020
2021TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2022 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2023 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2024 constexpr int32_t eventHubId = 1;
2025 const char* DEVICE_LOCATION = "BLUETOOTH";
2026 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2027 FakeVibratorInputMapper& mapper =
2028 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2029 mReader->pushNextDevice(device);
2030
2031 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2032 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2033
2034 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2035 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2036}
2037
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002038// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002039
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002040class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002041public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002042 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002043
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002044 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002045
2046 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2047
2048 void dump(std::string& dump) override {}
2049
2050 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2051 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002052 }
2053
Chris Yee2b1e5c2021-03-10 22:45:12 -08002054 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2055 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002056 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002057
2058 bool setLightColor(int32_t lightId, int32_t color) override {
2059 getDeviceContext().setLightBrightness(lightId, color >> 24);
2060 return true;
2061 }
2062
2063 std::optional<int32_t> getLightColor(int32_t lightId) override {
2064 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2065 if (!result.has_value()) {
2066 return std::nullopt;
2067 }
2068 return result.value() << 24;
2069 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002070
2071 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2072
2073 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2074
2075private:
2076 InputDeviceContext& mDeviceContext;
2077 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2078 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002079};
2080
Chris Yee2b1e5c2021-03-10 22:45:12 -08002081TEST_F(InputReaderTest, BatteryGetCapacity) {
2082 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2083 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2084 constexpr int32_t eventHubId = 1;
2085 const char* DEVICE_LOCATION = "BLUETOOTH";
2086 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002087 FakePeripheralController& controller =
2088 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002089 mReader->pushNextDevice(device);
2090
2091 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2092
2093 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2094 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2095}
2096
2097TEST_F(InputReaderTest, BatteryGetStatus) {
2098 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2099 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2100 constexpr int32_t eventHubId = 1;
2101 const char* DEVICE_LOCATION = "BLUETOOTH";
2102 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002103 FakePeripheralController& controller =
2104 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002105 mReader->pushNextDevice(device);
2106
2107 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2108
2109 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2110 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2111}
2112
Chris Ye3fdbfef2021-01-06 18:45:18 -08002113TEST_F(InputReaderTest, LightGetColor) {
2114 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2115 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2116 constexpr int32_t eventHubId = 1;
2117 const char* DEVICE_LOCATION = "BLUETOOTH";
2118 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002119 FakePeripheralController& controller =
2120 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002121 mReader->pushNextDevice(device);
2122 RawLightInfo info = {.id = 1,
2123 .name = "Mono",
2124 .maxBrightness = 255,
2125 .flags = InputLightClass::BRIGHTNESS,
2126 .path = ""};
2127 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2128 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2129
2130 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002131
Chris Yee2b1e5c2021-03-10 22:45:12 -08002132 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2133 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002134 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2135 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2136}
2137
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002138// --- InputReaderIntegrationTest ---
2139
2140// These tests create and interact with the InputReader only through its interface.
2141// The InputReader is started during SetUp(), which starts its processing in its own
2142// thread. The tests use linux uinput to emulate input devices.
2143// NOTE: Interacting with the physical device while these tests are running may cause
2144// the tests to fail.
2145class InputReaderIntegrationTest : public testing::Test {
2146protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002147 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002148 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002149 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002150
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002151 std::shared_ptr<FakePointerController> mFakePointerController;
2152
Chris Yea52ade12020-08-27 16:49:20 -07002153 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002154 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002155 mFakePointerController = std::make_shared<FakePointerController>();
2156 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002157 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2158 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002159
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002160 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2161 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002162 ASSERT_EQ(mReader->start(), OK);
2163
2164 // Since this test is run on a real device, all the input devices connected
2165 // to the test device will show up in mReader. We wait for those input devices to
2166 // show up before beginning the tests.
2167 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2168 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2169 }
2170
Chris Yea52ade12020-08-27 16:49:20 -07002171 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002172 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002173 mReader.reset();
2174 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002175 mFakePolicy.clear();
2176 }
2177};
2178
2179TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2180 // An invalid input device that is only used for this test.
2181 class InvalidUinputDevice : public UinputDevice {
2182 public:
2183 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2184
2185 private:
2186 void configureDevice(int fd, uinput_user_dev* device) override {}
2187 };
2188
2189 const size_t numDevices = mFakePolicy->getInputDevices().size();
2190
2191 // UinputDevice does not set any event or key bits, so InputReader should not
2192 // consider it as a valid device.
2193 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2194 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2195 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2196 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2197
2198 invalidDevice.reset();
2199 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2200 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2201 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2202}
2203
2204TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2205 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2206
2207 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2208 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2210 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2211
2212 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002213 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002214 const auto& it =
2215 std::find_if(inputDevices.begin(), inputDevices.end(),
2216 [&keyboard](const InputDeviceInfo& info) {
2217 return info.getIdentifier().name == keyboard->getName();
2218 });
2219
2220 ASSERT_NE(it, inputDevices.end());
2221 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2222 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2223 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002224
2225 keyboard.reset();
2226 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2228 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2229}
2230
2231TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2232 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2233 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2234
2235 NotifyConfigurationChangedArgs configChangedArgs;
2236 ASSERT_NO_FATAL_FAILURE(
2237 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002238 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002239 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2240
2241 NotifyKeyArgs keyArgs;
2242 keyboard->pressAndReleaseHomeKey();
2243 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2244 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002245 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002246 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002247 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002248 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002249 prevTimestamp = keyArgs.eventTime;
2250
2251 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2252 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002253 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002254 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002255 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002256}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002258/**
2259 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2260 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2261 * are passed to the listener.
2262 */
2263static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2264TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2265 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2266 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2267 NotifyKeyArgs keyArgs;
2268
2269 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2270 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2272 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2273
2274 controller->pressAndReleaseKey(BTN_GEAR_UP);
2275 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2276 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2277 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2278}
2279
Arthur Hungaab25622020-01-16 11:22:11 +08002280// --- TouchProcessTest ---
2281class TouchIntegrationTest : public InputReaderIntegrationTest {
2282protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002283 const std::string UNIQUE_ID = "local:0";
2284
Chris Yea52ade12020-08-27 16:49:20 -07002285 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002286 InputReaderIntegrationTest::SetUp();
2287 // At least add an internal display.
2288 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2289 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002290 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002291
2292 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2293 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2295 }
2296
2297 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2298 int32_t orientation, const std::string& uniqueId,
2299 std::optional<uint8_t> physicalPort,
2300 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002301 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2302 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002303 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2304 }
2305
2306 std::unique_ptr<UinputTouchScreen> mDevice;
2307};
2308
2309TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2310 NotifyMotionArgs args;
2311 const Point centerPoint = mDevice->getCenterPoint();
2312
2313 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002314 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002315 mDevice->sendDown(centerPoint);
2316 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2318
2319 // ACTION_MOVE
2320 mDevice->sendMove(centerPoint + Point(1, 1));
2321 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2323
2324 // ACTION_UP
2325 mDevice->sendUp();
2326 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2327 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2328}
2329
2330TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2331 NotifyMotionArgs args;
2332 const Point centerPoint = mDevice->getCenterPoint();
2333
2334 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002335 mDevice->sendSlot(FIRST_SLOT);
2336 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002337 mDevice->sendDown(centerPoint);
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2339 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2340
2341 // ACTION_POINTER_DOWN (Second slot)
2342 const Point secondPoint = centerPoint + Point(100, 100);
2343 mDevice->sendSlot(SECOND_SLOT);
2344 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2345 mDevice->sendDown(secondPoint + Point(1, 1));
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2347 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2348 args.action);
2349
2350 // ACTION_MOVE (Second slot)
2351 mDevice->sendMove(secondPoint);
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2354
2355 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002356 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002357 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002358 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002359 args.action);
2360
2361 // ACTION_UP
2362 mDevice->sendSlot(FIRST_SLOT);
2363 mDevice->sendUp();
2364 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2365 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2366}
2367
2368TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2369 NotifyMotionArgs args;
2370 const Point centerPoint = mDevice->getCenterPoint();
2371
2372 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002373 mDevice->sendSlot(FIRST_SLOT);
2374 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002375 mDevice->sendDown(centerPoint);
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2377 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2378
arthurhungcc7f9802020-04-30 17:55:40 +08002379 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002380 const Point secondPoint = centerPoint + Point(100, 100);
2381 mDevice->sendSlot(SECOND_SLOT);
2382 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2383 mDevice->sendDown(secondPoint);
2384 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2385 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2386 args.action);
2387
arthurhungcc7f9802020-04-30 17:55:40 +08002388 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002389 mDevice->sendMove(secondPoint + Point(1, 1));
2390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2392
arthurhungcc7f9802020-04-30 17:55:40 +08002393 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2394 // a palm event.
2395 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002396 mDevice->sendToolType(MT_TOOL_PALM);
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002398 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2399 args.action);
2400 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002401
arthurhungcc7f9802020-04-30 17:55:40 +08002402 // Send up to second slot, expect first slot send moving.
2403 mDevice->sendPointerUp();
2404 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002406
arthurhungcc7f9802020-04-30 17:55:40 +08002407 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002408 mDevice->sendSlot(FIRST_SLOT);
2409 mDevice->sendUp();
2410
arthurhungcc7f9802020-04-30 17:55:40 +08002411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2412 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002413}
2414
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416class InputDeviceTest : public testing::Test {
2417protected:
2418 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002419 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 static const int32_t DEVICE_ID;
2421 static const int32_t DEVICE_GENERATION;
2422 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002423 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002424 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002426 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002428 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002429 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002430 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431
Chris Yea52ade12020-08-27 16:49:20 -07002432 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002433 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002435 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002436 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002437 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438 InputDeviceIdentifier identifier;
2439 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002440 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002441 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002442 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002443 mReader->pushNextDevice(mDevice);
2444 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2445 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446 }
2447
Chris Yea52ade12020-08-27 16:49:20 -07002448 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002449 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 }
2452};
2453
2454const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002455const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002456const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2458const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002459const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2460 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002461const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002462
2463TEST_F(InputDeviceTest, ImmutableProperties) {
2464 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002465 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002466 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467}
2468
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002469TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2470 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002471}
2472
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2474 // Configuration.
2475 InputReaderConfiguration config;
2476 mDevice->configure(ARBITRARY_TIME, &config, 0);
2477
2478 // Reset.
2479 mDevice->reset(ARBITRARY_TIME);
2480
2481 NotifyDeviceResetArgs resetArgs;
2482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2483 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2484 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2485
2486 // Metadata.
2487 ASSERT_TRUE(mDevice->isIgnored());
2488 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2489
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002490 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002492 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002493 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2494 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2495
2496 // State queries.
2497 ASSERT_EQ(0, mDevice->getMetaState());
2498
2499 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2500 << "Ignored device should return unknown key code state.";
2501 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2502 << "Ignored device should return unknown scan code state.";
2503 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2504 << "Ignored device should return unknown switch state.";
2505
2506 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2507 uint8_t flags[2] = { 0, 1 };
2508 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2509 << "Ignored device should never mark any key codes.";
2510 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2511 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2512}
2513
2514TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2515 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002516 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002518 FakeInputMapper& mapper1 =
2519 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002520 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2521 mapper1.setMetaState(AMETA_ALT_ON);
2522 mapper1.addSupportedKeyCode(AKEYCODE_A);
2523 mapper1.addSupportedKeyCode(AKEYCODE_B);
2524 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2525 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2526 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2527 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2528 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002530 FakeInputMapper& mapper2 =
2531 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002532 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533
2534 InputReaderConfiguration config;
2535 mDevice->configure(ARBITRARY_TIME, &config, 0);
2536
2537 String8 propertyValue;
2538 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2539 << "Device should have read configuration during configuration phase.";
2540 ASSERT_STREQ("value", propertyValue.string());
2541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002542 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2543 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544
2545 // Reset
2546 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002547 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2548 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549
2550 NotifyDeviceResetArgs resetArgs;
2551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2552 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2553 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2554
2555 // Metadata.
2556 ASSERT_FALSE(mDevice->isIgnored());
2557 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2558
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002559 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002561 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002562 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2563 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2564
2565 // State queries.
2566 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2567 << "Should query mappers and combine meta states.";
2568
2569 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2570 << "Should return unknown key code state when source not supported.";
2571 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2572 << "Should return unknown scan code state when source not supported.";
2573 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2574 << "Should return unknown switch state when source not supported.";
2575
2576 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2577 << "Should query mapper when source is supported.";
2578 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2579 << "Should query mapper when source is supported.";
2580 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2581 << "Should query mapper when source is supported.";
2582
2583 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2584 uint8_t flags[4] = { 0, 0, 0, 1 };
2585 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2586 << "Should do nothing when source is unsupported.";
2587 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2588 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2589 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2590 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2591
2592 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2593 << "Should query mapper when source is supported.";
2594 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2595 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2596 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2597 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2598
2599 // Event handling.
2600 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002601 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602 mDevice->process(&event, 1);
2603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002604 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2605 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606}
2607
Arthur Hung2c9a3342019-07-23 14:18:59 +08002608// A single input device is associated with a specific display. Check that:
2609// 1. Device is disabled if the viewport corresponding to the associated display is not found
2610// 2. Device is disabled when setEnabled API is called
2611TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002612 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002613
2614 // First Configuration.
2615 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2616
2617 // Device should be enabled by default.
2618 ASSERT_TRUE(mDevice->isEnabled());
2619
2620 // Prepare associated info.
2621 constexpr uint8_t hdmi = 1;
2622 const std::string UNIQUE_ID = "local:1";
2623
2624 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2625 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2626 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2627 // Device should be disabled because it is associated with a specific display via
2628 // input port <-> display port association, but the corresponding display is not found
2629 ASSERT_FALSE(mDevice->isEnabled());
2630
2631 // Prepare displays.
2632 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002633 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2634 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002635 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2636 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2637 ASSERT_TRUE(mDevice->isEnabled());
2638
2639 // Device should be disabled after set disable.
2640 mFakePolicy->addDisabledDevice(mDevice->getId());
2641 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2642 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2643 ASSERT_FALSE(mDevice->isEnabled());
2644
2645 // Device should still be disabled even found the associated display.
2646 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2647 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2648 ASSERT_FALSE(mDevice->isEnabled());
2649}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650
Christine Franks1ba71cc2021-04-07 14:37:42 -07002651TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2652 // Device should be enabled by default.
2653 mFakePolicy->clearViewports();
2654 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2655 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2656 ASSERT_TRUE(mDevice->isEnabled());
2657
2658 // Device should be disabled because it is associated with a specific display, but the
2659 // corresponding display is not found.
2660 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2661 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2662 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2663 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2664 ASSERT_FALSE(mDevice->isEnabled());
2665
2666 // Device should be enabled when a display is found.
2667 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2668 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2669 NO_PORT, ViewportType::INTERNAL);
2670 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2671 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2672 ASSERT_TRUE(mDevice->isEnabled());
2673
2674 // Device should be disabled after set disable.
2675 mFakePolicy->addDisabledDevice(mDevice->getId());
2676 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2677 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2678 ASSERT_FALSE(mDevice->isEnabled());
2679
2680 // Device should still be disabled even found the associated display.
2681 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2682 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2683 ASSERT_FALSE(mDevice->isEnabled());
2684}
2685
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686// --- InputMapperTest ---
2687
2688class InputMapperTest : public testing::Test {
2689protected:
2690 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002691 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 static const int32_t DEVICE_ID;
2693 static const int32_t DEVICE_GENERATION;
2694 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002695 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002696 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002698 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002699 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002700 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002701 std::unique_ptr<InstrumentedInputReader> mReader;
2702 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703
Chris Ye1b0c7342020-07-28 21:57:03 -07002704 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002705 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002707 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002708 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002709 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002710 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 }
2712
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002713 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002714 SetUp(DEVICE_CLASSES);
2715 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002716
Chris Yea52ade12020-08-27 16:49:20 -07002717 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002718 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720 }
2721
2722 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002723 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724 }
2725
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002726 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002727 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002728 mReader->requestRefreshConfiguration(changes);
2729 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002730 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002731 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2732 }
2733
arthurhungdcef2dc2020-08-11 14:47:50 +08002734 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2735 const std::string& location, int32_t eventHubId,
2736 Flags<InputDeviceClass> classes) {
2737 InputDeviceIdentifier identifier;
2738 identifier.name = name;
2739 identifier.location = location;
2740 std::shared_ptr<InputDevice> device =
2741 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2742 identifier);
2743 mReader->pushNextDevice(device);
2744 mFakeEventHub->addDevice(eventHubId, name, classes);
2745 mReader->loopOnce();
2746 return device;
2747 }
2748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002749 template <class T, typename... Args>
2750 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002751 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002752 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002754 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002755 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756 }
2757
2758 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002759 int32_t orientation, const std::string& uniqueId,
2760 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002761 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2762 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002763 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2764 }
2765
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002766 void clearViewports() {
2767 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768 }
2769
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002770 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2771 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 RawEvent event;
2773 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002774 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002775 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776 event.type = type;
2777 event.code = code;
2778 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002780 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781 }
2782
2783 static void assertMotionRange(const InputDeviceInfo& info,
2784 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2785 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002786 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2788 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2789 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2790 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2791 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2792 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2793 }
2794
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002795 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2796 float size, float touchMajor, float touchMinor, float toolMajor,
2797 float toolMinor, float orientation, float distance,
2798 float scaledAxisEpsilon = 1.f) {
2799 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2800 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2802 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002803 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2804 scaledAxisEpsilon);
2805 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2806 scaledAxisEpsilon);
2807 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2808 scaledAxisEpsilon);
2809 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2810 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2812 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2813 }
2814
Michael Wright17db18e2020-06-26 20:51:44 +01002815 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002817 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818 ASSERT_NEAR(x, actualX, 1);
2819 ASSERT_NEAR(y, actualY, 1);
2820 }
2821};
2822
2823const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002824const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002825const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2827const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002828const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2829 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002830const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831
2832// --- SwitchInputMapperTest ---
2833
2834class SwitchInputMapperTest : public InputMapperTest {
2835protected:
2836};
2837
2838TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002839 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002841 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842}
2843
2844TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002845 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002847 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002848 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002850 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852}
2853
2854TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002855 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861
2862 NotifySwitchArgs args;
2863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2864 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002865 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2866 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867 args.switchMask);
2868 ASSERT_EQ(uint32_t(0), args.policyFlags);
2869}
2870
Chris Ye87143712020-11-10 05:05:58 +00002871// --- VibratorInputMapperTest ---
2872class VibratorInputMapperTest : public InputMapperTest {
2873protected:
2874 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2875};
2876
2877TEST_F(VibratorInputMapperTest, GetSources) {
2878 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2879
2880 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2881}
2882
2883TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2884 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2885
2886 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2887}
2888
2889TEST_F(VibratorInputMapperTest, Vibrate) {
2890 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002891 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002892 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2893
2894 VibrationElement pattern(2);
2895 VibrationSequence sequence(2);
2896 pattern.duration = std::chrono::milliseconds(200);
2897 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2898 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2899 sequence.addElement(pattern);
2900 pattern.duration = std::chrono::milliseconds(500);
2901 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2902 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2903 sequence.addElement(pattern);
2904
2905 std::vector<int64_t> timings = {0, 1};
2906 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2907
2908 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002909 // Start vibrating
2910 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002911 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002912 // Verify vibrator state listener was notified.
2913 mReader->loopOnce();
2914 NotifyVibratorStateArgs args;
2915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2916 ASSERT_EQ(DEVICE_ID, args.deviceId);
2917 ASSERT_TRUE(args.isOn);
2918 // Stop vibrating
2919 mapper.cancelVibrate(VIBRATION_TOKEN);
2920 ASSERT_FALSE(mapper.isVibrating());
2921 // Verify vibrator state listener was notified.
2922 mReader->loopOnce();
2923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2924 ASSERT_EQ(DEVICE_ID, args.deviceId);
2925 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002926}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
Chris Yef59a2f42020-10-16 12:55:26 -07002928// --- SensorInputMapperTest ---
2929
2930class SensorInputMapperTest : public InputMapperTest {
2931protected:
2932 static const int32_t ACCEL_RAW_MIN;
2933 static const int32_t ACCEL_RAW_MAX;
2934 static const int32_t ACCEL_RAW_FUZZ;
2935 static const int32_t ACCEL_RAW_FLAT;
2936 static const int32_t ACCEL_RAW_RESOLUTION;
2937
2938 static const int32_t GYRO_RAW_MIN;
2939 static const int32_t GYRO_RAW_MAX;
2940 static const int32_t GYRO_RAW_FUZZ;
2941 static const int32_t GYRO_RAW_FLAT;
2942 static const int32_t GYRO_RAW_RESOLUTION;
2943
2944 static const float GRAVITY_MS2_UNIT;
2945 static const float DEGREE_RADIAN_UNIT;
2946
2947 void prepareAccelAxes();
2948 void prepareGyroAxes();
2949 void setAccelProperties();
2950 void setGyroProperties();
2951 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2952};
2953
2954const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2955const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2956const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2957const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2958const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2959
2960const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2961const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2962const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2963const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2964const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2965
2966const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2967const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2968
2969void SensorInputMapperTest::prepareAccelAxes() {
2970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2971 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2972 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2973 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2975 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2976}
2977
2978void SensorInputMapperTest::prepareGyroAxes() {
2979 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2980 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2981 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2982 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2983 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2984 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2985}
2986
2987void SensorInputMapperTest::setAccelProperties() {
2988 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2989 /* sensorDataIndex */ 0);
2990 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2991 /* sensorDataIndex */ 1);
2992 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2993 /* sensorDataIndex */ 2);
2994 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2995 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2996 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2997 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2998 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2999}
3000
3001void SensorInputMapperTest::setGyroProperties() {
3002 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3003 /* sensorDataIndex */ 0);
3004 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3005 /* sensorDataIndex */ 1);
3006 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3007 /* sensorDataIndex */ 2);
3008 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3009 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3010 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3011 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3012 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3013}
3014
3015TEST_F(SensorInputMapperTest, GetSources) {
3016 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3017
3018 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3019}
3020
3021TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3022 setAccelProperties();
3023 prepareAccelAxes();
3024 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3025
3026 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3027 std::chrono::microseconds(10000),
3028 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003029 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003035
3036 NotifySensorArgs args;
3037 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3038 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3039 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3040
3041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3042 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3043 ASSERT_EQ(args.deviceId, DEVICE_ID);
3044 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3045 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3046 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3047 ASSERT_EQ(args.values, values);
3048 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3049}
3050
3051TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3052 setGyroProperties();
3053 prepareGyroAxes();
3054 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3055
3056 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3057 std::chrono::microseconds(10000),
3058 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003059 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003065
3066 NotifySensorArgs args;
3067 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3068 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3069 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3070
3071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3072 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3073 ASSERT_EQ(args.deviceId, DEVICE_ID);
3074 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3075 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3076 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3077 ASSERT_EQ(args.values, values);
3078 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3079}
3080
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081// --- KeyboardInputMapperTest ---
3082
3083class KeyboardInputMapperTest : public InputMapperTest {
3084protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003085 const std::string UNIQUE_ID = "local:0";
3086
3087 void prepareDisplay(int32_t orientation);
3088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003090 int32_t originalKeyCode, int32_t rotatedKeyCode,
3091 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092};
3093
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003094/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3095 * orientation.
3096 */
3097void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003098 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3099 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003100}
3101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003103 int32_t originalScanCode, int32_t originalKeyCode,
3104 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 NotifyKeyArgs args;
3106
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3109 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3110 ASSERT_EQ(originalScanCode, args.scanCode);
3111 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003112 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3117 ASSERT_EQ(originalScanCode, args.scanCode);
3118 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003119 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120}
3121
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122TEST_F(KeyboardInputMapperTest, GetSources) {
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
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128}
3129
3130TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3131 const int32_t USAGE_A = 0x070004;
3132 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3134 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003135 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3136 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3137 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003139 KeyboardInputMapper& mapper =
3140 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3141 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003142 // Initial metastate to AMETA_NONE.
3143 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3144 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145
3146 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148 NotifyKeyArgs args;
3149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3150 ASSERT_EQ(DEVICE_ID, args.deviceId);
3151 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3152 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3153 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3154 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3155 ASSERT_EQ(KEY_HOME, args.scanCode);
3156 ASSERT_EQ(AMETA_NONE, args.metaState);
3157 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3158 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3159 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3160
3161 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003162 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3164 ASSERT_EQ(DEVICE_ID, args.deviceId);
3165 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3166 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3167 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3168 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3169 ASSERT_EQ(KEY_HOME, args.scanCode);
3170 ASSERT_EQ(AMETA_NONE, args.metaState);
3171 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3172 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3173 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3174
3175 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3179 ASSERT_EQ(DEVICE_ID, args.deviceId);
3180 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3181 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3182 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3183 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3184 ASSERT_EQ(0, args.scanCode);
3185 ASSERT_EQ(AMETA_NONE, args.metaState);
3186 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3187 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3188 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3189
3190 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3192 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3194 ASSERT_EQ(DEVICE_ID, args.deviceId);
3195 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3196 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3198 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3199 ASSERT_EQ(0, args.scanCode);
3200 ASSERT_EQ(AMETA_NONE, args.metaState);
3201 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3202 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3203 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3204
3205 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3209 ASSERT_EQ(DEVICE_ID, args.deviceId);
3210 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3211 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3212 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3213 ASSERT_EQ(0, args.keyCode);
3214 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3215 ASSERT_EQ(AMETA_NONE, args.metaState);
3216 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3217 ASSERT_EQ(0U, args.policyFlags);
3218 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3219
3220 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3222 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3224 ASSERT_EQ(DEVICE_ID, args.deviceId);
3225 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3226 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3228 ASSERT_EQ(0, args.keyCode);
3229 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3230 ASSERT_EQ(AMETA_NONE, args.metaState);
3231 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3232 ASSERT_EQ(0U, args.policyFlags);
3233 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3234}
3235
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003236/**
3237 * Ensure that the readTime is set to the time when the EV_KEY is received.
3238 */
3239TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3240 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3241
3242 KeyboardInputMapper& mapper =
3243 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3244 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3245 NotifyKeyArgs args;
3246
3247 // Key down
3248 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3250 ASSERT_EQ(12, args.readTime);
3251
3252 // Key up
3253 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3255 ASSERT_EQ(15, args.readTime);
3256}
3257
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003259 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3260 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003261 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3262 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3263 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 KeyboardInputMapper& mapper =
3266 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3267 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268
arthurhungc903df12020-08-11 15:08:42 +08003269 // Initial metastate to AMETA_NONE.
3270 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3271 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272
3273 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 NotifyKeyArgs args;
3276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003279 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280
3281 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003282 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003285 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286
3287 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003288 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292
3293 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003294 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3296 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003298 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299}
3300
3301TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003302 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3303 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3304 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3305 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003307 KeyboardInputMapper& mapper =
3308 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3309 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003311 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3313 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3314 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3315 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3316 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3317 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3318 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3319 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3320}
3321
3322TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003323 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3324 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3325 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3326 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003329 KeyboardInputMapper& mapper =
3330 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3331 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003333 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003334 ASSERT_NO_FATAL_FAILURE(
3335 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3336 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3337 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3338 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3339 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3340 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3341 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003343 clearViewports();
3344 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003345 ASSERT_NO_FATAL_FAILURE(
3346 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3347 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3348 AKEYCODE_DPAD_UP, DISPLAY_ID));
3349 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3350 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3351 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3352 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003354 clearViewports();
3355 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003356 ASSERT_NO_FATAL_FAILURE(
3357 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3358 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3359 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3360 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3361 AKEYCODE_DPAD_UP, DISPLAY_ID));
3362 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3363 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003365 clearViewports();
3366 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003367 ASSERT_NO_FATAL_FAILURE(
3368 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3369 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3370 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3371 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3372 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3373 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3374 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375
3376 // Special case: if orientation changes while key is down, we still emit the same keycode
3377 // in the key up as we did in the key down.
3378 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003379 clearViewports();
3380 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3384 ASSERT_EQ(KEY_UP, args.scanCode);
3385 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3386
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003387 clearViewports();
3388 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3391 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3392 ASSERT_EQ(KEY_UP, args.scanCode);
3393 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3394}
3395
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003396TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3397 // If the keyboard is not orientation aware,
3398 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003399 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401 KeyboardInputMapper& mapper =
3402 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3403 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003404 NotifyKeyArgs args;
3405
3406 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3412
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003413 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3418 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3419}
3420
3421TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3422 // If the keyboard is orientation aware,
3423 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003424 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003425
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003426 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003427 KeyboardInputMapper& mapper =
3428 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3429 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003430 NotifyKeyArgs args;
3431
3432 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3433 // ^--- already checked by the previous test
3434
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003435 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003436 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(DISPLAY_ID, args.displayId);
3442
3443 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003444 clearViewports();
3445 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003446 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3451 ASSERT_EQ(newDisplayId, args.displayId);
3452}
3453
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003455 KeyboardInputMapper& mapper =
3456 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3457 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003459 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003462 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464}
3465
3466TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467 KeyboardInputMapper& mapper =
3468 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3469 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003471 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003472 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003474 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003475 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476}
3477
3478TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003479 KeyboardInputMapper& mapper =
3480 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3481 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003483 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484
3485 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3486 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003487 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488 ASSERT_TRUE(flags[0]);
3489 ASSERT_FALSE(flags[1]);
3490}
3491
3492TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003493 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3494 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3495 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3498 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 KeyboardInputMapper& mapper =
3501 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3502 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003503 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003504 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3505 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
3507 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003508 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3509 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3510 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003515 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3516 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3517 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519
3520 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003523 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3524 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3525 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
3528 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003531 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3532 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3533 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003534 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
3536 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003539 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3540 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3541 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
3544 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003547 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3548 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3549 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003550 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551
3552 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003555 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3556 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3557 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003558 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559}
3560
Chris Yea52ade12020-08-27 16:49:20 -07003561TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3562 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3563 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3564 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3565 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3566
3567 KeyboardInputMapper& mapper =
3568 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3569 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3570
3571 // Initial metastate should be AMETA_NONE as no meta keys added.
3572 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3573 // Meta state should be AMETA_NONE after reset
3574 mapper.reset(ARBITRARY_TIME);
3575 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3576 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3577 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3578 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3579
3580 NotifyKeyArgs args;
3581 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3584 ASSERT_EQ(AMETA_NONE, args.metaState);
3585 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3586 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3587 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3588
3589 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003590 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3592 ASSERT_EQ(AMETA_NONE, args.metaState);
3593 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3594 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3595 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3596}
3597
Arthur Hung2c9a3342019-07-23 14:18:59 +08003598TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3599 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3601 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3602 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3603 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003604
3605 // keyboard 2.
3606 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003607 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003609 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003610 std::shared_ptr<InputDevice> device2 =
3611 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3612 Flags<InputDeviceClass>(0));
3613
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003614 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3615 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3616 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3617 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003619 KeyboardInputMapper& mapper =
3620 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3621 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003622
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003623 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003624 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003625 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003626 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3627 device2->reset(ARBITRARY_TIME);
3628
3629 // Prepared displays and associated info.
3630 constexpr uint8_t hdmi1 = 0;
3631 constexpr uint8_t hdmi2 = 1;
3632 const std::string SECONDARY_UNIQUE_ID = "local:1";
3633
3634 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3635 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3636
3637 // No associated display viewport found, should disable the device.
3638 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3639 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3640 ASSERT_FALSE(device2->isEnabled());
3641
3642 // Prepare second display.
3643 constexpr int32_t newDisplayId = 2;
3644 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003645 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003646 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003647 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003648 // Default device will reconfigure above, need additional reconfiguration for another device.
3649 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3650 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3651
3652 // Device should be enabled after the associated display is found.
3653 ASSERT_TRUE(mDevice->isEnabled());
3654 ASSERT_TRUE(device2->isEnabled());
3655
3656 // Test pad key events
3657 ASSERT_NO_FATAL_FAILURE(
3658 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3660 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3661 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3662 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3663 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3664 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3665
3666 ASSERT_NO_FATAL_FAILURE(
3667 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3668 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3669 AKEYCODE_DPAD_RIGHT, newDisplayId));
3670 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3671 AKEYCODE_DPAD_DOWN, newDisplayId));
3672 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3673 AKEYCODE_DPAD_LEFT, newDisplayId));
3674}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675
arthurhungc903df12020-08-11 15:08:42 +08003676TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3677 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3678 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3679 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3680 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3681 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3682 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3683
3684 KeyboardInputMapper& mapper =
3685 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3686 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3687 // Initial metastate to AMETA_NONE.
3688 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3689 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3690
3691 // Initialization should have turned all of the lights off.
3692 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3693 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3694 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3695
3696 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003699 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3700 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3701
3702 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003705 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3706 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3707
3708 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003711 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3712 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3713
3714 mFakeEventHub->removeDevice(EVENTHUB_ID);
3715 mReader->loopOnce();
3716
3717 // keyboard 2 should default toggle keys.
3718 const std::string USB2 = "USB2";
3719 const std::string DEVICE_NAME2 = "KEYBOARD2";
3720 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3721 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3722 std::shared_ptr<InputDevice> device2 =
3723 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3724 Flags<InputDeviceClass>(0));
3725 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3726 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3727 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3728 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3729 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3730 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3731
arthurhung6fe95782020-10-05 22:41:16 +08003732 KeyboardInputMapper& mapper2 =
3733 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3734 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003735 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3736 device2->reset(ARBITRARY_TIME);
3737
3738 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3739 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3740 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003741 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3742 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003743}
3744
Arthur Hungcb40a002021-08-03 14:31:01 +00003745TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3749
3750 // Suppose we have two mappers. (DPAD + KEYBOARD)
3751 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3752 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3753 KeyboardInputMapper& mapper =
3754 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3755 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3756 // Initialize metastate to AMETA_NUM_LOCK_ON.
3757 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3758 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3759
3760 mReader->toggleCapsLockState(DEVICE_ID);
3761 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3762}
3763
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003764// --- KeyboardInputMapperTest_ExternalDevice ---
3765
3766class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3767protected:
Chris Yea52ade12020-08-27 16:49:20 -07003768 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003769};
3770
3771TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003772 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3773 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003774
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003775 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3776 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3777 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3778 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003780 KeyboardInputMapper& mapper =
3781 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3782 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003783
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003785 NotifyKeyArgs args;
3786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3787 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3788
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003789 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3791 ASSERT_EQ(uint32_t(0), args.policyFlags);
3792
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3795 ASSERT_EQ(uint32_t(0), args.policyFlags);
3796
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003797 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3799 ASSERT_EQ(uint32_t(0), args.policyFlags);
3800
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3803 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3804
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003805 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3807 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3808}
3809
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003810TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003811 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003812
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003813 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3814 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3815 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003816
Powei Fengd041c5d2019-05-03 17:11:33 -07003817 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003818 KeyboardInputMapper& mapper =
3819 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3820 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003821
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003823 NotifyKeyArgs args;
3824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3825 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3826
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003827 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3829 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3830
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3833 ASSERT_EQ(uint32_t(0), args.policyFlags);
3834
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003835 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3837 ASSERT_EQ(uint32_t(0), args.policyFlags);
3838
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3841 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3842
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003843 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3845 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3846}
3847
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848// --- CursorInputMapperTest ---
3849
3850class CursorInputMapperTest : public InputMapperTest {
3851protected:
3852 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3853
Michael Wright17db18e2020-06-26 20:51:44 +01003854 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855
Chris Yea52ade12020-08-27 16:49:20 -07003856 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857 InputMapperTest::SetUp();
3858
Michael Wright17db18e2020-06-26 20:51:44 +01003859 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003860 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861 }
3862
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3864 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003865
3866 void prepareDisplay(int32_t orientation) {
3867 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003868 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003869 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3870 orientation, uniqueId, NO_PORT, viewportType);
3871 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003872
3873 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3874 float pressure) {
3875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3876 0.0f, 0.0f, 0.0f, EPSILON));
3877 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878};
3879
3880const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3883 int32_t originalY, int32_t rotatedX,
3884 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885 NotifyMotionArgs args;
3886
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003892 ASSERT_NO_FATAL_FAILURE(
3893 assertCursorPointerCoords(args.pointerCoords[0],
3894 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3895 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896}
3897
3898TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003902 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903}
3904
3905TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003909 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910}
3911
3912TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003914 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915
3916 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003917 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003920 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3921 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3923 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3924
3925 // When the bounds are set, then there should be a valid motion range.
3926 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3927
3928 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003929 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930
3931 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3932 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3933 1, 800 - 1, 0.0f, 0.0f));
3934 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3935 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3936 2, 480 - 1, 0.0f, 0.0f));
3937 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3938 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3939 0.0f, 1.0f, 0.0f, 0.0f));
3940}
3941
3942TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003944 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945
3946 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003947 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948
3949 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3950 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3951 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3952 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3953 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3954 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3955 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3956 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3957 0.0f, 1.0f, 0.0f, 0.0f));
3958}
3959
3960TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003962 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963
arthurhungdcef2dc2020-08-11 14:47:50 +08003964 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965
3966 NotifyMotionArgs args;
3967
3968 // Button press.
3969 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3973 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3974 ASSERT_EQ(DEVICE_ID, args.deviceId);
3975 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3976 ASSERT_EQ(uint32_t(0), args.policyFlags);
3977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3978 ASSERT_EQ(0, args.flags);
3979 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3980 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3981 ASSERT_EQ(0, args.edgeFlags);
3982 ASSERT_EQ(uint32_t(1), args.pointerCount);
3983 ASSERT_EQ(0, args.pointerProperties[0].id);
3984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003985 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3987 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3988 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3989
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3991 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3992 ASSERT_EQ(DEVICE_ID, args.deviceId);
3993 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3994 ASSERT_EQ(uint32_t(0), args.policyFlags);
3995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3996 ASSERT_EQ(0, args.flags);
3997 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3998 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3999 ASSERT_EQ(0, args.edgeFlags);
4000 ASSERT_EQ(uint32_t(1), args.pointerCount);
4001 ASSERT_EQ(0, args.pointerProperties[0].id);
4002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004003 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004004 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4005 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4006 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4007
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004009 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4010 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4012 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4013 ASSERT_EQ(DEVICE_ID, args.deviceId);
4014 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4015 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4017 ASSERT_EQ(0, args.flags);
4018 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4019 ASSERT_EQ(0, args.buttonState);
4020 ASSERT_EQ(0, args.edgeFlags);
4021 ASSERT_EQ(uint32_t(1), args.pointerCount);
4022 ASSERT_EQ(0, args.pointerProperties[0].id);
4023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004024 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004025 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4026 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4027 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4028
4029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4030 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4031 ASSERT_EQ(DEVICE_ID, args.deviceId);
4032 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4033 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4035 ASSERT_EQ(0, args.flags);
4036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4037 ASSERT_EQ(0, args.buttonState);
4038 ASSERT_EQ(0, args.edgeFlags);
4039 ASSERT_EQ(uint32_t(1), args.pointerCount);
4040 ASSERT_EQ(0, args.pointerProperties[0].id);
4041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004042 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4044 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4045 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4046}
4047
4048TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051
4052 NotifyMotionArgs args;
4053
4054 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004059 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4060 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4061 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062
4063 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004068 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4069 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070}
4071
4072TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004074 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075
4076 NotifyMotionArgs args;
4077
4078 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4082 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004083 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004087 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004088
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004094 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004095
4096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004098 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099}
4100
4101TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004103 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104
4105 NotifyMotionArgs args;
4106
4107 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4113 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004114 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4115 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4116 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004120 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4121 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4122 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004123
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004130 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4131 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4132 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133
4134 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004139 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004140
4141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004143 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144}
4145
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004146TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004148 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4149 // need to be rotated.
4150 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004153 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4162}
4163
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004164TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004166 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4167 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004168 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004170 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4179
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004180 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004190 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4199
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004200 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209}
4210
4211TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004213 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214
4215 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4216 mFakePointerController->setPosition(100, 200);
4217 mFakePointerController->setButtonState(0);
4218
4219 NotifyMotionArgs motionArgs;
4220 NotifyKeyArgs keyArgs;
4221
4222 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4226 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4227 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4228 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004229 ASSERT_NO_FATAL_FAILURE(
4230 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4234 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4235 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004236 ASSERT_NO_FATAL_FAILURE(
4237 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_EQ(0, motionArgs.buttonState);
4244 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004245 ASSERT_NO_FATAL_FAILURE(
4246 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247
4248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250 ASSERT_EQ(0, motionArgs.buttonState);
4251 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004252 ASSERT_NO_FATAL_FAILURE(
4253 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254
4255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257 ASSERT_EQ(0, motionArgs.buttonState);
4258 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004259 ASSERT_NO_FATAL_FAILURE(
4260 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261
4262 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4268 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4269 motionArgs.buttonState);
4270 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4271 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004272 ASSERT_NO_FATAL_FAILURE(
4273 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4277 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4278 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4279 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(
4281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282
4283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4285 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4286 motionArgs.buttonState);
4287 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4288 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004289 ASSERT_NO_FATAL_FAILURE(
4290 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004291
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4304 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(
4306 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4312 ASSERT_EQ(0, motionArgs.buttonState);
4313 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004314 ASSERT_NO_FATAL_FAILURE(
4315 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318
4319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_EQ(0, motionArgs.buttonState);
4321 ASSERT_EQ(0, mFakePointerController->getButtonState());
4322 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004323 ASSERT_NO_FATAL_FAILURE(
4324 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4327 ASSERT_EQ(0, motionArgs.buttonState);
4328 ASSERT_EQ(0, mFakePointerController->getButtonState());
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004330 ASSERT_NO_FATAL_FAILURE(
4331 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4337 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4338 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4343 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004344 ASSERT_NO_FATAL_FAILURE(
4345 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004346
4347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4349 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4350 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(
4352 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_EQ(0, motionArgs.buttonState);
4359 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004360 ASSERT_NO_FATAL_FAILURE(
4361 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365 ASSERT_EQ(0, motionArgs.buttonState);
4366 ASSERT_EQ(0, mFakePointerController->getButtonState());
4367
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004368 ASSERT_NO_FATAL_FAILURE(
4369 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4371 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4372 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4373
4374 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4378 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4379 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4384 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004385 ASSERT_NO_FATAL_FAILURE(
4386 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004387
4388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4390 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004392 ASSERT_NO_FATAL_FAILURE(
4393 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 ASSERT_EQ(0, motionArgs.buttonState);
4400 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004401 ASSERT_NO_FATAL_FAILURE(
4402 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403
4404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4406 ASSERT_EQ(0, motionArgs.buttonState);
4407 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(
4409 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4412 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4413 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4414
4415 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4419 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4420 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4425 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004426 ASSERT_NO_FATAL_FAILURE(
4427 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4431 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4432 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004433 ASSERT_NO_FATAL_FAILURE(
4434 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_EQ(0, motionArgs.buttonState);
4441 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442 ASSERT_NO_FATAL_FAILURE(
4443 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444
4445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4447 ASSERT_EQ(0, motionArgs.buttonState);
4448 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(
4450 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4453 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4454 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4455
4456 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4460 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4461 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4466 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004467 ASSERT_NO_FATAL_FAILURE(
4468 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469
4470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4472 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4473 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004474 ASSERT_NO_FATAL_FAILURE(
4475 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_EQ(0, motionArgs.buttonState);
4482 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004483 ASSERT_NO_FATAL_FAILURE(
4484 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004485
4486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4488 ASSERT_EQ(0, motionArgs.buttonState);
4489 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004490 ASSERT_NO_FATAL_FAILURE(
4491 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4494 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4495 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4496}
4497
4498TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004500 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501
4502 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4503 mFakePointerController->setPosition(100, 200);
4504 mFakePointerController->setButtonState(0);
4505
4506 NotifyMotionArgs args;
4507
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004512 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4515 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 +01004516 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004517}
4518
4519TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004520 addConfigurationProperty("cursor.mode", "pointer");
4521 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004522 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004523
4524 NotifyDeviceResetArgs resetArgs;
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4526 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4527 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4528
4529 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4530 mFakePointerController->setPosition(100, 200);
4531 mFakePointerController->setButtonState(0);
4532
4533 NotifyMotionArgs args;
4534
4535 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4540 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4543 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 +01004544 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004545
4546 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4550 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4553 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4555 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4558 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4559
4560 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004561 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4562 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4564 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4567 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4569 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4572 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4573
4574 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4579 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4582 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 +01004583 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004584
4585 // Disable pointer capture and check that the device generation got bumped
4586 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004587 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004588 mFakePolicy->setPointerCapture(false);
4589 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004590 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004591
4592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4593 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4594 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4595
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4600 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4603 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 +01004604 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605}
4606
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004607TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004608 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004609
Garfield Tan888a6a42020-01-09 11:39:16 -08004610 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004611 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004612 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4613 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004614 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4615 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004616 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4617 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4618
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004619 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4620 mFakePointerController->setPosition(100, 200);
4621 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004622
4623 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4628 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4631 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 +01004632 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004633 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4634}
4635
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636// --- TouchInputMapperTest ---
4637
4638class TouchInputMapperTest : public InputMapperTest {
4639protected:
4640 static const int32_t RAW_X_MIN;
4641 static const int32_t RAW_X_MAX;
4642 static const int32_t RAW_Y_MIN;
4643 static const int32_t RAW_Y_MAX;
4644 static const int32_t RAW_TOUCH_MIN;
4645 static const int32_t RAW_TOUCH_MAX;
4646 static const int32_t RAW_TOOL_MIN;
4647 static const int32_t RAW_TOOL_MAX;
4648 static const int32_t RAW_PRESSURE_MIN;
4649 static const int32_t RAW_PRESSURE_MAX;
4650 static const int32_t RAW_ORIENTATION_MIN;
4651 static const int32_t RAW_ORIENTATION_MAX;
4652 static const int32_t RAW_DISTANCE_MIN;
4653 static const int32_t RAW_DISTANCE_MAX;
4654 static const int32_t RAW_TILT_MIN;
4655 static const int32_t RAW_TILT_MAX;
4656 static const int32_t RAW_ID_MIN;
4657 static const int32_t RAW_ID_MAX;
4658 static const int32_t RAW_SLOT_MIN;
4659 static const int32_t RAW_SLOT_MAX;
4660 static const float X_PRECISION;
4661 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004662 static const float X_PRECISION_VIRTUAL;
4663 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664
4665 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004666 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667
4668 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4669
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004670 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004671 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004672
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 enum Axes {
4674 POSITION = 1 << 0,
4675 TOUCH = 1 << 1,
4676 TOOL = 1 << 2,
4677 PRESSURE = 1 << 3,
4678 ORIENTATION = 1 << 4,
4679 MINOR = 1 << 5,
4680 ID = 1 << 6,
4681 DISTANCE = 1 << 7,
4682 TILT = 1 << 8,
4683 SLOT = 1 << 9,
4684 TOOL_TYPE = 1 << 10,
4685 };
4686
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004687 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4688 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004689 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004691 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 int32_t toRawX(float displayX);
4693 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004694 int32_t toRotatedRawX(float displayX);
4695 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004696 float toCookedX(float rawX, float rawY);
4697 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004699 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004701 float toDisplayY(int32_t rawY, int32_t displayHeight);
4702
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703};
4704
4705const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4706const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4707const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4708const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4709const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4710const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4711const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4712const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004713const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4714const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4716const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4717const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4718const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4719const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4720const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4721const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4722const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4723const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4724const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4725const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4726const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004727const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4728 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4729const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4730 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004731const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4732 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733
4734const float TouchInputMapperTest::GEOMETRIC_SCALE =
4735 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4736 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4737
4738const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4739 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4740 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4741};
4742
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004743void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004744 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4745 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004746}
4747
4748void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4749 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4750 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751}
4752
Santos Cordonfa5cf462017-04-05 10:37:00 -07004753void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004754 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4755 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4756 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004757}
4758
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004760 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4761 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4762 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4763 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764}
4765
Jason Gerecke489fda82012-09-07 17:19:40 -07004766void TouchInputMapperTest::prepareLocationCalibration() {
4767 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4768}
4769
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770int32_t TouchInputMapperTest::toRawX(float displayX) {
4771 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4772}
4773
4774int32_t TouchInputMapperTest::toRawY(float displayY) {
4775 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4776}
4777
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004778int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4779 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4780}
4781
4782int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4783 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4784}
4785
Jason Gerecke489fda82012-09-07 17:19:40 -07004786float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4787 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4788 return rawX;
4789}
4790
4791float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4792 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4793 return rawY;
4794}
4795
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004797 return toDisplayX(rawX, DISPLAY_WIDTH);
4798}
4799
4800float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4801 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802}
4803
4804float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004805 return toDisplayY(rawY, DISPLAY_HEIGHT);
4806}
4807
4808float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4809 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810}
4811
4812
4813// --- SingleTouchInputMapperTest ---
4814
4815class SingleTouchInputMapperTest : public TouchInputMapperTest {
4816protected:
4817 void prepareButtons();
4818 void prepareAxes(int axes);
4819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004820 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4821 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4822 void processUp(SingleTouchInputMapper& mappery);
4823 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4824 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4825 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4826 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4827 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4828 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829};
4830
4831void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004832 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833}
4834
4835void SingleTouchInputMapperTest::prepareAxes(int axes) {
4836 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004837 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4838 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 }
4840 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004841 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4842 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 }
4844 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4846 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 }
4848 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004849 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4850 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 }
4852 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004853 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4854 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 }
4856}
4857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004858void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862}
4863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004864void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867}
4868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004869void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871}
4872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004873void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4878 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880}
4881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004882void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884}
4885
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004886void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4887 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890}
4891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4893 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895}
4896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004897void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899}
4900
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 prepareButtons();
4903 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004904 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004906 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907}
4908
4909TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004910 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4911 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 prepareButtons();
4913 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004916 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917}
4918
4919TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 prepareButtons();
4921 prepareAxes(POSITION);
4922 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004925 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926}
4927
4928TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929 prepareButtons();
4930 prepareAxes(POSITION);
4931 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004932 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004934 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935}
4936
4937TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 addConfigurationProperty("touch.deviceType", "touchScreen");
4939 prepareDisplay(DISPLAY_ORIENTATION_0);
4940 prepareButtons();
4941 prepareAxes(POSITION);
4942 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004943 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944
4945 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004946 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
4948 // Virtual key is down.
4949 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4950 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4951 processDown(mapper, x, y);
4952 processSync(mapper);
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004955 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956
4957 // Virtual key is up.
4958 processUp(mapper);
4959 processSync(mapper);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004962 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963}
4964
4965TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 addConfigurationProperty("touch.deviceType", "touchScreen");
4967 prepareDisplay(DISPLAY_ORIENTATION_0);
4968 prepareButtons();
4969 prepareAxes(POSITION);
4970 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004971 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972
4973 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004974 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975
4976 // Virtual key is down.
4977 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4978 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4979 processDown(mapper, x, y);
4980 processSync(mapper);
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4982
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004983 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984
4985 // Virtual key is up.
4986 processUp(mapper);
4987 processSync(mapper);
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004990 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991}
4992
4993TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 addConfigurationProperty("touch.deviceType", "touchScreen");
4995 prepareDisplay(DISPLAY_ORIENTATION_0);
4996 prepareButtons();
4997 prepareAxes(POSITION);
4998 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004999 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000
5001 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5002 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005003 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 ASSERT_TRUE(flags[0]);
5005 ASSERT_FALSE(flags[1]);
5006}
5007
5008TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 addConfigurationProperty("touch.deviceType", "touchScreen");
5010 prepareDisplay(DISPLAY_ORIENTATION_0);
5011 prepareButtons();
5012 prepareAxes(POSITION);
5013 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005014 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015
arthurhungdcef2dc2020-08-11 14:47:50 +08005016 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017
5018 NotifyKeyArgs args;
5019
5020 // Press virtual key.
5021 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5022 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5023 processDown(mapper, x, y);
5024 processSync(mapper);
5025
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5027 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5028 ASSERT_EQ(DEVICE_ID, args.deviceId);
5029 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5030 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5031 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5032 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5033 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5034 ASSERT_EQ(KEY_HOME, args.scanCode);
5035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5036 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5037
5038 // Release virtual key.
5039 processUp(mapper);
5040 processSync(mapper);
5041
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5043 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5044 ASSERT_EQ(DEVICE_ID, args.deviceId);
5045 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5046 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5047 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5048 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5049 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5050 ASSERT_EQ(KEY_HOME, args.scanCode);
5051 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5052 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5053
5054 // Should not have sent any motions.
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5056}
5057
5058TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059 addConfigurationProperty("touch.deviceType", "touchScreen");
5060 prepareDisplay(DISPLAY_ORIENTATION_0);
5061 prepareButtons();
5062 prepareAxes(POSITION);
5063 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005064 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065
arthurhungdcef2dc2020-08-11 14:47:50 +08005066 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067
5068 NotifyKeyArgs keyArgs;
5069
5070 // Press virtual key.
5071 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5072 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5073 processDown(mapper, x, y);
5074 processSync(mapper);
5075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5077 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5078 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5079 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5080 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5081 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5082 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5083 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5084 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5085 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5086 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5087
5088 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5089 // into the display area.
5090 y -= 100;
5091 processMove(mapper, x, y);
5092 processSync(mapper);
5093
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5095 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5096 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5097 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5098 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5099 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5100 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5101 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5102 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5103 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5104 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5105 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5106
5107 NotifyMotionArgs motionArgs;
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5109 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5110 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5111 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5112 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5113 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5114 ASSERT_EQ(0, motionArgs.flags);
5115 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5116 ASSERT_EQ(0, motionArgs.buttonState);
5117 ASSERT_EQ(0, motionArgs.edgeFlags);
5118 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5119 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5122 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5123 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5124 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5125 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5126
5127 // Keep moving out of bounds. Should generate a pointer move.
5128 y -= 50;
5129 processMove(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_MOVE, 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 // Release out of bounds. Should generate a pointer up.
5152 processUp(mapper);
5153 processSync(mapper);
5154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5157 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5158 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5159 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5161 ASSERT_EQ(0, motionArgs.flags);
5162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5163 ASSERT_EQ(0, motionArgs.buttonState);
5164 ASSERT_EQ(0, motionArgs.edgeFlags);
5165 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5166 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5169 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5170 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5171 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5173
5174 // Should not have sent any more keys or motions.
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5177}
5178
5179TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180 addConfigurationProperty("touch.deviceType", "touchScreen");
5181 prepareDisplay(DISPLAY_ORIENTATION_0);
5182 prepareButtons();
5183 prepareAxes(POSITION);
5184 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005185 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005186
arthurhungdcef2dc2020-08-11 14:47:50 +08005187 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188
5189 NotifyMotionArgs motionArgs;
5190
5191 // Initially go down out of bounds.
5192 int32_t x = -10;
5193 int32_t y = -10;
5194 processDown(mapper, x, y);
5195 processSync(mapper);
5196
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5198
5199 // Move into the display area. Should generate a pointer down.
5200 x = 50;
5201 y = 75;
5202 processMove(mapper, x, y);
5203 processSync(mapper);
5204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5207 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5208 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5209 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5211 ASSERT_EQ(0, motionArgs.flags);
5212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5213 ASSERT_EQ(0, motionArgs.buttonState);
5214 ASSERT_EQ(0, motionArgs.edgeFlags);
5215 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5216 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5219 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5220 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5221 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5222 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5223
5224 // Release. Should generate a pointer up.
5225 processUp(mapper);
5226 processSync(mapper);
5227
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5229 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5230 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5231 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5232 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5233 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5234 ASSERT_EQ(0, motionArgs.flags);
5235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5236 ASSERT_EQ(0, motionArgs.buttonState);
5237 ASSERT_EQ(0, motionArgs.edgeFlags);
5238 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5242 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5243 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5244 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5246
5247 // Should not have sent any more keys or motions.
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5250}
5251
Santos Cordonfa5cf462017-04-05 10:37:00 -07005252TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005253 addConfigurationProperty("touch.deviceType", "touchScreen");
5254 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5255
5256 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5257 prepareButtons();
5258 prepareAxes(POSITION);
5259 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005260 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005261
arthurhungdcef2dc2020-08-11 14:47:50 +08005262 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005263
5264 NotifyMotionArgs motionArgs;
5265
5266 // Down.
5267 int32_t x = 100;
5268 int32_t y = 125;
5269 processDown(mapper, x, y);
5270 processSync(mapper);
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5275 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5276 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5277 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5278 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5279 ASSERT_EQ(0, motionArgs.flags);
5280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5281 ASSERT_EQ(0, motionArgs.buttonState);
5282 ASSERT_EQ(0, motionArgs.edgeFlags);
5283 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5287 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5288 1, 0, 0, 0, 0, 0, 0, 0));
5289 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5290 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5292
5293 // Move.
5294 x += 50;
5295 y += 75;
5296 processMove(mapper, x, y);
5297 processSync(mapper);
5298
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5301 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5302 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5303 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5304 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5306 ASSERT_EQ(0, motionArgs.flags);
5307 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5308 ASSERT_EQ(0, motionArgs.buttonState);
5309 ASSERT_EQ(0, motionArgs.edgeFlags);
5310 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5311 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5314 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5315 1, 0, 0, 0, 0, 0, 0, 0));
5316 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5317 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5318 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5319
5320 // Up.
5321 processUp(mapper);
5322 processSync(mapper);
5323
5324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5326 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5327 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5328 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5329 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5331 ASSERT_EQ(0, motionArgs.flags);
5332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5333 ASSERT_EQ(0, motionArgs.buttonState);
5334 ASSERT_EQ(0, motionArgs.edgeFlags);
5335 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5339 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5340 1, 0, 0, 0, 0, 0, 0, 0));
5341 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5342 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5344
5345 // Should not have sent any more keys or motions.
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5348}
5349
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351 addConfigurationProperty("touch.deviceType", "touchScreen");
5352 prepareDisplay(DISPLAY_ORIENTATION_0);
5353 prepareButtons();
5354 prepareAxes(POSITION);
5355 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005356 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357
arthurhungdcef2dc2020-08-11 14:47:50 +08005358 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359
5360 NotifyMotionArgs motionArgs;
5361
5362 // Down.
5363 int32_t x = 100;
5364 int32_t y = 125;
5365 processDown(mapper, x, y);
5366 processSync(mapper);
5367
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5370 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5371 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5372 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5373 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5374 ASSERT_EQ(0, motionArgs.flags);
5375 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5376 ASSERT_EQ(0, motionArgs.buttonState);
5377 ASSERT_EQ(0, motionArgs.edgeFlags);
5378 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5379 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5382 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5383 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5384 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5386
5387 // Move.
5388 x += 50;
5389 y += 75;
5390 processMove(mapper, x, y);
5391 processSync(mapper);
5392
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5395 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5396 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5397 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5399 ASSERT_EQ(0, motionArgs.flags);
5400 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5401 ASSERT_EQ(0, motionArgs.buttonState);
5402 ASSERT_EQ(0, motionArgs.edgeFlags);
5403 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5404 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5407 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5408 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5409 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5410 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5411
5412 // Up.
5413 processUp(mapper);
5414 processSync(mapper);
5415
5416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5418 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5419 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5420 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5421 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5422 ASSERT_EQ(0, motionArgs.flags);
5423 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5424 ASSERT_EQ(0, motionArgs.buttonState);
5425 ASSERT_EQ(0, motionArgs.edgeFlags);
5426 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5427 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5430 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5431 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5432 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5433 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5434
5435 // Should not have sent any more keys or motions.
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5438}
5439
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005440TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441 addConfigurationProperty("touch.deviceType", "touchScreen");
5442 prepareButtons();
5443 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005444 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5445 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005446 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447
5448 NotifyMotionArgs args;
5449
5450 // Rotation 90.
5451 prepareDisplay(DISPLAY_ORIENTATION_90);
5452 processDown(mapper, toRawX(50), toRawY(75));
5453 processSync(mapper);
5454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5456 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5457 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5458
5459 processUp(mapper);
5460 processSync(mapper);
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5462}
5463
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005464TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 addConfigurationProperty("touch.deviceType", "touchScreen");
5466 prepareButtons();
5467 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005468 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5469 // orientation-aware are affected by display rotation.
5470 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005471 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472
5473 NotifyMotionArgs args;
5474
5475 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005476 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 prepareDisplay(DISPLAY_ORIENTATION_0);
5478 processDown(mapper, toRawX(50), toRawY(75));
5479 processSync(mapper);
5480
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5482 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5483 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5484
5485 processUp(mapper);
5486 processSync(mapper);
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5488
5489 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005490 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005492 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493 processSync(mapper);
5494
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5496 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5497 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5498
5499 processUp(mapper);
5500 processSync(mapper);
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5502
5503 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005504 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 prepareDisplay(DISPLAY_ORIENTATION_180);
5506 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5507 processSync(mapper);
5508
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5510 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5511 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5512
5513 processUp(mapper);
5514 processSync(mapper);
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5516
5517 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005518 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005520 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521 processSync(mapper);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5524 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5525 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5526
5527 processUp(mapper);
5528 processSync(mapper);
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5530}
5531
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005532TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5533 addConfigurationProperty("touch.deviceType", "touchScreen");
5534 prepareButtons();
5535 prepareAxes(POSITION);
5536 addConfigurationProperty("touch.orientationAware", "1");
5537 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5538 clearViewports();
5539 prepareDisplay(DISPLAY_ORIENTATION_0);
5540 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5541 NotifyMotionArgs args;
5542
5543 // Orientation 0.
5544 processDown(mapper, toRawX(50), toRawY(75));
5545 processSync(mapper);
5546
5547 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5548 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5549 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5550
5551 processUp(mapper);
5552 processSync(mapper);
5553 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5554}
5555
5556TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5557 addConfigurationProperty("touch.deviceType", "touchScreen");
5558 prepareButtons();
5559 prepareAxes(POSITION);
5560 addConfigurationProperty("touch.orientationAware", "1");
5561 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5562 clearViewports();
5563 prepareDisplay(DISPLAY_ORIENTATION_0);
5564 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5565 NotifyMotionArgs args;
5566
5567 // Orientation 90.
5568 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5569 processSync(mapper);
5570
5571 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5572 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5573 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5574
5575 processUp(mapper);
5576 processSync(mapper);
5577 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5578}
5579
5580TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5581 addConfigurationProperty("touch.deviceType", "touchScreen");
5582 prepareButtons();
5583 prepareAxes(POSITION);
5584 addConfigurationProperty("touch.orientationAware", "1");
5585 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5586 clearViewports();
5587 prepareDisplay(DISPLAY_ORIENTATION_0);
5588 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5589 NotifyMotionArgs args;
5590
5591 // Orientation 180.
5592 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5593 processSync(mapper);
5594
5595 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5596 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5597 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5598
5599 processUp(mapper);
5600 processSync(mapper);
5601 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5602}
5603
5604TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5605 addConfigurationProperty("touch.deviceType", "touchScreen");
5606 prepareButtons();
5607 prepareAxes(POSITION);
5608 addConfigurationProperty("touch.orientationAware", "1");
5609 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5610 clearViewports();
5611 prepareDisplay(DISPLAY_ORIENTATION_0);
5612 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5613 NotifyMotionArgs args;
5614
5615 // Orientation 270.
5616 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5617 processSync(mapper);
5618
5619 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5620 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5621 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5622
5623 processUp(mapper);
5624 processSync(mapper);
5625 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5626}
5627
5628TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5629 addConfigurationProperty("touch.deviceType", "touchScreen");
5630 prepareButtons();
5631 prepareAxes(POSITION);
5632 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5633 // orientation-aware are affected by display rotation.
5634 addConfigurationProperty("touch.orientationAware", "0");
5635 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5636 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5637
5638 NotifyMotionArgs args;
5639
5640 // Orientation 90, Rotation 0.
5641 clearViewports();
5642 prepareDisplay(DISPLAY_ORIENTATION_0);
5643 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5644 processSync(mapper);
5645
5646 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5647 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5648 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5649
5650 processUp(mapper);
5651 processSync(mapper);
5652 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5653
5654 // Orientation 90, Rotation 90.
5655 clearViewports();
5656 prepareDisplay(DISPLAY_ORIENTATION_90);
5657 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5658 processSync(mapper);
5659
5660 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5661 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5662 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5663
5664 processUp(mapper);
5665 processSync(mapper);
5666 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5667
5668 // Orientation 90, Rotation 180.
5669 clearViewports();
5670 prepareDisplay(DISPLAY_ORIENTATION_180);
5671 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5672 processSync(mapper);
5673
5674 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5675 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5676 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5677
5678 processUp(mapper);
5679 processSync(mapper);
5680 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5681
5682 // Orientation 90, Rotation 270.
5683 clearViewports();
5684 prepareDisplay(DISPLAY_ORIENTATION_270);
5685 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5686 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5687 processSync(mapper);
5688
5689 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5690 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5691 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5692
5693 processUp(mapper);
5694 processSync(mapper);
5695 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5696}
5697
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 addConfigurationProperty("touch.deviceType", "touchScreen");
5700 prepareDisplay(DISPLAY_ORIENTATION_0);
5701 prepareButtons();
5702 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005703 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704
5705 // These calculations are based on the input device calibration documentation.
5706 int32_t rawX = 100;
5707 int32_t rawY = 200;
5708 int32_t rawPressure = 10;
5709 int32_t rawToolMajor = 12;
5710 int32_t rawDistance = 2;
5711 int32_t rawTiltX = 30;
5712 int32_t rawTiltY = 110;
5713
5714 float x = toDisplayX(rawX);
5715 float y = toDisplayY(rawY);
5716 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5717 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5718 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5719 float distance = float(rawDistance);
5720
5721 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5722 float tiltScale = M_PI / 180;
5723 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5724 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5725 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5726 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5727
5728 processDown(mapper, rawX, rawY);
5729 processPressure(mapper, rawPressure);
5730 processToolMajor(mapper, rawToolMajor);
5731 processDistance(mapper, rawDistance);
5732 processTilt(mapper, rawTiltX, rawTiltY);
5733 processSync(mapper);
5734
5735 NotifyMotionArgs args;
5736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5738 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5739 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5740}
5741
Jason Gerecke489fda82012-09-07 17:19:40 -07005742TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005743 addConfigurationProperty("touch.deviceType", "touchScreen");
5744 prepareDisplay(DISPLAY_ORIENTATION_0);
5745 prepareLocationCalibration();
5746 prepareButtons();
5747 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005748 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005749
5750 int32_t rawX = 100;
5751 int32_t rawY = 200;
5752
5753 float x = toDisplayX(toCookedX(rawX, rawY));
5754 float y = toDisplayY(toCookedY(rawX, rawY));
5755
5756 processDown(mapper, rawX, rawY);
5757 processSync(mapper);
5758
5759 NotifyMotionArgs args;
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5762 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5763}
5764
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766 addConfigurationProperty("touch.deviceType", "touchScreen");
5767 prepareDisplay(DISPLAY_ORIENTATION_0);
5768 prepareButtons();
5769 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005770 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771
5772 NotifyMotionArgs motionArgs;
5773 NotifyKeyArgs keyArgs;
5774
5775 processDown(mapper, 100, 200);
5776 processSync(mapper);
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5779 ASSERT_EQ(0, motionArgs.buttonState);
5780
5781 // press BTN_LEFT, release BTN_LEFT
5782 processKey(mapper, BTN_LEFT, 1);
5783 processSync(mapper);
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5786 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5787
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5790 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5791
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 processKey(mapper, BTN_LEFT, 0);
5793 processSync(mapper);
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005797
5798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005800 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801
5802 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5803 processKey(mapper, BTN_RIGHT, 1);
5804 processKey(mapper, BTN_MIDDLE, 1);
5805 processSync(mapper);
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5808 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5809 motionArgs.buttonState);
5810
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5813 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5814
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5817 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5818 motionArgs.buttonState);
5819
Michael Wrightd02c5b62014-02-10 15:10:22 -08005820 processKey(mapper, BTN_RIGHT, 0);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005825
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005828 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829
5830 processKey(mapper, BTN_MIDDLE, 0);
5831 processSync(mapper);
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005838 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839
5840 // press BTN_BACK, release BTN_BACK
5841 processKey(mapper, BTN_BACK, 1);
5842 processSync(mapper);
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5844 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5845 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005849 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5853 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854
5855 processKey(mapper, BTN_BACK, 0);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005863 ASSERT_EQ(0, motionArgs.buttonState);
5864
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5866 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5868
5869 // press BTN_SIDE, release BTN_SIDE
5870 processKey(mapper, BTN_SIDE, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5873 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5874 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005875
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005878 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5879
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5882 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883
5884 processKey(mapper, BTN_SIDE, 0);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005889
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005892 ASSERT_EQ(0, motionArgs.buttonState);
5893
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5895 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5896 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5897
5898 // press BTN_FORWARD, release BTN_FORWARD
5899 processKey(mapper, BTN_FORWARD, 1);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5902 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5903 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005904
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005907 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5911 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912
5913 processKey(mapper, BTN_FORWARD, 0);
5914 processSync(mapper);
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005921 ASSERT_EQ(0, motionArgs.buttonState);
5922
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5924 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5925 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5926
5927 // press BTN_EXTRA, release BTN_EXTRA
5928 processKey(mapper, BTN_EXTRA, 1);
5929 processSync(mapper);
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5931 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5932 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005933
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005936 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5937
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5940 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941
5942 processKey(mapper, BTN_EXTRA, 0);
5943 processSync(mapper);
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005947
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005950 ASSERT_EQ(0, motionArgs.buttonState);
5951
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5953 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5954 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5955
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5957
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958 // press BTN_STYLUS, release BTN_STYLUS
5959 processKey(mapper, BTN_STYLUS, 1);
5960 processSync(mapper);
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5964
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5967 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968
5969 processKey(mapper, BTN_STYLUS, 0);
5970 processSync(mapper);
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005974
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005977 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978
5979 // press BTN_STYLUS2, release BTN_STYLUS2
5980 processKey(mapper, BTN_STYLUS2, 1);
5981 processSync(mapper);
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005984 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5988 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989
5990 processKey(mapper, BTN_STYLUS2, 0);
5991 processSync(mapper);
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005998 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999
6000 // release touch
6001 processUp(mapper);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6004 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6005 ASSERT_EQ(0, motionArgs.buttonState);
6006}
6007
6008TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 addConfigurationProperty("touch.deviceType", "touchScreen");
6010 prepareDisplay(DISPLAY_ORIENTATION_0);
6011 prepareButtons();
6012 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006013 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014
6015 NotifyMotionArgs motionArgs;
6016
6017 // default tool type is finger
6018 processDown(mapper, 100, 200);
6019 processSync(mapper);
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6023
6024 // eraser
6025 processKey(mapper, BTN_TOOL_RUBBER, 1);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6030
6031 // stylus
6032 processKey(mapper, BTN_TOOL_RUBBER, 0);
6033 processKey(mapper, BTN_TOOL_PEN, 1);
6034 processSync(mapper);
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6038
6039 // brush
6040 processKey(mapper, BTN_TOOL_PEN, 0);
6041 processKey(mapper, BTN_TOOL_BRUSH, 1);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6046
6047 // pencil
6048 processKey(mapper, BTN_TOOL_BRUSH, 0);
6049 processKey(mapper, BTN_TOOL_PENCIL, 1);
6050 processSync(mapper);
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6054
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006055 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 processKey(mapper, BTN_TOOL_PENCIL, 0);
6057 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6062
6063 // mouse
6064 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6065 processKey(mapper, BTN_TOOL_MOUSE, 1);
6066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6070
6071 // lens
6072 processKey(mapper, BTN_TOOL_MOUSE, 0);
6073 processKey(mapper, BTN_TOOL_LENS, 1);
6074 processSync(mapper);
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6078
6079 // double-tap
6080 processKey(mapper, BTN_TOOL_LENS, 0);
6081 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6086
6087 // triple-tap
6088 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6089 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6094
6095 // quad-tap
6096 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6097 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6098 processSync(mapper);
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6102
6103 // finger
6104 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6105 processKey(mapper, BTN_TOOL_FINGER, 1);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6110
6111 // stylus trumps finger
6112 processKey(mapper, BTN_TOOL_PEN, 1);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6117
6118 // eraser trumps stylus
6119 processKey(mapper, BTN_TOOL_RUBBER, 1);
6120 processSync(mapper);
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6124
6125 // mouse trumps eraser
6126 processKey(mapper, BTN_TOOL_MOUSE, 1);
6127 processSync(mapper);
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6131
6132 // back to default tool type
6133 processKey(mapper, BTN_TOOL_MOUSE, 0);
6134 processKey(mapper, BTN_TOOL_RUBBER, 0);
6135 processKey(mapper, BTN_TOOL_PEN, 0);
6136 processKey(mapper, BTN_TOOL_FINGER, 0);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6141}
6142
6143TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 addConfigurationProperty("touch.deviceType", "touchScreen");
6145 prepareDisplay(DISPLAY_ORIENTATION_0);
6146 prepareButtons();
6147 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006148 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006149 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150
6151 NotifyMotionArgs motionArgs;
6152
6153 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6154 processKey(mapper, BTN_TOOL_FINGER, 1);
6155 processMove(mapper, 100, 200);
6156 processSync(mapper);
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6158 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6160 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6165 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6166
6167 // move a little
6168 processMove(mapper, 150, 250);
6169 processSync(mapper);
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6171 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6173 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6174
6175 // down when BTN_TOUCH is pressed, pressure defaults to 1
6176 processKey(mapper, BTN_TOUCH, 1);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6179 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6181 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6182
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6186 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6187
6188 // up when BTN_TOUCH is released, hover restored
6189 processKey(mapper, BTN_TOUCH, 0);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6194 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6195
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6199 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6200
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6204 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6205
6206 // exit hover when pointer goes away
6207 processKey(mapper, BTN_TOOL_FINGER, 0);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6212 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6213}
6214
6215TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 addConfigurationProperty("touch.deviceType", "touchScreen");
6217 prepareDisplay(DISPLAY_ORIENTATION_0);
6218 prepareButtons();
6219 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006220 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
6222 NotifyMotionArgs motionArgs;
6223
6224 // initially hovering because pressure is 0
6225 processDown(mapper, 100, 200);
6226 processPressure(mapper, 0);
6227 processSync(mapper);
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6231 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6232
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6234 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6236 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6237
6238 // move a little
6239 processMove(mapper, 150, 250);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6244 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6245
6246 // down when pressure is non-zero
6247 processPressure(mapper, RAW_PRESSURE_MAX);
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6252 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6253
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6257 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6258
6259 // up when pressure becomes 0, hover restored
6260 processPressure(mapper, 0);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6265 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6266
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6270 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6271
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6275 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6276
6277 // exit hover when pointer goes away
6278 processUp(mapper);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6283 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6284}
6285
Prabir Pradhan5632d622021-09-06 07:57:20 -07006286// --- TouchDisplayProjectionTest ---
6287
6288class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6289public:
6290 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6291 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6292 // rotated equivalent of the given un-rotated physical display bounds.
6293 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6294 uint32_t inverseRotationFlags;
6295 auto width = DISPLAY_WIDTH;
6296 auto height = DISPLAY_HEIGHT;
6297 switch (orientation) {
6298 case DISPLAY_ORIENTATION_90:
6299 inverseRotationFlags = ui::Transform::ROT_270;
6300 std::swap(width, height);
6301 break;
6302 case DISPLAY_ORIENTATION_180:
6303 inverseRotationFlags = ui::Transform::ROT_180;
6304 break;
6305 case DISPLAY_ORIENTATION_270:
6306 inverseRotationFlags = ui::Transform::ROT_90;
6307 std::swap(width, height);
6308 break;
6309 case DISPLAY_ORIENTATION_0:
6310 inverseRotationFlags = ui::Transform::ROT_0;
6311 break;
6312 default:
6313 FAIL() << "Invalid orientation: " << orientation;
6314 }
6315
6316 const ui::Transform rotation(inverseRotationFlags, width, height);
6317 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6318
6319 std::optional<DisplayViewport> internalViewport =
6320 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6321 DisplayViewport& v = *internalViewport;
6322 v.displayId = DISPLAY_ID;
6323 v.orientation = orientation;
6324
6325 v.logicalLeft = 0;
6326 v.logicalTop = 0;
6327 v.logicalRight = 100;
6328 v.logicalBottom = 100;
6329
6330 v.physicalLeft = rotatedPhysicalDisplay.left;
6331 v.physicalTop = rotatedPhysicalDisplay.top;
6332 v.physicalRight = rotatedPhysicalDisplay.right;
6333 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6334
6335 v.deviceWidth = width;
6336 v.deviceHeight = height;
6337
6338 v.isActive = true;
6339 v.uniqueId = UNIQUE_ID;
6340 v.type = ViewportType::INTERNAL;
6341 mFakePolicy->updateViewport(v);
6342 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6343 }
6344
6345 void assertReceivedMove(const Point& point) {
6346 NotifyMotionArgs motionArgs;
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6349 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6351 1, 0, 0, 0, 0, 0, 0, 0));
6352 }
6353};
6354
6355TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6356 addConfigurationProperty("touch.deviceType", "touchScreen");
6357 prepareDisplay(DISPLAY_ORIENTATION_0);
6358
6359 prepareButtons();
6360 prepareAxes(POSITION);
6361 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6362
6363 NotifyMotionArgs motionArgs;
6364
6365 // Configure the DisplayViewport such that the logical display maps to a subsection of
6366 // the display panel called the physical display. Here, the physical display is bounded by the
6367 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6368 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6369 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6370 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6371
6372 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6373 DISPLAY_ORIENTATION_270}) {
6374 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6375
6376 // Touches outside the physical display should be ignored, and should not generate any
6377 // events. Ensure touches at the following points that lie outside of the physical display
6378 // area do not generate any events.
6379 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6380 processDown(mapper, toRawX(point.x), toRawY(point.y));
6381 processSync(mapper);
6382 processUp(mapper);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6385 << "Unexpected event generated for touch outside physical display at point: "
6386 << point.x << ", " << point.y;
6387 }
6388 }
6389}
6390
6391TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6392 addConfigurationProperty("touch.deviceType", "touchScreen");
6393 prepareDisplay(DISPLAY_ORIENTATION_0);
6394
6395 prepareButtons();
6396 prepareAxes(POSITION);
6397 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6398
6399 NotifyMotionArgs motionArgs;
6400
6401 // Configure the DisplayViewport such that the logical display maps to a subsection of
6402 // the display panel called the physical display. Here, the physical display is bounded by the
6403 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6404 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6405
6406 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6407 DISPLAY_ORIENTATION_270}) {
6408 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6409
6410 // Touches that start outside the physical display should be ignored until it enters the
6411 // physical display bounds, at which point it should generate a down event. Start a touch at
6412 // the point (5, 100), which is outside the physical display bounds.
6413 static const Point kOutsidePoint{5, 100};
6414 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6417
6418 // Move the touch into the physical display area. This should generate a pointer down.
6419 processMove(mapper, toRawX(11), toRawY(21));
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6423 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6424 ASSERT_NO_FATAL_FAILURE(
6425 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6426
6427 // Move the touch inside the physical display area. This should generate a pointer move.
6428 processMove(mapper, toRawX(69), toRawY(159));
6429 processSync(mapper);
6430 assertReceivedMove({69, 159});
6431
6432 // Move outside the physical display area. Since the pointer is already down, this should
6433 // now continue generating events.
6434 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6435 processSync(mapper);
6436 assertReceivedMove(kOutsidePoint);
6437
6438 // Release. This should generate a pointer up.
6439 processUp(mapper);
6440 processSync(mapper);
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6444 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6445
6446 // Ensure no more events were generated.
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6449 }
6450}
6451
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452// --- MultiTouchInputMapperTest ---
6453
6454class MultiTouchInputMapperTest : public TouchInputMapperTest {
6455protected:
6456 void prepareAxes(int axes);
6457
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006458 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6459 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6460 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6461 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6462 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6463 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6464 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6465 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6466 void processId(MultiTouchInputMapper& mapper, int32_t id);
6467 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6468 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6469 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6470 void processMTSync(MultiTouchInputMapper& mapper);
6471 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472};
6473
6474void MultiTouchInputMapperTest::prepareAxes(int axes) {
6475 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006476 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6477 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 }
6479 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6481 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006483 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6484 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485 }
6486 }
6487 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006488 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6489 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006491 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6492 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493 }
6494 }
6495 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006496 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6497 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 }
6499 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006500 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6501 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006502 }
6503 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006504 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6505 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506 }
6507 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6509 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510 }
6511 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006512 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6513 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514 }
6515 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006516 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517 }
6518}
6519
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006520void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6521 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524}
6525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006526void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6527 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529}
6530
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006531void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6532 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534}
6535
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006536void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538}
6539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006540void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542}
6543
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006544void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6545 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547}
6548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006549void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551}
6552
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006553void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006555}
6556
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006557void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559}
6560
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006561void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563}
6564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006565void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567}
6568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006569void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6570 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006572}
6573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006574void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006576}
6577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006578void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006580}
6581
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006583 addConfigurationProperty("touch.deviceType", "touchScreen");
6584 prepareDisplay(DISPLAY_ORIENTATION_0);
6585 prepareAxes(POSITION);
6586 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006587 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588
arthurhungdcef2dc2020-08-11 14:47:50 +08006589 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590
6591 NotifyMotionArgs motionArgs;
6592
6593 // Two fingers down at once.
6594 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6595 processPosition(mapper, x1, y1);
6596 processMTSync(mapper);
6597 processPosition(mapper, x2, y2);
6598 processMTSync(mapper);
6599 processSync(mapper);
6600
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6603 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6604 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6605 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6607 ASSERT_EQ(0, motionArgs.flags);
6608 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6609 ASSERT_EQ(0, motionArgs.buttonState);
6610 ASSERT_EQ(0, motionArgs.edgeFlags);
6611 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6615 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6616 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6617 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6618 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6619
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6622 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6623 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6624 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6626 motionArgs.action);
6627 ASSERT_EQ(0, motionArgs.flags);
6628 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6629 ASSERT_EQ(0, motionArgs.buttonState);
6630 ASSERT_EQ(0, motionArgs.edgeFlags);
6631 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6632 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6634 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6637 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6639 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6640 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6641 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6643
6644 // Move.
6645 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6646 processPosition(mapper, x1, y1);
6647 processMTSync(mapper);
6648 processPosition(mapper, x2, y2);
6649 processMTSync(mapper);
6650 processSync(mapper);
6651
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6654 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6655 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6656 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6658 ASSERT_EQ(0, motionArgs.flags);
6659 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6660 ASSERT_EQ(0, motionArgs.buttonState);
6661 ASSERT_EQ(0, motionArgs.edgeFlags);
6662 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6665 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6666 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6668 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6670 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6671 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6672 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6673 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6674
6675 // First finger up.
6676 x2 += 15; y2 -= 20;
6677 processPosition(mapper, x2, y2);
6678 processMTSync(mapper);
6679 processSync(mapper);
6680
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6683 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6684 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6685 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6687 motionArgs.action);
6688 ASSERT_EQ(0, motionArgs.flags);
6689 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6690 ASSERT_EQ(0, motionArgs.buttonState);
6691 ASSERT_EQ(0, motionArgs.edgeFlags);
6692 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6693 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6695 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6698 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6700 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6701 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6702 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6703 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6704
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6706 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6707 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6708 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6709 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6711 ASSERT_EQ(0, motionArgs.flags);
6712 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6713 ASSERT_EQ(0, motionArgs.buttonState);
6714 ASSERT_EQ(0, motionArgs.edgeFlags);
6715 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6716 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6719 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6720 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6721 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6722 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6723
6724 // Move.
6725 x2 += 20; y2 -= 25;
6726 processPosition(mapper, x2, y2);
6727 processMTSync(mapper);
6728 processSync(mapper);
6729
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6732 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6733 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6734 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6736 ASSERT_EQ(0, motionArgs.flags);
6737 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6738 ASSERT_EQ(0, motionArgs.buttonState);
6739 ASSERT_EQ(0, motionArgs.edgeFlags);
6740 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6741 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6744 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6745 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6746 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6747 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6748
6749 // New finger down.
6750 int32_t x3 = 700, y3 = 300;
6751 processPosition(mapper, x2, y2);
6752 processMTSync(mapper);
6753 processPosition(mapper, x3, y3);
6754 processMTSync(mapper);
6755 processSync(mapper);
6756
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6758 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6759 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6760 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6761 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6762 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6763 motionArgs.action);
6764 ASSERT_EQ(0, motionArgs.flags);
6765 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6766 ASSERT_EQ(0, motionArgs.buttonState);
6767 ASSERT_EQ(0, motionArgs.edgeFlags);
6768 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6769 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6771 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6774 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6776 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6777 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6778 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6779 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6780
6781 // Second finger up.
6782 x3 += 30; y3 -= 20;
6783 processPosition(mapper, x3, y3);
6784 processMTSync(mapper);
6785 processSync(mapper);
6786
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6788 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6789 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6790 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6791 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6793 motionArgs.action);
6794 ASSERT_EQ(0, motionArgs.flags);
6795 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6796 ASSERT_EQ(0, motionArgs.buttonState);
6797 ASSERT_EQ(0, motionArgs.edgeFlags);
6798 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6801 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6804 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6806 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6807 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6808 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6810
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6812 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6813 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6814 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6815 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6817 ASSERT_EQ(0, motionArgs.flags);
6818 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6819 ASSERT_EQ(0, motionArgs.buttonState);
6820 ASSERT_EQ(0, motionArgs.edgeFlags);
6821 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6822 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6825 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6826 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6827 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6828 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6829
6830 // Last finger up.
6831 processMTSync(mapper);
6832 processSync(mapper);
6833
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6836 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6837 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6838 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6839 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6840 ASSERT_EQ(0, motionArgs.flags);
6841 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6842 ASSERT_EQ(0, motionArgs.buttonState);
6843 ASSERT_EQ(0, motionArgs.edgeFlags);
6844 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6845 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6848 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6849 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6850 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6851 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6852
6853 // Should not have sent any more keys or motions.
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6856}
6857
6858TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006859 addConfigurationProperty("touch.deviceType", "touchScreen");
6860 prepareDisplay(DISPLAY_ORIENTATION_0);
6861 prepareAxes(POSITION | ID);
6862 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006864
arthurhungdcef2dc2020-08-11 14:47:50 +08006865 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006866
6867 NotifyMotionArgs motionArgs;
6868
6869 // Two fingers down at once.
6870 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6871 processPosition(mapper, x1, y1);
6872 processId(mapper, 1);
6873 processMTSync(mapper);
6874 processPosition(mapper, x2, y2);
6875 processId(mapper, 2);
6876 processMTSync(mapper);
6877 processSync(mapper);
6878
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6880 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6881 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6882 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6885 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6886
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6888 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6889 motionArgs.action);
6890 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6891 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6893 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6896 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6898 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6899
6900 // Move.
6901 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6902 processPosition(mapper, x1, y1);
6903 processId(mapper, 1);
6904 processMTSync(mapper);
6905 processPosition(mapper, x2, y2);
6906 processId(mapper, 2);
6907 processMTSync(mapper);
6908 processSync(mapper);
6909
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6912 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6913 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6915 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6918 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6920 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6921
6922 // First finger up.
6923 x2 += 15; y2 -= 20;
6924 processPosition(mapper, x2, y2);
6925 processId(mapper, 2);
6926 processMTSync(mapper);
6927 processSync(mapper);
6928
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6930 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6931 motionArgs.action);
6932 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6933 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6935 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6938 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6940 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6941
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6944 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6945 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6948 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6949
6950 // Move.
6951 x2 += 20; y2 -= 25;
6952 processPosition(mapper, x2, y2);
6953 processId(mapper, 2);
6954 processMTSync(mapper);
6955 processSync(mapper);
6956
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6959 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6960 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6963 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6964
6965 // New finger down.
6966 int32_t x3 = 700, y3 = 300;
6967 processPosition(mapper, x2, y2);
6968 processId(mapper, 2);
6969 processMTSync(mapper);
6970 processPosition(mapper, x3, y3);
6971 processId(mapper, 3);
6972 processMTSync(mapper);
6973 processSync(mapper);
6974
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6976 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6977 motionArgs.action);
6978 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6981 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6984 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6986 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6987
6988 // Second finger up.
6989 x3 += 30; y3 -= 20;
6990 processPosition(mapper, x3, y3);
6991 processId(mapper, 3);
6992 processMTSync(mapper);
6993 processSync(mapper);
6994
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6996 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6997 motionArgs.action);
6998 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6999 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7001 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7004 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7006 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7007
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7010 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7011 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7014 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7015
7016 // Last finger up.
7017 processMTSync(mapper);
7018 processSync(mapper);
7019
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7021 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7022 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7023 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7026 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7027
7028 // Should not have sent any more keys or motions.
7029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7031}
7032
7033TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007034 addConfigurationProperty("touch.deviceType", "touchScreen");
7035 prepareDisplay(DISPLAY_ORIENTATION_0);
7036 prepareAxes(POSITION | ID | SLOT);
7037 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007038 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039
arthurhungdcef2dc2020-08-11 14:47:50 +08007040 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007041
7042 NotifyMotionArgs motionArgs;
7043
7044 // Two fingers down at once.
7045 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7046 processPosition(mapper, x1, y1);
7047 processId(mapper, 1);
7048 processSlot(mapper, 1);
7049 processPosition(mapper, x2, y2);
7050 processId(mapper, 2);
7051 processSync(mapper);
7052
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7054 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7055 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7056 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7059 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7060
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7063 motionArgs.action);
7064 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7065 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7067 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7070 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7072 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7073
7074 // Move.
7075 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7076 processSlot(mapper, 0);
7077 processPosition(mapper, x1, y1);
7078 processSlot(mapper, 1);
7079 processPosition(mapper, x2, y2);
7080 processSync(mapper);
7081
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7084 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7085 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7087 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7090 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7092 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7093
7094 // First finger up.
7095 x2 += 15; y2 -= 20;
7096 processSlot(mapper, 0);
7097 processId(mapper, -1);
7098 processSlot(mapper, 1);
7099 processPosition(mapper, x2, y2);
7100 processSync(mapper);
7101
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7103 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7104 motionArgs.action);
7105 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7106 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7108 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7111 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7113 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7114
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7117 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7118 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7121 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7122
7123 // Move.
7124 x2 += 20; y2 -= 25;
7125 processPosition(mapper, x2, y2);
7126 processSync(mapper);
7127
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7130 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7131 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7134 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7135
7136 // New finger down.
7137 int32_t x3 = 700, y3 = 300;
7138 processPosition(mapper, x2, y2);
7139 processSlot(mapper, 0);
7140 processId(mapper, 3);
7141 processPosition(mapper, x3, y3);
7142 processSync(mapper);
7143
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7146 motionArgs.action);
7147 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7148 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7150 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7153 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7155 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7156
7157 // Second finger up.
7158 x3 += 30; y3 -= 20;
7159 processSlot(mapper, 1);
7160 processId(mapper, -1);
7161 processSlot(mapper, 0);
7162 processPosition(mapper, x3, y3);
7163 processSync(mapper);
7164
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7166 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7167 motionArgs.action);
7168 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7169 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7171 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7174 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7176 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7177
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7180 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7181 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7184 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7185
7186 // Last finger up.
7187 processId(mapper, -1);
7188 processSync(mapper);
7189
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7191 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7192 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7193 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7196 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7197
7198 // Should not have sent any more keys or motions.
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7201}
7202
7203TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 addConfigurationProperty("touch.deviceType", "touchScreen");
7205 prepareDisplay(DISPLAY_ORIENTATION_0);
7206 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208
7209 // These calculations are based on the input device calibration documentation.
7210 int32_t rawX = 100;
7211 int32_t rawY = 200;
7212 int32_t rawTouchMajor = 7;
7213 int32_t rawTouchMinor = 6;
7214 int32_t rawToolMajor = 9;
7215 int32_t rawToolMinor = 8;
7216 int32_t rawPressure = 11;
7217 int32_t rawDistance = 0;
7218 int32_t rawOrientation = 3;
7219 int32_t id = 5;
7220
7221 float x = toDisplayX(rawX);
7222 float y = toDisplayY(rawY);
7223 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7224 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7225 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7226 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7227 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7228 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7229 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7230 float distance = float(rawDistance);
7231
7232 processPosition(mapper, rawX, rawY);
7233 processTouchMajor(mapper, rawTouchMajor);
7234 processTouchMinor(mapper, rawTouchMinor);
7235 processToolMajor(mapper, rawToolMajor);
7236 processToolMinor(mapper, rawToolMinor);
7237 processPressure(mapper, rawPressure);
7238 processOrientation(mapper, rawOrientation);
7239 processDistance(mapper, rawDistance);
7240 processId(mapper, id);
7241 processMTSync(mapper);
7242 processSync(mapper);
7243
7244 NotifyMotionArgs args;
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7246 ASSERT_EQ(0, args.pointerProperties[0].id);
7247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7248 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7249 orientation, distance));
7250}
7251
7252TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007253 addConfigurationProperty("touch.deviceType", "touchScreen");
7254 prepareDisplay(DISPLAY_ORIENTATION_0);
7255 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7256 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007257 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258
7259 // These calculations are based on the input device calibration documentation.
7260 int32_t rawX = 100;
7261 int32_t rawY = 200;
7262 int32_t rawTouchMajor = 140;
7263 int32_t rawTouchMinor = 120;
7264 int32_t rawToolMajor = 180;
7265 int32_t rawToolMinor = 160;
7266
7267 float x = toDisplayX(rawX);
7268 float y = toDisplayY(rawY);
7269 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7270 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7271 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7272 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7273 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7274
7275 processPosition(mapper, rawX, rawY);
7276 processTouchMajor(mapper, rawTouchMajor);
7277 processTouchMinor(mapper, rawTouchMinor);
7278 processToolMajor(mapper, rawToolMajor);
7279 processToolMinor(mapper, rawToolMinor);
7280 processMTSync(mapper);
7281 processSync(mapper);
7282
7283 NotifyMotionArgs args;
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7286 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7287}
7288
7289TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007290 addConfigurationProperty("touch.deviceType", "touchScreen");
7291 prepareDisplay(DISPLAY_ORIENTATION_0);
7292 prepareAxes(POSITION | TOUCH | TOOL);
7293 addConfigurationProperty("touch.size.calibration", "diameter");
7294 addConfigurationProperty("touch.size.scale", "10");
7295 addConfigurationProperty("touch.size.bias", "160");
7296 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007297 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298
7299 // These calculations are based on the input device calibration documentation.
7300 // Note: We only provide a single common touch/tool value because the device is assumed
7301 // not to emit separate values for each pointer (isSummed = 1).
7302 int32_t rawX = 100;
7303 int32_t rawY = 200;
7304 int32_t rawX2 = 150;
7305 int32_t rawY2 = 250;
7306 int32_t rawTouchMajor = 5;
7307 int32_t rawToolMajor = 8;
7308
7309 float x = toDisplayX(rawX);
7310 float y = toDisplayY(rawY);
7311 float x2 = toDisplayX(rawX2);
7312 float y2 = toDisplayY(rawY2);
7313 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7314 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7315 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7316
7317 processPosition(mapper, rawX, rawY);
7318 processTouchMajor(mapper, rawTouchMajor);
7319 processToolMajor(mapper, rawToolMajor);
7320 processMTSync(mapper);
7321 processPosition(mapper, rawX2, rawY2);
7322 processTouchMajor(mapper, rawTouchMajor);
7323 processToolMajor(mapper, rawToolMajor);
7324 processMTSync(mapper);
7325 processSync(mapper);
7326
7327 NotifyMotionArgs args;
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7330
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7333 args.action);
7334 ASSERT_EQ(size_t(2), args.pointerCount);
7335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7336 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7338 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7339}
7340
7341TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007342 addConfigurationProperty("touch.deviceType", "touchScreen");
7343 prepareDisplay(DISPLAY_ORIENTATION_0);
7344 prepareAxes(POSITION | TOUCH | TOOL);
7345 addConfigurationProperty("touch.size.calibration", "area");
7346 addConfigurationProperty("touch.size.scale", "43");
7347 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007348 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349
7350 // These calculations are based on the input device calibration documentation.
7351 int32_t rawX = 100;
7352 int32_t rawY = 200;
7353 int32_t rawTouchMajor = 5;
7354 int32_t rawToolMajor = 8;
7355
7356 float x = toDisplayX(rawX);
7357 float y = toDisplayY(rawY);
7358 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7359 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7360 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7361
7362 processPosition(mapper, rawX, rawY);
7363 processTouchMajor(mapper, rawTouchMajor);
7364 processToolMajor(mapper, rawToolMajor);
7365 processMTSync(mapper);
7366 processSync(mapper);
7367
7368 NotifyMotionArgs args;
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7371 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7372}
7373
7374TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007375 addConfigurationProperty("touch.deviceType", "touchScreen");
7376 prepareDisplay(DISPLAY_ORIENTATION_0);
7377 prepareAxes(POSITION | PRESSURE);
7378 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7379 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007380 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007381
Michael Wrightaa449c92017-12-13 21:21:43 +00007382 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007383 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007384 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7385 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7386 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7387
Michael Wrightd02c5b62014-02-10 15:10:22 -08007388 // These calculations are based on the input device calibration documentation.
7389 int32_t rawX = 100;
7390 int32_t rawY = 200;
7391 int32_t rawPressure = 60;
7392
7393 float x = toDisplayX(rawX);
7394 float y = toDisplayY(rawY);
7395 float pressure = float(rawPressure) * 0.01f;
7396
7397 processPosition(mapper, rawX, rawY);
7398 processPressure(mapper, rawPressure);
7399 processMTSync(mapper);
7400 processSync(mapper);
7401
7402 NotifyMotionArgs args;
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7405 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7406}
7407
7408TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409 addConfigurationProperty("touch.deviceType", "touchScreen");
7410 prepareDisplay(DISPLAY_ORIENTATION_0);
7411 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007412 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007413
7414 NotifyMotionArgs motionArgs;
7415 NotifyKeyArgs keyArgs;
7416
7417 processId(mapper, 1);
7418 processPosition(mapper, 100, 200);
7419 processSync(mapper);
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7421 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7422 ASSERT_EQ(0, motionArgs.buttonState);
7423
7424 // press BTN_LEFT, release BTN_LEFT
7425 processKey(mapper, BTN_LEFT, 1);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7429 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7430
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7433 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7434
Michael Wrightd02c5b62014-02-10 15:10:22 -08007435 processKey(mapper, BTN_LEFT, 0);
7436 processSync(mapper);
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007440
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007443 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007444
7445 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7446 processKey(mapper, BTN_RIGHT, 1);
7447 processKey(mapper, BTN_MIDDLE, 1);
7448 processSync(mapper);
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7451 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7452 motionArgs.buttonState);
7453
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7456 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7457
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7460 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7461 motionArgs.buttonState);
7462
Michael Wrightd02c5b62014-02-10 15:10:22 -08007463 processKey(mapper, BTN_RIGHT, 0);
7464 processSync(mapper);
7465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007467 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007468
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007471 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007472
7473 processKey(mapper, BTN_MIDDLE, 0);
7474 processSync(mapper);
7475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007477 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007478
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007481 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007482
7483 // press BTN_BACK, release BTN_BACK
7484 processKey(mapper, BTN_BACK, 1);
7485 processSync(mapper);
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7487 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7488 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007489
Michael Wrightd02c5b62014-02-10 15:10:22 -08007490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007492 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7493
7494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7496 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007497
7498 processKey(mapper, BTN_BACK, 0);
7499 processSync(mapper);
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007501 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007502 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007503
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007506 ASSERT_EQ(0, motionArgs.buttonState);
7507
Michael Wrightd02c5b62014-02-10 15:10:22 -08007508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7509 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7510 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7511
7512 // press BTN_SIDE, release BTN_SIDE
7513 processKey(mapper, BTN_SIDE, 1);
7514 processSync(mapper);
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7516 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7517 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007518
Michael Wrightd02c5b62014-02-10 15:10:22 -08007519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007521 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7522
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7525 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526
7527 processKey(mapper, BTN_SIDE, 0);
7528 processSync(mapper);
7529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007531 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007532
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007535 ASSERT_EQ(0, motionArgs.buttonState);
7536
Michael Wrightd02c5b62014-02-10 15:10:22 -08007537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7538 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7539 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7540
7541 // press BTN_FORWARD, release BTN_FORWARD
7542 processKey(mapper, BTN_FORWARD, 1);
7543 processSync(mapper);
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7545 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7546 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007547
Michael Wrightd02c5b62014-02-10 15:10:22 -08007548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007550 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7551
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7554 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007555
7556 processKey(mapper, BTN_FORWARD, 0);
7557 processSync(mapper);
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007560 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007561
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007564 ASSERT_EQ(0, motionArgs.buttonState);
7565
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7567 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7568 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7569
7570 // press BTN_EXTRA, release BTN_EXTRA
7571 processKey(mapper, BTN_EXTRA, 1);
7572 processSync(mapper);
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7574 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7575 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007576
Michael Wrightd02c5b62014-02-10 15:10:22 -08007577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007579 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7580
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7583 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007584
7585 processKey(mapper, BTN_EXTRA, 0);
7586 processSync(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007590
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007593 ASSERT_EQ(0, motionArgs.buttonState);
7594
Michael Wrightd02c5b62014-02-10 15:10:22 -08007595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7596 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7597 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7598
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7600
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601 // press BTN_STYLUS, release BTN_STYLUS
7602 processKey(mapper, BTN_STYLUS, 1);
7603 processSync(mapper);
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007606 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7607
7608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7610 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611
7612 processKey(mapper, BTN_STYLUS, 0);
7613 processSync(mapper);
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007616 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007617
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007620 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007621
7622 // press BTN_STYLUS2, release BTN_STYLUS2
7623 processKey(mapper, BTN_STYLUS2, 1);
7624 processSync(mapper);
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007627 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7628
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7631 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632
7633 processKey(mapper, BTN_STYLUS2, 0);
7634 processSync(mapper);
7635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007637 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007638
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007641 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642
7643 // release touch
7644 processId(mapper, -1);
7645 processSync(mapper);
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7647 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7648 ASSERT_EQ(0, motionArgs.buttonState);
7649}
7650
7651TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652 addConfigurationProperty("touch.deviceType", "touchScreen");
7653 prepareDisplay(DISPLAY_ORIENTATION_0);
7654 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007655 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656
7657 NotifyMotionArgs motionArgs;
7658
7659 // default tool type is finger
7660 processId(mapper, 1);
7661 processPosition(mapper, 100, 200);
7662 processSync(mapper);
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7666
7667 // eraser
7668 processKey(mapper, BTN_TOOL_RUBBER, 1);
7669 processSync(mapper);
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7673
7674 // stylus
7675 processKey(mapper, BTN_TOOL_RUBBER, 0);
7676 processKey(mapper, BTN_TOOL_PEN, 1);
7677 processSync(mapper);
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7681
7682 // brush
7683 processKey(mapper, BTN_TOOL_PEN, 0);
7684 processKey(mapper, BTN_TOOL_BRUSH, 1);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7689
7690 // pencil
7691 processKey(mapper, BTN_TOOL_BRUSH, 0);
7692 processKey(mapper, BTN_TOOL_PENCIL, 1);
7693 processSync(mapper);
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7697
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007698 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007699 processKey(mapper, BTN_TOOL_PENCIL, 0);
7700 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7701 processSync(mapper);
7702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7705
7706 // mouse
7707 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7708 processKey(mapper, BTN_TOOL_MOUSE, 1);
7709 processSync(mapper);
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7713
7714 // lens
7715 processKey(mapper, BTN_TOOL_MOUSE, 0);
7716 processKey(mapper, BTN_TOOL_LENS, 1);
7717 processSync(mapper);
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7721
7722 // double-tap
7723 processKey(mapper, BTN_TOOL_LENS, 0);
7724 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7725 processSync(mapper);
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7729
7730 // triple-tap
7731 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7732 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7733 processSync(mapper);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7737
7738 // quad-tap
7739 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7740 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7741 processSync(mapper);
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7745
7746 // finger
7747 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7748 processKey(mapper, BTN_TOOL_FINGER, 1);
7749 processSync(mapper);
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7753
7754 // stylus trumps finger
7755 processKey(mapper, BTN_TOOL_PEN, 1);
7756 processSync(mapper);
7757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7760
7761 // eraser trumps stylus
7762 processKey(mapper, BTN_TOOL_RUBBER, 1);
7763 processSync(mapper);
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7767
7768 // mouse trumps eraser
7769 processKey(mapper, BTN_TOOL_MOUSE, 1);
7770 processSync(mapper);
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7774
7775 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7776 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7777 processSync(mapper);
7778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7781
7782 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7783 processToolType(mapper, MT_TOOL_PEN);
7784 processSync(mapper);
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7788
7789 // back to default tool type
7790 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7791 processKey(mapper, BTN_TOOL_MOUSE, 0);
7792 processKey(mapper, BTN_TOOL_RUBBER, 0);
7793 processKey(mapper, BTN_TOOL_PEN, 0);
7794 processKey(mapper, BTN_TOOL_FINGER, 0);
7795 processSync(mapper);
7796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7799}
7800
7801TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802 addConfigurationProperty("touch.deviceType", "touchScreen");
7803 prepareDisplay(DISPLAY_ORIENTATION_0);
7804 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007805 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007806 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807
7808 NotifyMotionArgs motionArgs;
7809
7810 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7811 processId(mapper, 1);
7812 processPosition(mapper, 100, 200);
7813 processSync(mapper);
7814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7815 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7817 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7818
7819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7820 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7822 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7823
7824 // move a little
7825 processPosition(mapper, 150, 250);
7826 processSync(mapper);
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7830 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7831
7832 // down when BTN_TOUCH is pressed, pressure defaults to 1
7833 processKey(mapper, BTN_TOUCH, 1);
7834 processSync(mapper);
7835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7838 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7839
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7841 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7843 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7844
7845 // up when BTN_TOUCH is released, hover restored
7846 processKey(mapper, BTN_TOUCH, 0);
7847 processSync(mapper);
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7849 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7851 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7852
7853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7854 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7856 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7857
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7859 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7861 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7862
7863 // exit hover when pointer goes away
7864 processId(mapper, -1);
7865 processSync(mapper);
7866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7867 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7869 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7870}
7871
7872TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 addConfigurationProperty("touch.deviceType", "touchScreen");
7874 prepareDisplay(DISPLAY_ORIENTATION_0);
7875 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007876 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877
7878 NotifyMotionArgs motionArgs;
7879
7880 // initially hovering because pressure is 0
7881 processId(mapper, 1);
7882 processPosition(mapper, 100, 200);
7883 processPressure(mapper, 0);
7884 processSync(mapper);
7885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7886 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7888 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7889
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7891 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7893 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7894
7895 // move a little
7896 processPosition(mapper, 150, 250);
7897 processSync(mapper);
7898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7899 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7901 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7902
7903 // down when pressure becomes non-zero
7904 processPressure(mapper, RAW_PRESSURE_MAX);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7909 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7910
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7912 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7914 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7915
7916 // up when pressure becomes 0, hover restored
7917 processPressure(mapper, 0);
7918 processSync(mapper);
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7920 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7922 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7923
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7925 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7927 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7928
7929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7932 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7933
7934 // exit hover when pointer goes away
7935 processId(mapper, -1);
7936 processSync(mapper);
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7938 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7940 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7941}
7942
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007943/**
7944 * Set the input device port <--> display port associations, and check that the
7945 * events are routed to the display that matches the display port.
7946 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7947 */
7948TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007949 const std::string usb2 = "USB2";
7950 const uint8_t hdmi1 = 0;
7951 const uint8_t hdmi2 = 1;
7952 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007953 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007954
7955 addConfigurationProperty("touch.deviceType", "touchScreen");
7956 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007957 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007958
7959 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7960 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7961
7962 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7963 // for this input device is specified, and the matching viewport is not present,
7964 // the input device should be disabled (at the mapper level).
7965
7966 // Add viewport for display 2 on hdmi2
7967 prepareSecondaryDisplay(type, hdmi2);
7968 // Send a touch event
7969 processPosition(mapper, 100, 100);
7970 processSync(mapper);
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7972
7973 // Add viewport for display 1 on hdmi1
7974 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7975 // Send a touch event again
7976 processPosition(mapper, 100, 100);
7977 processSync(mapper);
7978
7979 NotifyMotionArgs args;
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7981 ASSERT_EQ(DISPLAY_ID, args.displayId);
7982}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007984TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007985 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007986 std::shared_ptr<FakePointerController> fakePointerController =
7987 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007988 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007989 fakePointerController->setPosition(100, 200);
7990 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00007991 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007992
Garfield Tan888a6a42020-01-09 11:39:16 -08007993 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007994 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007995
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007996 prepareDisplay(DISPLAY_ORIENTATION_0);
7997 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007998 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007999
8000 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008001 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008002
8003 NotifyMotionArgs motionArgs;
8004 processPosition(mapper, 100, 100);
8005 processSync(mapper);
8006
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8009 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8010}
8011
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008012/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008013 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8014 */
8015TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8016 addConfigurationProperty("touch.deviceType", "touchScreen");
8017 prepareAxes(POSITION);
8018 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8019
8020 prepareDisplay(DISPLAY_ORIENTATION_0);
8021 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8022 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8023 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8024 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8025
8026 NotifyMotionArgs args;
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8028 ASSERT_EQ(26, args.readTime);
8029
8030 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8031 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8032 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8033
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8035 ASSERT_EQ(33, args.readTime);
8036}
8037
8038/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008039 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8040 * events should not be delivered to the listener.
8041 */
8042TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8043 addConfigurationProperty("touch.deviceType", "touchScreen");
8044 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8045 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8046 ViewportType::INTERNAL);
8047 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8048 prepareAxes(POSITION);
8049 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8050
8051 NotifyMotionArgs motionArgs;
8052 processPosition(mapper, 100, 100);
8053 processSync(mapper);
8054
8055 mFakeListener->assertNotifyMotionWasNotCalled();
8056}
8057
Garfield Tanc734e4f2021-01-15 20:01:39 -08008058TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8059 addConfigurationProperty("touch.deviceType", "touchScreen");
8060 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8061 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8062 ViewportType::INTERNAL);
8063 std::optional<DisplayViewport> optionalDisplayViewport =
8064 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8065 ASSERT_TRUE(optionalDisplayViewport.has_value());
8066 DisplayViewport displayViewport = *optionalDisplayViewport;
8067
8068 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8069 prepareAxes(POSITION);
8070 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8071
8072 // Finger down
8073 int32_t x = 100, y = 100;
8074 processPosition(mapper, x, y);
8075 processSync(mapper);
8076
8077 NotifyMotionArgs motionArgs;
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8079 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8080
8081 // Deactivate display viewport
8082 displayViewport.isActive = false;
8083 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8084 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8085
8086 // Finger move
8087 x += 10, y += 10;
8088 processPosition(mapper, x, y);
8089 processSync(mapper);
8090
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8092 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8093
8094 // Reactivate display viewport
8095 displayViewport.isActive = true;
8096 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8097 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8098
8099 // Finger move again
8100 x += 10, y += 10;
8101 processPosition(mapper, x, y);
8102 processSync(mapper);
8103
8104 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8105 // no pointer on the touch device.
8106 mFakeListener->assertNotifyMotionWasNotCalled();
8107}
8108
Arthur Hung7c645402019-01-25 17:45:42 +08008109TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8110 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008111 prepareAxes(POSITION | ID | SLOT);
8112 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008113 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008114
8115 // Create the second touch screen device, and enable multi fingers.
8116 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008117 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008118 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008119 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008120 std::shared_ptr<InputDevice> device2 =
8121 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8122 Flags<InputDeviceClass>(0));
8123
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008124 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8125 0 /*flat*/, 0 /*fuzz*/);
8126 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8127 0 /*flat*/, 0 /*fuzz*/);
8128 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8129 0 /*flat*/, 0 /*fuzz*/);
8130 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8131 0 /*flat*/, 0 /*fuzz*/);
8132 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8133 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8134 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008135
8136 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008137 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008138 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8139 device2->reset(ARBITRARY_TIME);
8140
8141 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008142 std::shared_ptr<FakePointerController> fakePointerController =
8143 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008144 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008145
8146 // Setup policy for associated displays and show touches.
8147 const uint8_t hdmi1 = 0;
8148 const uint8_t hdmi2 = 1;
8149 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8150 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8151 mFakePolicy->setShowTouches(true);
8152
8153 // Create displays.
8154 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008155 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008156
8157 // Default device will reconfigure above, need additional reconfiguration for another device.
8158 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008159 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008160
8161 // Two fingers down at default display.
8162 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8163 processPosition(mapper, x1, y1);
8164 processId(mapper, 1);
8165 processSlot(mapper, 1);
8166 processPosition(mapper, x2, y2);
8167 processId(mapper, 2);
8168 processSync(mapper);
8169
8170 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8171 fakePointerController->getSpots().find(DISPLAY_ID);
8172 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8173 ASSERT_EQ(size_t(2), iter->second.size());
8174
8175 // Two fingers down at second display.
8176 processPosition(mapper2, x1, y1);
8177 processId(mapper2, 1);
8178 processSlot(mapper2, 1);
8179 processPosition(mapper2, x2, y2);
8180 processId(mapper2, 2);
8181 processSync(mapper2);
8182
8183 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8184 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8185 ASSERT_EQ(size_t(2), iter->second.size());
8186}
8187
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008188TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008189 prepareAxes(POSITION);
8190 addConfigurationProperty("touch.deviceType", "touchScreen");
8191 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008192 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008193
8194 NotifyMotionArgs motionArgs;
8195 // Unrotated video frame
8196 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8197 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008198 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008199 processPosition(mapper, 100, 200);
8200 processSync(mapper);
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8202 ASSERT_EQ(frames, motionArgs.videoFrames);
8203
8204 // Subsequent touch events should not have any videoframes
8205 // This is implemented separately in FakeEventHub,
8206 // but that should match the behaviour of TouchVideoDevice.
8207 processPosition(mapper, 200, 200);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8211}
8212
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008213TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008214 prepareAxes(POSITION);
8215 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008216 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008217 // Unrotated video frame
8218 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8219 NotifyMotionArgs motionArgs;
8220
8221 // Test all 4 orientations
8222 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008223 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8224 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8225 clearViewports();
8226 prepareDisplay(orientation);
8227 std::vector<TouchVideoFrame> frames{frame};
8228 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8229 processPosition(mapper, 100, 200);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(frames, motionArgs.videoFrames);
8233 }
8234}
8235
8236TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8237 prepareAxes(POSITION);
8238 addConfigurationProperty("touch.deviceType", "touchScreen");
8239 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8240 // orientation-aware are affected by display rotation.
8241 addConfigurationProperty("touch.orientationAware", "0");
8242 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8243 // Unrotated video frame
8244 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8245 NotifyMotionArgs motionArgs;
8246
8247 // Test all 4 orientations
8248 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008249 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8250 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8251 clearViewports();
8252 prepareDisplay(orientation);
8253 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008254 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008255 processPosition(mapper, 100, 200);
8256 processSync(mapper);
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008258 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8259 // compared to the display. This is so that when the window transform (which contains the
8260 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8261 // window's coordinate space.
8262 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008263 ASSERT_EQ(frames, motionArgs.videoFrames);
8264 }
8265}
8266
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008267TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008268 prepareAxes(POSITION);
8269 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008270 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008271 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8272 // so mix these.
8273 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8274 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8275 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8276 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8277 NotifyMotionArgs motionArgs;
8278
8279 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008280 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008281 processPosition(mapper, 100, 200);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008284 ASSERT_EQ(frames, motionArgs.videoFrames);
8285}
8286
8287TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8288 prepareAxes(POSITION);
8289 addConfigurationProperty("touch.deviceType", "touchScreen");
8290 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8291 // orientation-aware are affected by display rotation.
8292 addConfigurationProperty("touch.orientationAware", "0");
8293 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8294 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8295 // so mix these.
8296 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8297 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8298 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8299 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8300 NotifyMotionArgs motionArgs;
8301
8302 prepareDisplay(DISPLAY_ORIENTATION_90);
8303 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8304 processPosition(mapper, 100, 200);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8307 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8308 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8309 // compared to the display. This is so that when the window transform (which contains the
8310 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8311 // window's coordinate space.
8312 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8313 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008314 ASSERT_EQ(frames, motionArgs.videoFrames);
8315}
8316
Arthur Hung9da14732019-09-02 16:16:58 +08008317/**
8318 * If we had defined port associations, but the viewport is not ready, the touch device would be
8319 * expected to be disabled, and it should be enabled after the viewport has found.
8320 */
8321TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008322 constexpr uint8_t hdmi2 = 1;
8323 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008324 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008325
8326 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8327
8328 addConfigurationProperty("touch.deviceType", "touchScreen");
8329 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008330 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008331
8332 ASSERT_EQ(mDevice->isEnabled(), false);
8333
8334 // Add display on hdmi2, the device should be enabled and can receive touch event.
8335 prepareSecondaryDisplay(type, hdmi2);
8336 ASSERT_EQ(mDevice->isEnabled(), true);
8337
8338 // Send a touch event.
8339 processPosition(mapper, 100, 100);
8340 processSync(mapper);
8341
8342 NotifyMotionArgs args;
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8344 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8345}
8346
Arthur Hung421eb1c2020-01-16 00:09:42 +08008347TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008348 addConfigurationProperty("touch.deviceType", "touchScreen");
8349 prepareDisplay(DISPLAY_ORIENTATION_0);
8350 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008351 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008352
8353 NotifyMotionArgs motionArgs;
8354
8355 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8356 // finger down
8357 processId(mapper, 1);
8358 processPosition(mapper, x1, y1);
8359 processSync(mapper);
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8361 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8363
8364 // finger move
8365 processId(mapper, 1);
8366 processPosition(mapper, x2, y2);
8367 processSync(mapper);
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8371
8372 // finger up.
8373 processId(mapper, -1);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8378
8379 // new finger down
8380 processId(mapper, 1);
8381 processPosition(mapper, x3, y3);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8386}
8387
8388/**
arthurhungcc7f9802020-04-30 17:55:40 +08008389 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8390 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008391 */
arthurhungcc7f9802020-04-30 17:55:40 +08008392TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008393 addConfigurationProperty("touch.deviceType", "touchScreen");
8394 prepareDisplay(DISPLAY_ORIENTATION_0);
8395 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008396 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008397
8398 NotifyMotionArgs motionArgs;
8399
8400 // default tool type is finger
8401 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008402 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008403 processPosition(mapper, x1, y1);
8404 processSync(mapper);
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8406 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8408
8409 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8410 processToolType(mapper, MT_TOOL_PALM);
8411 processSync(mapper);
8412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8413 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8414
8415 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008416 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008417 processPosition(mapper, x2, y2);
8418 processSync(mapper);
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8420
8421 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008422 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008423 processSync(mapper);
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8425
8426 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008427 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008428 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008429 processPosition(mapper, x3, y3);
8430 processSync(mapper);
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8434}
8435
arthurhungbf89a482020-04-17 17:37:55 +08008436/**
arthurhungcc7f9802020-04-30 17:55:40 +08008437 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8438 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008439 */
arthurhungcc7f9802020-04-30 17:55:40 +08008440TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008441 addConfigurationProperty("touch.deviceType", "touchScreen");
8442 prepareDisplay(DISPLAY_ORIENTATION_0);
8443 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8444 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8445
8446 NotifyMotionArgs motionArgs;
8447
8448 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008449 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8450 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008451 processPosition(mapper, x1, y1);
8452 processSync(mapper);
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8456
8457 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008458 processSlot(mapper, SECOND_SLOT);
8459 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008460 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8464 motionArgs.action);
8465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8466
8467 // If the tool type of the first finger changes to MT_TOOL_PALM,
8468 // we expect to receive ACTION_POINTER_UP with cancel flag.
8469 processSlot(mapper, FIRST_SLOT);
8470 processId(mapper, FIRST_TRACKING_ID);
8471 processToolType(mapper, MT_TOOL_PALM);
8472 processSync(mapper);
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8475 motionArgs.action);
8476 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8477
8478 // The following MOVE events of second finger should be processed.
8479 processSlot(mapper, SECOND_SLOT);
8480 processId(mapper, SECOND_TRACKING_ID);
8481 processPosition(mapper, x2 + 1, y2 + 1);
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8485 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8486
8487 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8488 // it. Second finger receive move.
8489 processSlot(mapper, FIRST_SLOT);
8490 processId(mapper, INVALID_TRACKING_ID);
8491 processSync(mapper);
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8494 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8495
8496 // Second finger keeps moving.
8497 processSlot(mapper, SECOND_SLOT);
8498 processId(mapper, SECOND_TRACKING_ID);
8499 processPosition(mapper, x2 + 2, y2 + 2);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8503 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8504
8505 // Second finger up.
8506 processId(mapper, INVALID_TRACKING_ID);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8510 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8511}
8512
8513/**
8514 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8515 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8516 */
8517TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8518 addConfigurationProperty("touch.deviceType", "touchScreen");
8519 prepareDisplay(DISPLAY_ORIENTATION_0);
8520 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8521 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8522
8523 NotifyMotionArgs motionArgs;
8524
8525 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8526 // First finger down.
8527 processId(mapper, FIRST_TRACKING_ID);
8528 processPosition(mapper, x1, y1);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8533
8534 // Second finger down.
8535 processSlot(mapper, SECOND_SLOT);
8536 processId(mapper, SECOND_TRACKING_ID);
8537 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008538 processSync(mapper);
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8540 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8541 motionArgs.action);
8542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8543
arthurhungcc7f9802020-04-30 17:55:40 +08008544 // If the tool type of the first finger changes to MT_TOOL_PALM,
8545 // we expect to receive ACTION_POINTER_UP with cancel flag.
8546 processSlot(mapper, FIRST_SLOT);
8547 processId(mapper, FIRST_TRACKING_ID);
8548 processToolType(mapper, MT_TOOL_PALM);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8552 motionArgs.action);
8553 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8554
8555 // Second finger keeps moving.
8556 processSlot(mapper, SECOND_SLOT);
8557 processId(mapper, SECOND_TRACKING_ID);
8558 processPosition(mapper, x2 + 1, y2 + 1);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8562
8563 // second finger becomes palm, receive cancel due to only 1 finger is active.
8564 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008565 processToolType(mapper, MT_TOOL_PALM);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8569
arthurhungcc7f9802020-04-30 17:55:40 +08008570 // third finger down.
8571 processSlot(mapper, THIRD_SLOT);
8572 processId(mapper, THIRD_TRACKING_ID);
8573 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008574 processPosition(mapper, x3, y3);
8575 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008579 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8580
8581 // third finger move
8582 processId(mapper, THIRD_TRACKING_ID);
8583 processPosition(mapper, x3 + 1, y3 + 1);
8584 processSync(mapper);
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8587
8588 // first finger up, third finger receive move.
8589 processSlot(mapper, FIRST_SLOT);
8590 processId(mapper, INVALID_TRACKING_ID);
8591 processSync(mapper);
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8594 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8595
8596 // second finger up, third finger receive move.
8597 processSlot(mapper, SECOND_SLOT);
8598 processId(mapper, INVALID_TRACKING_ID);
8599 processSync(mapper);
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8602 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8603
8604 // third finger up.
8605 processSlot(mapper, THIRD_SLOT);
8606 processId(mapper, INVALID_TRACKING_ID);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8610 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8611}
8612
8613/**
8614 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8615 * and the active finger could still be allowed to receive the events
8616 */
8617TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8618 addConfigurationProperty("touch.deviceType", "touchScreen");
8619 prepareDisplay(DISPLAY_ORIENTATION_0);
8620 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8621 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8622
8623 NotifyMotionArgs motionArgs;
8624
8625 // default tool type is finger
8626 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8627 processId(mapper, FIRST_TRACKING_ID);
8628 processPosition(mapper, x1, y1);
8629 processSync(mapper);
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8631 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8633
8634 // Second finger down.
8635 processSlot(mapper, SECOND_SLOT);
8636 processId(mapper, SECOND_TRACKING_ID);
8637 processPosition(mapper, x2, y2);
8638 processSync(mapper);
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8641 motionArgs.action);
8642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8643
8644 // If the tool type of the second finger changes to MT_TOOL_PALM,
8645 // we expect to receive ACTION_POINTER_UP with cancel flag.
8646 processId(mapper, SECOND_TRACKING_ID);
8647 processToolType(mapper, MT_TOOL_PALM);
8648 processSync(mapper);
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8651 motionArgs.action);
8652 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8653
8654 // The following MOVE event should be processed.
8655 processSlot(mapper, FIRST_SLOT);
8656 processId(mapper, FIRST_TRACKING_ID);
8657 processPosition(mapper, x1 + 1, y1 + 1);
8658 processSync(mapper);
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8661 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8662
8663 // second finger up.
8664 processSlot(mapper, SECOND_SLOT);
8665 processId(mapper, INVALID_TRACKING_ID);
8666 processSync(mapper);
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8669
8670 // first finger keep moving
8671 processSlot(mapper, FIRST_SLOT);
8672 processId(mapper, FIRST_TRACKING_ID);
8673 processPosition(mapper, x1 + 2, y1 + 2);
8674 processSync(mapper);
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8677
8678 // first finger up.
8679 processId(mapper, INVALID_TRACKING_ID);
8680 processSync(mapper);
8681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8682 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8683 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008684}
8685
Arthur Hung9ad18942021-06-19 02:04:46 +00008686/**
8687 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8688 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8689 * cause slot be valid again.
8690 */
8691TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8692 addConfigurationProperty("touch.deviceType", "touchScreen");
8693 prepareDisplay(DISPLAY_ORIENTATION_0);
8694 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8695 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8696
8697 NotifyMotionArgs motionArgs;
8698
8699 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8700 // First finger down.
8701 processId(mapper, FIRST_TRACKING_ID);
8702 processPosition(mapper, x1, y1);
8703 processPressure(mapper, RAW_PRESSURE_MAX);
8704 processSync(mapper);
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8706 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8707 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8708
8709 // First finger move.
8710 processId(mapper, FIRST_TRACKING_ID);
8711 processPosition(mapper, x1 + 1, y1 + 1);
8712 processPressure(mapper, RAW_PRESSURE_MAX);
8713 processSync(mapper);
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8716 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8717
8718 // Second finger down.
8719 processSlot(mapper, SECOND_SLOT);
8720 processId(mapper, SECOND_TRACKING_ID);
8721 processPosition(mapper, x2, y2);
8722 processPressure(mapper, RAW_PRESSURE_MAX);
8723 processSync(mapper);
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8725 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8726 motionArgs.action);
8727 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8728
8729 // second finger up with some unexpected data.
8730 processSlot(mapper, SECOND_SLOT);
8731 processId(mapper, INVALID_TRACKING_ID);
8732 processPosition(mapper, x2, y2);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8736 motionArgs.action);
8737 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8738
8739 // first finger up with some unexpected data.
8740 processSlot(mapper, FIRST_SLOT);
8741 processId(mapper, INVALID_TRACKING_ID);
8742 processPosition(mapper, x2, y2);
8743 processPressure(mapper, RAW_PRESSURE_MAX);
8744 processSync(mapper);
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8746 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8747 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8748}
8749
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008750// --- MultiTouchInputMapperTest_ExternalDevice ---
8751
8752class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8753protected:
Chris Yea52ade12020-08-27 16:49:20 -07008754 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008755};
8756
8757/**
8758 * Expect fallback to internal viewport if device is external and external viewport is not present.
8759 */
8760TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8761 prepareAxes(POSITION);
8762 addConfigurationProperty("touch.deviceType", "touchScreen");
8763 prepareDisplay(DISPLAY_ORIENTATION_0);
8764 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8765
8766 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8767
8768 NotifyMotionArgs motionArgs;
8769
8770 // Expect the event to be sent to the internal viewport,
8771 // because an external viewport is not present.
8772 processPosition(mapper, 100, 100);
8773 processSync(mapper);
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8776
8777 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008778 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008779 processPosition(mapper, 100, 100);
8780 processSync(mapper);
8781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8782 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8783}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008784
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008785TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8786 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8787 std::shared_ptr<FakePointerController> fakePointerController =
8788 std::make_shared<FakePointerController>();
8789 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8790 fakePointerController->setPosition(0, 0);
8791 fakePointerController->setButtonState(0);
8792
8793 // prepare device and capture
8794 prepareDisplay(DISPLAY_ORIENTATION_0);
8795 prepareAxes(POSITION | ID | SLOT);
8796 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8797 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8798 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008799 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8801
8802 // captured touchpad should be a touchpad source
8803 NotifyDeviceResetArgs resetArgs;
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8805 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8806
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008807 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008808
8809 const InputDeviceInfo::MotionRange* relRangeX =
8810 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8811 ASSERT_NE(relRangeX, nullptr);
8812 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8813 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8814 const InputDeviceInfo::MotionRange* relRangeY =
8815 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8816 ASSERT_NE(relRangeY, nullptr);
8817 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8818 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8819
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008820 // run captured pointer tests - note that this is unscaled, so input listener events should be
8821 // identical to what the hardware sends (accounting for any
8822 // calibration).
8823 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008824 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008825 processId(mapper, 1);
8826 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8827 processKey(mapper, BTN_TOUCH, 1);
8828 processSync(mapper);
8829
8830 // expect coord[0] to contain initial location of touch 0
8831 NotifyMotionArgs args;
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8834 ASSERT_EQ(1U, args.pointerCount);
8835 ASSERT_EQ(0, args.pointerProperties[0].id);
8836 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8837 ASSERT_NO_FATAL_FAILURE(
8838 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8839
8840 // FINGER 1 DOWN
8841 processSlot(mapper, 1);
8842 processId(mapper, 2);
8843 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8844 processSync(mapper);
8845
8846 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008848 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8849 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008850 ASSERT_EQ(2U, args.pointerCount);
8851 ASSERT_EQ(0, args.pointerProperties[0].id);
8852 ASSERT_EQ(1, args.pointerProperties[1].id);
8853 ASSERT_NO_FATAL_FAILURE(
8854 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8855 ASSERT_NO_FATAL_FAILURE(
8856 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8857
8858 // FINGER 1 MOVE
8859 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8860 processSync(mapper);
8861
8862 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8863 // from move
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8866 ASSERT_NO_FATAL_FAILURE(
8867 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8868 ASSERT_NO_FATAL_FAILURE(
8869 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8870
8871 // FINGER 0 MOVE
8872 processSlot(mapper, 0);
8873 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8874 processSync(mapper);
8875
8876 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8879 ASSERT_NO_FATAL_FAILURE(
8880 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8881 ASSERT_NO_FATAL_FAILURE(
8882 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8883
8884 // BUTTON DOWN
8885 processKey(mapper, BTN_LEFT, 1);
8886 processSync(mapper);
8887
8888 // touchinputmapper design sends a move before button press
8889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8892 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8893
8894 // BUTTON UP
8895 processKey(mapper, BTN_LEFT, 0);
8896 processSync(mapper);
8897
8898 // touchinputmapper design sends a move after button release
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8903
8904 // FINGER 0 UP
8905 processId(mapper, -1);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8908 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8909
8910 // FINGER 1 MOVE
8911 processSlot(mapper, 1);
8912 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8913 processSync(mapper);
8914
8915 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8918 ASSERT_EQ(1U, args.pointerCount);
8919 ASSERT_EQ(1, args.pointerProperties[0].id);
8920 ASSERT_NO_FATAL_FAILURE(
8921 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8922
8923 // FINGER 1 UP
8924 processId(mapper, -1);
8925 processKey(mapper, BTN_TOUCH, 0);
8926 processSync(mapper);
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8928 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8929
8930 // non captured touchpad should be a mouse source
8931 mFakePolicy->setPointerCapture(false);
8932 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8934 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8935}
8936
8937TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8938 std::shared_ptr<FakePointerController> fakePointerController =
8939 std::make_shared<FakePointerController>();
8940 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8941 fakePointerController->setPosition(0, 0);
8942 fakePointerController->setButtonState(0);
8943
8944 // prepare device and capture
8945 prepareDisplay(DISPLAY_ORIENTATION_0);
8946 prepareAxes(POSITION | ID | SLOT);
8947 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8948 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008949 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008950 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8951 // run uncaptured pointer tests - pushes out generic events
8952 // FINGER 0 DOWN
8953 processId(mapper, 3);
8954 processPosition(mapper, 100, 100);
8955 processKey(mapper, BTN_TOUCH, 1);
8956 processSync(mapper);
8957
8958 // start at (100,100), cursor should be at (0,0) * scale
8959 NotifyMotionArgs args;
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8961 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8962 ASSERT_NO_FATAL_FAILURE(
8963 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8964
8965 // FINGER 0 MOVE
8966 processPosition(mapper, 200, 200);
8967 processSync(mapper);
8968
8969 // compute scaling to help with touch position checking
8970 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8971 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8972 float scale =
8973 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8974
8975 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8977 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8979 0, 0, 0, 0, 0, 0, 0));
8980}
8981
8982TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8983 std::shared_ptr<FakePointerController> fakePointerController =
8984 std::make_shared<FakePointerController>();
8985
8986 prepareDisplay(DISPLAY_ORIENTATION_0);
8987 prepareAxes(POSITION | ID | SLOT);
8988 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008989 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008990 mFakePolicy->setPointerCapture(false);
8991 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8992
8993 // uncaptured touchpad should be a pointer device
8994 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8995
8996 // captured touchpad should be a touchpad device
8997 mFakePolicy->setPointerCapture(true);
8998 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8999 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9000}
9001
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009002// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009003
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009004class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009005protected:
9006 static const char* DEVICE_NAME;
9007 static const char* DEVICE_LOCATION;
9008 static const int32_t DEVICE_ID;
9009 static const int32_t DEVICE_GENERATION;
9010 static const int32_t DEVICE_CONTROLLER_NUMBER;
9011 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9012 static const int32_t EVENTHUB_ID;
9013
9014 std::shared_ptr<FakeEventHub> mFakeEventHub;
9015 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009016 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009017 std::unique_ptr<InstrumentedInputReader> mReader;
9018 std::shared_ptr<InputDevice> mDevice;
9019
9020 virtual void SetUp(Flags<InputDeviceClass> classes) {
9021 mFakeEventHub = std::make_unique<FakeEventHub>();
9022 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009023 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009024 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009025 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009026 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9027 }
9028
9029 void SetUp() override { SetUp(DEVICE_CLASSES); }
9030
9031 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009032 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009033 mFakePolicy.clear();
9034 }
9035
9036 void configureDevice(uint32_t changes) {
9037 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9038 mReader->requestRefreshConfiguration(changes);
9039 mReader->loopOnce();
9040 }
9041 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9042 }
9043
9044 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9045 const std::string& location, int32_t eventHubId,
9046 Flags<InputDeviceClass> classes) {
9047 InputDeviceIdentifier identifier;
9048 identifier.name = name;
9049 identifier.location = location;
9050 std::shared_ptr<InputDevice> device =
9051 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9052 identifier);
9053 mReader->pushNextDevice(device);
9054 mFakeEventHub->addDevice(eventHubId, name, classes);
9055 mReader->loopOnce();
9056 return device;
9057 }
9058
9059 template <class T, typename... Args>
9060 T& addControllerAndConfigure(Args... args) {
9061 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9062
9063 return controller;
9064 }
9065};
9066
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009067const char* PeripheralControllerTest::DEVICE_NAME = "device";
9068const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9069const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9070const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9071const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9072const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009073 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009074const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009075
9076// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009077class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009078protected:
9079 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009080 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009081 }
9082};
9083
9084TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009085 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009086
9087 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9088 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9089}
9090
9091TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009092 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009093
9094 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9095 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9096}
9097
9098// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009099class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009100protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009101 void SetUp() override {
9102 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9103 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009104};
9105
Chris Ye85758332021-05-16 23:05:17 -07009106TEST_F(LightControllerTest, MonoLight) {
9107 RawLightInfo infoMono = {.id = 1,
9108 .name = "Mono",
9109 .maxBrightness = 255,
9110 .flags = InputLightClass::BRIGHTNESS,
9111 .path = ""};
9112 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009113
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009114 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009115 InputDeviceInfo info;
9116 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009117 std::vector<InputDeviceLightInfo> lights = info.getLights();
9118 ASSERT_EQ(1U, lights.size());
9119 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009120
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009121 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9122 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009123}
9124
9125TEST_F(LightControllerTest, RGBLight) {
9126 RawLightInfo infoRed = {.id = 1,
9127 .name = "red",
9128 .maxBrightness = 255,
9129 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9130 .path = ""};
9131 RawLightInfo infoGreen = {.id = 2,
9132 .name = "green",
9133 .maxBrightness = 255,
9134 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9135 .path = ""};
9136 RawLightInfo infoBlue = {.id = 3,
9137 .name = "blue",
9138 .maxBrightness = 255,
9139 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9140 .path = ""};
9141 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9142 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9143 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9144
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009145 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009146 InputDeviceInfo info;
9147 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009148 std::vector<InputDeviceLightInfo> lights = info.getLights();
9149 ASSERT_EQ(1U, lights.size());
9150 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009151
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009152 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9153 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009154}
9155
9156TEST_F(LightControllerTest, MultiColorRGBLight) {
9157 RawLightInfo infoColor = {.id = 1,
9158 .name = "red",
9159 .maxBrightness = 255,
9160 .flags = InputLightClass::BRIGHTNESS |
9161 InputLightClass::MULTI_INTENSITY |
9162 InputLightClass::MULTI_INDEX,
9163 .path = ""};
9164
9165 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9166
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009167 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009168 InputDeviceInfo info;
9169 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009170 std::vector<InputDeviceLightInfo> lights = info.getLights();
9171 ASSERT_EQ(1U, lights.size());
9172 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009173
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009174 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9175 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009176}
9177
9178TEST_F(LightControllerTest, PlayerIdLight) {
9179 RawLightInfo info1 = {.id = 1,
9180 .name = "player1",
9181 .maxBrightness = 255,
9182 .flags = InputLightClass::BRIGHTNESS,
9183 .path = ""};
9184 RawLightInfo info2 = {.id = 2,
9185 .name = "player2",
9186 .maxBrightness = 255,
9187 .flags = InputLightClass::BRIGHTNESS,
9188 .path = ""};
9189 RawLightInfo info3 = {.id = 3,
9190 .name = "player3",
9191 .maxBrightness = 255,
9192 .flags = InputLightClass::BRIGHTNESS,
9193 .path = ""};
9194 RawLightInfo info4 = {.id = 4,
9195 .name = "player4",
9196 .maxBrightness = 255,
9197 .flags = InputLightClass::BRIGHTNESS,
9198 .path = ""};
9199 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9200 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9201 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9202 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9203
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009204 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009205 InputDeviceInfo info;
9206 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009207 std::vector<InputDeviceLightInfo> lights = info.getLights();
9208 ASSERT_EQ(1U, lights.size());
9209 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009210
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009211 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9212 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9213 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009214}
9215
Michael Wrightd02c5b62014-02-10 15:10:22 -08009216} // namespace android