blob: 41ce116d6cd7979225387d702959e598fce37706 [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
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800109static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
110 InputDeviceInfo info;
111 mapper.populateDeviceInfo(&info);
112
113 const InputDeviceInfo::MotionRange* motionRange =
114 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
115 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
116}
117
118static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
119 InputDeviceInfo info;
120 mapper.populateDeviceInfo(&info);
121
122 const InputDeviceInfo::MotionRange* motionRange =
123 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
124 ASSERT_EQ(nullptr, motionRange);
125}
126
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127// --- FakePointerController ---
128
129class FakePointerController : public PointerControllerInterface {
130 bool mHaveBounds;
131 float mMinX, mMinY, mMaxX, mMaxY;
132 float mX, mY;
133 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800134 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136public:
137 FakePointerController() :
138 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800139 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 }
141
Michael Wright17db18e2020-06-26 20:51:44 +0100142 virtual ~FakePointerController() {}
143
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 void setBounds(float minX, float minY, float maxX, float maxY) {
145 mHaveBounds = true;
146 mMinX = minX;
147 mMinY = minY;
148 mMaxX = maxX;
149 mMaxY = maxY;
150 }
151
Chris Yea52ade12020-08-27 16:49:20 -0700152 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 mX = x;
154 mY = y;
155 }
156
Chris Yea52ade12020-08-27 16:49:20 -0700157 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158
Chris Yea52ade12020-08-27 16:49:20 -0700159 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160
Chris Yea52ade12020-08-27 16:49:20 -0700161 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162 *outX = mX;
163 *outY = mY;
164 }
165
Chris Yea52ade12020-08-27 16:49:20 -0700166 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800167
Chris Yea52ade12020-08-27 16:49:20 -0700168 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800169 mDisplayId = viewport.displayId;
170 }
171
Arthur Hung7c645402019-01-25 17:45:42 +0800172 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
173 return mSpotsByDisplay;
174 }
175
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176private:
Chris Yea52ade12020-08-27 16:49:20 -0700177 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 *outMinX = mMinX;
179 *outMinY = mMinY;
180 *outMaxX = mMaxX;
181 *outMaxY = mMaxY;
182 return mHaveBounds;
183 }
184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 mX += deltaX;
187 if (mX < mMinX) mX = mMinX;
188 if (mX > mMaxX) mX = mMaxX;
189 mY += deltaY;
190 if (mY < mMinY) mY = mMinY;
191 if (mY > mMaxY) mY = mMaxY;
192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195
Chris Yea52ade12020-08-27 16:49:20 -0700196 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197
Chris Yea52ade12020-08-27 16:49:20 -0700198 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199
Chris Yea52ade12020-08-27 16:49:20 -0700200 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
201 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800202 std::vector<int32_t> newSpots;
203 // Add spots for fingers that are down.
204 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
205 uint32_t id = idBits.clearFirstMarkedBit();
206 newSpots.push_back(id);
207 }
208
209 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210 }
211
Chris Yea52ade12020-08-27 16:49:20 -0700212 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800213
214 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215};
216
217
218// --- FakeInputReaderPolicy ---
219
220class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::mutex mLock;
222 std::condition_variable mDevicesChangedCondition;
223
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000225 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700226 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
227 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100228 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700229 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230
231protected:
Chris Yea52ade12020-08-27 16:49:20 -0700232 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233
234public:
235 FakeInputReaderPolicy() {
236 }
237
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700238 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800239 waitForInputDevices([](bool devicesChanged) {
240 if (!devicesChanged) {
241 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
242 }
243 });
244 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700245
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800246 void assertInputDevicesNotChanged() {
247 waitForInputDevices([](bool devicesChanged) {
248 if (devicesChanged) {
249 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
250 }
251 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700252 }
253
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700254 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100255 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100256 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700257 }
258
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700259 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
260 return mConfig.getDisplayViewportByUniqueId(uniqueId);
261 }
262 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
263 return mConfig.getDisplayViewportByType(type);
264 }
265
266 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
267 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700268 }
269
Prabir Pradhan5632d622021-09-06 07:57:20 -0700270 void addDisplayViewport(DisplayViewport viewport) {
271 mViewports.push_back(std::move(viewport));
272 mConfig.setDisplayViewports(mViewports);
273 }
274
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700275 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000276 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700277 std::optional<uint8_t> physicalPort, ViewportType type) {
278 const bool isRotated =
279 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
280 DisplayViewport v;
281 v.displayId = displayId;
282 v.orientation = orientation;
283 v.logicalLeft = 0;
284 v.logicalTop = 0;
285 v.logicalRight = isRotated ? height : width;
286 v.logicalBottom = isRotated ? width : height;
287 v.physicalLeft = 0;
288 v.physicalTop = 0;
289 v.physicalRight = isRotated ? height : width;
290 v.physicalBottom = isRotated ? width : height;
291 v.deviceWidth = isRotated ? height : width;
292 v.deviceHeight = isRotated ? width : height;
293 v.isActive = isActive;
294 v.uniqueId = uniqueId;
295 v.physicalPort = physicalPort;
296 v.type = type;
297
298 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 }
300
Arthur Hung6cd19a42019-08-30 19:04:12 +0800301 bool updateViewport(const DisplayViewport& viewport) {
302 size_t count = mViewports.size();
303 for (size_t i = 0; i < count; i++) {
304 const DisplayViewport& currentViewport = mViewports[i];
305 if (currentViewport.displayId == viewport.displayId) {
306 mViewports[i] = viewport;
307 mConfig.setDisplayViewports(mViewports);
308 return true;
309 }
310 }
311 // no viewport found.
312 return false;
313 }
314
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100315 void addExcludedDeviceName(const std::string& deviceName) {
316 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 }
318
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700319 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
320 mConfig.portAssociations.insert({inputPort, displayPort});
321 }
322
Christine Franks1ba71cc2021-04-07 14:37:42 -0700323 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
324 const std::string& displayUniqueId) {
325 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
326 }
327
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000328 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700329
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000330 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700331
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000332 void setPointerController(std::shared_ptr<FakePointerController> controller) {
333 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
336 const InputReaderConfiguration* getReaderConfiguration() const {
337 return &mConfig;
338 }
339
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800340 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 return mInputDevices;
342 }
343
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100344 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700345 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700346 return transform;
347 }
348
349 void setTouchAffineTransformation(const TouchAffineTransformation t) {
350 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800351 }
352
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000353 PointerCaptureRequest setPointerCapture(bool enabled) {
354 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
355 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800356 }
357
Arthur Hung7c645402019-01-25 17:45:42 +0800358 void setShowTouches(bool enabled) {
359 mConfig.showTouches = enabled;
360 }
361
Garfield Tan888a6a42020-01-09 11:39:16 -0800362 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
363 mConfig.defaultPointerDisplayId = pointerDisplayId;
364 }
365
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800366 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
367
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000369 uint32_t mNextPointerCaptureSequenceNumber = 0;
370
Chris Yea52ade12020-08-27 16:49:20 -0700371 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 *outConfig = mConfig;
373 }
374
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000375 std::shared_ptr<PointerControllerInterface> obtainPointerController(
376 int32_t /*deviceId*/) override {
377 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 }
379
Chris Yea52ade12020-08-27 16:49:20 -0700380 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700381 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700383 mInputDevicesChanged = true;
384 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 }
386
Chris Yea52ade12020-08-27 16:49:20 -0700387 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
388 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700389 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390 }
391
Chris Yea52ade12020-08-27 16:49:20 -0700392 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800393
394 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
395 std::unique_lock<std::mutex> lock(mLock);
396 base::ScopedLockAssertion assumeLocked(mLock);
397
398 const bool devicesChanged =
399 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
400 return mInputDevicesChanged;
401 });
402 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
403 mInputDevicesChanged = false;
404 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405};
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407// --- FakeEventHub ---
408
409class FakeEventHub : public EventHubInterface {
410 struct KeyInfo {
411 int32_t keyCode;
412 uint32_t flags;
413 };
414
Chris Yef59a2f42020-10-16 12:55:26 -0700415 struct SensorInfo {
416 InputDeviceSensorType sensorType;
417 int32_t sensorDataIndex;
418 };
419
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 struct Device {
421 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700422 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 PropertyMap configuration;
424 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
425 KeyedVector<int, bool> relativeAxes;
426 KeyedVector<int32_t, int32_t> keyCodeStates;
427 KeyedVector<int32_t, int32_t> scanCodeStates;
428 KeyedVector<int32_t, int32_t> switchStates;
429 KeyedVector<int32_t, int32_t> absoluteAxisValue;
430 KeyedVector<int32_t, KeyInfo> keysByScanCode;
431 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
432 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700433 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
434 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800435 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700436 bool enabled;
437
438 status_t enable() {
439 enabled = true;
440 return OK;
441 }
442
443 status_t disable() {
444 enabled = false;
445 return OK;
446 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447
Chris Ye1b0c7342020-07-28 21:57:03 -0700448 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449 };
450
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700451 std::mutex mLock;
452 std::condition_variable mEventsCondition;
453
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100455 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000456 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600457 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000458 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800459 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
460 // Simulates a device light brightness, from light id to light brightness.
461 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
462 // Simulates a device light intensities, from light id to light intensities map.
463 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
464 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700466public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 virtual ~FakeEventHub() {
468 for (size_t i = 0; i < mDevices.size(); i++) {
469 delete mDevices.valueAt(i);
470 }
471 }
472
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 FakeEventHub() { }
474
Chris Ye1b0c7342020-07-28 21:57:03 -0700475 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476 Device* device = new Device(classes);
477 device->identifier.name = name;
478 mDevices.add(deviceId, device);
479
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000480 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481 }
482
483 void removeDevice(int32_t deviceId) {
484 delete mDevices.valueFor(deviceId);
485 mDevices.removeItem(deviceId);
486
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000487 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 }
489
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700490 bool isDeviceEnabled(int32_t deviceId) {
491 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700492 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700493 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
494 return false;
495 }
496 return device->enabled;
497 }
498
499 status_t enableDevice(int32_t deviceId) {
500 status_t result;
501 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700502 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700503 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
504 return BAD_VALUE;
505 }
506 if (device->enabled) {
507 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
508 return OK;
509 }
510 result = device->enable();
511 return result;
512 }
513
514 status_t disableDevice(int32_t deviceId) {
515 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700516 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700517 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
518 return BAD_VALUE;
519 }
520 if (!device->enabled) {
521 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
522 return OK;
523 }
524 return device->disable();
525 }
526
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000528 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 }
530
531 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
532 Device* device = getDevice(deviceId);
533 device->configuration.addProperty(key, value);
534 }
535
536 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
537 Device* device = getDevice(deviceId);
538 device->configuration.addAll(configuration);
539 }
540
541 void addAbsoluteAxis(int32_t deviceId, int axis,
542 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
543 Device* device = getDevice(deviceId);
544
545 RawAbsoluteAxisInfo info;
546 info.valid = true;
547 info.minValue = minValue;
548 info.maxValue = maxValue;
549 info.flat = flat;
550 info.fuzz = fuzz;
551 info.resolution = resolution;
552 device->absoluteAxes.add(axis, info);
553 }
554
555 void addRelativeAxis(int32_t deviceId, int32_t axis) {
556 Device* device = getDevice(deviceId);
557 device->relativeAxes.add(axis, true);
558 }
559
560 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
561 Device* device = getDevice(deviceId);
562 device->keyCodeStates.replaceValueFor(keyCode, state);
563 }
564
565 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
566 Device* device = getDevice(deviceId);
567 device->scanCodeStates.replaceValueFor(scanCode, state);
568 }
569
570 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
571 Device* device = getDevice(deviceId);
572 device->switchStates.replaceValueFor(switchCode, state);
573 }
574
575 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
576 Device* device = getDevice(deviceId);
577 device->absoluteAxisValue.replaceValueFor(axis, value);
578 }
579
580 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
581 int32_t keyCode, uint32_t flags) {
582 Device* device = getDevice(deviceId);
583 KeyInfo info;
584 info.keyCode = keyCode;
585 info.flags = flags;
586 if (scanCode) {
587 device->keysByScanCode.add(scanCode, info);
588 }
589 if (usageCode) {
590 device->keysByUsageCode.add(usageCode, info);
591 }
592 }
593
594 void addLed(int32_t deviceId, int32_t led, bool initialState) {
595 Device* device = getDevice(deviceId);
596 device->leds.add(led, initialState);
597 }
598
Chris Yef59a2f42020-10-16 12:55:26 -0700599 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
600 int32_t sensorDataIndex) {
601 Device* device = getDevice(deviceId);
602 SensorInfo info;
603 info.sensorType = sensorType;
604 info.sensorDataIndex = sensorDataIndex;
605 device->sensorsByAbsCode.emplace(absCode, info);
606 }
607
608 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
609 Device* device = getDevice(deviceId);
610 typename BitArray<MSC_MAX>::Buffer buffer;
611 buffer[mscEvent / 32] = 1 << mscEvent % 32;
612 device->mscBitmask.loadFromBuffer(buffer);
613 }
614
Chris Ye3fdbfef2021-01-06 18:45:18 -0800615 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
616 mRawLightInfos.emplace(rawId, std::move(info));
617 }
618
619 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
620 mLightBrightness.emplace(rawId, brightness);
621 }
622
623 void fakeLightIntensities(int32_t rawId,
624 const std::unordered_map<LightColor, int32_t> intensities) {
625 mLightIntensities.emplace(rawId, std::move(intensities));
626 }
627
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 bool getLedState(int32_t deviceId, int32_t led) {
629 Device* device = getDevice(deviceId);
630 return device->leds.valueFor(led);
631 }
632
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100633 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 return mExcludedDevices;
635 }
636
637 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
638 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800639 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 }
641
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000642 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
643 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700644 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 RawEvent event;
646 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000647 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 event.deviceId = deviceId;
649 event.type = type;
650 event.code = code;
651 event.value = value;
652 mEvents.push_back(event);
653
654 if (type == EV_ABS) {
655 setAbsoluteAxisValue(deviceId, code, value);
656 }
657 }
658
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600659 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
660 std::vector<TouchVideoFrame>> videoFrames) {
661 mVideoFrames = std::move(videoFrames);
662 }
663
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700665 std::unique_lock<std::mutex> lock(mLock);
666 base::ScopedLockAssertion assumeLocked(mLock);
667 const bool queueIsEmpty =
668 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
669 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
670 if (!queueIsEmpty) {
671 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
672 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673 }
674
675private:
676 Device* getDevice(int32_t deviceId) const {
677 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100678 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 }
680
Chris Yea52ade12020-08-27 16:49:20 -0700681 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700683 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 }
685
Chris Yea52ade12020-08-27 16:49:20 -0700686 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 Device* device = getDevice(deviceId);
688 return device ? device->identifier : InputDeviceIdentifier();
689 }
690
Chris Yea52ade12020-08-27 16:49:20 -0700691 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692
Chris Yea52ade12020-08-27 16:49:20 -0700693 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 Device* device = getDevice(deviceId);
695 if (device) {
696 *outConfiguration = device->configuration;
697 }
698 }
699
Chris Yea52ade12020-08-27 16:49:20 -0700700 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
701 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800703 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704 ssize_t index = device->absoluteAxes.indexOfKey(axis);
705 if (index >= 0) {
706 *outAxisInfo = device->absoluteAxes.valueAt(index);
707 return OK;
708 }
709 }
710 outAxisInfo->clear();
711 return -1;
712 }
713
Chris Yea52ade12020-08-27 16:49:20 -0700714 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715 Device* device = getDevice(deviceId);
716 if (device) {
717 return device->relativeAxes.indexOfKey(axis) >= 0;
718 }
719 return false;
720 }
721
Chris Yea52ade12020-08-27 16:49:20 -0700722 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723
Chris Yef59a2f42020-10-16 12:55:26 -0700724 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
725 Device* device = getDevice(deviceId);
726 if (device) {
727 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
728 }
729 return false;
730 }
731
Chris Yea52ade12020-08-27 16:49:20 -0700732 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
733 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 Device* device = getDevice(deviceId);
735 if (device) {
736 const KeyInfo* key = getKey(device, scanCode, usageCode);
737 if (key) {
738 if (outKeycode) {
739 *outKeycode = key->keyCode;
740 }
741 if (outFlags) {
742 *outFlags = key->flags;
743 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700744 if (outMetaState) {
745 *outMetaState = metaState;
746 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 return OK;
748 }
749 }
750 return NAME_NOT_FOUND;
751 }
752
753 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
754 if (usageCode) {
755 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
756 if (index >= 0) {
757 return &device->keysByUsageCode.valueAt(index);
758 }
759 }
760 if (scanCode) {
761 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
762 if (index >= 0) {
763 return &device->keysByScanCode.valueAt(index);
764 }
765 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700766 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770
Chris Yef59a2f42020-10-16 12:55:26 -0700771 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
772 int32_t absCode) {
773 Device* device = getDevice(deviceId);
774 if (!device) {
775 return Errorf("Sensor device not found.");
776 }
777 auto it = device->sensorsByAbsCode.find(absCode);
778 if (it == device->sensorsByAbsCode.end()) {
779 return Errorf("Sensor map not found.");
780 }
781 const SensorInfo& info = it->second;
782 return std::make_pair(info.sensorType, info.sensorDataIndex);
783 }
784
Chris Yea52ade12020-08-27 16:49:20 -0700785 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800786 mExcludedDevices = devices;
787 }
788
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000789 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
790 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800791
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000792 const size_t filledSize = std::min(mEvents.size(), bufferSize);
793 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
794
795 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700796 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000797 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 }
799
Chris Yea52ade12020-08-27 16:49:20 -0700800 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600801 auto it = mVideoFrames.find(deviceId);
802 if (it != mVideoFrames.end()) {
803 std::vector<TouchVideoFrame> frames = std::move(it->second);
804 mVideoFrames.erase(deviceId);
805 return frames;
806 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800807 return {};
808 }
809
Chris Yea52ade12020-08-27 16:49:20 -0700810 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811 Device* device = getDevice(deviceId);
812 if (device) {
813 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
814 if (index >= 0) {
815 return device->scanCodeStates.valueAt(index);
816 }
817 }
818 return AKEY_STATE_UNKNOWN;
819 }
820
Chris Yea52ade12020-08-27 16:49:20 -0700821 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822 Device* device = getDevice(deviceId);
823 if (device) {
824 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
825 if (index >= 0) {
826 return device->keyCodeStates.valueAt(index);
827 }
828 }
829 return AKEY_STATE_UNKNOWN;
830 }
831
Chris Yea52ade12020-08-27 16:49:20 -0700832 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833 Device* device = getDevice(deviceId);
834 if (device) {
835 ssize_t index = device->switchStates.indexOfKey(sw);
836 if (index >= 0) {
837 return device->switchStates.valueAt(index);
838 }
839 }
840 return AKEY_STATE_UNKNOWN;
841 }
842
Chris Yea52ade12020-08-27 16:49:20 -0700843 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
844 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 Device* device = getDevice(deviceId);
846 if (device) {
847 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
848 if (index >= 0) {
849 *outValue = device->absoluteAxisValue.valueAt(index);
850 return OK;
851 }
852 }
853 *outValue = 0;
854 return -1;
855 }
856
Chris Yea52ade12020-08-27 16:49:20 -0700857 // Return true if the device has non-empty key layout.
858 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
859 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860 bool result = false;
861 Device* device = getDevice(deviceId);
862 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700863 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864 for (size_t i = 0; i < numCodes; i++) {
865 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
866 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
867 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 }
869 }
870 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
871 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
872 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 }
874 }
875 }
876 }
877 return result;
878 }
879
Chris Yea52ade12020-08-27 16:49:20 -0700880 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 Device* device = getDevice(deviceId);
882 if (device) {
883 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
884 return index >= 0;
885 }
886 return false;
887 }
888
Arthur Hungcb40a002021-08-03 14:31:01 +0000889 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
890 Device* device = getDevice(deviceId);
891 if (!device) {
892 return false;
893 }
894 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
895 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
896 return true;
897 }
898 }
899 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
900 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
901 return true;
902 }
903 }
904 return false;
905 }
906
Chris Yea52ade12020-08-27 16:49:20 -0700907 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908 Device* device = getDevice(deviceId);
909 return device && device->leds.indexOfKey(led) >= 0;
910 }
911
Chris Yea52ade12020-08-27 16:49:20 -0700912 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 Device* device = getDevice(deviceId);
914 if (device) {
915 ssize_t index = device->leds.indexOfKey(led);
916 if (index >= 0) {
917 device->leds.replaceValueAt(led, on);
918 } else {
919 ADD_FAILURE()
920 << "Attempted to set the state of an LED that the EventHub declared "
921 "was not present. led=" << led;
922 }
923 }
924 }
925
Chris Yea52ade12020-08-27 16:49:20 -0700926 void getVirtualKeyDefinitions(
927 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 outVirtualKeys.clear();
929
930 Device* device = getDevice(deviceId);
931 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800932 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 }
934 }
935
Chris Yea52ade12020-08-27 16:49:20 -0700936 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700937 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 }
939
Chris Yea52ade12020-08-27 16:49:20 -0700940 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 return false;
942 }
943
Chris Yea52ade12020-08-27 16:49:20 -0700944 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945
Chris Yea52ade12020-08-27 16:49:20 -0700946 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947
Chris Ye87143712020-11-10 05:05:58 +0000948 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
949
Chris Yee2b1e5c2021-03-10 22:45:12 -0800950 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
951 return BATTERY_CAPACITY;
952 }
Kim Low03ea0352020-11-06 12:45:07 -0800953
Chris Yee2b1e5c2021-03-10 22:45:12 -0800954 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
955 return BATTERY_STATUS;
956 }
957
958 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
959
960 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
961 return std::nullopt;
962 }
Kim Low03ea0352020-11-06 12:45:07 -0800963
Chris Ye3fdbfef2021-01-06 18:45:18 -0800964 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
965 std::vector<int32_t> ids;
966 for (const auto& [rawId, info] : mRawLightInfos) {
967 ids.push_back(rawId);
968 }
969 return ids;
970 }
971
972 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
973 auto it = mRawLightInfos.find(lightId);
974 if (it == mRawLightInfos.end()) {
975 return std::nullopt;
976 }
977 return it->second;
978 }
979
980 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
981 mLightBrightness.emplace(lightId, brightness);
982 }
983
984 void setLightIntensities(int32_t deviceId, int32_t lightId,
985 std::unordered_map<LightColor, int32_t> intensities) override {
986 mLightIntensities.emplace(lightId, intensities);
987 };
988
989 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
990 auto lightIt = mLightBrightness.find(lightId);
991 if (lightIt == mLightBrightness.end()) {
992 return std::nullopt;
993 }
994 return lightIt->second;
995 }
996
997 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
998 int32_t deviceId, int32_t lightId) override {
999 auto lightIt = mLightIntensities.find(lightId);
1000 if (lightIt == mLightIntensities.end()) {
1001 return std::nullopt;
1002 }
1003 return lightIt->second;
1004 };
1005
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001006 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007 return false;
1008 }
1009
Chris Yea52ade12020-08-27 16:49:20 -07001010 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011
Chris Yea52ade12020-08-27 16:49:20 -07001012 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013
Chris Yea52ade12020-08-27 16:49:20 -07001014 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015
Chris Yea52ade12020-08-27 16:49:20 -07001016 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017};
1018
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019// --- FakeInputMapper ---
1020
1021class FakeInputMapper : public InputMapper {
1022 uint32_t mSources;
1023 int32_t mKeyboardType;
1024 int32_t mMetaState;
1025 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1026 KeyedVector<int32_t, int32_t> mScanCodeStates;
1027 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001028 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001030 std::mutex mLock;
1031 std::condition_variable mStateChangedCondition;
1032 bool mConfigureWasCalled GUARDED_BY(mLock);
1033 bool mResetWasCalled GUARDED_BY(mLock);
1034 bool mProcessWasCalled GUARDED_BY(mLock);
1035 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036
Arthur Hungc23540e2018-11-29 20:42:11 +08001037 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001039 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1040 : InputMapper(deviceContext),
1041 mSources(sources),
1042 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001044 mConfigureWasCalled(false),
1045 mResetWasCalled(false),
1046 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
1050 void setKeyboardType(int32_t keyboardType) {
1051 mKeyboardType = keyboardType;
1052 }
1053
1054 void setMetaState(int32_t metaState) {
1055 mMetaState = metaState;
1056 }
1057
1058 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001059 std::unique_lock<std::mutex> lock(mLock);
1060 base::ScopedLockAssertion assumeLocked(mLock);
1061 const bool configureCalled =
1062 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1063 return mConfigureWasCalled;
1064 });
1065 if (!configureCalled) {
1066 FAIL() << "Expected configure() to have been called.";
1067 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068 mConfigureWasCalled = false;
1069 }
1070
1071 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001072 std::unique_lock<std::mutex> lock(mLock);
1073 base::ScopedLockAssertion assumeLocked(mLock);
1074 const bool resetCalled =
1075 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1076 return mResetWasCalled;
1077 });
1078 if (!resetCalled) {
1079 FAIL() << "Expected reset() to have been called.";
1080 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 mResetWasCalled = false;
1082 }
1083
Yi Kong9b14ac62018-07-17 13:48:38 -07001084 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001085 std::unique_lock<std::mutex> lock(mLock);
1086 base::ScopedLockAssertion assumeLocked(mLock);
1087 const bool processCalled =
1088 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1089 return mProcessWasCalled;
1090 });
1091 if (!processCalled) {
1092 FAIL() << "Expected process() to have been called.";
1093 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094 if (outLastEvent) {
1095 *outLastEvent = mLastEvent;
1096 }
1097 mProcessWasCalled = false;
1098 }
1099
1100 void setKeyCodeState(int32_t keyCode, int32_t state) {
1101 mKeyCodeStates.replaceValueFor(keyCode, state);
1102 }
1103
1104 void setScanCodeState(int32_t scanCode, int32_t state) {
1105 mScanCodeStates.replaceValueFor(scanCode, state);
1106 }
1107
1108 void setSwitchState(int32_t switchCode, int32_t state) {
1109 mSwitchStates.replaceValueFor(switchCode, state);
1110 }
1111
1112 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001113 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114 }
1115
1116private:
Chris Yea52ade12020-08-27 16:49:20 -07001117 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118
Chris Yea52ade12020-08-27 16:49:20 -07001119 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 InputMapper::populateDeviceInfo(deviceInfo);
1121
1122 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1123 deviceInfo->setKeyboardType(mKeyboardType);
1124 }
1125 }
1126
Chris Yea52ade12020-08-27 16:49:20 -07001127 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001128 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001130
1131 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001132 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001133 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1134 mViewport = config->getDisplayViewportByPort(*displayPort);
1135 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001136
1137 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 }
1139
Chris Yea52ade12020-08-27 16:49:20 -07001140 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001141 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001143 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 }
1145
Chris Yea52ade12020-08-27 16:49:20 -07001146 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001147 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 mLastEvent = *rawEvent;
1149 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001150 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 }
1152
Chris Yea52ade12020-08-27 16:49:20 -07001153 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1155 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1156 }
1157
Chris Yea52ade12020-08-27 16:49:20 -07001158 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1160 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1161 }
1162
Chris Yea52ade12020-08-27 16:49:20 -07001163 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1165 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1166 }
1167
Chris Yea52ade12020-08-27 16:49:20 -07001168 // Return true if the device has non-empty key layout.
1169 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1170 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 for (size_t i = 0; i < numCodes; i++) {
1172 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1173 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1174 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175 }
1176 }
1177 }
Chris Yea52ade12020-08-27 16:49:20 -07001178 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179 return result;
1180 }
1181
1182 virtual int32_t getMetaState() {
1183 return mMetaState;
1184 }
1185
1186 virtual void fadePointer() {
1187 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001188
1189 virtual std::optional<int32_t> getAssociatedDisplay() {
1190 if (mViewport) {
1191 return std::make_optional(mViewport->displayId);
1192 }
1193 return std::nullopt;
1194 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195};
1196
1197
1198// --- InstrumentedInputReader ---
1199
1200class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001201 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202
1203public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001204 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1205 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001206 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001207 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001209 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001211 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001213 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001214 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 InputDeviceIdentifier identifier;
1216 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001217 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001219 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001220 }
1221
Prabir Pradhan28efc192019-11-05 01:10:04 +00001222 // Make the protected loopOnce method accessible to tests.
1223 using InputReader::loopOnce;
1224
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001226 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1227 const InputDeviceIdentifier& identifier)
1228 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001229 if (!mNextDevices.empty()) {
1230 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1231 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001232 return device;
1233 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001234 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 }
1236
arthurhungdcef2dc2020-08-11 14:47:50 +08001237 // --- FakeInputReaderContext ---
1238 class FakeInputReaderContext : public ContextImpl {
1239 int32_t mGlobalMetaState;
1240 bool mUpdateGlobalMetaStateWasCalled;
1241 int32_t mGeneration;
1242
1243 public:
1244 FakeInputReaderContext(InputReader* reader)
1245 : ContextImpl(reader),
1246 mGlobalMetaState(0),
1247 mUpdateGlobalMetaStateWasCalled(false),
1248 mGeneration(1) {}
1249
1250 virtual ~FakeInputReaderContext() {}
1251
1252 void assertUpdateGlobalMetaStateWasCalled() {
1253 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1254 << "Expected updateGlobalMetaState() to have been called.";
1255 mUpdateGlobalMetaStateWasCalled = false;
1256 }
1257
1258 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1259
1260 uint32_t getGeneration() { return mGeneration; }
1261
1262 void updateGlobalMetaState() override {
1263 mUpdateGlobalMetaStateWasCalled = true;
1264 ContextImpl::updateGlobalMetaState();
1265 }
1266
1267 int32_t getGlobalMetaState() override {
1268 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1269 }
1270
1271 int32_t bumpGeneration() override {
1272 mGeneration = ContextImpl::bumpGeneration();
1273 return mGeneration;
1274 }
1275 } mFakeContext;
1276
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001278
1279public:
1280 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281};
1282
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001283// --- InputReaderPolicyTest ---
1284class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001285protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001286 sp<FakeInputReaderPolicy> mFakePolicy;
1287
Chris Yea52ade12020-08-27 16:49:20 -07001288 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1289 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001290};
1291
1292/**
1293 * Check that empty set of viewports is an acceptable configuration.
1294 * Also try to get internal viewport two different ways - by type and by uniqueId.
1295 *
1296 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1297 * Such configuration is not currently allowed.
1298 */
1299TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001300 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001301
1302 // We didn't add any viewports yet, so there shouldn't be any.
1303 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001304 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001305 ASSERT_FALSE(internalViewport);
1306
1307 // Add an internal viewport, then clear it
1308 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001309 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001310 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001311
1312 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001313 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001314 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001315 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001316
1317 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001318 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001319 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001320 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001321
1322 mFakePolicy->clearViewports();
1323 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001324 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001325 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001326 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001327 ASSERT_FALSE(internalViewport);
1328}
1329
1330TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1331 const std::string internalUniqueId = "local:0";
1332 const std::string externalUniqueId = "local:1";
1333 const std::string virtualUniqueId1 = "virtual:2";
1334 const std::string virtualUniqueId2 = "virtual:3";
1335 constexpr int32_t virtualDisplayId1 = 2;
1336 constexpr int32_t virtualDisplayId2 = 3;
1337
1338 // Add an internal viewport
1339 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001340 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1341 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001342 // Add an external viewport
1343 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001344 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1345 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346 // Add an virtual viewport
1347 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001348 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1349 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001350 // Add another virtual viewport
1351 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001352 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1353 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354
1355 // Check matching by type for internal
1356 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001357 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358 ASSERT_TRUE(internalViewport);
1359 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1360
1361 // Check matching by type for external
1362 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001363 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364 ASSERT_TRUE(externalViewport);
1365 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1366
1367 // Check matching by uniqueId for virtual viewport #1
1368 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001369 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001371 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1373 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1374
1375 // Check matching by uniqueId for virtual viewport #2
1376 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001377 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001378 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001379 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001380 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1381 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1382}
1383
1384
1385/**
1386 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1387 * that lookup works by checking display id.
1388 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1389 */
1390TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1391 const std::string uniqueId1 = "uniqueId1";
1392 const std::string uniqueId2 = "uniqueId2";
1393 constexpr int32_t displayId1 = 2;
1394 constexpr int32_t displayId2 = 3;
1395
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001396 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1397 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001398 for (const ViewportType& type : types) {
1399 mFakePolicy->clearViewports();
1400 // Add a viewport
1401 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001402 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1403 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001404 // Add another viewport
1405 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001406 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1407 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001408
1409 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001410 std::optional<DisplayViewport> viewport1 =
1411 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412 ASSERT_TRUE(viewport1);
1413 ASSERT_EQ(displayId1, viewport1->displayId);
1414 ASSERT_EQ(type, viewport1->type);
1415
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001416 std::optional<DisplayViewport> viewport2 =
1417 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 ASSERT_TRUE(viewport2);
1419 ASSERT_EQ(displayId2, viewport2->displayId);
1420 ASSERT_EQ(type, viewport2->type);
1421
1422 // When there are multiple viewports of the same kind, and uniqueId is not specified
1423 // in the call to getDisplayViewport, then that situation is not supported.
1424 // The viewports can be stored in any order, so we cannot rely on the order, since that
1425 // is just implementation detail.
1426 // However, we can check that it still returns *a* viewport, we just cannot assert
1427 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001428 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429 ASSERT_TRUE(someViewport);
1430 }
1431}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001432
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001433/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001434 * When we have multiple internal displays make sure we always return the default display when
1435 * querying by type.
1436 */
1437TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1438 const std::string uniqueId1 = "uniqueId1";
1439 const std::string uniqueId2 = "uniqueId2";
1440 constexpr int32_t nonDefaultDisplayId = 2;
1441 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1442 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1443
1444 // Add the default display first and ensure it gets returned.
1445 mFakePolicy->clearViewports();
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 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001450 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001451 ViewportType::INTERNAL);
1452
1453 std::optional<DisplayViewport> viewport =
1454 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1455 ASSERT_TRUE(viewport);
1456 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1457 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1458
1459 // Add the default display second to make sure order doesn't matter.
1460 mFakePolicy->clearViewports();
1461 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001462 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001463 ViewportType::INTERNAL);
1464 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001465 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001466 ViewportType::INTERNAL);
1467
1468 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1469 ASSERT_TRUE(viewport);
1470 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1471 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1472}
1473
1474/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001475 * Check getDisplayViewportByPort
1476 */
1477TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001478 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479 const std::string uniqueId1 = "uniqueId1";
1480 const std::string uniqueId2 = "uniqueId2";
1481 constexpr int32_t displayId1 = 1;
1482 constexpr int32_t displayId2 = 2;
1483 const uint8_t hdmi1 = 0;
1484 const uint8_t hdmi2 = 1;
1485 const uint8_t hdmi3 = 2;
1486
1487 mFakePolicy->clearViewports();
1488 // Add a viewport that's associated with some display port that's not of interest.
1489 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001490 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1491 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001492 // Add another viewport, connected to HDMI1 port
1493 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001494 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1495 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001496
1497 // Check that correct display viewport was returned by comparing the display ports.
1498 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1499 ASSERT_TRUE(hdmi1Viewport);
1500 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1501 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1502
1503 // Check that we can still get the same viewport using the uniqueId
1504 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1505 ASSERT_TRUE(hdmi1Viewport);
1506 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1507 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1508 ASSERT_EQ(type, hdmi1Viewport->type);
1509
1510 // Check that we cannot find a port with "HDMI2", because we never added one
1511 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1512 ASSERT_FALSE(hdmi2Viewport);
1513}
1514
Michael Wrightd02c5b62014-02-10 15:10:22 -08001515// --- InputReaderTest ---
1516
1517class InputReaderTest : public testing::Test {
1518protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001519 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001520 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001521 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001522 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001523
Chris Yea52ade12020-08-27 16:49:20 -07001524 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001525 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001527 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528
Prabir Pradhan28efc192019-11-05 01:10:04 +00001529 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001530 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001531 }
1532
Chris Yea52ade12020-08-27 16:49:20 -07001533 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001534 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536 }
1537
Chris Ye1b0c7342020-07-28 21:57:03 -07001538 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001539 const PropertyMap* configuration) {
1540 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541
1542 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001543 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544 }
1545 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001546 mReader->loopOnce();
1547 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001548 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1549 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001550 }
1551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001552 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001553 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001554 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001555 }
1556
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001557 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001558 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001559 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001560 }
1561
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001562 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001563 const std::string& name,
1564 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001565 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001566 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1567 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001568 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001569 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570 return mapper;
1571 }
1572};
1573
Chris Ye98d3f532020-10-01 21:48:59 -07001574TEST_F(InputReaderTest, PolicyGetInputDevices) {
1575 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1576 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1577 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578
1579 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001580 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001581 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001582 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001583 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1585 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001586 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587}
1588
Chris Yee7310032020-09-22 15:36:28 -07001589TEST_F(InputReaderTest, GetMergedInputDevices) {
1590 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1591 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1592 // Add two subdevices to device
1593 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1594 // Must add at least one mapper or the device will be ignored!
1595 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1596 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1597
1598 // Push same device instance for next device to be added, so they'll have same identifier.
1599 mReader->pushNextDevice(device);
1600 mReader->pushNextDevice(device);
1601 ASSERT_NO_FATAL_FAILURE(
1602 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1603 ASSERT_NO_FATAL_FAILURE(
1604 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1605
1606 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001607 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001608}
1609
Chris Yee14523a2020-12-19 13:46:00 -08001610TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1611 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1612 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1613 // Add two subdevices to device
1614 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1615 // Must add at least one mapper or the device will be ignored!
1616 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1617 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1618
1619 // Push same device instance for next device to be added, so they'll have same identifier.
1620 mReader->pushNextDevice(device);
1621 mReader->pushNextDevice(device);
1622 // Sensor device is initially disabled
1623 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1624 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1625 nullptr));
1626 // Device is disabled because the only sub device is a sensor device and disabled initially.
1627 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1628 ASSERT_FALSE(device->isEnabled());
1629 ASSERT_NO_FATAL_FAILURE(
1630 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1631 // The merged device is enabled if any sub device is enabled
1632 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1633 ASSERT_TRUE(device->isEnabled());
1634}
1635
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001636TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001638 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 constexpr int32_t eventHubId = 1;
1640 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001641 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001643 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001645
Yi Kong9b14ac62018-07-17 13:48:38 -07001646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001647
1648 NotifyDeviceResetArgs resetArgs;
1649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001650 ASSERT_EQ(deviceId, resetArgs.deviceId);
1651
1652 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001653 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001654 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001655
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001657 ASSERT_EQ(deviceId, resetArgs.deviceId);
1658 ASSERT_EQ(device->isEnabled(), false);
1659
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001660 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001661 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001664 ASSERT_EQ(device->isEnabled(), false);
1665
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001666 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001667 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001669 ASSERT_EQ(deviceId, resetArgs.deviceId);
1670 ASSERT_EQ(device->isEnabled(), true);
1671}
1672
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001674 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001675 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001677 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001679 AINPUT_SOURCE_KEYBOARD, nullptr);
1680 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681
1682 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1683 AINPUT_SOURCE_ANY, AKEYCODE_A))
1684 << "Should return unknown when the device id is >= 0 but unknown.";
1685
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1687 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1688 << "Should return unknown when the device id is valid but the sources are not "
1689 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 ASSERT_EQ(AKEY_STATE_DOWN,
1692 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1693 AKEYCODE_A))
1694 << "Should return value provided by mapper when device id is valid and the device "
1695 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696
1697 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1698 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1699 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1700
1701 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1702 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1703 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1704}
1705
1706TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001708 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001712 AINPUT_SOURCE_KEYBOARD, nullptr);
1713 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714
1715 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1716 AINPUT_SOURCE_ANY, KEY_A))
1717 << "Should return unknown when the device id is >= 0 but unknown.";
1718
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001719 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1720 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1721 << "Should return unknown when the device id is valid but the sources are not "
1722 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001723
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 ASSERT_EQ(AKEY_STATE_DOWN,
1725 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1726 KEY_A))
1727 << "Should return value provided by mapper when device id is valid and the device "
1728 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
1730 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1731 AINPUT_SOURCE_TRACKBALL, KEY_A))
1732 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1733
1734 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1735 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1736 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1737}
1738
1739TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001741 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 AINPUT_SOURCE_KEYBOARD, nullptr);
1746 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001747
1748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1749 AINPUT_SOURCE_ANY, SW_LID))
1750 << "Should return unknown when the device id is >= 0 but unknown.";
1751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001752 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1753 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1754 << "Should return unknown when the device id is valid but the sources are not "
1755 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001757 ASSERT_EQ(AKEY_STATE_DOWN,
1758 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1759 SW_LID))
1760 << "Should return value provided by mapper when device id is valid and the device "
1761 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001762
1763 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1764 AINPUT_SOURCE_TRACKBALL, SW_LID))
1765 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1766
1767 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1768 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1769 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1770}
1771
1772TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001773 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001774 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001775 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001776 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001778 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001780 mapper.addSupportedKeyCode(AKEYCODE_A);
1781 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001782
1783 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1784 uint8_t flags[4] = { 0, 0, 0, 1 };
1785
1786 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1787 << "Should return false when device id is >= 0 but unknown.";
1788 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1789
1790 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001791 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1792 << "Should return false when device id is valid but the sources are not supported by "
1793 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001794 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1795
1796 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001797 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1798 keyCodes, flags))
1799 << "Should return value provided by mapper when device id is valid and the device "
1800 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1802
1803 flags[3] = 1;
1804 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1805 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1806 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1807
1808 flags[3] = 1;
1809 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1810 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1811 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1812}
1813
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001814TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001816 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001817
1818 NotifyConfigurationChangedArgs args;
1819
1820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1821 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1822}
1823
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001824TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001825 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001826 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001827 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001828 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001829 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001830 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001831 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001832 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001833
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001834 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001835 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001836 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1837
1838 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001839 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001840 ASSERT_EQ(when, event.when);
1841 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001842 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001843 ASSERT_EQ(EV_KEY, event.type);
1844 ASSERT_EQ(KEY_A, event.code);
1845 ASSERT_EQ(1, event.value);
1846}
1847
Garfield Tan1c7bc862020-01-28 13:24:04 -08001848TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001850 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 constexpr int32_t eventHubId = 1;
1852 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001853 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001855 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001857
1858 NotifyDeviceResetArgs resetArgs;
1859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001860 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001862 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001863 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001865 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001866 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001868 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001869 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001871 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001872 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001874 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001875 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001877 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001878 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001879}
1880
Garfield Tan1c7bc862020-01-28 13:24:04 -08001881TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1882 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001883 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001884 constexpr int32_t eventHubId = 1;
1885 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1886 // Must add at least one mapper or the device will be ignored!
1887 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001888 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001889 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1890
1891 NotifyDeviceResetArgs resetArgs;
1892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1893 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1894}
1895
Arthur Hungc23540e2018-11-29 20:42:11 +08001896TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001897 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001898 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001899 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001900 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1902 FakeInputMapper& mapper =
1903 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001904 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001905
1906 const uint8_t hdmi1 = 1;
1907
1908 // Associated touch screen with second display.
1909 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1910
1911 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001912 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001913 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001914 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001915 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001916 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001917 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001918 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001919 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001920 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001921
1922 // Add the device, and make sure all of the callbacks are triggered.
1923 // The device is added after the input port associations are processed since
1924 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001925 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001928 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001929
Arthur Hung2c9a3342019-07-23 14:18:59 +08001930 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001931 ASSERT_EQ(deviceId, device->getId());
1932 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1933 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001934
1935 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001936 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001937 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001938 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001939}
1940
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001941TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1942 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1943 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1944 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1945 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1946 // Must add at least one mapper or the device will be ignored!
1947 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1948 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1949 mReader->pushNextDevice(device);
1950 mReader->pushNextDevice(device);
1951 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1952 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1953
1954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1955
1956 NotifyDeviceResetArgs resetArgs;
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 disableDevice(deviceId);
1964 mReader->loopOnce();
1965
1966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1967 ASSERT_EQ(deviceId, resetArgs.deviceId);
1968 ASSERT_FALSE(device->isEnabled());
1969 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1970 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1971
1972 enableDevice(deviceId);
1973 mReader->loopOnce();
1974
1975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1976 ASSERT_EQ(deviceId, resetArgs.deviceId);
1977 ASSERT_TRUE(device->isEnabled());
1978 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1979 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1980}
1981
1982TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1983 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1984 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1985 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1986 // Add two subdevices to device
1987 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1988 FakeInputMapper& mapperDevice1 =
1989 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1990 FakeInputMapper& mapperDevice2 =
1991 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1992 mReader->pushNextDevice(device);
1993 mReader->pushNextDevice(device);
1994 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1995 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1996
1997 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1998 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1999
2000 ASSERT_EQ(AKEY_STATE_DOWN,
2001 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2002 ASSERT_EQ(AKEY_STATE_DOWN,
2003 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2004 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2005 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2006}
2007
Prabir Pradhan7e186182020-11-10 13:56:45 -08002008TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2009 NotifyPointerCaptureChangedArgs args;
2010
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002011 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002012 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2013 mReader->loopOnce();
2014 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002015 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2016 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002017
2018 mFakePolicy->setPointerCapture(false);
2019 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2020 mReader->loopOnce();
2021 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002022 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002023
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002024 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002025 // does not change.
2026 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2027 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002028 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002029}
2030
Chris Ye87143712020-11-10 05:05:58 +00002031class FakeVibratorInputMapper : public FakeInputMapper {
2032public:
2033 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2034 : FakeInputMapper(deviceContext, sources) {}
2035
2036 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2037};
2038
2039TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2040 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2041 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2042 constexpr int32_t eventHubId = 1;
2043 const char* DEVICE_LOCATION = "BLUETOOTH";
2044 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2045 FakeVibratorInputMapper& mapper =
2046 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2047 mReader->pushNextDevice(device);
2048
2049 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2050 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2051
2052 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2053 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2054}
2055
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002056// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002057
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002058class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002059public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002060 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002061
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002062 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002063
2064 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2065
2066 void dump(std::string& dump) override {}
2067
2068 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2069 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002070 }
2071
Chris Yee2b1e5c2021-03-10 22:45:12 -08002072 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2073 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002074 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002075
2076 bool setLightColor(int32_t lightId, int32_t color) override {
2077 getDeviceContext().setLightBrightness(lightId, color >> 24);
2078 return true;
2079 }
2080
2081 std::optional<int32_t> getLightColor(int32_t lightId) override {
2082 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2083 if (!result.has_value()) {
2084 return std::nullopt;
2085 }
2086 return result.value() << 24;
2087 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002088
2089 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2090
2091 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2092
2093private:
2094 InputDeviceContext& mDeviceContext;
2095 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2096 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002097};
2098
Chris Yee2b1e5c2021-03-10 22:45:12 -08002099TEST_F(InputReaderTest, BatteryGetCapacity) {
2100 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2101 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2102 constexpr int32_t eventHubId = 1;
2103 const char* DEVICE_LOCATION = "BLUETOOTH";
2104 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002105 FakePeripheralController& controller =
2106 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002107 mReader->pushNextDevice(device);
2108
2109 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2110
2111 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2112 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2113}
2114
2115TEST_F(InputReaderTest, BatteryGetStatus) {
2116 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2117 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2118 constexpr int32_t eventHubId = 1;
2119 const char* DEVICE_LOCATION = "BLUETOOTH";
2120 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002121 FakePeripheralController& controller =
2122 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002123 mReader->pushNextDevice(device);
2124
2125 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2126
2127 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2128 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2129}
2130
Chris Ye3fdbfef2021-01-06 18:45:18 -08002131TEST_F(InputReaderTest, LightGetColor) {
2132 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2133 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2134 constexpr int32_t eventHubId = 1;
2135 const char* DEVICE_LOCATION = "BLUETOOTH";
2136 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002137 FakePeripheralController& controller =
2138 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002139 mReader->pushNextDevice(device);
2140 RawLightInfo info = {.id = 1,
2141 .name = "Mono",
2142 .maxBrightness = 255,
2143 .flags = InputLightClass::BRIGHTNESS,
2144 .path = ""};
2145 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2146 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2147
2148 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002149
Chris Yee2b1e5c2021-03-10 22:45:12 -08002150 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2151 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002152 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2153 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2154}
2155
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002156// --- InputReaderIntegrationTest ---
2157
2158// These tests create and interact with the InputReader only through its interface.
2159// The InputReader is started during SetUp(), which starts its processing in its own
2160// thread. The tests use linux uinput to emulate input devices.
2161// NOTE: Interacting with the physical device while these tests are running may cause
2162// the tests to fail.
2163class InputReaderIntegrationTest : public testing::Test {
2164protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002165 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002166 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002167 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002168
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002169 std::shared_ptr<FakePointerController> mFakePointerController;
2170
Chris Yea52ade12020-08-27 16:49:20 -07002171 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002172 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002173 mFakePointerController = std::make_shared<FakePointerController>();
2174 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002175 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2176 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002177
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002178 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2179 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002180 ASSERT_EQ(mReader->start(), OK);
2181
2182 // Since this test is run on a real device, all the input devices connected
2183 // to the test device will show up in mReader. We wait for those input devices to
2184 // show up before beginning the tests.
2185 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2186 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2187 }
2188
Chris Yea52ade12020-08-27 16:49:20 -07002189 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002190 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002191 mReader.reset();
2192 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002193 mFakePolicy.clear();
2194 }
2195};
2196
2197TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2198 // An invalid input device that is only used for this test.
2199 class InvalidUinputDevice : public UinputDevice {
2200 public:
2201 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2202
2203 private:
2204 void configureDevice(int fd, uinput_user_dev* device) override {}
2205 };
2206
2207 const size_t numDevices = mFakePolicy->getInputDevices().size();
2208
2209 // UinputDevice does not set any event or key bits, so InputReader should not
2210 // consider it as a valid device.
2211 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2212 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2213 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2214 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2215
2216 invalidDevice.reset();
2217 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2218 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2219 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2220}
2221
2222TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2223 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2224
2225 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2226 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2228 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2229
2230 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002231 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002232 const auto& it =
2233 std::find_if(inputDevices.begin(), inputDevices.end(),
2234 [&keyboard](const InputDeviceInfo& info) {
2235 return info.getIdentifier().name == keyboard->getName();
2236 });
2237
2238 ASSERT_NE(it, inputDevices.end());
2239 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2240 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2241 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002242
2243 keyboard.reset();
2244 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2245 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2246 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2247}
2248
2249TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2250 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2251 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2252
2253 NotifyConfigurationChangedArgs configChangedArgs;
2254 ASSERT_NO_FATAL_FAILURE(
2255 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002256 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002257 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2258
2259 NotifyKeyArgs keyArgs;
2260 keyboard->pressAndReleaseHomeKey();
2261 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2262 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002263 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002264 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002265 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002266 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002267 prevTimestamp = keyArgs.eventTime;
2268
2269 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2270 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002271 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002272 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002273 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002274}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002275
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002276/**
2277 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2278 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2279 * are passed to the listener.
2280 */
2281static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2282TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2283 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2284 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2285 NotifyKeyArgs keyArgs;
2286
2287 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2288 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2289 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2290 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2291
2292 controller->pressAndReleaseKey(BTN_GEAR_UP);
2293 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2295 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2296}
2297
Arthur Hungaab25622020-01-16 11:22:11 +08002298// --- TouchProcessTest ---
2299class TouchIntegrationTest : public InputReaderIntegrationTest {
2300protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002301 const std::string UNIQUE_ID = "local:0";
2302
Chris Yea52ade12020-08-27 16:49:20 -07002303 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002304 InputReaderIntegrationTest::SetUp();
2305 // At least add an internal display.
2306 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2307 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002308 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002309
2310 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2311 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2313 }
2314
2315 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2316 int32_t orientation, const std::string& uniqueId,
2317 std::optional<uint8_t> physicalPort,
2318 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002319 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2320 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002321 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2322 }
2323
2324 std::unique_ptr<UinputTouchScreen> mDevice;
2325};
2326
2327TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2328 NotifyMotionArgs args;
2329 const Point centerPoint = mDevice->getCenterPoint();
2330
2331 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002332 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002333 mDevice->sendDown(centerPoint);
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2336
2337 // ACTION_MOVE
2338 mDevice->sendMove(centerPoint + Point(1, 1));
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2341
2342 // ACTION_UP
2343 mDevice->sendUp();
2344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2345 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2346}
2347
2348TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2349 NotifyMotionArgs args;
2350 const Point centerPoint = mDevice->getCenterPoint();
2351
2352 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002353 mDevice->sendSlot(FIRST_SLOT);
2354 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002355 mDevice->sendDown(centerPoint);
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2357 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2358
2359 // ACTION_POINTER_DOWN (Second slot)
2360 const Point secondPoint = centerPoint + Point(100, 100);
2361 mDevice->sendSlot(SECOND_SLOT);
2362 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2363 mDevice->sendDown(secondPoint + Point(1, 1));
2364 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2365 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2366 args.action);
2367
2368 // ACTION_MOVE (Second slot)
2369 mDevice->sendMove(secondPoint);
2370 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2372
2373 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002374 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002376 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002377 args.action);
2378
2379 // ACTION_UP
2380 mDevice->sendSlot(FIRST_SLOT);
2381 mDevice->sendUp();
2382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2383 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2384}
2385
2386TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2387 NotifyMotionArgs args;
2388 const Point centerPoint = mDevice->getCenterPoint();
2389
2390 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002391 mDevice->sendSlot(FIRST_SLOT);
2392 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002393 mDevice->sendDown(centerPoint);
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2395 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2396
arthurhungcc7f9802020-04-30 17:55:40 +08002397 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002398 const Point secondPoint = centerPoint + Point(100, 100);
2399 mDevice->sendSlot(SECOND_SLOT);
2400 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2401 mDevice->sendDown(secondPoint);
2402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2403 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2404 args.action);
2405
arthurhungcc7f9802020-04-30 17:55:40 +08002406 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002407 mDevice->sendMove(secondPoint + Point(1, 1));
2408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2410
arthurhungcc7f9802020-04-30 17:55:40 +08002411 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2412 // a palm event.
2413 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002414 mDevice->sendToolType(MT_TOOL_PALM);
2415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002416 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2417 args.action);
2418 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002419
arthurhungcc7f9802020-04-30 17:55:40 +08002420 // Send up to second slot, expect first slot send moving.
2421 mDevice->sendPointerUp();
2422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002424
arthurhungcc7f9802020-04-30 17:55:40 +08002425 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002426 mDevice->sendSlot(FIRST_SLOT);
2427 mDevice->sendUp();
2428
arthurhungcc7f9802020-04-30 17:55:40 +08002429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2430 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002431}
2432
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434class InputDeviceTest : public testing::Test {
2435protected:
2436 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002437 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438 static const int32_t DEVICE_ID;
2439 static const int32_t DEVICE_GENERATION;
2440 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002441 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002442 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002444 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002446 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002447 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002448 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449
Chris Yea52ade12020-08-27 16:49:20 -07002450 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002451 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002453 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002454 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002455 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 InputDeviceIdentifier identifier;
2457 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002458 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002459 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002460 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002461 mReader->pushNextDevice(mDevice);
2462 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2463 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002464 }
2465
Chris Yea52ade12020-08-27 16:49:20 -07002466 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002467 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469 }
2470};
2471
2472const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002473const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002474const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2476const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002477const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2478 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002479const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480
2481TEST_F(InputDeviceTest, ImmutableProperties) {
2482 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002483 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002484 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485}
2486
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002487TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2488 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002489}
2490
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2492 // Configuration.
2493 InputReaderConfiguration config;
2494 mDevice->configure(ARBITRARY_TIME, &config, 0);
2495
2496 // Reset.
2497 mDevice->reset(ARBITRARY_TIME);
2498
2499 NotifyDeviceResetArgs resetArgs;
2500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2501 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2502 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2503
2504 // Metadata.
2505 ASSERT_TRUE(mDevice->isIgnored());
2506 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2507
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002508 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002510 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2512 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2513
2514 // State queries.
2515 ASSERT_EQ(0, mDevice->getMetaState());
2516
2517 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2518 << "Ignored device should return unknown key code state.";
2519 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2520 << "Ignored device should return unknown scan code state.";
2521 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2522 << "Ignored device should return unknown switch state.";
2523
2524 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2525 uint8_t flags[2] = { 0, 1 };
2526 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2527 << "Ignored device should never mark any key codes.";
2528 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2529 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2530}
2531
2532TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2533 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002534 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002535
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002536 FakeInputMapper& mapper1 =
2537 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002538 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2539 mapper1.setMetaState(AMETA_ALT_ON);
2540 mapper1.addSupportedKeyCode(AKEYCODE_A);
2541 mapper1.addSupportedKeyCode(AKEYCODE_B);
2542 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2543 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2544 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2545 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2546 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002548 FakeInputMapper& mapper2 =
2549 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002550 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551
2552 InputReaderConfiguration config;
2553 mDevice->configure(ARBITRARY_TIME, &config, 0);
2554
2555 String8 propertyValue;
2556 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2557 << "Device should have read configuration during configuration phase.";
2558 ASSERT_STREQ("value", propertyValue.string());
2559
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002560 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2561 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002562
2563 // Reset
2564 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002565 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2566 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567
2568 NotifyDeviceResetArgs resetArgs;
2569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2570 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2571 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2572
2573 // Metadata.
2574 ASSERT_FALSE(mDevice->isIgnored());
2575 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2576
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002577 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002578 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002579 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2581 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2582
2583 // State queries.
2584 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2585 << "Should query mappers and combine meta states.";
2586
2587 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2588 << "Should return unknown key code state when source not supported.";
2589 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2590 << "Should return unknown scan code state when source not supported.";
2591 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2592 << "Should return unknown switch state when source not supported.";
2593
2594 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2595 << "Should query mapper when source is supported.";
2596 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2597 << "Should query mapper when source is supported.";
2598 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2599 << "Should query mapper when source is supported.";
2600
2601 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2602 uint8_t flags[4] = { 0, 0, 0, 1 };
2603 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2604 << "Should do nothing when source is unsupported.";
2605 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2606 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2607 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2608 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2609
2610 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2611 << "Should query mapper when source is supported.";
2612 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2613 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2614 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2615 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2616
2617 // Event handling.
2618 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002619 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620 mDevice->process(&event, 1);
2621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002622 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2623 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624}
2625
Arthur Hung2c9a3342019-07-23 14:18:59 +08002626// A single input device is associated with a specific display. Check that:
2627// 1. Device is disabled if the viewport corresponding to the associated display is not found
2628// 2. Device is disabled when setEnabled API is called
2629TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002630 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002631
2632 // First Configuration.
2633 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2634
2635 // Device should be enabled by default.
2636 ASSERT_TRUE(mDevice->isEnabled());
2637
2638 // Prepare associated info.
2639 constexpr uint8_t hdmi = 1;
2640 const std::string UNIQUE_ID = "local:1";
2641
2642 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2643 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2644 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2645 // Device should be disabled because it is associated with a specific display via
2646 // input port <-> display port association, but the corresponding display is not found
2647 ASSERT_FALSE(mDevice->isEnabled());
2648
2649 // Prepare displays.
2650 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002651 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2652 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002653 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2654 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2655 ASSERT_TRUE(mDevice->isEnabled());
2656
2657 // Device should be disabled after set disable.
2658 mFakePolicy->addDisabledDevice(mDevice->getId());
2659 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2660 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2661 ASSERT_FALSE(mDevice->isEnabled());
2662
2663 // Device should still be disabled even found the associated display.
2664 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2665 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2666 ASSERT_FALSE(mDevice->isEnabled());
2667}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668
Christine Franks1ba71cc2021-04-07 14:37:42 -07002669TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2670 // Device should be enabled by default.
2671 mFakePolicy->clearViewports();
2672 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2673 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2674 ASSERT_TRUE(mDevice->isEnabled());
2675
2676 // Device should be disabled because it is associated with a specific display, but the
2677 // corresponding display is not found.
2678 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2679 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2680 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2681 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2682 ASSERT_FALSE(mDevice->isEnabled());
2683
2684 // Device should be enabled when a display is found.
2685 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2686 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2687 NO_PORT, ViewportType::INTERNAL);
2688 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2689 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2690 ASSERT_TRUE(mDevice->isEnabled());
2691
2692 // Device should be disabled after set disable.
2693 mFakePolicy->addDisabledDevice(mDevice->getId());
2694 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2695 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2696 ASSERT_FALSE(mDevice->isEnabled());
2697
2698 // Device should still be disabled even found the associated display.
2699 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2700 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2701 ASSERT_FALSE(mDevice->isEnabled());
2702}
2703
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704// --- InputMapperTest ---
2705
2706class InputMapperTest : public testing::Test {
2707protected:
2708 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002709 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 static const int32_t DEVICE_ID;
2711 static const int32_t DEVICE_GENERATION;
2712 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002713 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002714 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002716 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002718 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002719 std::unique_ptr<InstrumentedInputReader> mReader;
2720 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721
Chris Ye1b0c7342020-07-28 21:57:03 -07002722 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002723 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002725 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002726 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002727 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002728 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 }
2730
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002731 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002732 SetUp(DEVICE_CLASSES);
2733 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002734
Chris Yea52ade12020-08-27 16:49:20 -07002735 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002736 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738 }
2739
2740 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002741 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742 }
2743
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002744 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002745 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002746 mReader->requestRefreshConfiguration(changes);
2747 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002748 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002749 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2750 }
2751
arthurhungdcef2dc2020-08-11 14:47:50 +08002752 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2753 const std::string& location, int32_t eventHubId,
2754 Flags<InputDeviceClass> classes) {
2755 InputDeviceIdentifier identifier;
2756 identifier.name = name;
2757 identifier.location = location;
2758 std::shared_ptr<InputDevice> device =
2759 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2760 identifier);
2761 mReader->pushNextDevice(device);
2762 mFakeEventHub->addDevice(eventHubId, name, classes);
2763 mReader->loopOnce();
2764 return device;
2765 }
2766
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 template <class T, typename... Args>
2768 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002769 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002770 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002772 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002773 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774 }
2775
2776 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002777 int32_t orientation, const std::string& uniqueId,
2778 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002779 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2780 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002781 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2782 }
2783
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002784 void clearViewports() {
2785 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 }
2787
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002788 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2789 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790 RawEvent event;
2791 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002792 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794 event.type = type;
2795 event.code = code;
2796 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002797 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002798 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 }
2800
2801 static void assertMotionRange(const InputDeviceInfo& info,
2802 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2803 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002804 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2806 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2807 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2808 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2809 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2810 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2811 }
2812
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002813 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2814 float size, float touchMajor, float touchMinor, float toolMajor,
2815 float toolMinor, float orientation, float distance,
2816 float scaledAxisEpsilon = 1.f) {
2817 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2818 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2820 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002821 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2822 scaledAxisEpsilon);
2823 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2824 scaledAxisEpsilon);
2825 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2826 scaledAxisEpsilon);
2827 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2828 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2830 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2831 }
2832
Michael Wright17db18e2020-06-26 20:51:44 +01002833 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002835 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836 ASSERT_NEAR(x, actualX, 1);
2837 ASSERT_NEAR(y, actualY, 1);
2838 }
2839};
2840
2841const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002842const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002843const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2845const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002846const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2847 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002848const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849
2850// --- SwitchInputMapperTest ---
2851
2852class SwitchInputMapperTest : public InputMapperTest {
2853protected:
2854};
2855
2856TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002859 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860}
2861
2862TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002863 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002865 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002866 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002868 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002869 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870}
2871
2872TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002873 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002874
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879
2880 NotifySwitchArgs args;
2881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2882 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002883 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2884 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885 args.switchMask);
2886 ASSERT_EQ(uint32_t(0), args.policyFlags);
2887}
2888
Chris Ye87143712020-11-10 05:05:58 +00002889// --- VibratorInputMapperTest ---
2890class VibratorInputMapperTest : public InputMapperTest {
2891protected:
2892 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2893};
2894
2895TEST_F(VibratorInputMapperTest, GetSources) {
2896 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2897
2898 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2899}
2900
2901TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2902 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2903
2904 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2905}
2906
2907TEST_F(VibratorInputMapperTest, Vibrate) {
2908 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002909 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002910 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2911
2912 VibrationElement pattern(2);
2913 VibrationSequence sequence(2);
2914 pattern.duration = std::chrono::milliseconds(200);
2915 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2916 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2917 sequence.addElement(pattern);
2918 pattern.duration = std::chrono::milliseconds(500);
2919 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2920 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2921 sequence.addElement(pattern);
2922
2923 std::vector<int64_t> timings = {0, 1};
2924 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2925
2926 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002927 // Start vibrating
2928 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002929 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002930 // Verify vibrator state listener was notified.
2931 mReader->loopOnce();
2932 NotifyVibratorStateArgs args;
2933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2934 ASSERT_EQ(DEVICE_ID, args.deviceId);
2935 ASSERT_TRUE(args.isOn);
2936 // Stop vibrating
2937 mapper.cancelVibrate(VIBRATION_TOKEN);
2938 ASSERT_FALSE(mapper.isVibrating());
2939 // Verify vibrator state listener was notified.
2940 mReader->loopOnce();
2941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2942 ASSERT_EQ(DEVICE_ID, args.deviceId);
2943 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002944}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945
Chris Yef59a2f42020-10-16 12:55:26 -07002946// --- SensorInputMapperTest ---
2947
2948class SensorInputMapperTest : public InputMapperTest {
2949protected:
2950 static const int32_t ACCEL_RAW_MIN;
2951 static const int32_t ACCEL_RAW_MAX;
2952 static const int32_t ACCEL_RAW_FUZZ;
2953 static const int32_t ACCEL_RAW_FLAT;
2954 static const int32_t ACCEL_RAW_RESOLUTION;
2955
2956 static const int32_t GYRO_RAW_MIN;
2957 static const int32_t GYRO_RAW_MAX;
2958 static const int32_t GYRO_RAW_FUZZ;
2959 static const int32_t GYRO_RAW_FLAT;
2960 static const int32_t GYRO_RAW_RESOLUTION;
2961
2962 static const float GRAVITY_MS2_UNIT;
2963 static const float DEGREE_RADIAN_UNIT;
2964
2965 void prepareAccelAxes();
2966 void prepareGyroAxes();
2967 void setAccelProperties();
2968 void setGyroProperties();
2969 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2970};
2971
2972const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2973const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2974const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2975const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2976const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2977
2978const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2979const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2980const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2981const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2982const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2983
2984const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2985const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2986
2987void SensorInputMapperTest::prepareAccelAxes() {
2988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2989 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2991 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2993 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2994}
2995
2996void SensorInputMapperTest::prepareGyroAxes() {
2997 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2998 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2999 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3000 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3001 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3002 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3003}
3004
3005void SensorInputMapperTest::setAccelProperties() {
3006 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3007 /* sensorDataIndex */ 0);
3008 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3009 /* sensorDataIndex */ 1);
3010 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3011 /* sensorDataIndex */ 2);
3012 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3013 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3014 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3015 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3016 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3017}
3018
3019void SensorInputMapperTest::setGyroProperties() {
3020 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3021 /* sensorDataIndex */ 0);
3022 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3023 /* sensorDataIndex */ 1);
3024 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3025 /* sensorDataIndex */ 2);
3026 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3027 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3028 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3029 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3030 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3031}
3032
3033TEST_F(SensorInputMapperTest, GetSources) {
3034 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3035
3036 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3037}
3038
3039TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3040 setAccelProperties();
3041 prepareAccelAxes();
3042 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3043
3044 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3045 std::chrono::microseconds(10000),
3046 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003047 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003053
3054 NotifySensorArgs args;
3055 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3056 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3057 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3058
3059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3060 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3061 ASSERT_EQ(args.deviceId, DEVICE_ID);
3062 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3063 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3064 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3065 ASSERT_EQ(args.values, values);
3066 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3067}
3068
3069TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3070 setGyroProperties();
3071 prepareGyroAxes();
3072 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3073
3074 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3075 std::chrono::microseconds(10000),
3076 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003077 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003083
3084 NotifySensorArgs args;
3085 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3086 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3087 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3088
3089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3090 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3091 ASSERT_EQ(args.deviceId, DEVICE_ID);
3092 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3093 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3094 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3095 ASSERT_EQ(args.values, values);
3096 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3097}
3098
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099// --- KeyboardInputMapperTest ---
3100
3101class KeyboardInputMapperTest : public InputMapperTest {
3102protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003103 const std::string UNIQUE_ID = "local:0";
3104
3105 void prepareDisplay(int32_t orientation);
3106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003107 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003108 int32_t originalKeyCode, int32_t rotatedKeyCode,
3109 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110};
3111
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003112/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3113 * orientation.
3114 */
3115void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003116 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3117 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003118}
3119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003120void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003121 int32_t originalScanCode, int32_t originalKeyCode,
3122 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 NotifyKeyArgs args;
3124
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3127 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3128 ASSERT_EQ(originalScanCode, args.scanCode);
3129 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003130 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3134 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3135 ASSERT_EQ(originalScanCode, args.scanCode);
3136 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003137 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138}
3139
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003141 KeyboardInputMapper& mapper =
3142 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3143 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003145 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146}
3147
3148TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3149 const int32_t USAGE_A = 0x070004;
3150 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003151 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3152 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003153 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3154 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3155 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003157 KeyboardInputMapper& mapper =
3158 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3159 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003160 // Initial metastate to AMETA_NONE.
3161 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3162 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163
3164 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 NotifyKeyArgs args;
3167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3168 ASSERT_EQ(DEVICE_ID, args.deviceId);
3169 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3170 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3171 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3172 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3173 ASSERT_EQ(KEY_HOME, args.scanCode);
3174 ASSERT_EQ(AMETA_NONE, args.metaState);
3175 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3176 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3177 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3178
3179 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003180 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3182 ASSERT_EQ(DEVICE_ID, args.deviceId);
3183 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3184 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3185 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3186 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3187 ASSERT_EQ(KEY_HOME, args.scanCode);
3188 ASSERT_EQ(AMETA_NONE, args.metaState);
3189 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3190 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3191 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3192
3193 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3197 ASSERT_EQ(DEVICE_ID, args.deviceId);
3198 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3199 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3200 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3201 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3202 ASSERT_EQ(0, args.scanCode);
3203 ASSERT_EQ(AMETA_NONE, args.metaState);
3204 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3205 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3206 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3207
3208 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3210 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3212 ASSERT_EQ(DEVICE_ID, args.deviceId);
3213 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3214 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3215 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3216 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3217 ASSERT_EQ(0, args.scanCode);
3218 ASSERT_EQ(AMETA_NONE, args.metaState);
3219 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3220 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3221 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3222
3223 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3227 ASSERT_EQ(DEVICE_ID, args.deviceId);
3228 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3229 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3230 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3231 ASSERT_EQ(0, args.keyCode);
3232 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3233 ASSERT_EQ(AMETA_NONE, args.metaState);
3234 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3235 ASSERT_EQ(0U, args.policyFlags);
3236 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3237
3238 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3240 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(DEVICE_ID, args.deviceId);
3243 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3244 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3245 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3246 ASSERT_EQ(0, args.keyCode);
3247 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3248 ASSERT_EQ(AMETA_NONE, args.metaState);
3249 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3250 ASSERT_EQ(0U, args.policyFlags);
3251 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3252}
3253
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003254/**
3255 * Ensure that the readTime is set to the time when the EV_KEY is received.
3256 */
3257TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3258 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3259
3260 KeyboardInputMapper& mapper =
3261 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3262 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3263 NotifyKeyArgs args;
3264
3265 // Key down
3266 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3268 ASSERT_EQ(12, args.readTime);
3269
3270 // Key up
3271 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3273 ASSERT_EQ(15, args.readTime);
3274}
3275
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3278 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003279 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3280 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3281 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003283 KeyboardInputMapper& mapper =
3284 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3285 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286
arthurhungc903df12020-08-11 15:08:42 +08003287 // Initial metastate to AMETA_NONE.
3288 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3289 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
3291 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293 NotifyKeyArgs args;
3294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003297 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
3299 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304
3305 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003306 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310
3311 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003312 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3314 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003315 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003316 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317}
3318
3319TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003320 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3321 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3322 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3323 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003325 KeyboardInputMapper& mapper =
3326 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3327 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003329 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3331 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3332 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3333 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3335 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3336 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3337 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3338}
3339
3340TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003341 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3342 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3343 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3344 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003347 KeyboardInputMapper& mapper =
3348 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3349 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003351 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_NO_FATAL_FAILURE(
3353 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3354 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3355 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3356 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3357 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3358 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3359 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003361 clearViewports();
3362 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003363 ASSERT_NO_FATAL_FAILURE(
3364 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3365 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3366 AKEYCODE_DPAD_UP, DISPLAY_ID));
3367 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3368 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3369 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3370 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003372 clearViewports();
3373 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003374 ASSERT_NO_FATAL_FAILURE(
3375 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3376 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3377 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3378 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3379 AKEYCODE_DPAD_UP, DISPLAY_ID));
3380 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3381 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003383 clearViewports();
3384 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003385 ASSERT_NO_FATAL_FAILURE(
3386 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3387 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3388 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3389 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3390 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3391 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3392 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393
3394 // Special case: if orientation changes while key is down, we still emit the same keycode
3395 // in the key up as we did in the key down.
3396 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003397 clearViewports();
3398 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3402 ASSERT_EQ(KEY_UP, args.scanCode);
3403 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3404
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003405 clearViewports();
3406 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3409 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3410 ASSERT_EQ(KEY_UP, args.scanCode);
3411 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3412}
3413
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003414TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3415 // If the keyboard is not orientation aware,
3416 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003417 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 KeyboardInputMapper& mapper =
3420 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3421 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003422 NotifyKeyArgs args;
3423
3424 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3429 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3430
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003431 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3436 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3437}
3438
3439TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3440 // If the keyboard is orientation aware,
3441 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003442 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003443
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003444 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003445 KeyboardInputMapper& mapper =
3446 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3447 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003448 NotifyKeyArgs args;
3449
3450 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3451 // ^--- already checked by the previous test
3452
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003453 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003454 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(DISPLAY_ID, args.displayId);
3460
3461 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003462 clearViewports();
3463 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003464 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3469 ASSERT_EQ(newDisplayId, args.displayId);
3470}
3471
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003473 KeyboardInputMapper& mapper =
3474 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3475 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003477 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003480 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003481 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482}
3483
3484TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003485 KeyboardInputMapper& mapper =
3486 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3487 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003489 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003492 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003493 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494}
3495
3496TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 KeyboardInputMapper& mapper =
3498 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3499 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003501 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502
3503 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3504 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 ASSERT_TRUE(flags[0]);
3507 ASSERT_FALSE(flags[1]);
3508}
3509
3510TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003511 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3512 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3513 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3514 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3515 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3516 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 KeyboardInputMapper& mapper =
3519 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3520 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003521 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003522 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3523 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003526 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3527 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3528 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
3530 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003533 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3534 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3535 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
3538 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003541 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3542 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3543 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003544 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545
3546 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003549 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3550 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3551 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553
3554 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003557 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3558 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3559 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
3562 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003565 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3566 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3567 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003568 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569
3570 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003573 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3574 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3575 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003576 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577}
3578
Chris Yea52ade12020-08-27 16:49:20 -07003579TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3580 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3581 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3582 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3583 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3584
3585 KeyboardInputMapper& mapper =
3586 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3587 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3588
3589 // Initial metastate should be AMETA_NONE as no meta keys added.
3590 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3591 // Meta state should be AMETA_NONE after reset
3592 mapper.reset(ARBITRARY_TIME);
3593 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3594 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3595 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3596 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3597
3598 NotifyKeyArgs args;
3599 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3602 ASSERT_EQ(AMETA_NONE, args.metaState);
3603 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3604 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3605 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3606
3607 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003608 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3610 ASSERT_EQ(AMETA_NONE, args.metaState);
3611 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3612 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3613 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3614}
3615
Arthur Hung2c9a3342019-07-23 14:18:59 +08003616TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3617 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003618 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3619 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3620 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3621 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003622
3623 // keyboard 2.
3624 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003625 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003626 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003627 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003628 std::shared_ptr<InputDevice> device2 =
3629 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3630 Flags<InputDeviceClass>(0));
3631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003632 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3633 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3634 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3635 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 KeyboardInputMapper& mapper =
3638 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003640
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003641 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003642 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003643 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003644 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3645 device2->reset(ARBITRARY_TIME);
3646
3647 // Prepared displays and associated info.
3648 constexpr uint8_t hdmi1 = 0;
3649 constexpr uint8_t hdmi2 = 1;
3650 const std::string SECONDARY_UNIQUE_ID = "local:1";
3651
3652 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3653 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3654
3655 // No associated display viewport found, should disable the device.
3656 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3657 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3658 ASSERT_FALSE(device2->isEnabled());
3659
3660 // Prepare second display.
3661 constexpr int32_t newDisplayId = 2;
3662 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003663 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003664 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003665 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003666 // Default device will reconfigure above, need additional reconfiguration for another device.
3667 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3668 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3669
3670 // Device should be enabled after the associated display is found.
3671 ASSERT_TRUE(mDevice->isEnabled());
3672 ASSERT_TRUE(device2->isEnabled());
3673
3674 // Test pad key events
3675 ASSERT_NO_FATAL_FAILURE(
3676 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3677 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3678 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3679 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3680 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3681 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3682 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3683
3684 ASSERT_NO_FATAL_FAILURE(
3685 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3686 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3687 AKEYCODE_DPAD_RIGHT, newDisplayId));
3688 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3689 AKEYCODE_DPAD_DOWN, newDisplayId));
3690 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3691 AKEYCODE_DPAD_LEFT, newDisplayId));
3692}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693
arthurhungc903df12020-08-11 15:08:42 +08003694TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3695 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3696 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3697 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3700 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3701
3702 KeyboardInputMapper& mapper =
3703 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3704 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3705 // Initial metastate to AMETA_NONE.
3706 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3707 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3708
3709 // Initialization should have turned all of the lights off.
3710 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3711 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3712 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3713
3714 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003717 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3718 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3719
3720 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003723 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3724 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3725
3726 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003729 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3730 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3731
3732 mFakeEventHub->removeDevice(EVENTHUB_ID);
3733 mReader->loopOnce();
3734
3735 // keyboard 2 should default toggle keys.
3736 const std::string USB2 = "USB2";
3737 const std::string DEVICE_NAME2 = "KEYBOARD2";
3738 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3739 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3740 std::shared_ptr<InputDevice> device2 =
3741 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3742 Flags<InputDeviceClass>(0));
3743 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3744 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3745 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3746 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3747 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3748 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3749
arthurhung6fe95782020-10-05 22:41:16 +08003750 KeyboardInputMapper& mapper2 =
3751 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3752 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003753 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3754 device2->reset(ARBITRARY_TIME);
3755
3756 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3757 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3758 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003759 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3760 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003761}
3762
Arthur Hungcb40a002021-08-03 14:31:01 +00003763TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3764 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3765 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3766 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3767
3768 // Suppose we have two mappers. (DPAD + KEYBOARD)
3769 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3770 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3771 KeyboardInputMapper& mapper =
3772 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3773 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3774 // Initialize metastate to AMETA_NUM_LOCK_ON.
3775 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3776 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3777
3778 mReader->toggleCapsLockState(DEVICE_ID);
3779 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3780}
3781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003782// --- KeyboardInputMapperTest_ExternalDevice ---
3783
3784class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3785protected:
Chris Yea52ade12020-08-27 16:49:20 -07003786 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787};
3788
3789TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003790 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3791 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3795 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3796 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003797
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003798 KeyboardInputMapper& mapper =
3799 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3800 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003801
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003803 NotifyKeyArgs args;
3804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3805 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(uint32_t(0), args.policyFlags);
3810
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3813 ASSERT_EQ(uint32_t(0), args.policyFlags);
3814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(uint32_t(0), args.policyFlags);
3818
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3821 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3822
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3825 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3826}
3827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003828TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003829 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003831 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003834
Powei Fengd041c5d2019-05-03 17:11:33 -07003835 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 KeyboardInputMapper& mapper =
3837 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3838 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003839
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003841 NotifyKeyArgs args;
3842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3844
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3847 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3848
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3851 ASSERT_EQ(uint32_t(0), args.policyFlags);
3852
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3855 ASSERT_EQ(uint32_t(0), args.policyFlags);
3856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3859 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3860
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003861 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3863 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3864}
3865
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866// --- CursorInputMapperTest ---
3867
3868class CursorInputMapperTest : public InputMapperTest {
3869protected:
3870 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3871
Michael Wright17db18e2020-06-26 20:51:44 +01003872 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873
Chris Yea52ade12020-08-27 16:49:20 -07003874 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875 InputMapperTest::SetUp();
3876
Michael Wright17db18e2020-06-26 20:51:44 +01003877 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003878 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 }
3880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003881 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3882 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003883
3884 void prepareDisplay(int32_t orientation) {
3885 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003886 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003887 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3888 orientation, uniqueId, NO_PORT, viewportType);
3889 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003890
3891 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3892 float pressure) {
3893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3894 0.0f, 0.0f, 0.0f, EPSILON));
3895 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896};
3897
3898const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3901 int32_t originalY, int32_t rotatedX,
3902 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903 NotifyMotionArgs args;
3904
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003910 ASSERT_NO_FATAL_FAILURE(
3911 assertCursorPointerCoords(args.pointerCoords[0],
3912 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3913 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914}
3915
3916TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003918 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003920 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921}
3922
3923TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003925 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003927 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928}
3929
3930TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003932 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933
3934 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003935 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
3937 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003938 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3939 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3941 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3942
3943 // When the bounds are set, then there should be a valid motion range.
3944 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3945
3946 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003947 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948
3949 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3950 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3951 1, 800 - 1, 0.0f, 0.0f));
3952 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3953 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3954 2, 480 - 1, 0.0f, 0.0f));
3955 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3956 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3957 0.0f, 1.0f, 0.0f, 0.0f));
3958}
3959
3960TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
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
3964 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003965 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966
3967 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3968 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3969 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3970 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3971 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3972 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3973 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3974 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3975 0.0f, 1.0f, 0.0f, 0.0f));
3976}
3977
3978TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003980 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981
arthurhungdcef2dc2020-08-11 14:47:50 +08003982 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983
3984 NotifyMotionArgs args;
3985
3986 // Button press.
3987 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -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_DOWN, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4005 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4006 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4007
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4009 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4010 ASSERT_EQ(DEVICE_ID, args.deviceId);
4011 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4012 ASSERT_EQ(uint32_t(0), args.policyFlags);
4013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4014 ASSERT_EQ(0, args.flags);
4015 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4016 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4017 ASSERT_EQ(0, args.edgeFlags);
4018 ASSERT_EQ(uint32_t(1), args.pointerCount);
4019 ASSERT_EQ(0, args.pointerProperties[0].id);
4020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004021 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004022 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4023 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4024 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4025
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004027 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4028 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029 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);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4048 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4049 ASSERT_EQ(DEVICE_ID, args.deviceId);
4050 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4051 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4053 ASSERT_EQ(0, args.flags);
4054 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4055 ASSERT_EQ(0, args.buttonState);
4056 ASSERT_EQ(0, args.edgeFlags);
4057 ASSERT_EQ(uint32_t(1), args.pointerCount);
4058 ASSERT_EQ(0, args.pointerProperties[0].id);
4059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004060 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4062 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4063 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4064}
4065
4066TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004068 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069
4070 NotifyMotionArgs args;
4071
4072 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004077 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4078 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4079 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
4081 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004086 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4087 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088}
4089
4090TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004092 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093
4094 NotifyMotionArgs args;
4095
4096 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004101 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004105 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004112 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113
4114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004116 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117}
4118
4119TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004121 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122
4123 NotifyMotionArgs args;
4124
4125 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004132 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4133 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4134 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004138 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4139 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4140 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004141
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004148 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4149 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4150 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151
4152 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004157 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004158
4159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004161 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162}
4163
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004164TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004166 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4167 // need to be rotated.
4168 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004169 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004171 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4180}
4181
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004182TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004184 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4185 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004186 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004188 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4197
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004198 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004208 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4217
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004218 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4222 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4223 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4225 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4226 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227}
4228
4229TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
4233 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4234 mFakePointerController->setPosition(100, 200);
4235 mFakePointerController->setButtonState(0);
4236
4237 NotifyMotionArgs motionArgs;
4238 NotifyKeyArgs keyArgs;
4239
4240 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4245 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4246 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004247 ASSERT_NO_FATAL_FAILURE(
4248 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4252 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4253 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004254 ASSERT_NO_FATAL_FAILURE(
4255 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_EQ(0, motionArgs.buttonState);
4262 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004263 ASSERT_NO_FATAL_FAILURE(
4264 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265
4266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_EQ(0, motionArgs.buttonState);
4269 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004270 ASSERT_NO_FATAL_FAILURE(
4271 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004272
4273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004275 ASSERT_EQ(0, motionArgs.buttonState);
4276 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004277 ASSERT_NO_FATAL_FAILURE(
4278 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4286 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4287 motionArgs.buttonState);
4288 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4289 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004290 ASSERT_NO_FATAL_FAILURE(
4291 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4295 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4296 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4297 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));
4302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4304 motionArgs.buttonState);
4305 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4306 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4315 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004316 ASSERT_NO_FATAL_FAILURE(
4317 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
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(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4322 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004323 ASSERT_NO_FATAL_FAILURE(
4324 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4330 ASSERT_EQ(0, motionArgs.buttonState);
4331 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(
4333 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336
4337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_EQ(0, motionArgs.buttonState);
4339 ASSERT_EQ(0, mFakePointerController->getButtonState());
4340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(
4342 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4345 ASSERT_EQ(0, motionArgs.buttonState);
4346 ASSERT_EQ(0, mFakePointerController->getButtonState());
4347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(
4349 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
4351 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4355 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4356 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004359 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4361 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004362 ASSERT_NO_FATAL_FAILURE(
4363 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364
4365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4367 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4368 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004369 ASSERT_NO_FATAL_FAILURE(
4370 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_EQ(0, motionArgs.buttonState);
4377 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004378 ASSERT_NO_FATAL_FAILURE(
4379 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004383 ASSERT_EQ(0, motionArgs.buttonState);
4384 ASSERT_EQ(0, mFakePointerController->getButtonState());
4385
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004386 ASSERT_NO_FATAL_FAILURE(
4387 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4389 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4390 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4391
4392 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4397 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4402 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004403 ASSERT_NO_FATAL_FAILURE(
4404 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405
4406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4408 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4409 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004410 ASSERT_NO_FATAL_FAILURE(
4411 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_EQ(0, motionArgs.buttonState);
4418 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004419 ASSERT_NO_FATAL_FAILURE(
4420 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421
4422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4424 ASSERT_EQ(0, motionArgs.buttonState);
4425 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4430 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4431 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4432
4433 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4438 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004439
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4443 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004444 ASSERT_NO_FATAL_FAILURE(
4445 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4449 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4450 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(
4452 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_EQ(0, motionArgs.buttonState);
4459 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(
4461 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4465 ASSERT_EQ(0, motionArgs.buttonState);
4466 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4471 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4472 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4473
4474 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4478 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4479 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004480
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4484 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(
4486 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4490 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4491 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004492 ASSERT_NO_FATAL_FAILURE(
4493 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 ASSERT_EQ(0, motionArgs.buttonState);
4500 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004501 ASSERT_NO_FATAL_FAILURE(
4502 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503
4504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4506 ASSERT_EQ(0, motionArgs.buttonState);
4507 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004508 ASSERT_NO_FATAL_FAILURE(
4509 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004510
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4512 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4513 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4514}
4515
4516TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004518 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519
4520 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4521 mFakePointerController->setPosition(100, 200);
4522 mFakePointerController->setButtonState(0);
4523
4524 NotifyMotionArgs args;
4525
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004530 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4533 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 +01004534 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004535}
4536
4537TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004538 addConfigurationProperty("cursor.mode", "pointer");
4539 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004540 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004541
4542 NotifyDeviceResetArgs resetArgs;
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4544 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4545 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4546
4547 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4548 mFakePointerController->setPosition(100, 200);
4549 mFakePointerController->setButtonState(0);
4550
4551 NotifyMotionArgs args;
4552
4553 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4558 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4561 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 +01004562 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004563
4564 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4568 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4569 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4571 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4573 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4576 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4577
4578 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004579 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4580 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4582 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4585 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4587 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4588 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4590 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4591
4592 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4597 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4600 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 +01004601 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004602
4603 // Disable pointer capture and check that the device generation got bumped
4604 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004605 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004606 mFakePolicy->setPointerCapture(false);
4607 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004608 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004609
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4611 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4612 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4618 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4621 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 +01004622 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623}
4624
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004625TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004626 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004627
Garfield Tan888a6a42020-01-09 11:39:16 -08004628 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004629 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004630 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4631 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004632 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4633 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004634 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4635 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4636
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004637 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4638 mFakePointerController->setPosition(100, 200);
4639 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004640
4641 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4646 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4649 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 +01004650 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004651 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4652}
4653
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654// --- TouchInputMapperTest ---
4655
4656class TouchInputMapperTest : public InputMapperTest {
4657protected:
4658 static const int32_t RAW_X_MIN;
4659 static const int32_t RAW_X_MAX;
4660 static const int32_t RAW_Y_MIN;
4661 static const int32_t RAW_Y_MAX;
4662 static const int32_t RAW_TOUCH_MIN;
4663 static const int32_t RAW_TOUCH_MAX;
4664 static const int32_t RAW_TOOL_MIN;
4665 static const int32_t RAW_TOOL_MAX;
4666 static const int32_t RAW_PRESSURE_MIN;
4667 static const int32_t RAW_PRESSURE_MAX;
4668 static const int32_t RAW_ORIENTATION_MIN;
4669 static const int32_t RAW_ORIENTATION_MAX;
4670 static const int32_t RAW_DISTANCE_MIN;
4671 static const int32_t RAW_DISTANCE_MAX;
4672 static const int32_t RAW_TILT_MIN;
4673 static const int32_t RAW_TILT_MAX;
4674 static const int32_t RAW_ID_MIN;
4675 static const int32_t RAW_ID_MAX;
4676 static const int32_t RAW_SLOT_MIN;
4677 static const int32_t RAW_SLOT_MAX;
4678 static const float X_PRECISION;
4679 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004680 static const float X_PRECISION_VIRTUAL;
4681 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682
4683 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004684 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685
4686 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4687
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004688 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004689 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004690
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 enum Axes {
4692 POSITION = 1 << 0,
4693 TOUCH = 1 << 1,
4694 TOOL = 1 << 2,
4695 PRESSURE = 1 << 3,
4696 ORIENTATION = 1 << 4,
4697 MINOR = 1 << 5,
4698 ID = 1 << 6,
4699 DISTANCE = 1 << 7,
4700 TILT = 1 << 8,
4701 SLOT = 1 << 9,
4702 TOOL_TYPE = 1 << 10,
4703 };
4704
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004705 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4706 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004707 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004709 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 int32_t toRawX(float displayX);
4711 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004712 int32_t toRotatedRawX(float displayX);
4713 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004714 float toCookedX(float rawX, float rawY);
4715 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004717 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004719 float toDisplayY(int32_t rawY, int32_t displayHeight);
4720
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721};
4722
4723const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4724const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4725const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4726const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4727const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4728const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4729const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4730const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004731const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4732const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4734const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4735const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4736const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4737const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4738const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4739const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4740const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4741const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4742const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4743const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4744const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004745const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4746 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4747const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4748 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004749const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4750 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751
4752const float TouchInputMapperTest::GEOMETRIC_SCALE =
4753 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4754 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4755
4756const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4757 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4758 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4759};
4760
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004761void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004762 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4763 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004764}
4765
4766void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4767 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4768 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769}
4770
Santos Cordonfa5cf462017-04-05 10:37:00 -07004771void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004772 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4773 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4774 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004775}
4776
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004778 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4779 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782}
4783
Jason Gerecke489fda82012-09-07 17:19:40 -07004784void TouchInputMapperTest::prepareLocationCalibration() {
4785 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4786}
4787
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788int32_t TouchInputMapperTest::toRawX(float displayX) {
4789 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4790}
4791
4792int32_t TouchInputMapperTest::toRawY(float displayY) {
4793 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4794}
4795
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004796int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4797 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4798}
4799
4800int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4801 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4802}
4803
Jason Gerecke489fda82012-09-07 17:19:40 -07004804float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4805 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4806 return rawX;
4807}
4808
4809float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4810 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4811 return rawY;
4812}
4813
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004815 return toDisplayX(rawX, DISPLAY_WIDTH);
4816}
4817
4818float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4819 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820}
4821
4822float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004823 return toDisplayY(rawY, DISPLAY_HEIGHT);
4824}
4825
4826float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4827 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828}
4829
4830
4831// --- SingleTouchInputMapperTest ---
4832
4833class SingleTouchInputMapperTest : public TouchInputMapperTest {
4834protected:
4835 void prepareButtons();
4836 void prepareAxes(int axes);
4837
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004838 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4839 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4840 void processUp(SingleTouchInputMapper& mappery);
4841 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4842 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4843 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4844 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4845 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4846 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847};
4848
4849void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004850 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851}
4852
4853void SingleTouchInputMapperTest::prepareAxes(int axes) {
4854 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004855 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4856 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 }
4858 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004859 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4860 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 }
4862 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004863 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4864 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 }
4866 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004867 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4868 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 }
4870 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004871 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4872 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873 }
4874}
4875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004876void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880}
4881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004882void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885}
4886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004887void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889}
4890
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004891void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893}
4894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004895void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4896 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004900void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902}
4903
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004904void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4905 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908}
4909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004910void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4911 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913}
4914
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004915void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917}
4918
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 prepareButtons();
4921 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004922 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004924 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925}
4926
4927TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004928 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4929 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 prepareButtons();
4931 prepareAxes(POSITION);
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_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935}
4936
4937TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 prepareButtons();
4939 prepareAxes(POSITION);
4940 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004943 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944}
4945
4946TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 prepareButtons();
4948 prepareAxes(POSITION);
4949 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004950 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004952 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953}
4954
4955TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956 addConfigurationProperty("touch.deviceType", "touchScreen");
4957 prepareDisplay(DISPLAY_ORIENTATION_0);
4958 prepareButtons();
4959 prepareAxes(POSITION);
4960 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004961 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962
4963 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004964 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965
4966 // Virtual key is down.
4967 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4968 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4969 processDown(mapper, x, y);
4970 processSync(mapper);
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4972
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004973 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974
4975 // Virtual key is up.
4976 processUp(mapper);
4977 processSync(mapper);
4978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004980 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981}
4982
4983TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 addConfigurationProperty("touch.deviceType", "touchScreen");
4985 prepareDisplay(DISPLAY_ORIENTATION_0);
4986 prepareButtons();
4987 prepareAxes(POSITION);
4988 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004989 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990
4991 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004992 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
4994 // Virtual key is down.
4995 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4996 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4997 processDown(mapper, x, y);
4998 processSync(mapper);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005001 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002
5003 // Virtual key is up.
5004 processUp(mapper);
5005 processSync(mapper);
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5007
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005008 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009}
5010
5011TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 addConfigurationProperty("touch.deviceType", "touchScreen");
5013 prepareDisplay(DISPLAY_ORIENTATION_0);
5014 prepareButtons();
5015 prepareAxes(POSITION);
5016 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018
5019 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5020 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005021 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 ASSERT_TRUE(flags[0]);
5023 ASSERT_FALSE(flags[1]);
5024}
5025
5026TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027 addConfigurationProperty("touch.deviceType", "touchScreen");
5028 prepareDisplay(DISPLAY_ORIENTATION_0);
5029 prepareButtons();
5030 prepareAxes(POSITION);
5031 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005032 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033
arthurhungdcef2dc2020-08-11 14:47:50 +08005034 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035
5036 NotifyKeyArgs args;
5037
5038 // Press virtual key.
5039 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5040 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5041 processDown(mapper, x, y);
5042 processSync(mapper);
5043
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5045 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5046 ASSERT_EQ(DEVICE_ID, args.deviceId);
5047 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5048 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5049 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5050 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5051 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5052 ASSERT_EQ(KEY_HOME, args.scanCode);
5053 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5054 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5055
5056 // Release virtual key.
5057 processUp(mapper);
5058 processSync(mapper);
5059
5060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5061 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5062 ASSERT_EQ(DEVICE_ID, args.deviceId);
5063 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5064 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5065 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5066 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5067 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5068 ASSERT_EQ(KEY_HOME, args.scanCode);
5069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5070 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5071
5072 // Should not have sent any motions.
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5074}
5075
5076TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 addConfigurationProperty("touch.deviceType", "touchScreen");
5078 prepareDisplay(DISPLAY_ORIENTATION_0);
5079 prepareButtons();
5080 prepareAxes(POSITION);
5081 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005082 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083
arthurhungdcef2dc2020-08-11 14:47:50 +08005084 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085
5086 NotifyKeyArgs keyArgs;
5087
5088 // Press virtual key.
5089 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5090 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5091 processDown(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_DOWN, keyArgs.action);
5100 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5101 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5102 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5103 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5104 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5105
5106 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5107 // into the display area.
5108 y -= 100;
5109 processMove(mapper, x, y);
5110 processSync(mapper);
5111
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5113 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5114 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5115 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5116 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5117 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5118 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5119 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5120 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5121 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5122 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5123 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5124
5125 NotifyMotionArgs motionArgs;
5126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5128 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5129 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5130 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5132 ASSERT_EQ(0, motionArgs.flags);
5133 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5134 ASSERT_EQ(0, motionArgs.buttonState);
5135 ASSERT_EQ(0, motionArgs.edgeFlags);
5136 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5137 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5140 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5144
5145 // Keep moving out of bounds. Should generate a pointer move.
5146 y -= 50;
5147 processMove(mapper, x, y);
5148 processSync(mapper);
5149
5150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5151 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5152 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5153 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5154 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5156 ASSERT_EQ(0, motionArgs.flags);
5157 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5158 ASSERT_EQ(0, motionArgs.buttonState);
5159 ASSERT_EQ(0, motionArgs.edgeFlags);
5160 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5161 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5164 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5165 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5166 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5167 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5168
5169 // Release out of bounds. Should generate a pointer up.
5170 processUp(mapper);
5171 processSync(mapper);
5172
5173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5174 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5175 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5176 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5177 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5178 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5179 ASSERT_EQ(0, motionArgs.flags);
5180 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5181 ASSERT_EQ(0, motionArgs.buttonState);
5182 ASSERT_EQ(0, motionArgs.edgeFlags);
5183 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5184 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5187 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5188 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5189 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5190 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5191
5192 // Should not have sent any more keys or motions.
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5195}
5196
5197TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198 addConfigurationProperty("touch.deviceType", "touchScreen");
5199 prepareDisplay(DISPLAY_ORIENTATION_0);
5200 prepareButtons();
5201 prepareAxes(POSITION);
5202 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005203 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204
arthurhungdcef2dc2020-08-11 14:47:50 +08005205 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206
5207 NotifyMotionArgs motionArgs;
5208
5209 // Initially go down out of bounds.
5210 int32_t x = -10;
5211 int32_t y = -10;
5212 processDown(mapper, x, y);
5213 processSync(mapper);
5214
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5216
5217 // Move into the display area. Should generate a pointer down.
5218 x = 50;
5219 y = 75;
5220 processMove(mapper, x, y);
5221 processSync(mapper);
5222
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5225 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5226 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5227 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5228 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5229 ASSERT_EQ(0, motionArgs.flags);
5230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5231 ASSERT_EQ(0, motionArgs.buttonState);
5232 ASSERT_EQ(0, motionArgs.edgeFlags);
5233 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5234 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5237 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5238 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5239 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5241
5242 // Release. Should generate a pointer up.
5243 processUp(mapper);
5244 processSync(mapper);
5245
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5248 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5249 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5250 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5252 ASSERT_EQ(0, motionArgs.flags);
5253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5254 ASSERT_EQ(0, motionArgs.buttonState);
5255 ASSERT_EQ(0, motionArgs.edgeFlags);
5256 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5257 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5260 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5261 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5262 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5264
5265 // Should not have sent any more keys or motions.
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5268}
5269
Santos Cordonfa5cf462017-04-05 10:37:00 -07005270TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005271 addConfigurationProperty("touch.deviceType", "touchScreen");
5272 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5273
5274 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5275 prepareButtons();
5276 prepareAxes(POSITION);
5277 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005278 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005279
arthurhungdcef2dc2020-08-11 14:47:50 +08005280 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005281
5282 NotifyMotionArgs motionArgs;
5283
5284 // Down.
5285 int32_t x = 100;
5286 int32_t y = 125;
5287 processDown(mapper, x, y);
5288 processSync(mapper);
5289
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5292 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5293 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5294 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5295 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5296 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5297 ASSERT_EQ(0, motionArgs.flags);
5298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5299 ASSERT_EQ(0, motionArgs.buttonState);
5300 ASSERT_EQ(0, motionArgs.edgeFlags);
5301 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5302 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5305 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5306 1, 0, 0, 0, 0, 0, 0, 0));
5307 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5308 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5310
5311 // Move.
5312 x += 50;
5313 y += 75;
5314 processMove(mapper, x, y);
5315 processSync(mapper);
5316
5317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5318 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5319 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5320 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5321 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5322 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5324 ASSERT_EQ(0, motionArgs.flags);
5325 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5326 ASSERT_EQ(0, motionArgs.buttonState);
5327 ASSERT_EQ(0, motionArgs.edgeFlags);
5328 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5329 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5332 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5333 1, 0, 0, 0, 0, 0, 0, 0));
5334 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5335 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5337
5338 // Up.
5339 processUp(mapper);
5340 processSync(mapper);
5341
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5344 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5345 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5346 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5347 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5348 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5349 ASSERT_EQ(0, motionArgs.flags);
5350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5351 ASSERT_EQ(0, motionArgs.buttonState);
5352 ASSERT_EQ(0, motionArgs.edgeFlags);
5353 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5354 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5357 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5358 1, 0, 0, 0, 0, 0, 0, 0));
5359 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5360 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5362
5363 // Should not have sent any more keys or motions.
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5366}
5367
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369 addConfigurationProperty("touch.deviceType", "touchScreen");
5370 prepareDisplay(DISPLAY_ORIENTATION_0);
5371 prepareButtons();
5372 prepareAxes(POSITION);
5373 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375
arthurhungdcef2dc2020-08-11 14:47:50 +08005376 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377
5378 NotifyMotionArgs motionArgs;
5379
5380 // Down.
5381 int32_t x = 100;
5382 int32_t y = 125;
5383 processDown(mapper, x, y);
5384 processSync(mapper);
5385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5388 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5389 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5390 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5392 ASSERT_EQ(0, motionArgs.flags);
5393 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5394 ASSERT_EQ(0, motionArgs.buttonState);
5395 ASSERT_EQ(0, motionArgs.edgeFlags);
5396 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5397 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5400 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5401 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5402 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5404
5405 // Move.
5406 x += 50;
5407 y += 75;
5408 processMove(mapper, x, y);
5409 processSync(mapper);
5410
5411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5412 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5413 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5414 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5415 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5417 ASSERT_EQ(0, motionArgs.flags);
5418 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5419 ASSERT_EQ(0, motionArgs.buttonState);
5420 ASSERT_EQ(0, motionArgs.edgeFlags);
5421 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5422 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5425 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5426 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5427 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5428 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5429
5430 // Up.
5431 processUp(mapper);
5432 processSync(mapper);
5433
5434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5435 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5436 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5437 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5438 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5439 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5440 ASSERT_EQ(0, motionArgs.flags);
5441 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5442 ASSERT_EQ(0, motionArgs.buttonState);
5443 ASSERT_EQ(0, motionArgs.edgeFlags);
5444 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5445 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5448 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5449 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5450 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5451 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5452
5453 // Should not have sent any more keys or motions.
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5456}
5457
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005458TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 addConfigurationProperty("touch.deviceType", "touchScreen");
5460 prepareButtons();
5461 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005462 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5463 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465
5466 NotifyMotionArgs args;
5467
5468 // Rotation 90.
5469 prepareDisplay(DISPLAY_ORIENTATION_90);
5470 processDown(mapper, toRawX(50), toRawY(75));
5471 processSync(mapper);
5472
5473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5474 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5475 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5476
5477 processUp(mapper);
5478 processSync(mapper);
5479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5480}
5481
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005482TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483 addConfigurationProperty("touch.deviceType", "touchScreen");
5484 prepareButtons();
5485 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005486 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5487 // orientation-aware are affected by display rotation.
5488 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005489 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490
5491 NotifyMotionArgs args;
5492
5493 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005494 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495 prepareDisplay(DISPLAY_ORIENTATION_0);
5496 processDown(mapper, toRawX(50), toRawY(75));
5497 processSync(mapper);
5498
5499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5500 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5501 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5502
5503 processUp(mapper);
5504 processSync(mapper);
5505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5506
5507 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005508 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005510 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 processSync(mapper);
5512
5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5514 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5515 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5516
5517 processUp(mapper);
5518 processSync(mapper);
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5520
5521 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005522 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523 prepareDisplay(DISPLAY_ORIENTATION_180);
5524 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5525 processSync(mapper);
5526
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5528 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5529 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5530
5531 processUp(mapper);
5532 processSync(mapper);
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5534
5535 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005536 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005538 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 processSync(mapper);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5542 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5543 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5544
5545 processUp(mapper);
5546 processSync(mapper);
5547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5548}
5549
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005550TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5551 addConfigurationProperty("touch.deviceType", "touchScreen");
5552 prepareButtons();
5553 prepareAxes(POSITION);
5554 addConfigurationProperty("touch.orientationAware", "1");
5555 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5556 clearViewports();
5557 prepareDisplay(DISPLAY_ORIENTATION_0);
5558 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5559 NotifyMotionArgs args;
5560
5561 // Orientation 0.
5562 processDown(mapper, toRawX(50), toRawY(75));
5563 processSync(mapper);
5564
5565 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5566 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5567 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5568
5569 processUp(mapper);
5570 processSync(mapper);
5571 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5572}
5573
5574TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5575 addConfigurationProperty("touch.deviceType", "touchScreen");
5576 prepareButtons();
5577 prepareAxes(POSITION);
5578 addConfigurationProperty("touch.orientationAware", "1");
5579 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5580 clearViewports();
5581 prepareDisplay(DISPLAY_ORIENTATION_0);
5582 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5583 NotifyMotionArgs args;
5584
5585 // Orientation 90.
5586 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5587 processSync(mapper);
5588
5589 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5590 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5591 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5592
5593 processUp(mapper);
5594 processSync(mapper);
5595 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5596}
5597
5598TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5599 addConfigurationProperty("touch.deviceType", "touchScreen");
5600 prepareButtons();
5601 prepareAxes(POSITION);
5602 addConfigurationProperty("touch.orientationAware", "1");
5603 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5604 clearViewports();
5605 prepareDisplay(DISPLAY_ORIENTATION_0);
5606 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5607 NotifyMotionArgs args;
5608
5609 // Orientation 180.
5610 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5611 processSync(mapper);
5612
5613 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5614 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5615 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5616
5617 processUp(mapper);
5618 processSync(mapper);
5619 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5620}
5621
5622TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5623 addConfigurationProperty("touch.deviceType", "touchScreen");
5624 prepareButtons();
5625 prepareAxes(POSITION);
5626 addConfigurationProperty("touch.orientationAware", "1");
5627 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5628 clearViewports();
5629 prepareDisplay(DISPLAY_ORIENTATION_0);
5630 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5631 NotifyMotionArgs args;
5632
5633 // Orientation 270.
5634 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5635 processSync(mapper);
5636
5637 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5638 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5639 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5640
5641 processUp(mapper);
5642 processSync(mapper);
5643 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5644}
5645
5646TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5647 addConfigurationProperty("touch.deviceType", "touchScreen");
5648 prepareButtons();
5649 prepareAxes(POSITION);
5650 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5651 // orientation-aware are affected by display rotation.
5652 addConfigurationProperty("touch.orientationAware", "0");
5653 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5654 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5655
5656 NotifyMotionArgs args;
5657
5658 // Orientation 90, Rotation 0.
5659 clearViewports();
5660 prepareDisplay(DISPLAY_ORIENTATION_0);
5661 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5662 processSync(mapper);
5663
5664 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5665 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5666 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5667
5668 processUp(mapper);
5669 processSync(mapper);
5670 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5671
5672 // Orientation 90, Rotation 90.
5673 clearViewports();
5674 prepareDisplay(DISPLAY_ORIENTATION_90);
5675 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5676 processSync(mapper);
5677
5678 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5679 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5680 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5681
5682 processUp(mapper);
5683 processSync(mapper);
5684 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5685
5686 // Orientation 90, Rotation 180.
5687 clearViewports();
5688 prepareDisplay(DISPLAY_ORIENTATION_180);
5689 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5690 processSync(mapper);
5691
5692 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5693 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5694 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5695
5696 processUp(mapper);
5697 processSync(mapper);
5698 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5699
5700 // Orientation 90, Rotation 270.
5701 clearViewports();
5702 prepareDisplay(DISPLAY_ORIENTATION_270);
5703 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5704 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5705 processSync(mapper);
5706
5707 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5708 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5709 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5710
5711 processUp(mapper);
5712 processSync(mapper);
5713 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5714}
5715
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 addConfigurationProperty("touch.deviceType", "touchScreen");
5718 prepareDisplay(DISPLAY_ORIENTATION_0);
5719 prepareButtons();
5720 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005721 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722
5723 // These calculations are based on the input device calibration documentation.
5724 int32_t rawX = 100;
5725 int32_t rawY = 200;
5726 int32_t rawPressure = 10;
5727 int32_t rawToolMajor = 12;
5728 int32_t rawDistance = 2;
5729 int32_t rawTiltX = 30;
5730 int32_t rawTiltY = 110;
5731
5732 float x = toDisplayX(rawX);
5733 float y = toDisplayY(rawY);
5734 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5735 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5736 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5737 float distance = float(rawDistance);
5738
5739 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5740 float tiltScale = M_PI / 180;
5741 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5742 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5743 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5744 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5745
5746 processDown(mapper, rawX, rawY);
5747 processPressure(mapper, rawPressure);
5748 processToolMajor(mapper, rawToolMajor);
5749 processDistance(mapper, rawDistance);
5750 processTilt(mapper, rawTiltX, rawTiltY);
5751 processSync(mapper);
5752
5753 NotifyMotionArgs args;
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5756 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5757 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5758}
5759
Jason Gerecke489fda82012-09-07 17:19:40 -07005760TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005761 addConfigurationProperty("touch.deviceType", "touchScreen");
5762 prepareDisplay(DISPLAY_ORIENTATION_0);
5763 prepareLocationCalibration();
5764 prepareButtons();
5765 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005766 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005767
5768 int32_t rawX = 100;
5769 int32_t rawY = 200;
5770
5771 float x = toDisplayX(toCookedX(rawX, rawY));
5772 float y = toDisplayY(toCookedY(rawX, rawY));
5773
5774 processDown(mapper, rawX, rawY);
5775 processSync(mapper);
5776
5777 NotifyMotionArgs args;
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5780 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5781}
5782
Michael Wrightd02c5b62014-02-10 15:10:22 -08005783TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005784 addConfigurationProperty("touch.deviceType", "touchScreen");
5785 prepareDisplay(DISPLAY_ORIENTATION_0);
5786 prepareButtons();
5787 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005788 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789
5790 NotifyMotionArgs motionArgs;
5791 NotifyKeyArgs keyArgs;
5792
5793 processDown(mapper, 100, 200);
5794 processSync(mapper);
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5797 ASSERT_EQ(0, motionArgs.buttonState);
5798
5799 // press BTN_LEFT, release BTN_LEFT
5800 processKey(mapper, BTN_LEFT, 1);
5801 processSync(mapper);
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5804 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5805
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5808 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5809
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810 processKey(mapper, BTN_LEFT, 0);
5811 processSync(mapper);
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005813 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005815
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005818 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819
5820 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5821 processKey(mapper, BTN_RIGHT, 1);
5822 processKey(mapper, BTN_MIDDLE, 1);
5823 processSync(mapper);
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5826 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5827 motionArgs.buttonState);
5828
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5831 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5835 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5836 motionArgs.buttonState);
5837
Michael Wrightd02c5b62014-02-10 15:10:22 -08005838 processKey(mapper, BTN_RIGHT, 0);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847
5848 processKey(mapper, BTN_MIDDLE, 0);
5849 processSync(mapper);
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005853
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005856 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857
5858 // press BTN_BACK, release BTN_BACK
5859 processKey(mapper, BTN_BACK, 1);
5860 processSync(mapper);
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5862 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5863 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005864
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005867 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5868
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5871 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872
5873 processKey(mapper, BTN_BACK, 0);
5874 processSync(mapper);
5875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005878
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005881 ASSERT_EQ(0, motionArgs.buttonState);
5882
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5884 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5885 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5886
5887 // press BTN_SIDE, release BTN_SIDE
5888 processKey(mapper, BTN_SIDE, 1);
5889 processSync(mapper);
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5891 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5892 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005893
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005896 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5897
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5899 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5900 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901
5902 processKey(mapper, BTN_SIDE, 0);
5903 processSync(mapper);
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005905 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005907
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005910 ASSERT_EQ(0, motionArgs.buttonState);
5911
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5913 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5914 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5915
5916 // press BTN_FORWARD, release BTN_FORWARD
5917 processKey(mapper, BTN_FORWARD, 1);
5918 processSync(mapper);
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5920 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5921 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005922
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005925 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5926
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5928 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5929 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930
5931 processKey(mapper, BTN_FORWARD, 0);
5932 processSync(mapper);
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005936
5937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005939 ASSERT_EQ(0, motionArgs.buttonState);
5940
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5942 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5943 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5944
5945 // press BTN_EXTRA, release BTN_EXTRA
5946 processKey(mapper, BTN_EXTRA, 1);
5947 processSync(mapper);
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5949 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5950 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005951
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005954 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5958 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959
5960 processKey(mapper, BTN_EXTRA, 0);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968 ASSERT_EQ(0, motionArgs.buttonState);
5969
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5971 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5972 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5973
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5975
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 // press BTN_STYLUS, release BTN_STYLUS
5977 processKey(mapper, BTN_STYLUS, 1);
5978 processSync(mapper);
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005981 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5985 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986
5987 processKey(mapper, BTN_STYLUS, 0);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005996
5997 // press BTN_STYLUS2, release BTN_STYLUS2
5998 processKey(mapper, BTN_STYLUS2, 1);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006002 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6006 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007
6008 processKey(mapper, BTN_STYLUS2, 0);
6009 processSync(mapper);
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006016 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017
6018 // release touch
6019 processUp(mapper);
6020 processSync(mapper);
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6022 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6023 ASSERT_EQ(0, motionArgs.buttonState);
6024}
6025
6026TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027 addConfigurationProperty("touch.deviceType", "touchScreen");
6028 prepareDisplay(DISPLAY_ORIENTATION_0);
6029 prepareButtons();
6030 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006031 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032
6033 NotifyMotionArgs motionArgs;
6034
6035 // default tool type is finger
6036 processDown(mapper, 100, 200);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6041
6042 // eraser
6043 processKey(mapper, BTN_TOOL_RUBBER, 1);
6044 processSync(mapper);
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6048
6049 // stylus
6050 processKey(mapper, BTN_TOOL_RUBBER, 0);
6051 processKey(mapper, BTN_TOOL_PEN, 1);
6052 processSync(mapper);
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6056
6057 // brush
6058 processKey(mapper, BTN_TOOL_PEN, 0);
6059 processKey(mapper, BTN_TOOL_BRUSH, 1);
6060 processSync(mapper);
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6064
6065 // pencil
6066 processKey(mapper, BTN_TOOL_BRUSH, 0);
6067 processKey(mapper, BTN_TOOL_PENCIL, 1);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6072
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006073 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 processKey(mapper, BTN_TOOL_PENCIL, 0);
6075 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6076 processSync(mapper);
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6080
6081 // mouse
6082 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6083 processKey(mapper, BTN_TOOL_MOUSE, 1);
6084 processSync(mapper);
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6088
6089 // lens
6090 processKey(mapper, BTN_TOOL_MOUSE, 0);
6091 processKey(mapper, BTN_TOOL_LENS, 1);
6092 processSync(mapper);
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6096
6097 // double-tap
6098 processKey(mapper, BTN_TOOL_LENS, 0);
6099 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6100 processSync(mapper);
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6104
6105 // triple-tap
6106 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6107 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6108 processSync(mapper);
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6112
6113 // quad-tap
6114 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6115 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6120
6121 // finger
6122 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6123 processKey(mapper, BTN_TOOL_FINGER, 1);
6124 processSync(mapper);
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6128
6129 // stylus trumps finger
6130 processKey(mapper, BTN_TOOL_PEN, 1);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6135
6136 // eraser trumps stylus
6137 processKey(mapper, BTN_TOOL_RUBBER, 1);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6142
6143 // mouse trumps eraser
6144 processKey(mapper, BTN_TOOL_MOUSE, 1);
6145 processSync(mapper);
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6149
6150 // back to default tool type
6151 processKey(mapper, BTN_TOOL_MOUSE, 0);
6152 processKey(mapper, BTN_TOOL_RUBBER, 0);
6153 processKey(mapper, BTN_TOOL_PEN, 0);
6154 processKey(mapper, BTN_TOOL_FINGER, 0);
6155 processSync(mapper);
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6159}
6160
6161TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162 addConfigurationProperty("touch.deviceType", "touchScreen");
6163 prepareDisplay(DISPLAY_ORIENTATION_0);
6164 prepareButtons();
6165 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006166 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006167 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168
6169 NotifyMotionArgs motionArgs;
6170
6171 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6172 processKey(mapper, BTN_TOOL_FINGER, 1);
6173 processMove(mapper, 100, 200);
6174 processSync(mapper);
6175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6176 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6178 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6179
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6183 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6184
6185 // move a little
6186 processMove(mapper, 150, 250);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6191 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6192
6193 // down when BTN_TOUCH is pressed, pressure defaults to 1
6194 processKey(mapper, BTN_TOUCH, 1);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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_DOWN, motionArgs.action);
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6204 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6205
6206 // up when BTN_TOUCH is released, hover restored
6207 processKey(mapper, BTN_TOUCH, 0);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6212 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6213
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6217 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6222 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6223
6224 // exit hover when pointer goes away
6225 processKey(mapper, BTN_TOOL_FINGER, 0);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6230 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6231}
6232
6233TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 addConfigurationProperty("touch.deviceType", "touchScreen");
6235 prepareDisplay(DISPLAY_ORIENTATION_0);
6236 prepareButtons();
6237 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006238 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239
6240 NotifyMotionArgs motionArgs;
6241
6242 // initially hovering because pressure is 0
6243 processDown(mapper, 100, 200);
6244 processPressure(mapper, 0);
6245 processSync(mapper);
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6249 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6250
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6254 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6255
6256 // move a little
6257 processMove(mapper, 150, 250);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6262 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6263
6264 // down when pressure is non-zero
6265 processPressure(mapper, RAW_PRESSURE_MAX);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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_DOWN, motionArgs.action);
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6275 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6276
6277 // up when pressure becomes 0, hover restored
6278 processPressure(mapper, 0);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6283 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6284
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6288 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6289
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6293 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6294
6295 // exit hover when pointer goes away
6296 processUp(mapper);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6301 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6302}
6303
Prabir Pradhan5632d622021-09-06 07:57:20 -07006304// --- TouchDisplayProjectionTest ---
6305
6306class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6307public:
6308 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6309 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6310 // rotated equivalent of the given un-rotated physical display bounds.
6311 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6312 uint32_t inverseRotationFlags;
6313 auto width = DISPLAY_WIDTH;
6314 auto height = DISPLAY_HEIGHT;
6315 switch (orientation) {
6316 case DISPLAY_ORIENTATION_90:
6317 inverseRotationFlags = ui::Transform::ROT_270;
6318 std::swap(width, height);
6319 break;
6320 case DISPLAY_ORIENTATION_180:
6321 inverseRotationFlags = ui::Transform::ROT_180;
6322 break;
6323 case DISPLAY_ORIENTATION_270:
6324 inverseRotationFlags = ui::Transform::ROT_90;
6325 std::swap(width, height);
6326 break;
6327 case DISPLAY_ORIENTATION_0:
6328 inverseRotationFlags = ui::Transform::ROT_0;
6329 break;
6330 default:
6331 FAIL() << "Invalid orientation: " << orientation;
6332 }
6333
6334 const ui::Transform rotation(inverseRotationFlags, width, height);
6335 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6336
6337 std::optional<DisplayViewport> internalViewport =
6338 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6339 DisplayViewport& v = *internalViewport;
6340 v.displayId = DISPLAY_ID;
6341 v.orientation = orientation;
6342
6343 v.logicalLeft = 0;
6344 v.logicalTop = 0;
6345 v.logicalRight = 100;
6346 v.logicalBottom = 100;
6347
6348 v.physicalLeft = rotatedPhysicalDisplay.left;
6349 v.physicalTop = rotatedPhysicalDisplay.top;
6350 v.physicalRight = rotatedPhysicalDisplay.right;
6351 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6352
6353 v.deviceWidth = width;
6354 v.deviceHeight = height;
6355
6356 v.isActive = true;
6357 v.uniqueId = UNIQUE_ID;
6358 v.type = ViewportType::INTERNAL;
6359 mFakePolicy->updateViewport(v);
6360 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6361 }
6362
6363 void assertReceivedMove(const Point& point) {
6364 NotifyMotionArgs motionArgs;
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6367 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6369 1, 0, 0, 0, 0, 0, 0, 0));
6370 }
6371};
6372
6373TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6374 addConfigurationProperty("touch.deviceType", "touchScreen");
6375 prepareDisplay(DISPLAY_ORIENTATION_0);
6376
6377 prepareButtons();
6378 prepareAxes(POSITION);
6379 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6380
6381 NotifyMotionArgs motionArgs;
6382
6383 // Configure the DisplayViewport such that the logical display maps to a subsection of
6384 // the display panel called the physical display. Here, the physical display is bounded by the
6385 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6386 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6387 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6388 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6389
6390 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6391 DISPLAY_ORIENTATION_270}) {
6392 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6393
6394 // Touches outside the physical display should be ignored, and should not generate any
6395 // events. Ensure touches at the following points that lie outside of the physical display
6396 // area do not generate any events.
6397 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6398 processDown(mapper, toRawX(point.x), toRawY(point.y));
6399 processSync(mapper);
6400 processUp(mapper);
6401 processSync(mapper);
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6403 << "Unexpected event generated for touch outside physical display at point: "
6404 << point.x << ", " << point.y;
6405 }
6406 }
6407}
6408
6409TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6410 addConfigurationProperty("touch.deviceType", "touchScreen");
6411 prepareDisplay(DISPLAY_ORIENTATION_0);
6412
6413 prepareButtons();
6414 prepareAxes(POSITION);
6415 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6416
6417 NotifyMotionArgs motionArgs;
6418
6419 // Configure the DisplayViewport such that the logical display maps to a subsection of
6420 // the display panel called the physical display. Here, the physical display is bounded by the
6421 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6422 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6423
6424 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6425 DISPLAY_ORIENTATION_270}) {
6426 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6427
6428 // Touches that start outside the physical display should be ignored until it enters the
6429 // physical display bounds, at which point it should generate a down event. Start a touch at
6430 // the point (5, 100), which is outside the physical display bounds.
6431 static const Point kOutsidePoint{5, 100};
6432 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6435
6436 // Move the touch into the physical display area. This should generate a pointer down.
6437 processMove(mapper, toRawX(11), toRawY(21));
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6441 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6442 ASSERT_NO_FATAL_FAILURE(
6443 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6444
6445 // Move the touch inside the physical display area. This should generate a pointer move.
6446 processMove(mapper, toRawX(69), toRawY(159));
6447 processSync(mapper);
6448 assertReceivedMove({69, 159});
6449
6450 // Move outside the physical display area. Since the pointer is already down, this should
6451 // now continue generating events.
6452 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6453 processSync(mapper);
6454 assertReceivedMove(kOutsidePoint);
6455
6456 // Release. This should generate a pointer up.
6457 processUp(mapper);
6458 processSync(mapper);
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6462 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6463
6464 // Ensure no more events were generated.
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6467 }
6468}
6469
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470// --- MultiTouchInputMapperTest ---
6471
6472class MultiTouchInputMapperTest : public TouchInputMapperTest {
6473protected:
6474 void prepareAxes(int axes);
6475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006476 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6477 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6478 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6479 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6480 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6481 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6482 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6483 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6484 void processId(MultiTouchInputMapper& mapper, int32_t id);
6485 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6486 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6487 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6488 void processMTSync(MultiTouchInputMapper& mapper);
6489 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490};
6491
6492void MultiTouchInputMapperTest::prepareAxes(int axes) {
6493 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006494 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6495 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 }
6497 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006498 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6499 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006501 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6502 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503 }
6504 }
6505 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6507 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006510 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 }
6512 }
6513 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006514 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6515 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516 }
6517 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006518 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6519 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520 }
6521 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006522 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6523 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524 }
6525 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006526 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6527 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528 }
6529 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006530 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6531 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532 }
6533 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006534 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006535 }
6536}
6537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006538void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6539 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542}
6543
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006544void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6545 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547}
6548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006549void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6550 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552}
6553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006554void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006556}
6557
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006558void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560}
6561
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006562void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6563 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006565}
6566
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006567void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006569}
6570
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006571void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006573}
6574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006575void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006577}
6578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006579void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006581}
6582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006583void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006585}
6586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006587void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6588 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590}
6591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006592void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006594}
6595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006596void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598}
6599
Michael Wrightd02c5b62014-02-10 15:10:22 -08006600TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006601 addConfigurationProperty("touch.deviceType", "touchScreen");
6602 prepareDisplay(DISPLAY_ORIENTATION_0);
6603 prepareAxes(POSITION);
6604 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006605 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006606
arthurhungdcef2dc2020-08-11 14:47:50 +08006607 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608
6609 NotifyMotionArgs motionArgs;
6610
6611 // Two fingers down at once.
6612 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6613 processPosition(mapper, x1, y1);
6614 processMTSync(mapper);
6615 processPosition(mapper, x2, y2);
6616 processMTSync(mapper);
6617 processSync(mapper);
6618
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6621 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6622 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6623 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6625 ASSERT_EQ(0, motionArgs.flags);
6626 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6627 ASSERT_EQ(0, motionArgs.buttonState);
6628 ASSERT_EQ(0, motionArgs.edgeFlags);
6629 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6633 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6634 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6635 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6636 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6637
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6639 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6640 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6641 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6642 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6644 motionArgs.action);
6645 ASSERT_EQ(0, motionArgs.flags);
6646 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6647 ASSERT_EQ(0, motionArgs.buttonState);
6648 ASSERT_EQ(0, motionArgs.edgeFlags);
6649 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6652 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6655 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6657 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6658 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6659 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6661
6662 // Move.
6663 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6664 processPosition(mapper, x1, y1);
6665 processMTSync(mapper);
6666 processPosition(mapper, x2, y2);
6667 processMTSync(mapper);
6668 processSync(mapper);
6669
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6671 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6672 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6673 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6674 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6676 ASSERT_EQ(0, motionArgs.flags);
6677 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6678 ASSERT_EQ(0, motionArgs.buttonState);
6679 ASSERT_EQ(0, motionArgs.edgeFlags);
6680 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6683 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6686 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6688 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6689 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6690 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6691 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6692
6693 // First finger up.
6694 x2 += 15; y2 -= 20;
6695 processPosition(mapper, x2, y2);
6696 processMTSync(mapper);
6697 processSync(mapper);
6698
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6701 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6702 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6703 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6704 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6705 motionArgs.action);
6706 ASSERT_EQ(0, motionArgs.flags);
6707 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6708 ASSERT_EQ(0, motionArgs.buttonState);
6709 ASSERT_EQ(0, motionArgs.edgeFlags);
6710 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6711 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6713 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6716 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6718 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6719 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6720 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6722
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6725 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6726 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6727 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6729 ASSERT_EQ(0, motionArgs.flags);
6730 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6731 ASSERT_EQ(0, motionArgs.buttonState);
6732 ASSERT_EQ(0, motionArgs.edgeFlags);
6733 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6734 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6738 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6739 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6741
6742 // Move.
6743 x2 += 20; y2 -= 25;
6744 processPosition(mapper, x2, y2);
6745 processMTSync(mapper);
6746 processSync(mapper);
6747
6748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6749 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6750 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6751 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6752 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6754 ASSERT_EQ(0, motionArgs.flags);
6755 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6756 ASSERT_EQ(0, motionArgs.buttonState);
6757 ASSERT_EQ(0, motionArgs.edgeFlags);
6758 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6759 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6762 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6763 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6764 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6765 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6766
6767 // New finger down.
6768 int32_t x3 = 700, y3 = 300;
6769 processPosition(mapper, x2, y2);
6770 processMTSync(mapper);
6771 processPosition(mapper, x3, y3);
6772 processMTSync(mapper);
6773 processSync(mapper);
6774
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6776 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6777 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6778 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6779 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6780 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6781 motionArgs.action);
6782 ASSERT_EQ(0, motionArgs.flags);
6783 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6784 ASSERT_EQ(0, motionArgs.buttonState);
6785 ASSERT_EQ(0, motionArgs.edgeFlags);
6786 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6789 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6792 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6794 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6795 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6796 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6797 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6798
6799 // Second finger up.
6800 x3 += 30; y3 -= 20;
6801 processPosition(mapper, x3, y3);
6802 processMTSync(mapper);
6803 processSync(mapper);
6804
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6806 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6807 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6808 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6809 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6810 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6811 motionArgs.action);
6812 ASSERT_EQ(0, motionArgs.flags);
6813 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6814 ASSERT_EQ(0, motionArgs.buttonState);
6815 ASSERT_EQ(0, motionArgs.edgeFlags);
6816 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6817 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6819 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6822 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6824 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6825 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6826 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6828
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6831 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6832 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6833 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6835 ASSERT_EQ(0, motionArgs.flags);
6836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6837 ASSERT_EQ(0, motionArgs.buttonState);
6838 ASSERT_EQ(0, motionArgs.edgeFlags);
6839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6843 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6844 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6845 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6847
6848 // Last finger up.
6849 processMTSync(mapper);
6850 processSync(mapper);
6851
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6854 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6855 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6856 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6857 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6858 ASSERT_EQ(0, motionArgs.flags);
6859 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6860 ASSERT_EQ(0, motionArgs.buttonState);
6861 ASSERT_EQ(0, motionArgs.edgeFlags);
6862 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6863 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6866 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6867 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6868 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6869 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6870
6871 // Should not have sent any more keys or motions.
6872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6874}
6875
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006876TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
6877 addConfigurationProperty("touch.deviceType", "touchScreen");
6878 prepareDisplay(DISPLAY_ORIENTATION_0);
6879
6880 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
6881 /*fuzz*/ 0, /*resolution*/ 10);
6882 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
6883 /*fuzz*/ 0, /*resolution*/ 11);
6884 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
6885 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
6886 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
6887 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
6888 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6889 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
6890 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6891 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
6892
6893 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6894
6895 // X and Y axes
6896 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
6897 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
6898 // Touch major and minor
6899 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
6900 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
6901 // Tool major and minor
6902 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
6903 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
6904}
6905
6906TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
6907 addConfigurationProperty("touch.deviceType", "touchScreen");
6908 prepareDisplay(DISPLAY_ORIENTATION_0);
6909
6910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
6911 /*fuzz*/ 0, /*resolution*/ 10);
6912 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
6913 /*fuzz*/ 0, /*resolution*/ 11);
6914
6915 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
6916
6917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6918
6919 // Touch major and minor
6920 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
6921 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
6922 // Tool major and minor
6923 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
6924 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
6925}
6926
Michael Wrightd02c5b62014-02-10 15:10:22 -08006927TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928 addConfigurationProperty("touch.deviceType", "touchScreen");
6929 prepareDisplay(DISPLAY_ORIENTATION_0);
6930 prepareAxes(POSITION | ID);
6931 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006932 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933
arthurhungdcef2dc2020-08-11 14:47:50 +08006934 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006935
6936 NotifyMotionArgs motionArgs;
6937
6938 // Two fingers down at once.
6939 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6940 processPosition(mapper, x1, y1);
6941 processId(mapper, 1);
6942 processMTSync(mapper);
6943 processPosition(mapper, x2, y2);
6944 processId(mapper, 2);
6945 processMTSync(mapper);
6946 processSync(mapper);
6947
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6950 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6951 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6954 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6955
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6957 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6958 motionArgs.action);
6959 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6960 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6962 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6965 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6967 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6968
6969 // Move.
6970 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6971 processPosition(mapper, x1, y1);
6972 processId(mapper, 1);
6973 processMTSync(mapper);
6974 processPosition(mapper, x2, y2);
6975 processId(mapper, 2);
6976 processMTSync(mapper);
6977 processSync(mapper);
6978
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6981 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6982 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6984 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6985 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6987 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6989 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6990
6991 // First finger up.
6992 x2 += 15; y2 -= 20;
6993 processPosition(mapper, x2, y2);
6994 processId(mapper, 2);
6995 processMTSync(mapper);
6996 processSync(mapper);
6997
6998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6999 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7000 motionArgs.action);
7001 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7002 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7004 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7007 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7009 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7010
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7013 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7014 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7017 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7018
7019 // Move.
7020 x2 += 20; y2 -= 25;
7021 processPosition(mapper, x2, y2);
7022 processId(mapper, 2);
7023 processMTSync(mapper);
7024 processSync(mapper);
7025
7026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7028 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7029 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7032 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7033
7034 // New finger down.
7035 int32_t x3 = 700, y3 = 300;
7036 processPosition(mapper, x2, y2);
7037 processId(mapper, 2);
7038 processMTSync(mapper);
7039 processPosition(mapper, x3, y3);
7040 processId(mapper, 3);
7041 processMTSync(mapper);
7042 processSync(mapper);
7043
7044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7045 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7046 motionArgs.action);
7047 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7048 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7050 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7053 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7055 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7056
7057 // Second finger up.
7058 x3 += 30; y3 -= 20;
7059 processPosition(mapper, x3, y3);
7060 processId(mapper, 3);
7061 processMTSync(mapper);
7062 processSync(mapper);
7063
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7065 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7066 motionArgs.action);
7067 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7068 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7070 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7073 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7075 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7076
7077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7079 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7080 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7083 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7084
7085 // Last finger up.
7086 processMTSync(mapper);
7087 processSync(mapper);
7088
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7091 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7095 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7096
7097 // Should not have sent any more keys or motions.
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7100}
7101
7102TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 addConfigurationProperty("touch.deviceType", "touchScreen");
7104 prepareDisplay(DISPLAY_ORIENTATION_0);
7105 prepareAxes(POSITION | ID | SLOT);
7106 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007107 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108
arthurhungdcef2dc2020-08-11 14:47:50 +08007109 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110
7111 NotifyMotionArgs motionArgs;
7112
7113 // Two fingers down at once.
7114 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7115 processPosition(mapper, x1, y1);
7116 processId(mapper, 1);
7117 processSlot(mapper, 1);
7118 processPosition(mapper, x2, y2);
7119 processId(mapper, 2);
7120 processSync(mapper);
7121
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7124 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7125 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7128 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7129
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7131 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7132 motionArgs.action);
7133 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7134 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7136 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7139 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7141 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7142
7143 // Move.
7144 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7145 processSlot(mapper, 0);
7146 processPosition(mapper, x1, y1);
7147 processSlot(mapper, 1);
7148 processPosition(mapper, x2, y2);
7149 processSync(mapper);
7150
7151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7153 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7154 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7156 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7159 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7161 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7162
7163 // First finger up.
7164 x2 += 15; y2 -= 20;
7165 processSlot(mapper, 0);
7166 processId(mapper, -1);
7167 processSlot(mapper, 1);
7168 processPosition(mapper, x2, y2);
7169 processSync(mapper);
7170
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7172 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7173 motionArgs.action);
7174 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7177 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7180 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7182 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7183
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7186 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7187 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7190 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7191
7192 // Move.
7193 x2 += 20; y2 -= 25;
7194 processPosition(mapper, x2, y2);
7195 processSync(mapper);
7196
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7199 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7200 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7203 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7204
7205 // New finger down.
7206 int32_t x3 = 700, y3 = 300;
7207 processPosition(mapper, x2, y2);
7208 processSlot(mapper, 0);
7209 processId(mapper, 3);
7210 processPosition(mapper, x3, y3);
7211 processSync(mapper);
7212
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7214 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7215 motionArgs.action);
7216 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7217 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7219 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7222 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7224 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7225
7226 // Second finger up.
7227 x3 += 30; y3 -= 20;
7228 processSlot(mapper, 1);
7229 processId(mapper, -1);
7230 processSlot(mapper, 0);
7231 processPosition(mapper, x3, y3);
7232 processSync(mapper);
7233
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7235 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7236 motionArgs.action);
7237 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7240 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7243 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7245 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7246
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7249 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7250 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7253 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7254
7255 // Last finger up.
7256 processId(mapper, -1);
7257 processSync(mapper);
7258
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7260 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7261 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7265 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7266
7267 // Should not have sent any more keys or motions.
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7270}
7271
7272TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007273 addConfigurationProperty("touch.deviceType", "touchScreen");
7274 prepareDisplay(DISPLAY_ORIENTATION_0);
7275 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007276 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007277
7278 // These calculations are based on the input device calibration documentation.
7279 int32_t rawX = 100;
7280 int32_t rawY = 200;
7281 int32_t rawTouchMajor = 7;
7282 int32_t rawTouchMinor = 6;
7283 int32_t rawToolMajor = 9;
7284 int32_t rawToolMinor = 8;
7285 int32_t rawPressure = 11;
7286 int32_t rawDistance = 0;
7287 int32_t rawOrientation = 3;
7288 int32_t id = 5;
7289
7290 float x = toDisplayX(rawX);
7291 float y = toDisplayY(rawY);
7292 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7293 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7294 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7295 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7296 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7297 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7298 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7299 float distance = float(rawDistance);
7300
7301 processPosition(mapper, rawX, rawY);
7302 processTouchMajor(mapper, rawTouchMajor);
7303 processTouchMinor(mapper, rawTouchMinor);
7304 processToolMajor(mapper, rawToolMajor);
7305 processToolMinor(mapper, rawToolMinor);
7306 processPressure(mapper, rawPressure);
7307 processOrientation(mapper, rawOrientation);
7308 processDistance(mapper, rawDistance);
7309 processId(mapper, id);
7310 processMTSync(mapper);
7311 processSync(mapper);
7312
7313 NotifyMotionArgs args;
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7315 ASSERT_EQ(0, args.pointerProperties[0].id);
7316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7317 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7318 orientation, distance));
7319}
7320
7321TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007322 addConfigurationProperty("touch.deviceType", "touchScreen");
7323 prepareDisplay(DISPLAY_ORIENTATION_0);
7324 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7325 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007326 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007327
7328 // These calculations are based on the input device calibration documentation.
7329 int32_t rawX = 100;
7330 int32_t rawY = 200;
7331 int32_t rawTouchMajor = 140;
7332 int32_t rawTouchMinor = 120;
7333 int32_t rawToolMajor = 180;
7334 int32_t rawToolMinor = 160;
7335
7336 float x = toDisplayX(rawX);
7337 float y = toDisplayY(rawY);
7338 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7339 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7340 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7341 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7342 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7343
7344 processPosition(mapper, rawX, rawY);
7345 processTouchMajor(mapper, rawTouchMajor);
7346 processTouchMinor(mapper, rawTouchMinor);
7347 processToolMajor(mapper, rawToolMajor);
7348 processToolMinor(mapper, rawToolMinor);
7349 processMTSync(mapper);
7350 processSync(mapper);
7351
7352 NotifyMotionArgs args;
7353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7355 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7356}
7357
7358TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007359 addConfigurationProperty("touch.deviceType", "touchScreen");
7360 prepareDisplay(DISPLAY_ORIENTATION_0);
7361 prepareAxes(POSITION | TOUCH | TOOL);
7362 addConfigurationProperty("touch.size.calibration", "diameter");
7363 addConfigurationProperty("touch.size.scale", "10");
7364 addConfigurationProperty("touch.size.bias", "160");
7365 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007366 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007367
7368 // These calculations are based on the input device calibration documentation.
7369 // Note: We only provide a single common touch/tool value because the device is assumed
7370 // not to emit separate values for each pointer (isSummed = 1).
7371 int32_t rawX = 100;
7372 int32_t rawY = 200;
7373 int32_t rawX2 = 150;
7374 int32_t rawY2 = 250;
7375 int32_t rawTouchMajor = 5;
7376 int32_t rawToolMajor = 8;
7377
7378 float x = toDisplayX(rawX);
7379 float y = toDisplayY(rawY);
7380 float x2 = toDisplayX(rawX2);
7381 float y2 = toDisplayY(rawY2);
7382 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7383 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7384 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7385
7386 processPosition(mapper, rawX, rawY);
7387 processTouchMajor(mapper, rawTouchMajor);
7388 processToolMajor(mapper, rawToolMajor);
7389 processMTSync(mapper);
7390 processPosition(mapper, rawX2, rawY2);
7391 processTouchMajor(mapper, rawTouchMajor);
7392 processToolMajor(mapper, rawToolMajor);
7393 processMTSync(mapper);
7394 processSync(mapper);
7395
7396 NotifyMotionArgs args;
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7398 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7399
7400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7401 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7402 args.action);
7403 ASSERT_EQ(size_t(2), args.pointerCount);
7404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7405 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7407 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7408}
7409
7410TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007411 addConfigurationProperty("touch.deviceType", "touchScreen");
7412 prepareDisplay(DISPLAY_ORIENTATION_0);
7413 prepareAxes(POSITION | TOUCH | TOOL);
7414 addConfigurationProperty("touch.size.calibration", "area");
7415 addConfigurationProperty("touch.size.scale", "43");
7416 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007417 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418
7419 // These calculations are based on the input device calibration documentation.
7420 int32_t rawX = 100;
7421 int32_t rawY = 200;
7422 int32_t rawTouchMajor = 5;
7423 int32_t rawToolMajor = 8;
7424
7425 float x = toDisplayX(rawX);
7426 float y = toDisplayY(rawY);
7427 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7428 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7429 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7430
7431 processPosition(mapper, rawX, rawY);
7432 processTouchMajor(mapper, rawTouchMajor);
7433 processToolMajor(mapper, rawToolMajor);
7434 processMTSync(mapper);
7435 processSync(mapper);
7436
7437 NotifyMotionArgs args;
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7440 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7441}
7442
7443TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007444 addConfigurationProperty("touch.deviceType", "touchScreen");
7445 prepareDisplay(DISPLAY_ORIENTATION_0);
7446 prepareAxes(POSITION | PRESSURE);
7447 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7448 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007449 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007450
Michael Wrightaa449c92017-12-13 21:21:43 +00007451 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007452 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007453 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7454 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7455 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7456
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457 // These calculations are based on the input device calibration documentation.
7458 int32_t rawX = 100;
7459 int32_t rawY = 200;
7460 int32_t rawPressure = 60;
7461
7462 float x = toDisplayX(rawX);
7463 float y = toDisplayY(rawY);
7464 float pressure = float(rawPressure) * 0.01f;
7465
7466 processPosition(mapper, rawX, rawY);
7467 processPressure(mapper, rawPressure);
7468 processMTSync(mapper);
7469 processSync(mapper);
7470
7471 NotifyMotionArgs args;
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7474 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7475}
7476
7477TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007478 addConfigurationProperty("touch.deviceType", "touchScreen");
7479 prepareDisplay(DISPLAY_ORIENTATION_0);
7480 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007481 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007482
7483 NotifyMotionArgs motionArgs;
7484 NotifyKeyArgs keyArgs;
7485
7486 processId(mapper, 1);
7487 processPosition(mapper, 100, 200);
7488 processSync(mapper);
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7491 ASSERT_EQ(0, motionArgs.buttonState);
7492
7493 // press BTN_LEFT, release BTN_LEFT
7494 processKey(mapper, BTN_LEFT, 1);
7495 processSync(mapper);
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7498 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7499
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7501 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7502 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7503
Michael Wrightd02c5b62014-02-10 15:10:22 -08007504 processKey(mapper, BTN_LEFT, 0);
7505 processSync(mapper);
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007508 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007509
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007512 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007513
7514 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7515 processKey(mapper, BTN_RIGHT, 1);
7516 processKey(mapper, BTN_MIDDLE, 1);
7517 processSync(mapper);
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7520 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7521 motionArgs.buttonState);
7522
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7525 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7526
7527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7528 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7529 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7530 motionArgs.buttonState);
7531
Michael Wrightd02c5b62014-02-10 15:10:22 -08007532 processKey(mapper, BTN_RIGHT, 0);
7533 processSync(mapper);
7534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007537
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007540 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541
7542 processKey(mapper, BTN_MIDDLE, 0);
7543 processSync(mapper);
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007547
7548 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007551
7552 // press BTN_BACK, release BTN_BACK
7553 processKey(mapper, BTN_BACK, 1);
7554 processSync(mapper);
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7556 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7557 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007558
Michael Wrightd02c5b62014-02-10 15:10:22 -08007559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007561 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7562
7563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7565 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566
7567 processKey(mapper, BTN_BACK, 0);
7568 processSync(mapper);
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007571 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007572
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007575 ASSERT_EQ(0, motionArgs.buttonState);
7576
Michael Wrightd02c5b62014-02-10 15:10:22 -08007577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7578 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7579 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7580
7581 // press BTN_SIDE, release BTN_SIDE
7582 processKey(mapper, BTN_SIDE, 1);
7583 processSync(mapper);
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7585 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7586 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007587
Michael Wrightd02c5b62014-02-10 15:10:22 -08007588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007590 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7591
7592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7594 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007595
7596 processKey(mapper, BTN_SIDE, 0);
7597 processSync(mapper);
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007600 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007601
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007604 ASSERT_EQ(0, motionArgs.buttonState);
7605
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7607 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7608 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7609
7610 // press BTN_FORWARD, release BTN_FORWARD
7611 processKey(mapper, BTN_FORWARD, 1);
7612 processSync(mapper);
7613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7614 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7615 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007616
Michael Wrightd02c5b62014-02-10 15:10:22 -08007617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7620
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7622 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7623 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624
7625 processKey(mapper, BTN_FORWARD, 0);
7626 processSync(mapper);
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007628 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007629 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007630
7631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007633 ASSERT_EQ(0, motionArgs.buttonState);
7634
Michael Wrightd02c5b62014-02-10 15:10:22 -08007635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7636 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7637 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7638
7639 // press BTN_EXTRA, release BTN_EXTRA
7640 processKey(mapper, BTN_EXTRA, 1);
7641 processSync(mapper);
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7643 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7644 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007645
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007648 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7649
7650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7651 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7652 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653
7654 processKey(mapper, BTN_EXTRA, 0);
7655 processSync(mapper);
7656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007658 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007659
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007662 ASSERT_EQ(0, motionArgs.buttonState);
7663
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7665 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7666 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7667
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7669
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670 // press BTN_STYLUS, release BTN_STYLUS
7671 processKey(mapper, BTN_STYLUS, 1);
7672 processSync(mapper);
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007675 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7676
7677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7679 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680
7681 processKey(mapper, BTN_STYLUS, 0);
7682 processSync(mapper);
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007686
7687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007689 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690
7691 // press BTN_STYLUS2, release BTN_STYLUS2
7692 processKey(mapper, BTN_STYLUS2, 1);
7693 processSync(mapper);
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007696 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7697
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7700 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701
7702 processKey(mapper, BTN_STYLUS2, 0);
7703 processSync(mapper);
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007705 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007706 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007707
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007710 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711
7712 // release touch
7713 processId(mapper, -1);
7714 processSync(mapper);
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7716 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7717 ASSERT_EQ(0, motionArgs.buttonState);
7718}
7719
7720TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007721 addConfigurationProperty("touch.deviceType", "touchScreen");
7722 prepareDisplay(DISPLAY_ORIENTATION_0);
7723 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725
7726 NotifyMotionArgs motionArgs;
7727
7728 // default tool type is finger
7729 processId(mapper, 1);
7730 processPosition(mapper, 100, 200);
7731 processSync(mapper);
7732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7733 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7735
7736 // eraser
7737 processKey(mapper, BTN_TOOL_RUBBER, 1);
7738 processSync(mapper);
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7742
7743 // stylus
7744 processKey(mapper, BTN_TOOL_RUBBER, 0);
7745 processKey(mapper, BTN_TOOL_PEN, 1);
7746 processSync(mapper);
7747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7750
7751 // brush
7752 processKey(mapper, BTN_TOOL_PEN, 0);
7753 processKey(mapper, BTN_TOOL_BRUSH, 1);
7754 processSync(mapper);
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7758
7759 // pencil
7760 processKey(mapper, BTN_TOOL_BRUSH, 0);
7761 processKey(mapper, BTN_TOOL_PENCIL, 1);
7762 processSync(mapper);
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7766
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007767 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 processKey(mapper, BTN_TOOL_PENCIL, 0);
7769 processKey(mapper, BTN_TOOL_AIRBRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
7774
7775 // mouse
7776 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7777 processKey(mapper, BTN_TOOL_MOUSE, 1);
7778 processSync(mapper);
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7782
7783 // lens
7784 processKey(mapper, BTN_TOOL_MOUSE, 0);
7785 processKey(mapper, BTN_TOOL_LENS, 1);
7786 processSync(mapper);
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7790
7791 // double-tap
7792 processKey(mapper, BTN_TOOL_LENS, 0);
7793 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7794 processSync(mapper);
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7798
7799 // triple-tap
7800 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7801 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7802 processSync(mapper);
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7806
7807 // quad-tap
7808 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7809 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7810 processSync(mapper);
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7814
7815 // finger
7816 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7817 processKey(mapper, BTN_TOOL_FINGER, 1);
7818 processSync(mapper);
7819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7822
7823 // stylus trumps finger
7824 processKey(mapper, BTN_TOOL_PEN, 1);
7825 processSync(mapper);
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7829
7830 // eraser trumps stylus
7831 processKey(mapper, BTN_TOOL_RUBBER, 1);
7832 processSync(mapper);
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7836
7837 // mouse trumps eraser
7838 processKey(mapper, BTN_TOOL_MOUSE, 1);
7839 processSync(mapper);
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7843
7844 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7845 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7846 processSync(mapper);
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7850
7851 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7852 processToolType(mapper, MT_TOOL_PEN);
7853 processSync(mapper);
7854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7857
7858 // back to default tool type
7859 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7860 processKey(mapper, BTN_TOOL_MOUSE, 0);
7861 processKey(mapper, BTN_TOOL_RUBBER, 0);
7862 processKey(mapper, BTN_TOOL_PEN, 0);
7863 processKey(mapper, BTN_TOOL_FINGER, 0);
7864 processSync(mapper);
7865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7868}
7869
7870TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 addConfigurationProperty("touch.deviceType", "touchScreen");
7872 prepareDisplay(DISPLAY_ORIENTATION_0);
7873 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007874 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007875 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007876
7877 NotifyMotionArgs motionArgs;
7878
7879 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7880 processId(mapper, 1);
7881 processPosition(mapper, 100, 200);
7882 processSync(mapper);
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7884 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7886 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7887
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7889 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7891 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7892
7893 // move a little
7894 processPosition(mapper, 150, 250);
7895 processSync(mapper);
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7899 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7900
7901 // down when BTN_TOUCH is pressed, pressure defaults to 1
7902 processKey(mapper, BTN_TOUCH, 1);
7903 processSync(mapper);
7904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7907 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7908
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7912 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7913
7914 // up when BTN_TOUCH is released, hover restored
7915 processKey(mapper, BTN_TOUCH, 0);
7916 processSync(mapper);
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7918 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7920 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7921
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7925 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7926
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7928 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7930 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7931
7932 // exit hover when pointer goes away
7933 processId(mapper, -1);
7934 processSync(mapper);
7935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7938 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7939}
7940
7941TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942 addConfigurationProperty("touch.deviceType", "touchScreen");
7943 prepareDisplay(DISPLAY_ORIENTATION_0);
7944 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946
7947 NotifyMotionArgs motionArgs;
7948
7949 // initially hovering because pressure is 0
7950 processId(mapper, 1);
7951 processPosition(mapper, 100, 200);
7952 processPressure(mapper, 0);
7953 processSync(mapper);
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7957 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7958
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7962 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7963
7964 // move a little
7965 processPosition(mapper, 150, 250);
7966 processSync(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7968 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7970 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7971
7972 // down when pressure becomes non-zero
7973 processPressure(mapper, RAW_PRESSURE_MAX);
7974 processSync(mapper);
7975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7976 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7978 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7979
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7983 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7984
7985 // up when pressure becomes 0, hover restored
7986 processPressure(mapper, 0);
7987 processSync(mapper);
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7989 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7991 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7992
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7996 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7997
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8001 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8002
8003 // exit hover when pointer goes away
8004 processId(mapper, -1);
8005 processSync(mapper);
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8009 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8010}
8011
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008012/**
8013 * Set the input device port <--> display port associations, and check that the
8014 * events are routed to the display that matches the display port.
8015 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8016 */
8017TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008018 const std::string usb2 = "USB2";
8019 const uint8_t hdmi1 = 0;
8020 const uint8_t hdmi2 = 1;
8021 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008022 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008023
8024 addConfigurationProperty("touch.deviceType", "touchScreen");
8025 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008026 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008027
8028 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8029 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8030
8031 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8032 // for this input device is specified, and the matching viewport is not present,
8033 // the input device should be disabled (at the mapper level).
8034
8035 // Add viewport for display 2 on hdmi2
8036 prepareSecondaryDisplay(type, hdmi2);
8037 // Send a touch event
8038 processPosition(mapper, 100, 100);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8041
8042 // Add viewport for display 1 on hdmi1
8043 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8044 // Send a touch event again
8045 processPosition(mapper, 100, 100);
8046 processSync(mapper);
8047
8048 NotifyMotionArgs args;
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8050 ASSERT_EQ(DISPLAY_ID, args.displayId);
8051}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008053TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008054 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008055 std::shared_ptr<FakePointerController> fakePointerController =
8056 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008057 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008058 fakePointerController->setPosition(100, 200);
8059 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008060 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008061
Garfield Tan888a6a42020-01-09 11:39:16 -08008062 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008063 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008064
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008065 prepareDisplay(DISPLAY_ORIENTATION_0);
8066 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008067 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008068
8069 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008070 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008071
8072 NotifyMotionArgs motionArgs;
8073 processPosition(mapper, 100, 100);
8074 processSync(mapper);
8075
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8078 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8079}
8080
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008081/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008082 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8083 */
8084TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8085 addConfigurationProperty("touch.deviceType", "touchScreen");
8086 prepareAxes(POSITION);
8087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8088
8089 prepareDisplay(DISPLAY_ORIENTATION_0);
8090 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8091 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8092 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8093 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8094
8095 NotifyMotionArgs args;
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8097 ASSERT_EQ(26, args.readTime);
8098
8099 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8100 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8101 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8102
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8104 ASSERT_EQ(33, args.readTime);
8105}
8106
8107/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008108 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8109 * events should not be delivered to the listener.
8110 */
8111TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8112 addConfigurationProperty("touch.deviceType", "touchScreen");
8113 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8114 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8115 ViewportType::INTERNAL);
8116 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8117 prepareAxes(POSITION);
8118 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8119
8120 NotifyMotionArgs motionArgs;
8121 processPosition(mapper, 100, 100);
8122 processSync(mapper);
8123
8124 mFakeListener->assertNotifyMotionWasNotCalled();
8125}
8126
Garfield Tanc734e4f2021-01-15 20:01:39 -08008127TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8128 addConfigurationProperty("touch.deviceType", "touchScreen");
8129 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8130 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8131 ViewportType::INTERNAL);
8132 std::optional<DisplayViewport> optionalDisplayViewport =
8133 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8134 ASSERT_TRUE(optionalDisplayViewport.has_value());
8135 DisplayViewport displayViewport = *optionalDisplayViewport;
8136
8137 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8138 prepareAxes(POSITION);
8139 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8140
8141 // Finger down
8142 int32_t x = 100, y = 100;
8143 processPosition(mapper, x, y);
8144 processSync(mapper);
8145
8146 NotifyMotionArgs motionArgs;
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8149
8150 // Deactivate display viewport
8151 displayViewport.isActive = false;
8152 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8153 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8154
8155 // Finger move
8156 x += 10, y += 10;
8157 processPosition(mapper, x, y);
8158 processSync(mapper);
8159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8161 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8162
8163 // Reactivate display viewport
8164 displayViewport.isActive = true;
8165 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8166 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8167
8168 // Finger move again
8169 x += 10, y += 10;
8170 processPosition(mapper, x, y);
8171 processSync(mapper);
8172
8173 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8174 // no pointer on the touch device.
8175 mFakeListener->assertNotifyMotionWasNotCalled();
8176}
8177
Arthur Hung7c645402019-01-25 17:45:42 +08008178TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8179 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008180 prepareAxes(POSITION | ID | SLOT);
8181 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008183
8184 // Create the second touch screen device, and enable multi fingers.
8185 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008186 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008187 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008188 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008189 std::shared_ptr<InputDevice> device2 =
8190 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8191 Flags<InputDeviceClass>(0));
8192
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008193 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8194 0 /*flat*/, 0 /*fuzz*/);
8195 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8196 0 /*flat*/, 0 /*fuzz*/);
8197 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8198 0 /*flat*/, 0 /*fuzz*/);
8199 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8200 0 /*flat*/, 0 /*fuzz*/);
8201 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8202 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8203 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008204
8205 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008206 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008207 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8208 device2->reset(ARBITRARY_TIME);
8209
8210 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008211 std::shared_ptr<FakePointerController> fakePointerController =
8212 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008213 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008214
8215 // Setup policy for associated displays and show touches.
8216 const uint8_t hdmi1 = 0;
8217 const uint8_t hdmi2 = 1;
8218 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8219 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8220 mFakePolicy->setShowTouches(true);
8221
8222 // Create displays.
8223 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008224 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008225
8226 // Default device will reconfigure above, need additional reconfiguration for another device.
8227 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008228 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008229
8230 // Two fingers down at default display.
8231 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8232 processPosition(mapper, x1, y1);
8233 processId(mapper, 1);
8234 processSlot(mapper, 1);
8235 processPosition(mapper, x2, y2);
8236 processId(mapper, 2);
8237 processSync(mapper);
8238
8239 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8240 fakePointerController->getSpots().find(DISPLAY_ID);
8241 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8242 ASSERT_EQ(size_t(2), iter->second.size());
8243
8244 // Two fingers down at second display.
8245 processPosition(mapper2, x1, y1);
8246 processId(mapper2, 1);
8247 processSlot(mapper2, 1);
8248 processPosition(mapper2, x2, y2);
8249 processId(mapper2, 2);
8250 processSync(mapper2);
8251
8252 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8253 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8254 ASSERT_EQ(size_t(2), iter->second.size());
8255}
8256
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008257TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008258 prepareAxes(POSITION);
8259 addConfigurationProperty("touch.deviceType", "touchScreen");
8260 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008261 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008262
8263 NotifyMotionArgs motionArgs;
8264 // Unrotated video frame
8265 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8266 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008267 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008268 processPosition(mapper, 100, 200);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8271 ASSERT_EQ(frames, motionArgs.videoFrames);
8272
8273 // Subsequent touch events should not have any videoframes
8274 // This is implemented separately in FakeEventHub,
8275 // but that should match the behaviour of TouchVideoDevice.
8276 processPosition(mapper, 200, 200);
8277 processSync(mapper);
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8279 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8280}
8281
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008282TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008283 prepareAxes(POSITION);
8284 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008285 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008286 // Unrotated video frame
8287 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8288 NotifyMotionArgs motionArgs;
8289
8290 // Test all 4 orientations
8291 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008292 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8293 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8294 clearViewports();
8295 prepareDisplay(orientation);
8296 std::vector<TouchVideoFrame> frames{frame};
8297 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8298 processPosition(mapper, 100, 200);
8299 processSync(mapper);
8300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8301 ASSERT_EQ(frames, motionArgs.videoFrames);
8302 }
8303}
8304
8305TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8306 prepareAxes(POSITION);
8307 addConfigurationProperty("touch.deviceType", "touchScreen");
8308 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8309 // orientation-aware are affected by display rotation.
8310 addConfigurationProperty("touch.orientationAware", "0");
8311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8312 // Unrotated video frame
8313 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8314 NotifyMotionArgs motionArgs;
8315
8316 // Test all 4 orientations
8317 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008318 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8319 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8320 clearViewports();
8321 prepareDisplay(orientation);
8322 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008323 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008324 processPosition(mapper, 100, 200);
8325 processSync(mapper);
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008327 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8328 // compared to the display. This is so that when the window transform (which contains the
8329 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8330 // window's coordinate space.
8331 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008332 ASSERT_EQ(frames, motionArgs.videoFrames);
8333 }
8334}
8335
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008336TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008337 prepareAxes(POSITION);
8338 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008339 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008340 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8341 // so mix these.
8342 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8343 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8344 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8345 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8346 NotifyMotionArgs motionArgs;
8347
8348 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008349 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008350 processPosition(mapper, 100, 200);
8351 processSync(mapper);
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008353 ASSERT_EQ(frames, motionArgs.videoFrames);
8354}
8355
8356TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8357 prepareAxes(POSITION);
8358 addConfigurationProperty("touch.deviceType", "touchScreen");
8359 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8360 // orientation-aware are affected by display rotation.
8361 addConfigurationProperty("touch.orientationAware", "0");
8362 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8363 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8364 // so mix these.
8365 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8366 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8367 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8368 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8369 NotifyMotionArgs motionArgs;
8370
8371 prepareDisplay(DISPLAY_ORIENTATION_90);
8372 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8373 processPosition(mapper, 100, 200);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8377 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8378 // compared to the display. This is so that when the window transform (which contains the
8379 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8380 // window's coordinate space.
8381 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8382 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008383 ASSERT_EQ(frames, motionArgs.videoFrames);
8384}
8385
Arthur Hung9da14732019-09-02 16:16:58 +08008386/**
8387 * If we had defined port associations, but the viewport is not ready, the touch device would be
8388 * expected to be disabled, and it should be enabled after the viewport has found.
8389 */
8390TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008391 constexpr uint8_t hdmi2 = 1;
8392 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008393 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008394
8395 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8396
8397 addConfigurationProperty("touch.deviceType", "touchScreen");
8398 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008399 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008400
8401 ASSERT_EQ(mDevice->isEnabled(), false);
8402
8403 // Add display on hdmi2, the device should be enabled and can receive touch event.
8404 prepareSecondaryDisplay(type, hdmi2);
8405 ASSERT_EQ(mDevice->isEnabled(), true);
8406
8407 // Send a touch event.
8408 processPosition(mapper, 100, 100);
8409 processSync(mapper);
8410
8411 NotifyMotionArgs args;
8412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8413 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8414}
8415
Arthur Hung421eb1c2020-01-16 00:09:42 +08008416TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008417 addConfigurationProperty("touch.deviceType", "touchScreen");
8418 prepareDisplay(DISPLAY_ORIENTATION_0);
8419 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008420 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008421
8422 NotifyMotionArgs motionArgs;
8423
8424 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8425 // finger down
8426 processId(mapper, 1);
8427 processPosition(mapper, x1, y1);
8428 processSync(mapper);
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8432
8433 // finger move
8434 processId(mapper, 1);
8435 processPosition(mapper, x2, y2);
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8440
8441 // finger up.
8442 processId(mapper, -1);
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8447
8448 // new finger down
8449 processId(mapper, 1);
8450 processPosition(mapper, x3, y3);
8451 processSync(mapper);
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8455}
8456
8457/**
arthurhungcc7f9802020-04-30 17:55:40 +08008458 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8459 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008460 */
arthurhungcc7f9802020-04-30 17:55:40 +08008461TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008462 addConfigurationProperty("touch.deviceType", "touchScreen");
8463 prepareDisplay(DISPLAY_ORIENTATION_0);
8464 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008465 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008466
8467 NotifyMotionArgs motionArgs;
8468
8469 // default tool type is finger
8470 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008471 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008472 processPosition(mapper, x1, y1);
8473 processSync(mapper);
8474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8477
8478 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8479 processToolType(mapper, MT_TOOL_PALM);
8480 processSync(mapper);
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8482 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8483
8484 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008485 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008486 processPosition(mapper, x2, y2);
8487 processSync(mapper);
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8489
8490 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008491 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8494
8495 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008496 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008497 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008498 processPosition(mapper, x3, y3);
8499 processSync(mapper);
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8503}
8504
arthurhungbf89a482020-04-17 17:37:55 +08008505/**
arthurhungcc7f9802020-04-30 17:55:40 +08008506 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8507 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008508 */
arthurhungcc7f9802020-04-30 17:55:40 +08008509TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008510 addConfigurationProperty("touch.deviceType", "touchScreen");
8511 prepareDisplay(DISPLAY_ORIENTATION_0);
8512 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8514
8515 NotifyMotionArgs motionArgs;
8516
8517 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008518 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8519 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008520 processPosition(mapper, x1, y1);
8521 processSync(mapper);
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8523 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8525
8526 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008527 processSlot(mapper, SECOND_SLOT);
8528 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008529 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008530 processSync(mapper);
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8532 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8533 motionArgs.action);
8534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8535
8536 // If the tool type of the first finger changes to MT_TOOL_PALM,
8537 // we expect to receive ACTION_POINTER_UP with cancel flag.
8538 processSlot(mapper, FIRST_SLOT);
8539 processId(mapper, FIRST_TRACKING_ID);
8540 processToolType(mapper, MT_TOOL_PALM);
8541 processSync(mapper);
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8544 motionArgs.action);
8545 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8546
8547 // The following MOVE events of second finger should be processed.
8548 processSlot(mapper, SECOND_SLOT);
8549 processId(mapper, SECOND_TRACKING_ID);
8550 processPosition(mapper, x2 + 1, y2 + 1);
8551 processSync(mapper);
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8554 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8555
8556 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8557 // it. Second finger receive move.
8558 processSlot(mapper, FIRST_SLOT);
8559 processId(mapper, INVALID_TRACKING_ID);
8560 processSync(mapper);
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8563 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8564
8565 // Second finger keeps moving.
8566 processSlot(mapper, SECOND_SLOT);
8567 processId(mapper, SECOND_TRACKING_ID);
8568 processPosition(mapper, x2 + 2, y2 + 2);
8569 processSync(mapper);
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8572 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8573
8574 // Second finger up.
8575 processId(mapper, INVALID_TRACKING_ID);
8576 processSync(mapper);
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8579 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8580}
8581
8582/**
8583 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8584 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8585 */
8586TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8587 addConfigurationProperty("touch.deviceType", "touchScreen");
8588 prepareDisplay(DISPLAY_ORIENTATION_0);
8589 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8591
8592 NotifyMotionArgs motionArgs;
8593
8594 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8595 // First finger down.
8596 processId(mapper, FIRST_TRACKING_ID);
8597 processPosition(mapper, x1, y1);
8598 processSync(mapper);
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8602
8603 // Second finger down.
8604 processSlot(mapper, SECOND_SLOT);
8605 processId(mapper, SECOND_TRACKING_ID);
8606 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8610 motionArgs.action);
8611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8612
arthurhungcc7f9802020-04-30 17:55:40 +08008613 // If the tool type of the first finger changes to MT_TOOL_PALM,
8614 // we expect to receive ACTION_POINTER_UP with cancel flag.
8615 processSlot(mapper, FIRST_SLOT);
8616 processId(mapper, FIRST_TRACKING_ID);
8617 processToolType(mapper, MT_TOOL_PALM);
8618 processSync(mapper);
8619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8620 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8621 motionArgs.action);
8622 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8623
8624 // Second finger keeps moving.
8625 processSlot(mapper, SECOND_SLOT);
8626 processId(mapper, SECOND_TRACKING_ID);
8627 processPosition(mapper, x2 + 1, y2 + 1);
8628 processSync(mapper);
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8631
8632 // second finger becomes palm, receive cancel due to only 1 finger is active.
8633 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008634 processToolType(mapper, MT_TOOL_PALM);
8635 processSync(mapper);
8636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8637 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8638
arthurhungcc7f9802020-04-30 17:55:40 +08008639 // third finger down.
8640 processSlot(mapper, THIRD_SLOT);
8641 processId(mapper, THIRD_TRACKING_ID);
8642 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008643 processPosition(mapper, x3, y3);
8644 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008648 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8649
8650 // third finger move
8651 processId(mapper, THIRD_TRACKING_ID);
8652 processPosition(mapper, x3 + 1, y3 + 1);
8653 processSync(mapper);
8654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8656
8657 // first finger up, third finger receive move.
8658 processSlot(mapper, FIRST_SLOT);
8659 processId(mapper, INVALID_TRACKING_ID);
8660 processSync(mapper);
8661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8663 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8664
8665 // second finger up, third finger receive move.
8666 processSlot(mapper, SECOND_SLOT);
8667 processId(mapper, INVALID_TRACKING_ID);
8668 processSync(mapper);
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8671 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8672
8673 // third finger up.
8674 processSlot(mapper, THIRD_SLOT);
8675 processId(mapper, INVALID_TRACKING_ID);
8676 processSync(mapper);
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8678 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8679 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8680}
8681
8682/**
8683 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8684 * and the active finger could still be allowed to receive the events
8685 */
8686TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8687 addConfigurationProperty("touch.deviceType", "touchScreen");
8688 prepareDisplay(DISPLAY_ORIENTATION_0);
8689 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8690 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8691
8692 NotifyMotionArgs motionArgs;
8693
8694 // default tool type is finger
8695 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8696 processId(mapper, FIRST_TRACKING_ID);
8697 processPosition(mapper, x1, y1);
8698 processSync(mapper);
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8700 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8702
8703 // Second finger down.
8704 processSlot(mapper, SECOND_SLOT);
8705 processId(mapper, SECOND_TRACKING_ID);
8706 processPosition(mapper, x2, y2);
8707 processSync(mapper);
8708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8709 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8710 motionArgs.action);
8711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8712
8713 // If the tool type of the second finger changes to MT_TOOL_PALM,
8714 // we expect to receive ACTION_POINTER_UP with cancel flag.
8715 processId(mapper, SECOND_TRACKING_ID);
8716 processToolType(mapper, MT_TOOL_PALM);
8717 processSync(mapper);
8718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8719 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8720 motionArgs.action);
8721 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8722
8723 // The following MOVE event should be processed.
8724 processSlot(mapper, FIRST_SLOT);
8725 processId(mapper, FIRST_TRACKING_ID);
8726 processPosition(mapper, x1 + 1, y1 + 1);
8727 processSync(mapper);
8728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8730 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8731
8732 // second finger up.
8733 processSlot(mapper, SECOND_SLOT);
8734 processId(mapper, INVALID_TRACKING_ID);
8735 processSync(mapper);
8736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8738
8739 // first finger keep moving
8740 processSlot(mapper, FIRST_SLOT);
8741 processId(mapper, FIRST_TRACKING_ID);
8742 processPosition(mapper, x1 + 2, y1 + 2);
8743 processSync(mapper);
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8746
8747 // first finger up.
8748 processId(mapper, INVALID_TRACKING_ID);
8749 processSync(mapper);
8750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8751 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8752 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008753}
8754
Arthur Hung9ad18942021-06-19 02:04:46 +00008755/**
8756 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8757 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8758 * cause slot be valid again.
8759 */
8760TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8761 addConfigurationProperty("touch.deviceType", "touchScreen");
8762 prepareDisplay(DISPLAY_ORIENTATION_0);
8763 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8764 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8765
8766 NotifyMotionArgs motionArgs;
8767
8768 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8769 // First finger down.
8770 processId(mapper, FIRST_TRACKING_ID);
8771 processPosition(mapper, x1, y1);
8772 processPressure(mapper, RAW_PRESSURE_MAX);
8773 processSync(mapper);
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8776 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8777
8778 // First finger move.
8779 processId(mapper, FIRST_TRACKING_ID);
8780 processPosition(mapper, x1 + 1, y1 + 1);
8781 processPressure(mapper, RAW_PRESSURE_MAX);
8782 processSync(mapper);
8783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8785 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8786
8787 // Second finger down.
8788 processSlot(mapper, SECOND_SLOT);
8789 processId(mapper, SECOND_TRACKING_ID);
8790 processPosition(mapper, x2, y2);
8791 processPressure(mapper, RAW_PRESSURE_MAX);
8792 processSync(mapper);
8793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8794 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8795 motionArgs.action);
8796 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8797
8798 // second finger up with some unexpected data.
8799 processSlot(mapper, SECOND_SLOT);
8800 processId(mapper, INVALID_TRACKING_ID);
8801 processPosition(mapper, x2, y2);
8802 processSync(mapper);
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8804 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8805 motionArgs.action);
8806 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8807
8808 // first finger up with some unexpected data.
8809 processSlot(mapper, FIRST_SLOT);
8810 processId(mapper, INVALID_TRACKING_ID);
8811 processPosition(mapper, x2, y2);
8812 processPressure(mapper, RAW_PRESSURE_MAX);
8813 processSync(mapper);
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8815 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8816 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8817}
8818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008819// --- MultiTouchInputMapperTest_ExternalDevice ---
8820
8821class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8822protected:
Chris Yea52ade12020-08-27 16:49:20 -07008823 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008824};
8825
8826/**
8827 * Expect fallback to internal viewport if device is external and external viewport is not present.
8828 */
8829TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8830 prepareAxes(POSITION);
8831 addConfigurationProperty("touch.deviceType", "touchScreen");
8832 prepareDisplay(DISPLAY_ORIENTATION_0);
8833 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8834
8835 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8836
8837 NotifyMotionArgs motionArgs;
8838
8839 // Expect the event to be sent to the internal viewport,
8840 // because an external viewport is not present.
8841 processPosition(mapper, 100, 100);
8842 processSync(mapper);
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8844 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8845
8846 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008847 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008848 processPosition(mapper, 100, 100);
8849 processSync(mapper);
8850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8851 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8852}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008853
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008854TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8855 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8856 std::shared_ptr<FakePointerController> fakePointerController =
8857 std::make_shared<FakePointerController>();
8858 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8859 fakePointerController->setPosition(0, 0);
8860 fakePointerController->setButtonState(0);
8861
8862 // prepare device and capture
8863 prepareDisplay(DISPLAY_ORIENTATION_0);
8864 prepareAxes(POSITION | ID | SLOT);
8865 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8866 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8867 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008868 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008869 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8870
8871 // captured touchpad should be a touchpad source
8872 NotifyDeviceResetArgs resetArgs;
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8874 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8875
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008876 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008877
8878 const InputDeviceInfo::MotionRange* relRangeX =
8879 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8880 ASSERT_NE(relRangeX, nullptr);
8881 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8882 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8883 const InputDeviceInfo::MotionRange* relRangeY =
8884 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8885 ASSERT_NE(relRangeY, nullptr);
8886 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8887 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8888
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008889 // run captured pointer tests - note that this is unscaled, so input listener events should be
8890 // identical to what the hardware sends (accounting for any
8891 // calibration).
8892 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008893 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008894 processId(mapper, 1);
8895 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8896 processKey(mapper, BTN_TOUCH, 1);
8897 processSync(mapper);
8898
8899 // expect coord[0] to contain initial location of touch 0
8900 NotifyMotionArgs args;
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8902 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8903 ASSERT_EQ(1U, args.pointerCount);
8904 ASSERT_EQ(0, args.pointerProperties[0].id);
8905 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8906 ASSERT_NO_FATAL_FAILURE(
8907 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8908
8909 // FINGER 1 DOWN
8910 processSlot(mapper, 1);
8911 processId(mapper, 2);
8912 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8913 processSync(mapper);
8914
8915 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008917 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8918 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008919 ASSERT_EQ(2U, args.pointerCount);
8920 ASSERT_EQ(0, args.pointerProperties[0].id);
8921 ASSERT_EQ(1, args.pointerProperties[1].id);
8922 ASSERT_NO_FATAL_FAILURE(
8923 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8924 ASSERT_NO_FATAL_FAILURE(
8925 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8926
8927 // FINGER 1 MOVE
8928 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8929 processSync(mapper);
8930
8931 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8932 // from move
8933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8935 ASSERT_NO_FATAL_FAILURE(
8936 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8937 ASSERT_NO_FATAL_FAILURE(
8938 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8939
8940 // FINGER 0 MOVE
8941 processSlot(mapper, 0);
8942 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8943 processSync(mapper);
8944
8945 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8948 ASSERT_NO_FATAL_FAILURE(
8949 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8950 ASSERT_NO_FATAL_FAILURE(
8951 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8952
8953 // BUTTON DOWN
8954 processKey(mapper, BTN_LEFT, 1);
8955 processSync(mapper);
8956
8957 // touchinputmapper design sends a move before button press
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8962
8963 // BUTTON UP
8964 processKey(mapper, BTN_LEFT, 0);
8965 processSync(mapper);
8966
8967 // touchinputmapper design sends a move after button release
8968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8972
8973 // FINGER 0 UP
8974 processId(mapper, -1);
8975 processSync(mapper);
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8977 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8978
8979 // FINGER 1 MOVE
8980 processSlot(mapper, 1);
8981 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8982 processSync(mapper);
8983
8984 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8987 ASSERT_EQ(1U, args.pointerCount);
8988 ASSERT_EQ(1, args.pointerProperties[0].id);
8989 ASSERT_NO_FATAL_FAILURE(
8990 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8991
8992 // FINGER 1 UP
8993 processId(mapper, -1);
8994 processKey(mapper, BTN_TOUCH, 0);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8997 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8998
8999 // non captured touchpad should be a mouse source
9000 mFakePolicy->setPointerCapture(false);
9001 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9003 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9004}
9005
9006TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9007 std::shared_ptr<FakePointerController> fakePointerController =
9008 std::make_shared<FakePointerController>();
9009 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9010 fakePointerController->setPosition(0, 0);
9011 fakePointerController->setButtonState(0);
9012
9013 // prepare device and capture
9014 prepareDisplay(DISPLAY_ORIENTATION_0);
9015 prepareAxes(POSITION | ID | SLOT);
9016 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9017 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009018 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9020 // run uncaptured pointer tests - pushes out generic events
9021 // FINGER 0 DOWN
9022 processId(mapper, 3);
9023 processPosition(mapper, 100, 100);
9024 processKey(mapper, BTN_TOUCH, 1);
9025 processSync(mapper);
9026
9027 // start at (100,100), cursor should be at (0,0) * scale
9028 NotifyMotionArgs args;
9029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9030 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9031 ASSERT_NO_FATAL_FAILURE(
9032 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9033
9034 // FINGER 0 MOVE
9035 processPosition(mapper, 200, 200);
9036 processSync(mapper);
9037
9038 // compute scaling to help with touch position checking
9039 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9040 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9041 float scale =
9042 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9043
9044 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9046 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9048 0, 0, 0, 0, 0, 0, 0));
9049}
9050
9051TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9052 std::shared_ptr<FakePointerController> fakePointerController =
9053 std::make_shared<FakePointerController>();
9054
9055 prepareDisplay(DISPLAY_ORIENTATION_0);
9056 prepareAxes(POSITION | ID | SLOT);
9057 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009058 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009059 mFakePolicy->setPointerCapture(false);
9060 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9061
9062 // uncaptured touchpad should be a pointer device
9063 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9064
9065 // captured touchpad should be a touchpad device
9066 mFakePolicy->setPointerCapture(true);
9067 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9068 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9069}
9070
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009071// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009072
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009073class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009074protected:
9075 static const char* DEVICE_NAME;
9076 static const char* DEVICE_LOCATION;
9077 static const int32_t DEVICE_ID;
9078 static const int32_t DEVICE_GENERATION;
9079 static const int32_t DEVICE_CONTROLLER_NUMBER;
9080 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9081 static const int32_t EVENTHUB_ID;
9082
9083 std::shared_ptr<FakeEventHub> mFakeEventHub;
9084 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009085 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009086 std::unique_ptr<InstrumentedInputReader> mReader;
9087 std::shared_ptr<InputDevice> mDevice;
9088
9089 virtual void SetUp(Flags<InputDeviceClass> classes) {
9090 mFakeEventHub = std::make_unique<FakeEventHub>();
9091 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009092 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009093 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009094 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009095 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9096 }
9097
9098 void SetUp() override { SetUp(DEVICE_CLASSES); }
9099
9100 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009101 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009102 mFakePolicy.clear();
9103 }
9104
9105 void configureDevice(uint32_t changes) {
9106 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9107 mReader->requestRefreshConfiguration(changes);
9108 mReader->loopOnce();
9109 }
9110 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9111 }
9112
9113 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9114 const std::string& location, int32_t eventHubId,
9115 Flags<InputDeviceClass> classes) {
9116 InputDeviceIdentifier identifier;
9117 identifier.name = name;
9118 identifier.location = location;
9119 std::shared_ptr<InputDevice> device =
9120 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9121 identifier);
9122 mReader->pushNextDevice(device);
9123 mFakeEventHub->addDevice(eventHubId, name, classes);
9124 mReader->loopOnce();
9125 return device;
9126 }
9127
9128 template <class T, typename... Args>
9129 T& addControllerAndConfigure(Args... args) {
9130 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9131
9132 return controller;
9133 }
9134};
9135
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009136const char* PeripheralControllerTest::DEVICE_NAME = "device";
9137const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9138const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9139const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9140const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9141const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009142 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009143const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009144
9145// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009146class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009147protected:
9148 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009149 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009150 }
9151};
9152
9153TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009154 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009155
9156 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9157 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9158}
9159
9160TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009161 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009162
9163 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9164 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9165}
9166
9167// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009168class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009169protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009170 void SetUp() override {
9171 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9172 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009173};
9174
Chris Ye85758332021-05-16 23:05:17 -07009175TEST_F(LightControllerTest, MonoLight) {
9176 RawLightInfo infoMono = {.id = 1,
9177 .name = "Mono",
9178 .maxBrightness = 255,
9179 .flags = InputLightClass::BRIGHTNESS,
9180 .path = ""};
9181 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009182
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009183 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009184 InputDeviceInfo info;
9185 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009186 std::vector<InputDeviceLightInfo> lights = info.getLights();
9187 ASSERT_EQ(1U, lights.size());
9188 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009189
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009190 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9191 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009192}
9193
9194TEST_F(LightControllerTest, RGBLight) {
9195 RawLightInfo infoRed = {.id = 1,
9196 .name = "red",
9197 .maxBrightness = 255,
9198 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9199 .path = ""};
9200 RawLightInfo infoGreen = {.id = 2,
9201 .name = "green",
9202 .maxBrightness = 255,
9203 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9204 .path = ""};
9205 RawLightInfo infoBlue = {.id = 3,
9206 .name = "blue",
9207 .maxBrightness = 255,
9208 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9209 .path = ""};
9210 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9211 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9212 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9213
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009214 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009215 InputDeviceInfo info;
9216 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009217 std::vector<InputDeviceLightInfo> lights = info.getLights();
9218 ASSERT_EQ(1U, lights.size());
9219 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009220
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009221 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9222 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009223}
9224
9225TEST_F(LightControllerTest, MultiColorRGBLight) {
9226 RawLightInfo infoColor = {.id = 1,
9227 .name = "red",
9228 .maxBrightness = 255,
9229 .flags = InputLightClass::BRIGHTNESS |
9230 InputLightClass::MULTI_INTENSITY |
9231 InputLightClass::MULTI_INDEX,
9232 .path = ""};
9233
9234 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9235
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009236 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009237 InputDeviceInfo info;
9238 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009239 std::vector<InputDeviceLightInfo> lights = info.getLights();
9240 ASSERT_EQ(1U, lights.size());
9241 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009242
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009243 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9244 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009245}
9246
9247TEST_F(LightControllerTest, PlayerIdLight) {
9248 RawLightInfo info1 = {.id = 1,
9249 .name = "player1",
9250 .maxBrightness = 255,
9251 .flags = InputLightClass::BRIGHTNESS,
9252 .path = ""};
9253 RawLightInfo info2 = {.id = 2,
9254 .name = "player2",
9255 .maxBrightness = 255,
9256 .flags = InputLightClass::BRIGHTNESS,
9257 .path = ""};
9258 RawLightInfo info3 = {.id = 3,
9259 .name = "player3",
9260 .maxBrightness = 255,
9261 .flags = InputLightClass::BRIGHTNESS,
9262 .path = ""};
9263 RawLightInfo info4 = {.id = 4,
9264 .name = "player4",
9265 .maxBrightness = 255,
9266 .flags = InputLightClass::BRIGHTNESS,
9267 .path = ""};
9268 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9269 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9270 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9271 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9272
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009273 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009274 InputDeviceInfo info;
9275 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009276 std::vector<InputDeviceLightInfo> lights = info.getLights();
9277 ASSERT_EQ(1U, lights.size());
9278 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009279
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009280 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9281 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9282 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009283}
9284
Michael Wrightd02c5b62014-02-10 15:10:22 -08009285} // namespace android