blob: 997cbe88a1c4b379167d21e7fe71b19b3e71fe80 [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>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080035#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036#include <math.h>
37
Michael Wright17db18e2020-06-26 20:51:44 +010038#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080039#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000040#include "input/DisplayViewport.h"
41#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010042
Michael Wrightd02c5b62014-02-10 15:10:22 -080043namespace android {
44
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070045using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070046using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070047
48// Timeout for waiting for an expected event
49static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
50
Michael Wrightd02c5b62014-02-10 15:10:22 -080051// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000052static constexpr nsecs_t ARBITRARY_TIME = 1234;
53static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080054
55// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080056static constexpr int32_t DISPLAY_ID = 0;
57static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
58static constexpr int32_t DISPLAY_WIDTH = 480;
59static constexpr int32_t DISPLAY_HEIGHT = 800;
60static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
61static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
62static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070063static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070064static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080065
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t FIRST_SLOT = 0;
67static constexpr int32_t SECOND_SLOT = 1;
68static constexpr int32_t THIRD_SLOT = 2;
69static constexpr int32_t INVALID_TRACKING_ID = -1;
70static constexpr int32_t FIRST_TRACKING_ID = 0;
71static constexpr int32_t SECOND_TRACKING_ID = 1;
72static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080073static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080074static constexpr int32_t BATTERY_STATUS = 4;
75static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080076static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
77static constexpr int32_t LIGHT_COLOR = 0x7F448866;
78static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080079
Michael Wrightd02c5b62014-02-10 15:10:22 -080080// Error tolerance for floating point assertions.
81static const float EPSILON = 0.001f;
82
83template<typename T>
84static inline T min(T a, T b) {
85 return a < b ? a : b;
86}
87
88static inline float avg(float x, float y) {
89 return (x + y) / 2;
90}
91
Chris Ye3fdbfef2021-01-06 18:45:18 -080092// Mapping for light color name and the light color
93const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
94 {"green", LightColor::GREEN},
95 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
97// --- FakePointerController ---
98
99class FakePointerController : public PointerControllerInterface {
100 bool mHaveBounds;
101 float mMinX, mMinY, mMaxX, mMaxY;
102 float mX, mY;
103 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800104 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106public:
107 FakePointerController() :
108 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800109 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110 }
111
Michael Wright17db18e2020-06-26 20:51:44 +0100112 virtual ~FakePointerController() {}
113
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114 void setBounds(float minX, float minY, float maxX, float maxY) {
115 mHaveBounds = true;
116 mMinX = minX;
117 mMinY = minY;
118 mMaxX = maxX;
119 mMaxY = maxY;
120 }
121
Chris Yea52ade12020-08-27 16:49:20 -0700122 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 mX = x;
124 mY = y;
125 }
126
Chris Yea52ade12020-08-27 16:49:20 -0700127 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128
Chris Yea52ade12020-08-27 16:49:20 -0700129 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800130
Chris Yea52ade12020-08-27 16:49:20 -0700131 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 *outX = mX;
133 *outY = mY;
134 }
135
Chris Yea52ade12020-08-27 16:49:20 -0700136 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800137
Chris Yea52ade12020-08-27 16:49:20 -0700138 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800139 mDisplayId = viewport.displayId;
140 }
141
Arthur Hung7c645402019-01-25 17:45:42 +0800142 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
143 return mSpotsByDisplay;
144 }
145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146private:
Chris Yea52ade12020-08-27 16:49:20 -0700147 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 *outMinX = mMinX;
149 *outMinY = mMinY;
150 *outMaxX = mMaxX;
151 *outMaxY = mMaxY;
152 return mHaveBounds;
153 }
154
Chris Yea52ade12020-08-27 16:49:20 -0700155 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 mX += deltaX;
157 if (mX < mMinX) mX = mMinX;
158 if (mX > mMaxX) mX = mMaxX;
159 mY += deltaY;
160 if (mY < mMinY) mY = mMinY;
161 if (mY > mMaxY) mY = mMaxY;
162 }
163
Chris Yea52ade12020-08-27 16:49:20 -0700164 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Chris Yea52ade12020-08-27 16:49:20 -0700166 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
Chris Yea52ade12020-08-27 16:49:20 -0700168 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Chris Yea52ade12020-08-27 16:49:20 -0700170 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
171 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800172 std::vector<int32_t> newSpots;
173 // Add spots for fingers that are down.
174 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
175 uint32_t id = idBits.clearFirstMarkedBit();
176 newSpots.push_back(id);
177 }
178
179 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 }
181
Chris Yea52ade12020-08-27 16:49:20 -0700182 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800183
184 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185};
186
187
188// --- FakeInputReaderPolicy ---
189
190class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700191 std::mutex mLock;
192 std::condition_variable mDevicesChangedCondition;
193
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100195 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700196 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
197 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100198 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700199 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200
201protected:
Chris Yea52ade12020-08-27 16:49:20 -0700202 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203
204public:
205 FakeInputReaderPolicy() {
206 }
207
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800209 waitForInputDevices([](bool devicesChanged) {
210 if (!devicesChanged) {
211 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
212 }
213 });
214 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700215
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800216 void assertInputDevicesNotChanged() {
217 waitForInputDevices([](bool devicesChanged) {
218 if (devicesChanged) {
219 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
220 }
221 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700222 }
223
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700224 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100225 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100226 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700227 }
228
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700229 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
230 return mConfig.getDisplayViewportByUniqueId(uniqueId);
231 }
232 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
233 return mConfig.getDisplayViewportByType(type);
234 }
235
236 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
237 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700238 }
239
240 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000241 bool isActive, const std::string& uniqueId,
242 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
243 const DisplayViewport viewport =
244 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
245 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700246 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100247 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 }
249
Arthur Hung6cd19a42019-08-30 19:04:12 +0800250 bool updateViewport(const DisplayViewport& viewport) {
251 size_t count = mViewports.size();
252 for (size_t i = 0; i < count; i++) {
253 const DisplayViewport& currentViewport = mViewports[i];
254 if (currentViewport.displayId == viewport.displayId) {
255 mViewports[i] = viewport;
256 mConfig.setDisplayViewports(mViewports);
257 return true;
258 }
259 }
260 // no viewport found.
261 return false;
262 }
263
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100264 void addExcludedDeviceName(const std::string& deviceName) {
265 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800266 }
267
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700268 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
269 mConfig.portAssociations.insert({inputPort, displayPort});
270 }
271
Christine Franks1ba71cc2021-04-07 14:37:42 -0700272 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
273 const std::string& displayUniqueId) {
274 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
275 }
276
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000277 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700278
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000279 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700280
Michael Wright17db18e2020-06-26 20:51:44 +0100281 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
282 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 }
284
285 const InputReaderConfiguration* getReaderConfiguration() const {
286 return &mConfig;
287 }
288
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800289 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290 return mInputDevices;
291 }
292
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100293 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700294 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700295 return transform;
296 }
297
298 void setTouchAffineTransformation(const TouchAffineTransformation t) {
299 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800300 }
301
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800302 void setPointerCapture(bool enabled) {
303 mConfig.pointerCapture = enabled;
304 }
305
Arthur Hung7c645402019-01-25 17:45:42 +0800306 void setShowTouches(bool enabled) {
307 mConfig.showTouches = enabled;
308 }
309
Garfield Tan888a6a42020-01-09 11:39:16 -0800310 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
311 mConfig.defaultPointerDisplayId = pointerDisplayId;
312 }
313
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800314 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
315
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700317 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000318 int32_t orientation, bool isActive,
319 const std::string& uniqueId,
320 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700321 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
322 || orientation == DISPLAY_ORIENTATION_270);
323 DisplayViewport v;
324 v.displayId = displayId;
325 v.orientation = orientation;
326 v.logicalLeft = 0;
327 v.logicalTop = 0;
328 v.logicalRight = isRotated ? height : width;
329 v.logicalBottom = isRotated ? width : height;
330 v.physicalLeft = 0;
331 v.physicalTop = 0;
332 v.physicalRight = isRotated ? height : width;
333 v.physicalBottom = isRotated ? width : height;
334 v.deviceWidth = isRotated ? height : width;
335 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000336 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700337 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700338 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100339 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700340 return v;
341 }
342
Chris Yea52ade12020-08-27 16:49:20 -0700343 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 *outConfig = mConfig;
345 }
346
Chris Yea52ade12020-08-27 16:49:20 -0700347 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100348 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 }
350
Chris Yea52ade12020-08-27 16:49:20 -0700351 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700352 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700354 mInputDevicesChanged = true;
355 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 }
357
Chris Yea52ade12020-08-27 16:49:20 -0700358 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
359 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700360 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361 }
362
Chris Yea52ade12020-08-27 16:49:20 -0700363 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800364
365 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
366 std::unique_lock<std::mutex> lock(mLock);
367 base::ScopedLockAssertion assumeLocked(mLock);
368
369 const bool devicesChanged =
370 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
371 return mInputDevicesChanged;
372 });
373 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
374 mInputDevicesChanged = false;
375 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376};
377
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378// --- FakeEventHub ---
379
380class FakeEventHub : public EventHubInterface {
381 struct KeyInfo {
382 int32_t keyCode;
383 uint32_t flags;
384 };
385
Chris Yef59a2f42020-10-16 12:55:26 -0700386 struct SensorInfo {
387 InputDeviceSensorType sensorType;
388 int32_t sensorDataIndex;
389 };
390
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 struct Device {
392 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700393 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 PropertyMap configuration;
395 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
396 KeyedVector<int, bool> relativeAxes;
397 KeyedVector<int32_t, int32_t> keyCodeStates;
398 KeyedVector<int32_t, int32_t> scanCodeStates;
399 KeyedVector<int32_t, int32_t> switchStates;
400 KeyedVector<int32_t, int32_t> absoluteAxisValue;
401 KeyedVector<int32_t, KeyInfo> keysByScanCode;
402 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
403 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700404 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
405 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800406 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700407 bool enabled;
408
409 status_t enable() {
410 enabled = true;
411 return OK;
412 }
413
414 status_t disable() {
415 enabled = false;
416 return OK;
417 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418
Chris Ye1b0c7342020-07-28 21:57:03 -0700419 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 };
421
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700422 std::mutex mLock;
423 std::condition_variable mEventsCondition;
424
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100426 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000427 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600428 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000429 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800430 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
431 // Simulates a device light brightness, from light id to light brightness.
432 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
433 // Simulates a device light intensities, from light id to light intensities map.
434 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
435 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700437public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 virtual ~FakeEventHub() {
439 for (size_t i = 0; i < mDevices.size(); i++) {
440 delete mDevices.valueAt(i);
441 }
442 }
443
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444 FakeEventHub() { }
445
Chris Ye1b0c7342020-07-28 21:57:03 -0700446 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447 Device* device = new Device(classes);
448 device->identifier.name = name;
449 mDevices.add(deviceId, device);
450
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000451 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452 }
453
454 void removeDevice(int32_t deviceId) {
455 delete mDevices.valueFor(deviceId);
456 mDevices.removeItem(deviceId);
457
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000458 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459 }
460
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700461 bool isDeviceEnabled(int32_t deviceId) {
462 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700463 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700464 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
465 return false;
466 }
467 return device->enabled;
468 }
469
470 status_t enableDevice(int32_t deviceId) {
471 status_t result;
472 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700473 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700474 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
475 return BAD_VALUE;
476 }
477 if (device->enabled) {
478 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
479 return OK;
480 }
481 result = device->enable();
482 return result;
483 }
484
485 status_t disableDevice(int32_t deviceId) {
486 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700487 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700488 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
489 return BAD_VALUE;
490 }
491 if (!device->enabled) {
492 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
493 return OK;
494 }
495 return device->disable();
496 }
497
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000499 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800500 }
501
502 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
503 Device* device = getDevice(deviceId);
504 device->configuration.addProperty(key, value);
505 }
506
507 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
508 Device* device = getDevice(deviceId);
509 device->configuration.addAll(configuration);
510 }
511
512 void addAbsoluteAxis(int32_t deviceId, int axis,
513 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
514 Device* device = getDevice(deviceId);
515
516 RawAbsoluteAxisInfo info;
517 info.valid = true;
518 info.minValue = minValue;
519 info.maxValue = maxValue;
520 info.flat = flat;
521 info.fuzz = fuzz;
522 info.resolution = resolution;
523 device->absoluteAxes.add(axis, info);
524 }
525
526 void addRelativeAxis(int32_t deviceId, int32_t axis) {
527 Device* device = getDevice(deviceId);
528 device->relativeAxes.add(axis, true);
529 }
530
531 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
532 Device* device = getDevice(deviceId);
533 device->keyCodeStates.replaceValueFor(keyCode, state);
534 }
535
536 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
537 Device* device = getDevice(deviceId);
538 device->scanCodeStates.replaceValueFor(scanCode, state);
539 }
540
541 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
542 Device* device = getDevice(deviceId);
543 device->switchStates.replaceValueFor(switchCode, state);
544 }
545
546 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
547 Device* device = getDevice(deviceId);
548 device->absoluteAxisValue.replaceValueFor(axis, value);
549 }
550
551 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
552 int32_t keyCode, uint32_t flags) {
553 Device* device = getDevice(deviceId);
554 KeyInfo info;
555 info.keyCode = keyCode;
556 info.flags = flags;
557 if (scanCode) {
558 device->keysByScanCode.add(scanCode, info);
559 }
560 if (usageCode) {
561 device->keysByUsageCode.add(usageCode, info);
562 }
563 }
564
565 void addLed(int32_t deviceId, int32_t led, bool initialState) {
566 Device* device = getDevice(deviceId);
567 device->leds.add(led, initialState);
568 }
569
Chris Yef59a2f42020-10-16 12:55:26 -0700570 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
571 int32_t sensorDataIndex) {
572 Device* device = getDevice(deviceId);
573 SensorInfo info;
574 info.sensorType = sensorType;
575 info.sensorDataIndex = sensorDataIndex;
576 device->sensorsByAbsCode.emplace(absCode, info);
577 }
578
579 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
580 Device* device = getDevice(deviceId);
581 typename BitArray<MSC_MAX>::Buffer buffer;
582 buffer[mscEvent / 32] = 1 << mscEvent % 32;
583 device->mscBitmask.loadFromBuffer(buffer);
584 }
585
Chris Ye3fdbfef2021-01-06 18:45:18 -0800586 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
587 mRawLightInfos.emplace(rawId, std::move(info));
588 }
589
590 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
591 mLightBrightness.emplace(rawId, brightness);
592 }
593
594 void fakeLightIntensities(int32_t rawId,
595 const std::unordered_map<LightColor, int32_t> intensities) {
596 mLightIntensities.emplace(rawId, std::move(intensities));
597 }
598
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 bool getLedState(int32_t deviceId, int32_t led) {
600 Device* device = getDevice(deviceId);
601 return device->leds.valueFor(led);
602 }
603
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100604 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 return mExcludedDevices;
606 }
607
608 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
609 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800610 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 }
612
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000613 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
614 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700615 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 RawEvent event;
617 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000618 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 event.deviceId = deviceId;
620 event.type = type;
621 event.code = code;
622 event.value = value;
623 mEvents.push_back(event);
624
625 if (type == EV_ABS) {
626 setAbsoluteAxisValue(deviceId, code, value);
627 }
628 }
629
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600630 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
631 std::vector<TouchVideoFrame>> videoFrames) {
632 mVideoFrames = std::move(videoFrames);
633 }
634
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700636 std::unique_lock<std::mutex> lock(mLock);
637 base::ScopedLockAssertion assumeLocked(mLock);
638 const bool queueIsEmpty =
639 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
640 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
641 if (!queueIsEmpty) {
642 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
643 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 }
645
646private:
647 Device* getDevice(int32_t deviceId) const {
648 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100649 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650 }
651
Chris Yea52ade12020-08-27 16:49:20 -0700652 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700654 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 }
656
Chris Yea52ade12020-08-27 16:49:20 -0700657 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 Device* device = getDevice(deviceId);
659 return device ? device->identifier : InputDeviceIdentifier();
660 }
661
Chris Yea52ade12020-08-27 16:49:20 -0700662 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
Chris Yea52ade12020-08-27 16:49:20 -0700664 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665 Device* device = getDevice(deviceId);
666 if (device) {
667 *outConfiguration = device->configuration;
668 }
669 }
670
Chris Yea52ade12020-08-27 16:49:20 -0700671 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
672 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800674 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 ssize_t index = device->absoluteAxes.indexOfKey(axis);
676 if (index >= 0) {
677 *outAxisInfo = device->absoluteAxes.valueAt(index);
678 return OK;
679 }
680 }
681 outAxisInfo->clear();
682 return -1;
683 }
684
Chris Yea52ade12020-08-27 16:49:20 -0700685 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 Device* device = getDevice(deviceId);
687 if (device) {
688 return device->relativeAxes.indexOfKey(axis) >= 0;
689 }
690 return false;
691 }
692
Chris Yea52ade12020-08-27 16:49:20 -0700693 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694
Chris Yef59a2f42020-10-16 12:55:26 -0700695 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
696 Device* device = getDevice(deviceId);
697 if (device) {
698 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
699 }
700 return false;
701 }
702
Chris Yea52ade12020-08-27 16:49:20 -0700703 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
704 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
706 if (device) {
707 const KeyInfo* key = getKey(device, scanCode, usageCode);
708 if (key) {
709 if (outKeycode) {
710 *outKeycode = key->keyCode;
711 }
712 if (outFlags) {
713 *outFlags = key->flags;
714 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700715 if (outMetaState) {
716 *outMetaState = metaState;
717 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 return OK;
719 }
720 }
721 return NAME_NOT_FOUND;
722 }
723
724 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
725 if (usageCode) {
726 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
727 if (index >= 0) {
728 return &device->keysByUsageCode.valueAt(index);
729 }
730 }
731 if (scanCode) {
732 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
733 if (index >= 0) {
734 return &device->keysByScanCode.valueAt(index);
735 }
736 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700737 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 }
739
Chris Yea52ade12020-08-27 16:49:20 -0700740 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741
Chris Yef59a2f42020-10-16 12:55:26 -0700742 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
743 int32_t absCode) {
744 Device* device = getDevice(deviceId);
745 if (!device) {
746 return Errorf("Sensor device not found.");
747 }
748 auto it = device->sensorsByAbsCode.find(absCode);
749 if (it == device->sensorsByAbsCode.end()) {
750 return Errorf("Sensor map not found.");
751 }
752 const SensorInfo& info = it->second;
753 return std::make_pair(info.sensorType, info.sensorDataIndex);
754 }
755
Chris Yea52ade12020-08-27 16:49:20 -0700756 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757 mExcludedDevices = devices;
758 }
759
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000760 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
761 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000763 const size_t filledSize = std::min(mEvents.size(), bufferSize);
764 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
765
766 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700767 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000768 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769 }
770
Chris Yea52ade12020-08-27 16:49:20 -0700771 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600772 auto it = mVideoFrames.find(deviceId);
773 if (it != mVideoFrames.end()) {
774 std::vector<TouchVideoFrame> frames = std::move(it->second);
775 mVideoFrames.erase(deviceId);
776 return frames;
777 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800778 return {};
779 }
780
Chris Yea52ade12020-08-27 16:49:20 -0700781 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782 Device* device = getDevice(deviceId);
783 if (device) {
784 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
785 if (index >= 0) {
786 return device->scanCodeStates.valueAt(index);
787 }
788 }
789 return AKEY_STATE_UNKNOWN;
790 }
791
Chris Yea52ade12020-08-27 16:49:20 -0700792 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793 Device* device = getDevice(deviceId);
794 if (device) {
795 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
796 if (index >= 0) {
797 return device->keyCodeStates.valueAt(index);
798 }
799 }
800 return AKEY_STATE_UNKNOWN;
801 }
802
Chris Yea52ade12020-08-27 16:49:20 -0700803 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 Device* device = getDevice(deviceId);
805 if (device) {
806 ssize_t index = device->switchStates.indexOfKey(sw);
807 if (index >= 0) {
808 return device->switchStates.valueAt(index);
809 }
810 }
811 return AKEY_STATE_UNKNOWN;
812 }
813
Chris Yea52ade12020-08-27 16:49:20 -0700814 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
815 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 Device* device = getDevice(deviceId);
817 if (device) {
818 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
819 if (index >= 0) {
820 *outValue = device->absoluteAxisValue.valueAt(index);
821 return OK;
822 }
823 }
824 *outValue = 0;
825 return -1;
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 // Return true if the device has non-empty key layout.
829 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
830 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 bool result = false;
832 Device* device = getDevice(deviceId);
833 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700834 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835 for (size_t i = 0; i < numCodes; i++) {
836 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
837 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
838 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839 }
840 }
841 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
842 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
843 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 }
845 }
846 }
847 }
848 return result;
849 }
850
Chris Yea52ade12020-08-27 16:49:20 -0700851 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 Device* device = getDevice(deviceId);
853 if (device) {
854 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
855 return index >= 0;
856 }
857 return false;
858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 Device* device = getDevice(deviceId);
862 return device && device->leds.indexOfKey(led) >= 0;
863 }
864
Chris Yea52ade12020-08-27 16:49:20 -0700865 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866 Device* device = getDevice(deviceId);
867 if (device) {
868 ssize_t index = device->leds.indexOfKey(led);
869 if (index >= 0) {
870 device->leds.replaceValueAt(led, on);
871 } else {
872 ADD_FAILURE()
873 << "Attempted to set the state of an LED that the EventHub declared "
874 "was not present. led=" << led;
875 }
876 }
877 }
878
Chris Yea52ade12020-08-27 16:49:20 -0700879 void getVirtualKeyDefinitions(
880 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 outVirtualKeys.clear();
882
883 Device* device = getDevice(deviceId);
884 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800885 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800886 }
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700890 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891 }
892
Chris Yea52ade12020-08-27 16:49:20 -0700893 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894 return false;
895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898
Chris Yea52ade12020-08-27 16:49:20 -0700899 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900
Chris Ye87143712020-11-10 05:05:58 +0000901 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
902
Chris Yee2b1e5c2021-03-10 22:45:12 -0800903 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
904 return BATTERY_CAPACITY;
905 }
Kim Low03ea0352020-11-06 12:45:07 -0800906
Chris Yee2b1e5c2021-03-10 22:45:12 -0800907 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
908 return BATTERY_STATUS;
909 }
910
911 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
912
913 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
914 return std::nullopt;
915 }
Kim Low03ea0352020-11-06 12:45:07 -0800916
Chris Ye3fdbfef2021-01-06 18:45:18 -0800917 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
918 std::vector<int32_t> ids;
919 for (const auto& [rawId, info] : mRawLightInfos) {
920 ids.push_back(rawId);
921 }
922 return ids;
923 }
924
925 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
926 auto it = mRawLightInfos.find(lightId);
927 if (it == mRawLightInfos.end()) {
928 return std::nullopt;
929 }
930 return it->second;
931 }
932
933 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
934 mLightBrightness.emplace(lightId, brightness);
935 }
936
937 void setLightIntensities(int32_t deviceId, int32_t lightId,
938 std::unordered_map<LightColor, int32_t> intensities) override {
939 mLightIntensities.emplace(lightId, intensities);
940 };
941
942 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
943 auto lightIt = mLightBrightness.find(lightId);
944 if (lightIt == mLightBrightness.end()) {
945 return std::nullopt;
946 }
947 return lightIt->second;
948 }
949
950 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
951 int32_t deviceId, int32_t lightId) override {
952 auto lightIt = mLightIntensities.find(lightId);
953 if (lightIt == mLightIntensities.end()) {
954 return std::nullopt;
955 }
956 return lightIt->second;
957 };
958
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100959 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 return false;
961 }
962
Chris Yea52ade12020-08-27 16:49:20 -0700963 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964
Chris Yea52ade12020-08-27 16:49:20 -0700965 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966
Chris Yea52ade12020-08-27 16:49:20 -0700967 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
Chris Yea52ade12020-08-27 16:49:20 -0700969 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970};
971
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972// --- FakeInputMapper ---
973
974class FakeInputMapper : public InputMapper {
975 uint32_t mSources;
976 int32_t mKeyboardType;
977 int32_t mMetaState;
978 KeyedVector<int32_t, int32_t> mKeyCodeStates;
979 KeyedVector<int32_t, int32_t> mScanCodeStates;
980 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800981 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700983 std::mutex mLock;
984 std::condition_variable mStateChangedCondition;
985 bool mConfigureWasCalled GUARDED_BY(mLock);
986 bool mResetWasCalled GUARDED_BY(mLock);
987 bool mProcessWasCalled GUARDED_BY(mLock);
988 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989
Arthur Hungc23540e2018-11-29 20:42:11 +0800990 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800992 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
993 : InputMapper(deviceContext),
994 mSources(sources),
995 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800996 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800997 mConfigureWasCalled(false),
998 mResetWasCalled(false),
999 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000
Chris Yea52ade12020-08-27 16:49:20 -07001001 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002
1003 void setKeyboardType(int32_t keyboardType) {
1004 mKeyboardType = keyboardType;
1005 }
1006
1007 void setMetaState(int32_t metaState) {
1008 mMetaState = metaState;
1009 }
1010
1011 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001012 std::unique_lock<std::mutex> lock(mLock);
1013 base::ScopedLockAssertion assumeLocked(mLock);
1014 const bool configureCalled =
1015 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1016 return mConfigureWasCalled;
1017 });
1018 if (!configureCalled) {
1019 FAIL() << "Expected configure() to have been called.";
1020 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021 mConfigureWasCalled = false;
1022 }
1023
1024 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 std::unique_lock<std::mutex> lock(mLock);
1026 base::ScopedLockAssertion assumeLocked(mLock);
1027 const bool resetCalled =
1028 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1029 return mResetWasCalled;
1030 });
1031 if (!resetCalled) {
1032 FAIL() << "Expected reset() to have been called.";
1033 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 mResetWasCalled = false;
1035 }
1036
Yi Kong9b14ac62018-07-17 13:48:38 -07001037 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001038 std::unique_lock<std::mutex> lock(mLock);
1039 base::ScopedLockAssertion assumeLocked(mLock);
1040 const bool processCalled =
1041 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1042 return mProcessWasCalled;
1043 });
1044 if (!processCalled) {
1045 FAIL() << "Expected process() to have been called.";
1046 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 if (outLastEvent) {
1048 *outLastEvent = mLastEvent;
1049 }
1050 mProcessWasCalled = false;
1051 }
1052
1053 void setKeyCodeState(int32_t keyCode, int32_t state) {
1054 mKeyCodeStates.replaceValueFor(keyCode, state);
1055 }
1056
1057 void setScanCodeState(int32_t scanCode, int32_t state) {
1058 mScanCodeStates.replaceValueFor(scanCode, state);
1059 }
1060
1061 void setSwitchState(int32_t switchCode, int32_t state) {
1062 mSwitchStates.replaceValueFor(switchCode, state);
1063 }
1064
1065 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001066 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067 }
1068
1069private:
Chris Yea52ade12020-08-27 16:49:20 -07001070 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071
Chris Yea52ade12020-08-27 16:49:20 -07001072 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073 InputMapper::populateDeviceInfo(deviceInfo);
1074
1075 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1076 deviceInfo->setKeyboardType(mKeyboardType);
1077 }
1078 }
1079
Chris Yea52ade12020-08-27 16:49:20 -07001080 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001083
1084 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001085 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001086 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1087 mViewport = config->getDisplayViewportByPort(*displayPort);
1088 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001089
1090 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091 }
1092
Chris Yea52ade12020-08-27 16:49:20 -07001093 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001094 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001096 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 }
1098
Chris Yea52ade12020-08-27 16:49:20 -07001099 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001100 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101 mLastEvent = *rawEvent;
1102 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001103 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 }
1105
Chris Yea52ade12020-08-27 16:49:20 -07001106 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1108 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1109 }
1110
Chris Yea52ade12020-08-27 16:49:20 -07001111 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1113 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1114 }
1115
Chris Yea52ade12020-08-27 16:49:20 -07001116 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1118 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1119 }
1120
Chris Yea52ade12020-08-27 16:49:20 -07001121 // Return true if the device has non-empty key layout.
1122 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1123 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001124 for (size_t i = 0; i < numCodes; i++) {
1125 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1126 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1127 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 }
1129 }
1130 }
Chris Yea52ade12020-08-27 16:49:20 -07001131 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 return result;
1133 }
1134
1135 virtual int32_t getMetaState() {
1136 return mMetaState;
1137 }
1138
1139 virtual void fadePointer() {
1140 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001141
1142 virtual std::optional<int32_t> getAssociatedDisplay() {
1143 if (mViewport) {
1144 return std::make_optional(mViewport->displayId);
1145 }
1146 return std::nullopt;
1147 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148};
1149
1150
1151// --- InstrumentedInputReader ---
1152
1153class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001154 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155
1156public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001157 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1158 const sp<InputReaderPolicyInterface>& policy,
1159 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001160 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001162 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001164 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001166 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001167 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 InputDeviceIdentifier identifier;
1169 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001170 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001172 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 }
1174
Prabir Pradhan28efc192019-11-05 01:10:04 +00001175 // Make the protected loopOnce method accessible to tests.
1176 using InputReader::loopOnce;
1177
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001179 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1180 const InputDeviceIdentifier& identifier)
1181 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001182 if (!mNextDevices.empty()) {
1183 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1184 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 return device;
1186 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001187 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188 }
1189
arthurhungdcef2dc2020-08-11 14:47:50 +08001190 // --- FakeInputReaderContext ---
1191 class FakeInputReaderContext : public ContextImpl {
1192 int32_t mGlobalMetaState;
1193 bool mUpdateGlobalMetaStateWasCalled;
1194 int32_t mGeneration;
1195
1196 public:
1197 FakeInputReaderContext(InputReader* reader)
1198 : ContextImpl(reader),
1199 mGlobalMetaState(0),
1200 mUpdateGlobalMetaStateWasCalled(false),
1201 mGeneration(1) {}
1202
1203 virtual ~FakeInputReaderContext() {}
1204
1205 void assertUpdateGlobalMetaStateWasCalled() {
1206 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1207 << "Expected updateGlobalMetaState() to have been called.";
1208 mUpdateGlobalMetaStateWasCalled = false;
1209 }
1210
1211 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1212
1213 uint32_t getGeneration() { return mGeneration; }
1214
1215 void updateGlobalMetaState() override {
1216 mUpdateGlobalMetaStateWasCalled = true;
1217 ContextImpl::updateGlobalMetaState();
1218 }
1219
1220 int32_t getGlobalMetaState() override {
1221 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1222 }
1223
1224 int32_t bumpGeneration() override {
1225 mGeneration = ContextImpl::bumpGeneration();
1226 return mGeneration;
1227 }
1228 } mFakeContext;
1229
Michael Wrightd02c5b62014-02-10 15:10:22 -08001230 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001231
1232public:
1233 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234};
1235
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001236// --- InputReaderPolicyTest ---
1237class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001238protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239 sp<FakeInputReaderPolicy> mFakePolicy;
1240
Chris Yea52ade12020-08-27 16:49:20 -07001241 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1242 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001243};
1244
1245/**
1246 * Check that empty set of viewports is an acceptable configuration.
1247 * Also try to get internal viewport two different ways - by type and by uniqueId.
1248 *
1249 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1250 * Such configuration is not currently allowed.
1251 */
1252TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001253 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254
1255 // We didn't add any viewports yet, so there shouldn't be any.
1256 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001257 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001258 ASSERT_FALSE(internalViewport);
1259
1260 // Add an internal viewport, then clear it
1261 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001262 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001263 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264
1265 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001266 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001268 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001269
1270 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001271 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001273 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001274
1275 mFakePolicy->clearViewports();
1276 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001277 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001278 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001279 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001280 ASSERT_FALSE(internalViewport);
1281}
1282
1283TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1284 const std::string internalUniqueId = "local:0";
1285 const std::string externalUniqueId = "local:1";
1286 const std::string virtualUniqueId1 = "virtual:2";
1287 const std::string virtualUniqueId2 = "virtual:3";
1288 constexpr int32_t virtualDisplayId1 = 2;
1289 constexpr int32_t virtualDisplayId2 = 3;
1290
1291 // Add an internal viewport
1292 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001293 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1294 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001295 // Add an external viewport
1296 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001297 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1298 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001299 // Add an virtual viewport
1300 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001301 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1302 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001303 // Add another virtual viewport
1304 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001305 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1306 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001307
1308 // Check matching by type for internal
1309 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001310 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001311 ASSERT_TRUE(internalViewport);
1312 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1313
1314 // Check matching by type for external
1315 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001316 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001317 ASSERT_TRUE(externalViewport);
1318 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1319
1320 // Check matching by uniqueId for virtual viewport #1
1321 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001322 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001323 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001324 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001325 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1326 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1327
1328 // Check matching by uniqueId for virtual viewport #2
1329 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001330 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001331 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001332 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1334 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1335}
1336
1337
1338/**
1339 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1340 * that lookup works by checking display id.
1341 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1342 */
1343TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1344 const std::string uniqueId1 = "uniqueId1";
1345 const std::string uniqueId2 = "uniqueId2";
1346 constexpr int32_t displayId1 = 2;
1347 constexpr int32_t displayId2 = 3;
1348
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001349 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1350 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001351 for (const ViewportType& type : types) {
1352 mFakePolicy->clearViewports();
1353 // Add a viewport
1354 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001355 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1356 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 // Add another viewport
1358 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001359 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1360 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361
1362 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001363 std::optional<DisplayViewport> viewport1 =
1364 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365 ASSERT_TRUE(viewport1);
1366 ASSERT_EQ(displayId1, viewport1->displayId);
1367 ASSERT_EQ(type, viewport1->type);
1368
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001369 std::optional<DisplayViewport> viewport2 =
1370 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001371 ASSERT_TRUE(viewport2);
1372 ASSERT_EQ(displayId2, viewport2->displayId);
1373 ASSERT_EQ(type, viewport2->type);
1374
1375 // When there are multiple viewports of the same kind, and uniqueId is not specified
1376 // in the call to getDisplayViewport, then that situation is not supported.
1377 // The viewports can be stored in any order, so we cannot rely on the order, since that
1378 // is just implementation detail.
1379 // However, we can check that it still returns *a* viewport, we just cannot assert
1380 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001381 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382 ASSERT_TRUE(someViewport);
1383 }
1384}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001385
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001386/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001387 * When we have multiple internal displays make sure we always return the default display when
1388 * querying by type.
1389 */
1390TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1391 const std::string uniqueId1 = "uniqueId1";
1392 const std::string uniqueId2 = "uniqueId2";
1393 constexpr int32_t nonDefaultDisplayId = 2;
1394 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1395 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1396
1397 // Add the default display first and ensure it gets returned.
1398 mFakePolicy->clearViewports();
1399 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001400 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001401 ViewportType::INTERNAL);
1402 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001403 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001404 ViewportType::INTERNAL);
1405
1406 std::optional<DisplayViewport> viewport =
1407 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1408 ASSERT_TRUE(viewport);
1409 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1410 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1411
1412 // Add the default display second to make sure order doesn't matter.
1413 mFakePolicy->clearViewports();
1414 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001416 ViewportType::INTERNAL);
1417 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001418 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001419 ViewportType::INTERNAL);
1420
1421 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1422 ASSERT_TRUE(viewport);
1423 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1424 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1425}
1426
1427/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001428 * Check getDisplayViewportByPort
1429 */
1430TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001431 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001432 const std::string uniqueId1 = "uniqueId1";
1433 const std::string uniqueId2 = "uniqueId2";
1434 constexpr int32_t displayId1 = 1;
1435 constexpr int32_t displayId2 = 2;
1436 const uint8_t hdmi1 = 0;
1437 const uint8_t hdmi2 = 1;
1438 const uint8_t hdmi3 = 2;
1439
1440 mFakePolicy->clearViewports();
1441 // Add a viewport that's associated with some display port that's not of interest.
1442 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001443 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1444 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001445 // Add another viewport, connected to HDMI1 port
1446 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001447 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1448 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001449
1450 // Check that correct display viewport was returned by comparing the display ports.
1451 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1452 ASSERT_TRUE(hdmi1Viewport);
1453 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1454 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1455
1456 // Check that we can still get the same viewport using the uniqueId
1457 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1458 ASSERT_TRUE(hdmi1Viewport);
1459 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1460 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1461 ASSERT_EQ(type, hdmi1Viewport->type);
1462
1463 // Check that we cannot find a port with "HDMI2", because we never added one
1464 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1465 ASSERT_FALSE(hdmi2Viewport);
1466}
1467
Michael Wrightd02c5b62014-02-10 15:10:22 -08001468// --- InputReaderTest ---
1469
1470class InputReaderTest : public testing::Test {
1471protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001472 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001473 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001474 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001475 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476
Chris Yea52ade12020-08-27 16:49:20 -07001477 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001478 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001480 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481
Prabir Pradhan28efc192019-11-05 01:10:04 +00001482 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1483 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001484 }
1485
Chris Yea52ade12020-08-27 16:49:20 -07001486 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001487 mFakeListener.clear();
1488 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001489 }
1490
Chris Ye1b0c7342020-07-28 21:57:03 -07001491 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001492 const PropertyMap* configuration) {
1493 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001494
1495 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001496 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497 }
1498 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001499 mReader->loopOnce();
1500 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001501 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1502 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503 }
1504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001505 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001506 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001507 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001508 }
1509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001510 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001511 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001512 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001513 }
1514
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001515 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001516 const std::string& name,
1517 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001518 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1520 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001521 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001522 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001523 return mapper;
1524 }
1525};
1526
Chris Ye98d3f532020-10-01 21:48:59 -07001527TEST_F(InputReaderTest, PolicyGetInputDevices) {
1528 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1529 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1530 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001531
1532 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001533 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001534 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001535 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001536 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1538 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001539 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001540}
1541
Chris Yee7310032020-09-22 15:36:28 -07001542TEST_F(InputReaderTest, GetMergedInputDevices) {
1543 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1544 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1545 // Add two subdevices to device
1546 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1547 // Must add at least one mapper or the device will be ignored!
1548 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1549 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1550
1551 // Push same device instance for next device to be added, so they'll have same identifier.
1552 mReader->pushNextDevice(device);
1553 mReader->pushNextDevice(device);
1554 ASSERT_NO_FATAL_FAILURE(
1555 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1556 ASSERT_NO_FATAL_FAILURE(
1557 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1558
1559 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001560 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001561}
1562
Chris Yee14523a2020-12-19 13:46:00 -08001563TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1564 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1565 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1566 // Add two subdevices to device
1567 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1568 // Must add at least one mapper or the device will be ignored!
1569 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1570 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1571
1572 // Push same device instance for next device to be added, so they'll have same identifier.
1573 mReader->pushNextDevice(device);
1574 mReader->pushNextDevice(device);
1575 // Sensor device is initially disabled
1576 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1577 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1578 nullptr));
1579 // Device is disabled because the only sub device is a sensor device and disabled initially.
1580 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1581 ASSERT_FALSE(device->isEnabled());
1582 ASSERT_NO_FATAL_FAILURE(
1583 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1584 // The merged device is enabled if any sub device is enabled
1585 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1586 ASSERT_TRUE(device->isEnabled());
1587}
1588
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001589TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001591 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001592 constexpr int32_t eventHubId = 1;
1593 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001594 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001595 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001596 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001597 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598
Yi Kong9b14ac62018-07-17 13:48:38 -07001599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001600
1601 NotifyDeviceResetArgs resetArgs;
1602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603 ASSERT_EQ(deviceId, resetArgs.deviceId);
1604
1605 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001606 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001607 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001608
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001610 ASSERT_EQ(deviceId, resetArgs.deviceId);
1611 ASSERT_EQ(device->isEnabled(), false);
1612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001613 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001614 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001617 ASSERT_EQ(device->isEnabled(), false);
1618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001619 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001620 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001622 ASSERT_EQ(deviceId, resetArgs.deviceId);
1623 ASSERT_EQ(device->isEnabled(), true);
1624}
1625
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001628 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001630 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001631 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001632 AINPUT_SOURCE_KEYBOARD, nullptr);
1633 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634
1635 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1636 AINPUT_SOURCE_ANY, AKEYCODE_A))
1637 << "Should return unknown when the device id is >= 0 but unknown.";
1638
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1640 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1641 << "Should return unknown when the device id is valid but the sources are not "
1642 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 ASSERT_EQ(AKEY_STATE_DOWN,
1645 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1646 AKEYCODE_A))
1647 << "Should return value provided by mapper when device id is valid and the device "
1648 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001649
1650 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1651 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1652 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1653
1654 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1655 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1656 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1657}
1658
1659TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001661 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001662 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001663 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001664 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001665 AINPUT_SOURCE_KEYBOARD, nullptr);
1666 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001667
1668 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1669 AINPUT_SOURCE_ANY, KEY_A))
1670 << "Should return unknown when the device id is >= 0 but unknown.";
1671
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001672 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1673 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1674 << "Should return unknown when the device id is valid but the sources are not "
1675 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001676
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001677 ASSERT_EQ(AKEY_STATE_DOWN,
1678 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1679 KEY_A))
1680 << "Should return value provided by mapper when device id is valid and the device "
1681 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682
1683 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1684 AINPUT_SOURCE_TRACKBALL, KEY_A))
1685 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1686
1687 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1688 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1689 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1690}
1691
1692TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001693 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001694 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001695 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001696 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001697 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001698 AINPUT_SOURCE_KEYBOARD, nullptr);
1699 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001700
1701 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1702 AINPUT_SOURCE_ANY, SW_LID))
1703 << "Should return unknown when the device id is >= 0 but unknown.";
1704
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001705 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1706 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1707 << "Should return unknown when the device id is valid but the sources are not "
1708 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001709
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 ASSERT_EQ(AKEY_STATE_DOWN,
1711 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1712 SW_LID))
1713 << "Should return value provided by mapper when device id is valid and the device "
1714 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001715
1716 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1717 AINPUT_SOURCE_TRACKBALL, SW_LID))
1718 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1719
1720 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1721 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1722 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1723}
1724
1725TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001727 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001728 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001729 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001731 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001732
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001733 mapper.addSupportedKeyCode(AKEYCODE_A);
1734 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001735
1736 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1737 uint8_t flags[4] = { 0, 0, 0, 1 };
1738
1739 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1740 << "Should return false when device id is >= 0 but unknown.";
1741 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1742
1743 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1745 << "Should return false when device id is valid but the sources are not supported by "
1746 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001747 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1748
1749 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1751 keyCodes, flags))
1752 << "Should return value provided by mapper when device id is valid and the device "
1753 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1755
1756 flags[3] = 1;
1757 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1758 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1759 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1760
1761 flags[3] = 1;
1762 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1763 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1764 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1765}
1766
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001767TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001768 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001769 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001770
1771 NotifyConfigurationChangedArgs args;
1772
1773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1774 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1775}
1776
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001777TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001778 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001779 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001780 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001782 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001783 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001785 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001787 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001788 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001789 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1790
1791 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001792 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001793 ASSERT_EQ(when, event.when);
1794 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001795 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001796 ASSERT_EQ(EV_KEY, event.type);
1797 ASSERT_EQ(KEY_A, event.code);
1798 ASSERT_EQ(1, event.value);
1799}
1800
Garfield Tan1c7bc862020-01-28 13:24:04 -08001801TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001803 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t eventHubId = 1;
1805 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001806 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001807 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001808 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001809 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001810
1811 NotifyDeviceResetArgs resetArgs;
1812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001813 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001816 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001818 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001819 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001820
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001821 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001822 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001824 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001825 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001827 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001828 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001830 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001831 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001832}
1833
Garfield Tan1c7bc862020-01-28 13:24:04 -08001834TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1835 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001836 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001837 constexpr int32_t eventHubId = 1;
1838 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1839 // Must add at least one mapper or the device will be ignored!
1840 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001841 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001842 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1843
1844 NotifyDeviceResetArgs resetArgs;
1845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1846 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1847}
1848
Arthur Hungc23540e2018-11-29 20:42:11 +08001849TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001850 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001851 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001853 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1855 FakeInputMapper& mapper =
1856 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001857 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001858
1859 const uint8_t hdmi1 = 1;
1860
1861 // Associated touch screen with second display.
1862 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1863
1864 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001865 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001866 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001867 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001868 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001869 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001870 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001871 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001872 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001873 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001874
1875 // Add the device, and make sure all of the callbacks are triggered.
1876 // The device is added after the input port associations are processed since
1877 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001878 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001881 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001882
Arthur Hung2c9a3342019-07-23 14:18:59 +08001883 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001884 ASSERT_EQ(deviceId, device->getId());
1885 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1886 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001887
1888 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001889 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001890 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001891 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001892}
1893
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001894TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1896 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1897 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1898 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1899 // Must add at least one mapper or the device will be ignored!
1900 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1901 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1902 mReader->pushNextDevice(device);
1903 mReader->pushNextDevice(device);
1904 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1905 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1906
1907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1908
1909 NotifyDeviceResetArgs resetArgs;
1910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1911 ASSERT_EQ(deviceId, resetArgs.deviceId);
1912 ASSERT_TRUE(device->isEnabled());
1913 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1914 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1915
1916 disableDevice(deviceId);
1917 mReader->loopOnce();
1918
1919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1920 ASSERT_EQ(deviceId, resetArgs.deviceId);
1921 ASSERT_FALSE(device->isEnabled());
1922 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1923 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1924
1925 enableDevice(deviceId);
1926 mReader->loopOnce();
1927
1928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1929 ASSERT_EQ(deviceId, resetArgs.deviceId);
1930 ASSERT_TRUE(device->isEnabled());
1931 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1932 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1933}
1934
1935TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1936 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1937 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1938 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1939 // Add two subdevices to device
1940 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1941 FakeInputMapper& mapperDevice1 =
1942 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1943 FakeInputMapper& mapperDevice2 =
1944 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1945 mReader->pushNextDevice(device);
1946 mReader->pushNextDevice(device);
1947 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1948 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1949
1950 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1951 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1952
1953 ASSERT_EQ(AKEY_STATE_DOWN,
1954 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1955 ASSERT_EQ(AKEY_STATE_DOWN,
1956 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1957 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1958 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1959}
1960
Prabir Pradhan7e186182020-11-10 13:56:45 -08001961TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1962 NotifyPointerCaptureChangedArgs args;
1963
1964 mFakePolicy->setPointerCapture(true);
1965 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1966 mReader->loopOnce();
1967 mFakeListener->assertNotifyCaptureWasCalled(&args);
1968 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1969
1970 mFakePolicy->setPointerCapture(false);
1971 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1972 mReader->loopOnce();
1973 mFakeListener->assertNotifyCaptureWasCalled(&args);
1974 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1975
1976 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1977 // does not change.
1978 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1979 mReader->loopOnce();
1980 mFakeListener->assertNotifyCaptureWasCalled(&args);
1981 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1982}
1983
Chris Ye87143712020-11-10 05:05:58 +00001984class FakeVibratorInputMapper : public FakeInputMapper {
1985public:
1986 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1987 : FakeInputMapper(deviceContext, sources) {}
1988
1989 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1990};
1991
1992TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1993 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1994 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1995 constexpr int32_t eventHubId = 1;
1996 const char* DEVICE_LOCATION = "BLUETOOTH";
1997 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1998 FakeVibratorInputMapper& mapper =
1999 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2000 mReader->pushNextDevice(device);
2001
2002 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2003 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2004
2005 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2006 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2007}
2008
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002009// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002010
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002011class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002012public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002013 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002014
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002015 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002016
2017 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2018
2019 void dump(std::string& dump) override {}
2020
2021 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2022 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002023 }
2024
Chris Yee2b1e5c2021-03-10 22:45:12 -08002025 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2026 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002027 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002028
2029 bool setLightColor(int32_t lightId, int32_t color) override {
2030 getDeviceContext().setLightBrightness(lightId, color >> 24);
2031 return true;
2032 }
2033
2034 std::optional<int32_t> getLightColor(int32_t lightId) override {
2035 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2036 if (!result.has_value()) {
2037 return std::nullopt;
2038 }
2039 return result.value() << 24;
2040 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002041
2042 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2043
2044 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2045
2046private:
2047 InputDeviceContext& mDeviceContext;
2048 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2049 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002050};
2051
Chris Yee2b1e5c2021-03-10 22:45:12 -08002052TEST_F(InputReaderTest, BatteryGetCapacity) {
2053 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2054 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2055 constexpr int32_t eventHubId = 1;
2056 const char* DEVICE_LOCATION = "BLUETOOTH";
2057 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002058 FakePeripheralController& controller =
2059 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002060 mReader->pushNextDevice(device);
2061
2062 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2063
2064 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2065 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2066}
2067
2068TEST_F(InputReaderTest, BatteryGetStatus) {
2069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2070 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2071 constexpr int32_t eventHubId = 1;
2072 const char* DEVICE_LOCATION = "BLUETOOTH";
2073 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002074 FakePeripheralController& controller =
2075 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002076 mReader->pushNextDevice(device);
2077
2078 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2079
2080 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2081 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2082}
2083
Chris Ye3fdbfef2021-01-06 18:45:18 -08002084TEST_F(InputReaderTest, LightGetColor) {
2085 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2086 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2087 constexpr int32_t eventHubId = 1;
2088 const char* DEVICE_LOCATION = "BLUETOOTH";
2089 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002090 FakePeripheralController& controller =
2091 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002092 mReader->pushNextDevice(device);
2093 RawLightInfo info = {.id = 1,
2094 .name = "Mono",
2095 .maxBrightness = 255,
2096 .flags = InputLightClass::BRIGHTNESS,
2097 .path = ""};
2098 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2099 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2100
2101 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002102
Chris Yee2b1e5c2021-03-10 22:45:12 -08002103 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2104 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002105 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2106 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2107}
2108
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002109// --- InputReaderIntegrationTest ---
2110
2111// These tests create and interact with the InputReader only through its interface.
2112// The InputReader is started during SetUp(), which starts its processing in its own
2113// thread. The tests use linux uinput to emulate input devices.
2114// NOTE: Interacting with the physical device while these tests are running may cause
2115// the tests to fail.
2116class InputReaderIntegrationTest : public testing::Test {
2117protected:
2118 sp<TestInputListener> mTestListener;
2119 sp<FakeInputReaderPolicy> mFakePolicy;
2120 sp<InputReaderInterface> mReader;
2121
Chris Yea52ade12020-08-27 16:49:20 -07002122 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002123 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002124 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2125 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002126
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002127 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002128 ASSERT_EQ(mReader->start(), OK);
2129
2130 // Since this test is run on a real device, all the input devices connected
2131 // to the test device will show up in mReader. We wait for those input devices to
2132 // show up before beginning the tests.
2133 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2134 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2135 }
2136
Chris Yea52ade12020-08-27 16:49:20 -07002137 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002138 ASSERT_EQ(mReader->stop(), OK);
2139 mTestListener.clear();
2140 mFakePolicy.clear();
2141 }
2142};
2143
2144TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2145 // An invalid input device that is only used for this test.
2146 class InvalidUinputDevice : public UinputDevice {
2147 public:
2148 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2149
2150 private:
2151 void configureDevice(int fd, uinput_user_dev* device) override {}
2152 };
2153
2154 const size_t numDevices = mFakePolicy->getInputDevices().size();
2155
2156 // UinputDevice does not set any event or key bits, so InputReader should not
2157 // consider it as a valid device.
2158 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2159 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2160 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2161 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2162
2163 invalidDevice.reset();
2164 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2165 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2166 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2167}
2168
2169TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2170 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2171
2172 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2173 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2174 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2175 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2176
2177 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002178 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002179 const auto& it =
2180 std::find_if(inputDevices.begin(), inputDevices.end(),
2181 [&keyboard](const InputDeviceInfo& info) {
2182 return info.getIdentifier().name == keyboard->getName();
2183 });
2184
2185 ASSERT_NE(it, inputDevices.end());
2186 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2187 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2188 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002189
2190 keyboard.reset();
2191 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2192 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2193 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2194}
2195
2196TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2197 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2198 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2199
2200 NotifyConfigurationChangedArgs configChangedArgs;
2201 ASSERT_NO_FATAL_FAILURE(
2202 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002203 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002204 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2205
2206 NotifyKeyArgs keyArgs;
2207 keyboard->pressAndReleaseHomeKey();
2208 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2209 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002210 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002211 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002212 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002213 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002214 prevTimestamp = keyArgs.eventTime;
2215
2216 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2217 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002218 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002219 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002220 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002221}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002222
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002223/**
2224 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2225 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2226 * are passed to the listener.
2227 */
2228static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2229TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2230 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2231 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2232 NotifyKeyArgs keyArgs;
2233
2234 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2235 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2236 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2237 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2238
2239 controller->pressAndReleaseKey(BTN_GEAR_UP);
2240 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2241 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2242 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2243}
2244
Arthur Hungaab25622020-01-16 11:22:11 +08002245// --- TouchProcessTest ---
2246class TouchIntegrationTest : public InputReaderIntegrationTest {
2247protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002248 const std::string UNIQUE_ID = "local:0";
2249
Chris Yea52ade12020-08-27 16:49:20 -07002250 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002251 InputReaderIntegrationTest::SetUp();
2252 // At least add an internal display.
2253 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2254 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002255 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002256
2257 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2258 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2259 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2260 }
2261
2262 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2263 int32_t orientation, const std::string& uniqueId,
2264 std::optional<uint8_t> physicalPort,
2265 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002266 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2267 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002268 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2269 }
2270
2271 std::unique_ptr<UinputTouchScreen> mDevice;
2272};
2273
2274TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2275 NotifyMotionArgs args;
2276 const Point centerPoint = mDevice->getCenterPoint();
2277
2278 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002279 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002280 mDevice->sendDown(centerPoint);
2281 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2283
2284 // ACTION_MOVE
2285 mDevice->sendMove(centerPoint + Point(1, 1));
2286 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2288
2289 // ACTION_UP
2290 mDevice->sendUp();
2291 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2292 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2293}
2294
2295TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2296 NotifyMotionArgs args;
2297 const Point centerPoint = mDevice->getCenterPoint();
2298
2299 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002300 mDevice->sendSlot(FIRST_SLOT);
2301 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002302 mDevice->sendDown(centerPoint);
2303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2304 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2305
2306 // ACTION_POINTER_DOWN (Second slot)
2307 const Point secondPoint = centerPoint + Point(100, 100);
2308 mDevice->sendSlot(SECOND_SLOT);
2309 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2310 mDevice->sendDown(secondPoint + Point(1, 1));
2311 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2312 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2313 args.action);
2314
2315 // ACTION_MOVE (Second slot)
2316 mDevice->sendMove(secondPoint);
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2319
2320 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002321 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002322 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002323 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002324 args.action);
2325
2326 // ACTION_UP
2327 mDevice->sendSlot(FIRST_SLOT);
2328 mDevice->sendUp();
2329 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2331}
2332
2333TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2334 NotifyMotionArgs args;
2335 const Point centerPoint = mDevice->getCenterPoint();
2336
2337 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002338 mDevice->sendSlot(FIRST_SLOT);
2339 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002340 mDevice->sendDown(centerPoint);
2341 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2342 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2343
arthurhungcc7f9802020-04-30 17:55:40 +08002344 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002345 const Point secondPoint = centerPoint + Point(100, 100);
2346 mDevice->sendSlot(SECOND_SLOT);
2347 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2348 mDevice->sendDown(secondPoint);
2349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2350 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2351 args.action);
2352
arthurhungcc7f9802020-04-30 17:55:40 +08002353 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002354 mDevice->sendMove(secondPoint + Point(1, 1));
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2357
arthurhungcc7f9802020-04-30 17:55:40 +08002358 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2359 // a palm event.
2360 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002361 mDevice->sendToolType(MT_TOOL_PALM);
2362 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002363 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2364 args.action);
2365 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002366
arthurhungcc7f9802020-04-30 17:55:40 +08002367 // Send up to second slot, expect first slot send moving.
2368 mDevice->sendPointerUp();
2369 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002371
arthurhungcc7f9802020-04-30 17:55:40 +08002372 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002373 mDevice->sendSlot(FIRST_SLOT);
2374 mDevice->sendUp();
2375
arthurhungcc7f9802020-04-30 17:55:40 +08002376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2377 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002378}
2379
Michael Wrightd02c5b62014-02-10 15:10:22 -08002380// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381class InputDeviceTest : public testing::Test {
2382protected:
2383 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002384 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002385 static const int32_t DEVICE_ID;
2386 static const int32_t DEVICE_GENERATION;
2387 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002388 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002389 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002391 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002393 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002394 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002395 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396
Chris Yea52ade12020-08-27 16:49:20 -07002397 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002398 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002400 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002401 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2402 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403 InputDeviceIdentifier identifier;
2404 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002405 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002406 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002407 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002408 mReader->pushNextDevice(mDevice);
2409 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2410 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411 }
2412
Chris Yea52ade12020-08-27 16:49:20 -07002413 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414 mFakeListener.clear();
2415 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416 }
2417};
2418
2419const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002420const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002421const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2423const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002424const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2425 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002426const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427
2428TEST_F(InputDeviceTest, ImmutableProperties) {
2429 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002430 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002431 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432}
2433
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002434TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2435 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002436}
2437
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2439 // Configuration.
2440 InputReaderConfiguration config;
2441 mDevice->configure(ARBITRARY_TIME, &config, 0);
2442
2443 // Reset.
2444 mDevice->reset(ARBITRARY_TIME);
2445
2446 NotifyDeviceResetArgs resetArgs;
2447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2448 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2449 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2450
2451 // Metadata.
2452 ASSERT_TRUE(mDevice->isIgnored());
2453 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2454
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002455 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002457 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2459 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2460
2461 // State queries.
2462 ASSERT_EQ(0, mDevice->getMetaState());
2463
2464 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2465 << "Ignored device should return unknown key code state.";
2466 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2467 << "Ignored device should return unknown scan code state.";
2468 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2469 << "Ignored device should return unknown switch state.";
2470
2471 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2472 uint8_t flags[2] = { 0, 1 };
2473 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2474 << "Ignored device should never mark any key codes.";
2475 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2476 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2477}
2478
2479TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2480 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002481 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002483 FakeInputMapper& mapper1 =
2484 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002485 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2486 mapper1.setMetaState(AMETA_ALT_ON);
2487 mapper1.addSupportedKeyCode(AKEYCODE_A);
2488 mapper1.addSupportedKeyCode(AKEYCODE_B);
2489 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2490 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2491 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2492 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2493 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002495 FakeInputMapper& mapper2 =
2496 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002497 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002498
2499 InputReaderConfiguration config;
2500 mDevice->configure(ARBITRARY_TIME, &config, 0);
2501
2502 String8 propertyValue;
2503 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2504 << "Device should have read configuration during configuration phase.";
2505 ASSERT_STREQ("value", propertyValue.string());
2506
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002507 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2508 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509
2510 // Reset
2511 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002512 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2513 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514
2515 NotifyDeviceResetArgs resetArgs;
2516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2517 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2518 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2519
2520 // Metadata.
2521 ASSERT_FALSE(mDevice->isIgnored());
2522 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2523
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002524 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002526 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2528 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2529
2530 // State queries.
2531 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2532 << "Should query mappers and combine meta states.";
2533
2534 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2535 << "Should return unknown key code state when source not supported.";
2536 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2537 << "Should return unknown scan code state when source not supported.";
2538 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2539 << "Should return unknown switch state when source not supported.";
2540
2541 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2542 << "Should query mapper when source is supported.";
2543 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2544 << "Should query mapper when source is supported.";
2545 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2546 << "Should query mapper when source is supported.";
2547
2548 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2549 uint8_t flags[4] = { 0, 0, 0, 1 };
2550 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2551 << "Should do nothing when source is unsupported.";
2552 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2553 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2554 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2555 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2556
2557 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2558 << "Should query mapper when source is supported.";
2559 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2560 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2561 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2562 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2563
2564 // Event handling.
2565 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002566 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 mDevice->process(&event, 1);
2568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002569 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2570 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571}
2572
Arthur Hung2c9a3342019-07-23 14:18:59 +08002573// A single input device is associated with a specific display. Check that:
2574// 1. Device is disabled if the viewport corresponding to the associated display is not found
2575// 2. Device is disabled when setEnabled API is called
2576TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002577 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002578
2579 // First Configuration.
2580 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2581
2582 // Device should be enabled by default.
2583 ASSERT_TRUE(mDevice->isEnabled());
2584
2585 // Prepare associated info.
2586 constexpr uint8_t hdmi = 1;
2587 const std::string UNIQUE_ID = "local:1";
2588
2589 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2590 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2591 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2592 // Device should be disabled because it is associated with a specific display via
2593 // input port <-> display port association, but the corresponding display is not found
2594 ASSERT_FALSE(mDevice->isEnabled());
2595
2596 // Prepare displays.
2597 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002598 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2599 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002600 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2601 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2602 ASSERT_TRUE(mDevice->isEnabled());
2603
2604 // Device should be disabled after set disable.
2605 mFakePolicy->addDisabledDevice(mDevice->getId());
2606 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2607 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2608 ASSERT_FALSE(mDevice->isEnabled());
2609
2610 // Device should still be disabled even found the associated display.
2611 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2612 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2613 ASSERT_FALSE(mDevice->isEnabled());
2614}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002615
Christine Franks1ba71cc2021-04-07 14:37:42 -07002616TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2617 // Device should be enabled by default.
2618 mFakePolicy->clearViewports();
2619 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2620 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2621 ASSERT_TRUE(mDevice->isEnabled());
2622
2623 // Device should be disabled because it is associated with a specific display, but the
2624 // corresponding display is not found.
2625 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2626 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2627 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2628 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2629 ASSERT_FALSE(mDevice->isEnabled());
2630
2631 // Device should be enabled when a display is found.
2632 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2633 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2634 NO_PORT, ViewportType::INTERNAL);
2635 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2636 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2637 ASSERT_TRUE(mDevice->isEnabled());
2638
2639 // Device should be disabled after set disable.
2640 mFakePolicy->addDisabledDevice(mDevice->getId());
2641 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2642 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2643 ASSERT_FALSE(mDevice->isEnabled());
2644
2645 // Device should still be disabled even found the associated display.
2646 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2647 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2648 ASSERT_FALSE(mDevice->isEnabled());
2649}
2650
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651// --- InputMapperTest ---
2652
2653class InputMapperTest : public testing::Test {
2654protected:
2655 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002656 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657 static const int32_t DEVICE_ID;
2658 static const int32_t DEVICE_GENERATION;
2659 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002660 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002661 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002663 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002665 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002666 std::unique_ptr<InstrumentedInputReader> mReader;
2667 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668
Chris Ye1b0c7342020-07-28 21:57:03 -07002669 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002670 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002672 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002673 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2674 mFakeListener);
2675 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676 }
2677
Chris Yea52ade12020-08-27 16:49:20 -07002678 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002679
Chris Yea52ade12020-08-27 16:49:20 -07002680 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681 mFakeListener.clear();
2682 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683 }
2684
2685 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002686 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 }
2688
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002689 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002690 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002691 mReader->requestRefreshConfiguration(changes);
2692 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002693 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002694 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2695 }
2696
arthurhungdcef2dc2020-08-11 14:47:50 +08002697 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2698 const std::string& location, int32_t eventHubId,
2699 Flags<InputDeviceClass> classes) {
2700 InputDeviceIdentifier identifier;
2701 identifier.name = name;
2702 identifier.location = location;
2703 std::shared_ptr<InputDevice> device =
2704 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2705 identifier);
2706 mReader->pushNextDevice(device);
2707 mFakeEventHub->addDevice(eventHubId, name, classes);
2708 mReader->loopOnce();
2709 return device;
2710 }
2711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002712 template <class T, typename... Args>
2713 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002714 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002715 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002717 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002718 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719 }
2720
2721 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002722 int32_t orientation, const std::string& uniqueId,
2723 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002724 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2725 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002726 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2727 }
2728
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002729 void clearViewports() {
2730 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 }
2732
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002733 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2734 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735 RawEvent event;
2736 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002737 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002738 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739 event.type = type;
2740 event.code = code;
2741 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002742 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002743 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744 }
2745
2746 static void assertMotionRange(const InputDeviceInfo& info,
2747 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2748 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002749 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002750 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2751 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2752 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2753 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2754 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2755 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2756 }
2757
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002758 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2759 float size, float touchMajor, float touchMinor, float toolMajor,
2760 float toolMinor, float orientation, float distance,
2761 float scaledAxisEpsilon = 1.f) {
2762 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2763 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2765 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002766 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2767 scaledAxisEpsilon);
2768 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2769 scaledAxisEpsilon);
2770 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2771 scaledAxisEpsilon);
2772 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2773 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2775 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2776 }
2777
Michael Wright17db18e2020-06-26 20:51:44 +01002778 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002780 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781 ASSERT_NEAR(x, actualX, 1);
2782 ASSERT_NEAR(y, actualY, 1);
2783 }
2784};
2785
2786const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002787const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002788const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2790const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002791const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2792 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794
2795// --- SwitchInputMapperTest ---
2796
2797class SwitchInputMapperTest : public InputMapperTest {
2798protected:
2799};
2800
2801TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002802 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002804 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805}
2806
2807TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002808 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002810 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002811 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002813 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002814 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815}
2816
2817TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002818 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
2825 NotifySwitchArgs args;
2826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2827 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002828 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2829 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830 args.switchMask);
2831 ASSERT_EQ(uint32_t(0), args.policyFlags);
2832}
2833
Chris Ye87143712020-11-10 05:05:58 +00002834// --- VibratorInputMapperTest ---
2835class VibratorInputMapperTest : public InputMapperTest {
2836protected:
2837 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2838};
2839
2840TEST_F(VibratorInputMapperTest, GetSources) {
2841 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2842
2843 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2844}
2845
2846TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2847 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2848
2849 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2850}
2851
2852TEST_F(VibratorInputMapperTest, Vibrate) {
2853 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002854 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002855 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2856
2857 VibrationElement pattern(2);
2858 VibrationSequence sequence(2);
2859 pattern.duration = std::chrono::milliseconds(200);
2860 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2861 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2862 sequence.addElement(pattern);
2863 pattern.duration = std::chrono::milliseconds(500);
2864 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2865 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2866 sequence.addElement(pattern);
2867
2868 std::vector<int64_t> timings = {0, 1};
2869 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2870
2871 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002872 // Start vibrating
2873 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002874 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002875 // Verify vibrator state listener was notified.
2876 mReader->loopOnce();
2877 NotifyVibratorStateArgs args;
2878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2879 ASSERT_EQ(DEVICE_ID, args.deviceId);
2880 ASSERT_TRUE(args.isOn);
2881 // Stop vibrating
2882 mapper.cancelVibrate(VIBRATION_TOKEN);
2883 ASSERT_FALSE(mapper.isVibrating());
2884 // Verify vibrator state listener was notified.
2885 mReader->loopOnce();
2886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2887 ASSERT_EQ(DEVICE_ID, args.deviceId);
2888 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002889}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002890
Chris Yef59a2f42020-10-16 12:55:26 -07002891// --- SensorInputMapperTest ---
2892
2893class SensorInputMapperTest : public InputMapperTest {
2894protected:
2895 static const int32_t ACCEL_RAW_MIN;
2896 static const int32_t ACCEL_RAW_MAX;
2897 static const int32_t ACCEL_RAW_FUZZ;
2898 static const int32_t ACCEL_RAW_FLAT;
2899 static const int32_t ACCEL_RAW_RESOLUTION;
2900
2901 static const int32_t GYRO_RAW_MIN;
2902 static const int32_t GYRO_RAW_MAX;
2903 static const int32_t GYRO_RAW_FUZZ;
2904 static const int32_t GYRO_RAW_FLAT;
2905 static const int32_t GYRO_RAW_RESOLUTION;
2906
2907 static const float GRAVITY_MS2_UNIT;
2908 static const float DEGREE_RADIAN_UNIT;
2909
2910 void prepareAccelAxes();
2911 void prepareGyroAxes();
2912 void setAccelProperties();
2913 void setGyroProperties();
2914 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2915};
2916
2917const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2918const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2919const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2920const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2921const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2922
2923const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2924const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2925const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2926const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2927const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2928
2929const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2930const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2931
2932void SensorInputMapperTest::prepareAccelAxes() {
2933 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2934 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2935 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2936 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2937 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2938 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2939}
2940
2941void SensorInputMapperTest::prepareGyroAxes() {
2942 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2943 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2944 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2945 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2946 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2947 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2948}
2949
2950void SensorInputMapperTest::setAccelProperties() {
2951 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2952 /* sensorDataIndex */ 0);
2953 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2954 /* sensorDataIndex */ 1);
2955 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2956 /* sensorDataIndex */ 2);
2957 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2958 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2959 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2960 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2961 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2962}
2963
2964void SensorInputMapperTest::setGyroProperties() {
2965 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2966 /* sensorDataIndex */ 0);
2967 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2968 /* sensorDataIndex */ 1);
2969 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2970 /* sensorDataIndex */ 2);
2971 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2972 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2973 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2974 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2975 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2976}
2977
2978TEST_F(SensorInputMapperTest, GetSources) {
2979 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2980
2981 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2982}
2983
2984TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2985 setAccelProperties();
2986 prepareAccelAxes();
2987 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2988
2989 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2990 std::chrono::microseconds(10000),
2991 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002992 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002998
2999 NotifySensorArgs args;
3000 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3001 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3002 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3003
3004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3005 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3006 ASSERT_EQ(args.deviceId, DEVICE_ID);
3007 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3008 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3009 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3010 ASSERT_EQ(args.values, values);
3011 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3012}
3013
3014TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3015 setGyroProperties();
3016 prepareGyroAxes();
3017 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3018
3019 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3020 std::chrono::microseconds(10000),
3021 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003022 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003028
3029 NotifySensorArgs args;
3030 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3031 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3032 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3033
3034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3035 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3036 ASSERT_EQ(args.deviceId, DEVICE_ID);
3037 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3038 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3039 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3040 ASSERT_EQ(args.values, values);
3041 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3042}
3043
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044// --- KeyboardInputMapperTest ---
3045
3046class KeyboardInputMapperTest : public InputMapperTest {
3047protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003048 const std::string UNIQUE_ID = "local:0";
3049
3050 void prepareDisplay(int32_t orientation);
3051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003052 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003053 int32_t originalKeyCode, int32_t rotatedKeyCode,
3054 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055};
3056
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003057/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3058 * orientation.
3059 */
3060void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003061 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3062 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003063}
3064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003065void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003066 int32_t originalScanCode, int32_t originalKeyCode,
3067 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 NotifyKeyArgs args;
3069
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3072 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3073 ASSERT_EQ(originalScanCode, args.scanCode);
3074 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003075 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3079 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3080 ASSERT_EQ(originalScanCode, args.scanCode);
3081 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003082 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083}
3084
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003086 KeyboardInputMapper& mapper =
3087 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3088 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003090 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091}
3092
3093TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3094 const int32_t USAGE_A = 0x070004;
3095 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3097 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003098 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3099 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3100 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102 KeyboardInputMapper& mapper =
3103 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3104 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003105 // Initial metastate to AMETA_NONE.
3106 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3107 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
3109 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111 NotifyKeyArgs args;
3112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3113 ASSERT_EQ(DEVICE_ID, args.deviceId);
3114 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3115 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3116 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3117 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3118 ASSERT_EQ(KEY_HOME, args.scanCode);
3119 ASSERT_EQ(AMETA_NONE, args.metaState);
3120 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3121 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3122 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3123
3124 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003125 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3127 ASSERT_EQ(DEVICE_ID, args.deviceId);
3128 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3129 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3130 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3131 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3132 ASSERT_EQ(KEY_HOME, args.scanCode);
3133 ASSERT_EQ(AMETA_NONE, args.metaState);
3134 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3135 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3136 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3137
3138 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3142 ASSERT_EQ(DEVICE_ID, args.deviceId);
3143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3144 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3146 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3147 ASSERT_EQ(0, args.scanCode);
3148 ASSERT_EQ(AMETA_NONE, args.metaState);
3149 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3150 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3151 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3152
3153 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3155 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3157 ASSERT_EQ(DEVICE_ID, args.deviceId);
3158 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3159 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3160 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3161 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3162 ASSERT_EQ(0, args.scanCode);
3163 ASSERT_EQ(AMETA_NONE, args.metaState);
3164 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3165 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3166 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3167
3168 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3172 ASSERT_EQ(DEVICE_ID, args.deviceId);
3173 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3174 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3175 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3176 ASSERT_EQ(0, args.keyCode);
3177 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3178 ASSERT_EQ(AMETA_NONE, args.metaState);
3179 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3180 ASSERT_EQ(0U, args.policyFlags);
3181 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3182
3183 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3185 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3187 ASSERT_EQ(DEVICE_ID, args.deviceId);
3188 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3189 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3190 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3191 ASSERT_EQ(0, args.keyCode);
3192 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3193 ASSERT_EQ(AMETA_NONE, args.metaState);
3194 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3195 ASSERT_EQ(0U, args.policyFlags);
3196 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3197}
3198
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199/**
3200 * Ensure that the readTime is set to the time when the EV_KEY is received.
3201 */
3202TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3203 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3204
3205 KeyboardInputMapper& mapper =
3206 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3207 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3208 NotifyKeyArgs args;
3209
3210 // Key down
3211 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3213 ASSERT_EQ(12, args.readTime);
3214
3215 // Key up
3216 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3218 ASSERT_EQ(15, args.readTime);
3219}
3220
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003222 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3223 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003224 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3225 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3226 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003228 KeyboardInputMapper& mapper =
3229 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3230 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231
arthurhungc903df12020-08-11 15:08:42 +08003232 // Initial metastate to AMETA_NONE.
3233 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3234 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235
3236 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 NotifyKeyArgs args;
3239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003241 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003242 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243
3244 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003245 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3247 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249
3250 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003251 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255
3256 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003257 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3259 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003260 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003261 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262}
3263
3264TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3266 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3267 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 KeyboardInputMapper& mapper =
3271 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3272 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003274 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3276 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3277 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3278 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3279 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3280 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3281 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3282 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3283}
3284
3285TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3288 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003292 KeyboardInputMapper& mapper =
3293 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3294 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003296 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003297 ASSERT_NO_FATAL_FAILURE(
3298 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3299 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3300 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3302 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3303 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3304 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003306 clearViewports();
3307 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003308 ASSERT_NO_FATAL_FAILURE(
3309 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3310 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3311 AKEYCODE_DPAD_UP, DISPLAY_ID));
3312 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3313 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3314 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3315 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003317 clearViewports();
3318 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003319 ASSERT_NO_FATAL_FAILURE(
3320 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3321 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3322 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3324 AKEYCODE_DPAD_UP, DISPLAY_ID));
3325 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3326 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003328 clearViewports();
3329 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003330 ASSERT_NO_FATAL_FAILURE(
3331 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3332 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3333 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3335 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3336 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3337 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
3339 // Special case: if orientation changes while key is down, we still emit the same keycode
3340 // in the key up as we did in the key down.
3341 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003342 clearViewports();
3343 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3347 ASSERT_EQ(KEY_UP, args.scanCode);
3348 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3349
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003350 clearViewports();
3351 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3354 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3355 ASSERT_EQ(KEY_UP, args.scanCode);
3356 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3357}
3358
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003359TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3360 // If the keyboard is not orientation aware,
3361 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003362 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003364 KeyboardInputMapper& mapper =
3365 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003367 NotifyKeyArgs args;
3368
3369 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3374 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3375
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003376 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3381 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3382}
3383
3384TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3385 // If the keyboard is orientation aware,
3386 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003387 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003388
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003389 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003390 KeyboardInputMapper& mapper =
3391 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3392 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003393 NotifyKeyArgs args;
3394
3395 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3396 // ^--- already checked by the previous test
3397
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003398 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003399 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3404 ASSERT_EQ(DISPLAY_ID, args.displayId);
3405
3406 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003407 clearViewports();
3408 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003409 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3414 ASSERT_EQ(newDisplayId, args.displayId);
3415}
3416
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003418 KeyboardInputMapper& mapper =
3419 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3420 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003422 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003425 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427}
3428
3429TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003430 KeyboardInputMapper& mapper =
3431 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3432 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003434 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003435 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003437 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003438 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439}
3440
3441TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003442 KeyboardInputMapper& mapper =
3443 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3444 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003446 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447
3448 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3449 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_TRUE(flags[0]);
3452 ASSERT_FALSE(flags[1]);
3453}
3454
3455TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003456 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3457 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3458 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3460 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3461 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 KeyboardInputMapper& mapper =
3464 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3465 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003466 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003467 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3468 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
3470 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003471 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3472 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3473 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
3475 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003478 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3479 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3480 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003481 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482
3483 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003486 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3487 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3488 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003489 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490
3491 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003494 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3495 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3496 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
3499 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003502 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3503 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3504 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
3507 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003510 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3511 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3512 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
3515 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3519 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3520 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522}
3523
Chris Yea52ade12020-08-27 16:49:20 -07003524TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3525 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3526 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3527 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3528 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3529
3530 KeyboardInputMapper& mapper =
3531 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3532 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3533
3534 // Initial metastate should be AMETA_NONE as no meta keys added.
3535 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3536 // Meta state should be AMETA_NONE after reset
3537 mapper.reset(ARBITRARY_TIME);
3538 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3539 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3540 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3541 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3542
3543 NotifyKeyArgs args;
3544 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3547 ASSERT_EQ(AMETA_NONE, args.metaState);
3548 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3549 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3550 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3551
3552 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003553 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3555 ASSERT_EQ(AMETA_NONE, args.metaState);
3556 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3557 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3558 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3559}
3560
Arthur Hung2c9a3342019-07-23 14:18:59 +08003561TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3562 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3565 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3566 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003567
3568 // keyboard 2.
3569 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003570 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003571 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003572 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003573 std::shared_ptr<InputDevice> device2 =
3574 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3575 Flags<InputDeviceClass>(0));
3576
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003577 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3578 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3579 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3580 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003582 KeyboardInputMapper& mapper =
3583 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3584 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003585
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003586 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003587 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003588 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003589 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3590 device2->reset(ARBITRARY_TIME);
3591
3592 // Prepared displays and associated info.
3593 constexpr uint8_t hdmi1 = 0;
3594 constexpr uint8_t hdmi2 = 1;
3595 const std::string SECONDARY_UNIQUE_ID = "local:1";
3596
3597 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3598 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3599
3600 // No associated display viewport found, should disable the device.
3601 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3602 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3603 ASSERT_FALSE(device2->isEnabled());
3604
3605 // Prepare second display.
3606 constexpr int32_t newDisplayId = 2;
3607 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003608 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003610 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003611 // Default device will reconfigure above, need additional reconfiguration for another device.
3612 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3613 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3614
3615 // Device should be enabled after the associated display is found.
3616 ASSERT_TRUE(mDevice->isEnabled());
3617 ASSERT_TRUE(device2->isEnabled());
3618
3619 // Test pad key events
3620 ASSERT_NO_FATAL_FAILURE(
3621 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3622 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3623 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3624 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3625 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3626 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3627 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3628
3629 ASSERT_NO_FATAL_FAILURE(
3630 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3631 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3632 AKEYCODE_DPAD_RIGHT, newDisplayId));
3633 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3634 AKEYCODE_DPAD_DOWN, newDisplayId));
3635 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3636 AKEYCODE_DPAD_LEFT, newDisplayId));
3637}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638
arthurhungc903df12020-08-11 15:08:42 +08003639TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3640 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3641 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3642 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3643 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3644 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3645 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3646
3647 KeyboardInputMapper& mapper =
3648 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3649 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3650 // Initial metastate to AMETA_NONE.
3651 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3652 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3653
3654 // Initialization should have turned all of the lights off.
3655 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3656 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3657 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3658
3659 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003662 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3663 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3664
3665 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003668 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3669 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3670
3671 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003674 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3675 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3676
3677 mFakeEventHub->removeDevice(EVENTHUB_ID);
3678 mReader->loopOnce();
3679
3680 // keyboard 2 should default toggle keys.
3681 const std::string USB2 = "USB2";
3682 const std::string DEVICE_NAME2 = "KEYBOARD2";
3683 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3684 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3685 std::shared_ptr<InputDevice> device2 =
3686 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3687 Flags<InputDeviceClass>(0));
3688 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3689 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3690 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3691 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3692 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3693 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3694
arthurhung6fe95782020-10-05 22:41:16 +08003695 KeyboardInputMapper& mapper2 =
3696 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3697 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003698 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3699 device2->reset(ARBITRARY_TIME);
3700
3701 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3702 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3703 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003704 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3705 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003706}
3707
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003708// --- KeyboardInputMapperTest_ExternalDevice ---
3709
3710class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3711protected:
Chris Yea52ade12020-08-27 16:49:20 -07003712 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003713};
3714
3715TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003716 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3717 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003718
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003719 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3720 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3722 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724 KeyboardInputMapper& mapper =
3725 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3726 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003727
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003729 NotifyKeyArgs args;
3730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3731 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3732
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003733 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3735 ASSERT_EQ(uint32_t(0), args.policyFlags);
3736
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3739 ASSERT_EQ(uint32_t(0), args.policyFlags);
3740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003741 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3743 ASSERT_EQ(uint32_t(0), args.policyFlags);
3744
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3747 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3748
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003749 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3751 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3752}
3753
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003754TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003755 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3758 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003760
Powei Fengd041c5d2019-05-03 17:11:33 -07003761 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003762 KeyboardInputMapper& mapper =
3763 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3764 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003765
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003767 NotifyKeyArgs args;
3768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3769 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3770
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003771 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3773 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3774
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3777 ASSERT_EQ(uint32_t(0), args.policyFlags);
3778
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003779 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3781 ASSERT_EQ(uint32_t(0), args.policyFlags);
3782
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3785 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003787 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3789 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3790}
3791
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792// --- CursorInputMapperTest ---
3793
3794class CursorInputMapperTest : public InputMapperTest {
3795protected:
3796 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3797
Michael Wright17db18e2020-06-26 20:51:44 +01003798 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799
Chris Yea52ade12020-08-27 16:49:20 -07003800 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801 InputMapperTest::SetUp();
3802
Michael Wright17db18e2020-06-26 20:51:44 +01003803 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003804 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805 }
3806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3808 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003809
3810 void prepareDisplay(int32_t orientation) {
3811 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003812 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003813 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3814 orientation, uniqueId, NO_PORT, viewportType);
3815 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003816
3817 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3818 float pressure) {
3819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3820 0.0f, 0.0f, 0.0f, EPSILON));
3821 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822};
3823
3824const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3827 int32_t originalY, int32_t rotatedX,
3828 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 NotifyMotionArgs args;
3830
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003836 ASSERT_NO_FATAL_FAILURE(
3837 assertCursorPointerCoords(args.pointerCoords[0],
3838 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3839 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840}
3841
3842TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003844 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003846 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847}
3848
3849TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003851 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003853 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854}
3855
3856TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859
3860 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003861 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862
3863 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003864 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3865 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3867 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3868
3869 // When the bounds are set, then there should be a valid motion range.
3870 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3871
3872 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874
3875 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3876 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3877 1, 800 - 1, 0.0f, 0.0f));
3878 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3879 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3880 2, 480 - 1, 0.0f, 0.0f));
3881 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3882 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3883 0.0f, 1.0f, 0.0f, 0.0f));
3884}
3885
3886TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003888 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889
3890 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003891 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892
3893 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3894 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3895 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3896 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3897 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3898 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3899 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3900 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3901 0.0f, 1.0f, 0.0f, 0.0f));
3902}
3903
3904TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003906 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003907
arthurhungdcef2dc2020-08-11 14:47:50 +08003908 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909
3910 NotifyMotionArgs args;
3911
3912 // Button press.
3913 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3917 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3918 ASSERT_EQ(DEVICE_ID, args.deviceId);
3919 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3920 ASSERT_EQ(uint32_t(0), args.policyFlags);
3921 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3922 ASSERT_EQ(0, args.flags);
3923 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3924 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3925 ASSERT_EQ(0, args.edgeFlags);
3926 ASSERT_EQ(uint32_t(1), args.pointerCount);
3927 ASSERT_EQ(0, args.pointerProperties[0].id);
3928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003929 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3931 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3932 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3933
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3935 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3936 ASSERT_EQ(DEVICE_ID, args.deviceId);
3937 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3938 ASSERT_EQ(uint32_t(0), args.policyFlags);
3939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3940 ASSERT_EQ(0, args.flags);
3941 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3942 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3943 ASSERT_EQ(0, args.edgeFlags);
3944 ASSERT_EQ(uint32_t(1), args.pointerCount);
3945 ASSERT_EQ(0, args.pointerProperties[0].id);
3946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003947 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003948 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3949 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3950 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3951
Michael Wrightd02c5b62014-02-10 15:10:22 -08003952 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003953 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3954 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3956 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3957 ASSERT_EQ(DEVICE_ID, args.deviceId);
3958 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3959 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3961 ASSERT_EQ(0, args.flags);
3962 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3963 ASSERT_EQ(0, args.buttonState);
3964 ASSERT_EQ(0, args.edgeFlags);
3965 ASSERT_EQ(uint32_t(1), args.pointerCount);
3966 ASSERT_EQ(0, args.pointerProperties[0].id);
3967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003968 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003969 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3970 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3971 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3972
3973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3974 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3975 ASSERT_EQ(DEVICE_ID, args.deviceId);
3976 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3977 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3979 ASSERT_EQ(0, args.flags);
3980 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3981 ASSERT_EQ(0, args.buttonState);
3982 ASSERT_EQ(0, args.edgeFlags);
3983 ASSERT_EQ(uint32_t(1), args.pointerCount);
3984 ASSERT_EQ(0, args.pointerProperties[0].id);
3985 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003986 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3988 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3989 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3990}
3991
3992TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003994 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995
3996 NotifyMotionArgs args;
3997
3998 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004003 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4004 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4005 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006
4007 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004012 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4013 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014}
4015
4016TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004018 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019
4020 NotifyMotionArgs args;
4021
4022 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4026 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004027 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004028
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004031 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004032
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004038 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004039
4040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004042 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
4045TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004047 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048
4049 NotifyMotionArgs args;
4050
4051 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004058 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4059 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4060 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004064 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4065 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4066 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004067
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004074 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4075 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4076 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077
4078 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004083 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004084
4085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004087 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088}
4089
4090TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004094 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4103}
4104
4105TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106 addConfigurationProperty("cursor.mode", "navigation");
4107 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004110 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4118 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4119
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004120 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4129
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004130 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4139
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004140 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4149}
4150
4151TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004153 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154
4155 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4156 mFakePointerController->setPosition(100, 200);
4157 mFakePointerController->setButtonState(0);
4158
4159 NotifyMotionArgs motionArgs;
4160 NotifyKeyArgs keyArgs;
4161
4162 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4166 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4167 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4168 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004169 ASSERT_NO_FATAL_FAILURE(
4170 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4173 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4174 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4175 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004176 ASSERT_NO_FATAL_FAILURE(
4177 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004178
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 ASSERT_EQ(0, motionArgs.buttonState);
4184 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004185 ASSERT_NO_FATAL_FAILURE(
4186 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187
4188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004189 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190 ASSERT_EQ(0, motionArgs.buttonState);
4191 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004192 ASSERT_NO_FATAL_FAILURE(
4193 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194
4195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197 ASSERT_EQ(0, motionArgs.buttonState);
4198 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199 ASSERT_NO_FATAL_FAILURE(
4200 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201
4202 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4208 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4209 motionArgs.buttonState);
4210 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4211 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004212 ASSERT_NO_FATAL_FAILURE(
4213 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4217 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4218 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4219 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004220 ASSERT_NO_FATAL_FAILURE(
4221 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004222
4223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4225 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4226 motionArgs.buttonState);
4227 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4228 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004229 ASSERT_NO_FATAL_FAILURE(
4230 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004231
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4237 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004238 ASSERT_NO_FATAL_FAILURE(
4239 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240
4241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4244 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004245 ASSERT_NO_FATAL_FAILURE(
4246 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4252 ASSERT_EQ(0, motionArgs.buttonState);
4253 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004254 ASSERT_NO_FATAL_FAILURE(
4255 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004258
4259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_EQ(0, motionArgs.buttonState);
4261 ASSERT_EQ(0, mFakePointerController->getButtonState());
4262 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004263 ASSERT_NO_FATAL_FAILURE(
4264 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4267 ASSERT_EQ(0, motionArgs.buttonState);
4268 ASSERT_EQ(0, mFakePointerController->getButtonState());
4269 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004270 ASSERT_NO_FATAL_FAILURE(
4271 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
4273 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4277 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4278 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004284 ASSERT_NO_FATAL_FAILURE(
4285 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4289 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004291 ASSERT_NO_FATAL_FAILURE(
4292 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_EQ(0, motionArgs.buttonState);
4299 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004300 ASSERT_NO_FATAL_FAILURE(
4301 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302
4303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305 ASSERT_EQ(0, motionArgs.buttonState);
4306 ASSERT_EQ(0, mFakePointerController->getButtonState());
4307
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(
4309 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4311 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4312 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4313
4314 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4319 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4324 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004325 ASSERT_NO_FATAL_FAILURE(
4326 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4330 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_EQ(0, motionArgs.buttonState);
4340 ASSERT_EQ(0, mFakePointerController->getButtonState());
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
4344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4346 ASSERT_EQ(0, motionArgs.buttonState);
4347 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(
4349 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4353 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4354
4355 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4359 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4360 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004366 ASSERT_NO_FATAL_FAILURE(
4367 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4371 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(
4374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_EQ(0, motionArgs.buttonState);
4381 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(
4383 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4387 ASSERT_EQ(0, motionArgs.buttonState);
4388 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004389 ASSERT_NO_FATAL_FAILURE(
4390 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4394 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4395
4396 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4400 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4401 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004404 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4406 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004407 ASSERT_NO_FATAL_FAILURE(
4408 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004409
4410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4412 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(
4415 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_EQ(0, motionArgs.buttonState);
4422 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004423 ASSERT_NO_FATAL_FAILURE(
4424 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004425
4426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4428 ASSERT_EQ(0, motionArgs.buttonState);
4429 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004430 ASSERT_NO_FATAL_FAILURE(
4431 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4434 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4435 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4436}
4437
4438TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004440 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441
4442 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4443 mFakePointerController->setPosition(100, 200);
4444 mFakePointerController->setButtonState(0);
4445
4446 NotifyMotionArgs args;
4447
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004452 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4455 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 +01004456 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004457}
4458
4459TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004460 addConfigurationProperty("cursor.mode", "pointer");
4461 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004462 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004463
4464 NotifyDeviceResetArgs resetArgs;
4465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4466 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4467 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4468
4469 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4470 mFakePointerController->setPosition(100, 200);
4471 mFakePointerController->setButtonState(0);
4472
4473 NotifyMotionArgs args;
4474
4475 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4480 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4483 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 +01004484 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004485
4486 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4490 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4491 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4493 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4495 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4498 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4499
4500 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004501 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4502 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4504 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4507 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4509 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4512 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4513
4514 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4519 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4522 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 +01004523 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004524
4525 // Disable pointer capture and check that the device generation got bumped
4526 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004527 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004528 mFakePolicy->setPointerCapture(false);
4529 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004530 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531
4532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4533 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4534 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4535
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4540 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4543 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 +01004544 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545}
4546
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004547TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004548 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004549
Garfield Tan888a6a42020-01-09 11:39:16 -08004550 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004551 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004552 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4553 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004554 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4555 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004556 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4557 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4558
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004559 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4560 mFakePointerController->setPosition(100, 200);
4561 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004562
4563 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4568 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4571 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 +01004572 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004573 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4574}
4575
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576// --- TouchInputMapperTest ---
4577
4578class TouchInputMapperTest : public InputMapperTest {
4579protected:
4580 static const int32_t RAW_X_MIN;
4581 static const int32_t RAW_X_MAX;
4582 static const int32_t RAW_Y_MIN;
4583 static const int32_t RAW_Y_MAX;
4584 static const int32_t RAW_TOUCH_MIN;
4585 static const int32_t RAW_TOUCH_MAX;
4586 static const int32_t RAW_TOOL_MIN;
4587 static const int32_t RAW_TOOL_MAX;
4588 static const int32_t RAW_PRESSURE_MIN;
4589 static const int32_t RAW_PRESSURE_MAX;
4590 static const int32_t RAW_ORIENTATION_MIN;
4591 static const int32_t RAW_ORIENTATION_MAX;
4592 static const int32_t RAW_DISTANCE_MIN;
4593 static const int32_t RAW_DISTANCE_MAX;
4594 static const int32_t RAW_TILT_MIN;
4595 static const int32_t RAW_TILT_MAX;
4596 static const int32_t RAW_ID_MIN;
4597 static const int32_t RAW_ID_MAX;
4598 static const int32_t RAW_SLOT_MIN;
4599 static const int32_t RAW_SLOT_MAX;
4600 static const float X_PRECISION;
4601 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004602 static const float X_PRECISION_VIRTUAL;
4603 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604
4605 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004606 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607
4608 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4609
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004610 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004611 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004612
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 enum Axes {
4614 POSITION = 1 << 0,
4615 TOUCH = 1 << 1,
4616 TOOL = 1 << 2,
4617 PRESSURE = 1 << 3,
4618 ORIENTATION = 1 << 4,
4619 MINOR = 1 << 5,
4620 ID = 1 << 6,
4621 DISTANCE = 1 << 7,
4622 TILT = 1 << 8,
4623 SLOT = 1 << 9,
4624 TOOL_TYPE = 1 << 10,
4625 };
4626
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004627 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4628 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004629 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004631 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 int32_t toRawX(float displayX);
4633 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004634 float toCookedX(float rawX, float rawY);
4635 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004637 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004639 float toDisplayY(int32_t rawY, int32_t displayHeight);
4640
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641};
4642
4643const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4644const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4645const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4646const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4647const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4648const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4649const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4650const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004651const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4652const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4654const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4655const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4656const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4657const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4658const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4659const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4660const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4661const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4662const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4663const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4664const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004665const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4666 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4667const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4668 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004669const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4670 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671
4672const float TouchInputMapperTest::GEOMETRIC_SCALE =
4673 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4674 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4675
4676const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4677 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4678 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4679};
4680
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004681void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004682 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4683 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004684}
4685
4686void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4687 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4688 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689}
4690
Santos Cordonfa5cf462017-04-05 10:37:00 -07004691void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004692 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4693 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4694 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004695}
4696
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004698 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4699 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4700 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4701 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702}
4703
Jason Gerecke489fda82012-09-07 17:19:40 -07004704void TouchInputMapperTest::prepareLocationCalibration() {
4705 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4706}
4707
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708int32_t TouchInputMapperTest::toRawX(float displayX) {
4709 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4710}
4711
4712int32_t TouchInputMapperTest::toRawY(float displayY) {
4713 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4714}
4715
Jason Gerecke489fda82012-09-07 17:19:40 -07004716float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4717 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4718 return rawX;
4719}
4720
4721float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4722 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4723 return rawY;
4724}
4725
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004727 return toDisplayX(rawX, DISPLAY_WIDTH);
4728}
4729
4730float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4731 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732}
4733
4734float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004735 return toDisplayY(rawY, DISPLAY_HEIGHT);
4736}
4737
4738float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4739 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740}
4741
4742
4743// --- SingleTouchInputMapperTest ---
4744
4745class SingleTouchInputMapperTest : public TouchInputMapperTest {
4746protected:
4747 void prepareButtons();
4748 void prepareAxes(int axes);
4749
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004750 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4751 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4752 void processUp(SingleTouchInputMapper& mappery);
4753 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4754 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4755 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4756 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4757 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4758 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759};
4760
4761void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004762 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763}
4764
4765void SingleTouchInputMapperTest::prepareAxes(int axes) {
4766 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004767 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4768 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 }
4770 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4772 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 }
4774 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004775 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4776 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777 }
4778 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004779 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4780 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 }
4782 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004783 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4784 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 }
4786}
4787
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004788void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792}
4793
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004794void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797}
4798
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004799void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801}
4802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004803void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805}
4806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004807void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4808 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810}
4811
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004812void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814}
4815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004816void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4817 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820}
4821
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004822void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4823 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825}
4826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004827void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829}
4830
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 prepareButtons();
4833 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004834 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004836 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837}
4838
4839TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004840 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4841 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 prepareButtons();
4843 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004846 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847}
4848
4849TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 prepareButtons();
4851 prepareAxes(POSITION);
4852 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004853 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004855 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856}
4857
4858TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 prepareButtons();
4860 prepareAxes(POSITION);
4861 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004862 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004864 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865}
4866
4867TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 addConfigurationProperty("touch.deviceType", "touchScreen");
4869 prepareDisplay(DISPLAY_ORIENTATION_0);
4870 prepareButtons();
4871 prepareAxes(POSITION);
4872 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004873 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874
4875 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877
4878 // Virtual key is down.
4879 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4880 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4881 processDown(mapper, x, y);
4882 processSync(mapper);
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886
4887 // Virtual key is up.
4888 processUp(mapper);
4889 processSync(mapper);
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893}
4894
4895TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 addConfigurationProperty("touch.deviceType", "touchScreen");
4897 prepareDisplay(DISPLAY_ORIENTATION_0);
4898 prepareButtons();
4899 prepareAxes(POSITION);
4900 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004901 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902
4903 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004904 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905
4906 // Virtual key is down.
4907 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4908 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4909 processDown(mapper, x, y);
4910 processSync(mapper);
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004913 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914
4915 // Virtual key is up.
4916 processUp(mapper);
4917 processSync(mapper);
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004920 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921}
4922
4923TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 addConfigurationProperty("touch.deviceType", "touchScreen");
4925 prepareDisplay(DISPLAY_ORIENTATION_0);
4926 prepareButtons();
4927 prepareAxes(POSITION);
4928 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004929 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930
4931 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4932 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004933 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 ASSERT_TRUE(flags[0]);
4935 ASSERT_FALSE(flags[1]);
4936}
4937
4938TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939 addConfigurationProperty("touch.deviceType", "touchScreen");
4940 prepareDisplay(DISPLAY_ORIENTATION_0);
4941 prepareButtons();
4942 prepareAxes(POSITION);
4943 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004944 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945
arthurhungdcef2dc2020-08-11 14:47:50 +08004946 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
4948 NotifyKeyArgs args;
4949
4950 // Press virtual key.
4951 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4952 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4953 processDown(mapper, x, y);
4954 processSync(mapper);
4955
4956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4957 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4958 ASSERT_EQ(DEVICE_ID, args.deviceId);
4959 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4960 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4961 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4962 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4963 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4964 ASSERT_EQ(KEY_HOME, args.scanCode);
4965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4966 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4967
4968 // Release virtual key.
4969 processUp(mapper);
4970 processSync(mapper);
4971
4972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4973 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4974 ASSERT_EQ(DEVICE_ID, args.deviceId);
4975 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4976 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4977 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4978 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4979 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4980 ASSERT_EQ(KEY_HOME, args.scanCode);
4981 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4982 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4983
4984 // Should not have sent any motions.
4985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4986}
4987
4988TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 addConfigurationProperty("touch.deviceType", "touchScreen");
4990 prepareDisplay(DISPLAY_ORIENTATION_0);
4991 prepareButtons();
4992 prepareAxes(POSITION);
4993 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004994 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995
arthurhungdcef2dc2020-08-11 14:47:50 +08004996 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997
4998 NotifyKeyArgs keyArgs;
4999
5000 // Press virtual key.
5001 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5002 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5003 processDown(mapper, x, y);
5004 processSync(mapper);
5005
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5007 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5008 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5009 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5010 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5011 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5012 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5013 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5014 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5015 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5016 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5017
5018 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5019 // into the display area.
5020 y -= 100;
5021 processMove(mapper, x, y);
5022 processSync(mapper);
5023
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5025 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5026 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5027 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5028 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5029 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5030 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5031 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5032 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5033 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5034 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5035 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5036
5037 NotifyMotionArgs motionArgs;
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5040 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5041 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5042 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5043 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5044 ASSERT_EQ(0, motionArgs.flags);
5045 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5046 ASSERT_EQ(0, motionArgs.buttonState);
5047 ASSERT_EQ(0, motionArgs.edgeFlags);
5048 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5049 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5052 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5053 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5054 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5055 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5056
5057 // Keep moving out of bounds. Should generate a pointer move.
5058 y -= 50;
5059 processMove(mapper, x, y);
5060 processSync(mapper);
5061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5065 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5066 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5068 ASSERT_EQ(0, motionArgs.flags);
5069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5070 ASSERT_EQ(0, motionArgs.buttonState);
5071 ASSERT_EQ(0, motionArgs.edgeFlags);
5072 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5076 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5080
5081 // Release out of bounds. Should generate a pointer up.
5082 processUp(mapper);
5083 processSync(mapper);
5084
5085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5086 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5087 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5088 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5089 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5090 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5091 ASSERT_EQ(0, motionArgs.flags);
5092 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5093 ASSERT_EQ(0, motionArgs.buttonState);
5094 ASSERT_EQ(0, motionArgs.edgeFlags);
5095 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5096 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5099 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5100 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5101 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5102 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5103
5104 // Should not have sent any more keys or motions.
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5107}
5108
5109TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 addConfigurationProperty("touch.deviceType", "touchScreen");
5111 prepareDisplay(DISPLAY_ORIENTATION_0);
5112 prepareButtons();
5113 prepareAxes(POSITION);
5114 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005115 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116
arthurhungdcef2dc2020-08-11 14:47:50 +08005117 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118
5119 NotifyMotionArgs motionArgs;
5120
5121 // Initially go down out of bounds.
5122 int32_t x = -10;
5123 int32_t y = -10;
5124 processDown(mapper, x, y);
5125 processSync(mapper);
5126
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5128
5129 // Move into the display area. Should generate a pointer down.
5130 x = 50;
5131 y = 75;
5132 processMove(mapper, x, y);
5133 processSync(mapper);
5134
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5136 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5137 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5138 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5139 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5140 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5141 ASSERT_EQ(0, motionArgs.flags);
5142 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5143 ASSERT_EQ(0, motionArgs.buttonState);
5144 ASSERT_EQ(0, motionArgs.edgeFlags);
5145 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5146 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5149 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5150 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5151 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5153
5154 // Release. Should generate a pointer up.
5155 processUp(mapper);
5156 processSync(mapper);
5157
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5160 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5161 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5162 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5163 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5164 ASSERT_EQ(0, motionArgs.flags);
5165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5166 ASSERT_EQ(0, motionArgs.buttonState);
5167 ASSERT_EQ(0, motionArgs.edgeFlags);
5168 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5169 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5172 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5173 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5174 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5175 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5176
5177 // Should not have sent any more keys or motions.
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5180}
5181
Santos Cordonfa5cf462017-04-05 10:37:00 -07005182TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005183 addConfigurationProperty("touch.deviceType", "touchScreen");
5184 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5185
5186 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5187 prepareButtons();
5188 prepareAxes(POSITION);
5189 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005190 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005191
arthurhungdcef2dc2020-08-11 14:47:50 +08005192 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005193
5194 NotifyMotionArgs motionArgs;
5195
5196 // Down.
5197 int32_t x = 100;
5198 int32_t y = 125;
5199 processDown(mapper, x, y);
5200 processSync(mapper);
5201
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5204 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5205 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5206 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5207 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5209 ASSERT_EQ(0, motionArgs.flags);
5210 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5211 ASSERT_EQ(0, motionArgs.buttonState);
5212 ASSERT_EQ(0, motionArgs.edgeFlags);
5213 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5214 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5217 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5218 1, 0, 0, 0, 0, 0, 0, 0));
5219 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5220 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5221 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5222
5223 // Move.
5224 x += 50;
5225 y += 75;
5226 processMove(mapper, x, y);
5227 processSync(mapper);
5228
5229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5230 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5231 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5232 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5233 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5234 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5236 ASSERT_EQ(0, motionArgs.flags);
5237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5238 ASSERT_EQ(0, motionArgs.buttonState);
5239 ASSERT_EQ(0, motionArgs.edgeFlags);
5240 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5241 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5244 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5245 1, 0, 0, 0, 0, 0, 0, 0));
5246 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5247 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5249
5250 // Up.
5251 processUp(mapper);
5252 processSync(mapper);
5253
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5256 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5257 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5258 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5259 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5260 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5261 ASSERT_EQ(0, motionArgs.flags);
5262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5263 ASSERT_EQ(0, motionArgs.buttonState);
5264 ASSERT_EQ(0, motionArgs.edgeFlags);
5265 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5266 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5269 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5270 1, 0, 0, 0, 0, 0, 0, 0));
5271 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5272 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5274
5275 // Should not have sent any more keys or motions.
5276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5278}
5279
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281 addConfigurationProperty("touch.deviceType", "touchScreen");
5282 prepareDisplay(DISPLAY_ORIENTATION_0);
5283 prepareButtons();
5284 prepareAxes(POSITION);
5285 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005286 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287
arthurhungdcef2dc2020-08-11 14:47:50 +08005288 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005289
5290 NotifyMotionArgs motionArgs;
5291
5292 // Down.
5293 int32_t x = 100;
5294 int32_t y = 125;
5295 processDown(mapper, x, y);
5296 processSync(mapper);
5297
5298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5299 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5300 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5301 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5302 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5303 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5304 ASSERT_EQ(0, motionArgs.flags);
5305 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5306 ASSERT_EQ(0, motionArgs.buttonState);
5307 ASSERT_EQ(0, motionArgs.edgeFlags);
5308 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5309 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5312 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5313 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5314 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5315 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5316
5317 // Move.
5318 x += 50;
5319 y += 75;
5320 processMove(mapper, x, y);
5321 processSync(mapper);
5322
5323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5324 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5325 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5326 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5327 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5329 ASSERT_EQ(0, motionArgs.flags);
5330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5331 ASSERT_EQ(0, motionArgs.buttonState);
5332 ASSERT_EQ(0, motionArgs.edgeFlags);
5333 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5334 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5337 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5338 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5339 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5341
5342 // Up.
5343 processUp(mapper);
5344 processSync(mapper);
5345
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5348 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5349 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5350 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5352 ASSERT_EQ(0, motionArgs.flags);
5353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5354 ASSERT_EQ(0, motionArgs.buttonState);
5355 ASSERT_EQ(0, motionArgs.edgeFlags);
5356 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5357 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5360 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5361 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5362 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5364
5365 // Should not have sent any more keys or motions.
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5368}
5369
5370TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371 addConfigurationProperty("touch.deviceType", "touchScreen");
5372 prepareButtons();
5373 prepareAxes(POSITION);
5374 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376
5377 NotifyMotionArgs args;
5378
5379 // Rotation 90.
5380 prepareDisplay(DISPLAY_ORIENTATION_90);
5381 processDown(mapper, toRawX(50), toRawY(75));
5382 processSync(mapper);
5383
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5385 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5386 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5387
5388 processUp(mapper);
5389 processSync(mapper);
5390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5391}
5392
5393TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394 addConfigurationProperty("touch.deviceType", "touchScreen");
5395 prepareButtons();
5396 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398
5399 NotifyMotionArgs args;
5400
5401 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005402 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403 prepareDisplay(DISPLAY_ORIENTATION_0);
5404 processDown(mapper, toRawX(50), toRawY(75));
5405 processSync(mapper);
5406
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5408 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5409 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5410
5411 processUp(mapper);
5412 processSync(mapper);
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5414
5415 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005416 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 prepareDisplay(DISPLAY_ORIENTATION_90);
5418 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5419 processSync(mapper);
5420
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5422 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5423 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5424
5425 processUp(mapper);
5426 processSync(mapper);
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5428
5429 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005430 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 prepareDisplay(DISPLAY_ORIENTATION_180);
5432 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5433 processSync(mapper);
5434
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5436 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5437 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5438
5439 processUp(mapper);
5440 processSync(mapper);
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5442
5443 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005444 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445 prepareDisplay(DISPLAY_ORIENTATION_270);
5446 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5447 processSync(mapper);
5448
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5450 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5451 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5452
5453 processUp(mapper);
5454 processSync(mapper);
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5456}
5457
5458TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 addConfigurationProperty("touch.deviceType", "touchScreen");
5460 prepareDisplay(DISPLAY_ORIENTATION_0);
5461 prepareButtons();
5462 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005463 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464
5465 // These calculations are based on the input device calibration documentation.
5466 int32_t rawX = 100;
5467 int32_t rawY = 200;
5468 int32_t rawPressure = 10;
5469 int32_t rawToolMajor = 12;
5470 int32_t rawDistance = 2;
5471 int32_t rawTiltX = 30;
5472 int32_t rawTiltY = 110;
5473
5474 float x = toDisplayX(rawX);
5475 float y = toDisplayY(rawY);
5476 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5477 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5478 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5479 float distance = float(rawDistance);
5480
5481 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5482 float tiltScale = M_PI / 180;
5483 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5484 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5485 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5486 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5487
5488 processDown(mapper, rawX, rawY);
5489 processPressure(mapper, rawPressure);
5490 processToolMajor(mapper, rawToolMajor);
5491 processDistance(mapper, rawDistance);
5492 processTilt(mapper, rawTiltX, rawTiltY);
5493 processSync(mapper);
5494
5495 NotifyMotionArgs args;
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5498 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5499 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5500}
5501
Jason Gerecke489fda82012-09-07 17:19:40 -07005502TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005503 addConfigurationProperty("touch.deviceType", "touchScreen");
5504 prepareDisplay(DISPLAY_ORIENTATION_0);
5505 prepareLocationCalibration();
5506 prepareButtons();
5507 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005508 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005509
5510 int32_t rawX = 100;
5511 int32_t rawY = 200;
5512
5513 float x = toDisplayX(toCookedX(rawX, rawY));
5514 float y = toDisplayY(toCookedY(rawX, rawY));
5515
5516 processDown(mapper, rawX, rawY);
5517 processSync(mapper);
5518
5519 NotifyMotionArgs args;
5520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5522 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5523}
5524
Michael Wrightd02c5b62014-02-10 15:10:22 -08005525TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526 addConfigurationProperty("touch.deviceType", "touchScreen");
5527 prepareDisplay(DISPLAY_ORIENTATION_0);
5528 prepareButtons();
5529 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005530 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531
5532 NotifyMotionArgs motionArgs;
5533 NotifyKeyArgs keyArgs;
5534
5535 processDown(mapper, 100, 200);
5536 processSync(mapper);
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5539 ASSERT_EQ(0, motionArgs.buttonState);
5540
5541 // press BTN_LEFT, release BTN_LEFT
5542 processKey(mapper, BTN_LEFT, 1);
5543 processSync(mapper);
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5546 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5547
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5550 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5551
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552 processKey(mapper, BTN_LEFT, 0);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561
5562 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5563 processKey(mapper, BTN_RIGHT, 1);
5564 processKey(mapper, BTN_MIDDLE, 1);
5565 processSync(mapper);
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5568 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5569 motionArgs.buttonState);
5570
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5573 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5574
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5577 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5578 motionArgs.buttonState);
5579
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580 processKey(mapper, BTN_RIGHT, 0);
5581 processSync(mapper);
5582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005585
5586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005588 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589
5590 processKey(mapper, BTN_MIDDLE, 0);
5591 processSync(mapper);
5592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005595
5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005598 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599
5600 // press BTN_BACK, release BTN_BACK
5601 processKey(mapper, BTN_BACK, 1);
5602 processSync(mapper);
5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5604 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5605 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005606
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005609 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5610
5611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5613 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614
5615 processKey(mapper, BTN_BACK, 0);
5616 processSync(mapper);
5617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005620
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005623 ASSERT_EQ(0, motionArgs.buttonState);
5624
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5626 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5627 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5628
5629 // press BTN_SIDE, release BTN_SIDE
5630 processKey(mapper, BTN_SIDE, 1);
5631 processSync(mapper);
5632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5633 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5634 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005635
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005638 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5639
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5642 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005643
5644 processKey(mapper, BTN_SIDE, 0);
5645 processSync(mapper);
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005649
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005652 ASSERT_EQ(0, motionArgs.buttonState);
5653
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5655 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5656 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5657
5658 // press BTN_FORWARD, release BTN_FORWARD
5659 processKey(mapper, BTN_FORWARD, 1);
5660 processSync(mapper);
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5662 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5663 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005664
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005667 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5668
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5671 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005672
5673 processKey(mapper, BTN_FORWARD, 0);
5674 processSync(mapper);
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005676 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005678
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005681 ASSERT_EQ(0, motionArgs.buttonState);
5682
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5684 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5685 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5686
5687 // press BTN_EXTRA, release BTN_EXTRA
5688 processKey(mapper, BTN_EXTRA, 1);
5689 processSync(mapper);
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5691 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5692 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005693
Michael Wrightd02c5b62014-02-10 15:10:22 -08005694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005696 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5697
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5700 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005701
5702 processKey(mapper, BTN_EXTRA, 0);
5703 processSync(mapper);
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005705 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005707
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005710 ASSERT_EQ(0, motionArgs.buttonState);
5711
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5713 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5714 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5715
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5717
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 // press BTN_STYLUS, release BTN_STYLUS
5719 processKey(mapper, BTN_STYLUS, 1);
5720 processSync(mapper);
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005723 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5724
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5726 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5727 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728
5729 processKey(mapper, BTN_STYLUS, 0);
5730 processSync(mapper);
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005732 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005734
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005737 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738
5739 // press BTN_STYLUS2, release BTN_STYLUS2
5740 processKey(mapper, BTN_STYLUS2, 1);
5741 processSync(mapper);
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5745
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5748 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749
5750 processKey(mapper, BTN_STYLUS2, 0);
5751 processSync(mapper);
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005755
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005758 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759
5760 // release touch
5761 processUp(mapper);
5762 processSync(mapper);
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5764 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5765 ASSERT_EQ(0, motionArgs.buttonState);
5766}
5767
5768TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769 addConfigurationProperty("touch.deviceType", "touchScreen");
5770 prepareDisplay(DISPLAY_ORIENTATION_0);
5771 prepareButtons();
5772 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005773 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774
5775 NotifyMotionArgs motionArgs;
5776
5777 // default tool type is finger
5778 processDown(mapper, 100, 200);
5779 processSync(mapper);
5780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5783
5784 // eraser
5785 processKey(mapper, BTN_TOOL_RUBBER, 1);
5786 processSync(mapper);
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5790
5791 // stylus
5792 processKey(mapper, BTN_TOOL_RUBBER, 0);
5793 processKey(mapper, BTN_TOOL_PEN, 1);
5794 processSync(mapper);
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5798
5799 // brush
5800 processKey(mapper, BTN_TOOL_PEN, 0);
5801 processKey(mapper, BTN_TOOL_BRUSH, 1);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5806
5807 // pencil
5808 processKey(mapper, BTN_TOOL_BRUSH, 0);
5809 processKey(mapper, BTN_TOOL_PENCIL, 1);
5810 processSync(mapper);
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5814
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005815 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816 processKey(mapper, BTN_TOOL_PENCIL, 0);
5817 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5818 processSync(mapper);
5819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5822
5823 // mouse
5824 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5825 processKey(mapper, BTN_TOOL_MOUSE, 1);
5826 processSync(mapper);
5827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5830
5831 // lens
5832 processKey(mapper, BTN_TOOL_MOUSE, 0);
5833 processKey(mapper, BTN_TOOL_LENS, 1);
5834 processSync(mapper);
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5838
5839 // double-tap
5840 processKey(mapper, BTN_TOOL_LENS, 0);
5841 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5842 processSync(mapper);
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5846
5847 // triple-tap
5848 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5849 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5854
5855 // quad-tap
5856 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5857 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5858 processSync(mapper);
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5861 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5862
5863 // finger
5864 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5865 processKey(mapper, BTN_TOOL_FINGER, 1);
5866 processSync(mapper);
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5870
5871 // stylus trumps finger
5872 processKey(mapper, BTN_TOOL_PEN, 1);
5873 processSync(mapper);
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5876 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5877
5878 // eraser trumps stylus
5879 processKey(mapper, BTN_TOOL_RUBBER, 1);
5880 processSync(mapper);
5881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5884
5885 // mouse trumps eraser
5886 processKey(mapper, BTN_TOOL_MOUSE, 1);
5887 processSync(mapper);
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5891
5892 // back to default tool type
5893 processKey(mapper, BTN_TOOL_MOUSE, 0);
5894 processKey(mapper, BTN_TOOL_RUBBER, 0);
5895 processKey(mapper, BTN_TOOL_PEN, 0);
5896 processKey(mapper, BTN_TOOL_FINGER, 0);
5897 processSync(mapper);
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5901}
5902
5903TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904 addConfigurationProperty("touch.deviceType", "touchScreen");
5905 prepareDisplay(DISPLAY_ORIENTATION_0);
5906 prepareButtons();
5907 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005908 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005909 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910
5911 NotifyMotionArgs motionArgs;
5912
5913 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5914 processKey(mapper, BTN_TOOL_FINGER, 1);
5915 processMove(mapper, 100, 200);
5916 processSync(mapper);
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5920 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5921
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5925 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5926
5927 // move a little
5928 processMove(mapper, 150, 250);
5929 processSync(mapper);
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5934
5935 // down when BTN_TOUCH is pressed, pressure defaults to 1
5936 processKey(mapper, BTN_TOUCH, 1);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5941 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5942
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5946 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5947
5948 // up when BTN_TOUCH is released, hover restored
5949 processKey(mapper, BTN_TOUCH, 0);
5950 processSync(mapper);
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5954 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5959 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5960
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5964 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5965
5966 // exit hover when pointer goes away
5967 processKey(mapper, BTN_TOOL_FINGER, 0);
5968 processSync(mapper);
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5972 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5973}
5974
5975TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 addConfigurationProperty("touch.deviceType", "touchScreen");
5977 prepareDisplay(DISPLAY_ORIENTATION_0);
5978 prepareButtons();
5979 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005980 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981
5982 NotifyMotionArgs motionArgs;
5983
5984 // initially hovering because pressure is 0
5985 processDown(mapper, 100, 200);
5986 processPressure(mapper, 0);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5991 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5996 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5997
5998 // move a little
5999 processMove(mapper, 150, 250);
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6004 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6005
6006 // down when pressure is non-zero
6007 processPressure(mapper, RAW_PRESSURE_MAX);
6008 processSync(mapper);
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6012 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6015 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6017 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6018
6019 // up when pressure becomes 0, hover restored
6020 processPressure(mapper, 0);
6021 processSync(mapper);
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6025 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6026
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6030 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6035 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6036
6037 // exit hover when pointer goes away
6038 processUp(mapper);
6039 processSync(mapper);
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6041 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6043 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6044}
6045
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046// --- MultiTouchInputMapperTest ---
6047
6048class MultiTouchInputMapperTest : public TouchInputMapperTest {
6049protected:
6050 void prepareAxes(int axes);
6051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006052 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6053 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6054 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6055 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6056 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6057 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6058 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6059 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6060 void processId(MultiTouchInputMapper& mapper, int32_t id);
6061 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6062 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6063 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6064 void processMTSync(MultiTouchInputMapper& mapper);
6065 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066};
6067
6068void MultiTouchInputMapperTest::prepareAxes(int axes) {
6069 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072 }
6073 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006074 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6075 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006077 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6078 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 }
6080 }
6081 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006082 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6083 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6086 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087 }
6088 }
6089 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6091 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 }
6093 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6095 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096 }
6097 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6099 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 }
6101 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006102 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6103 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104 }
6105 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006106 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6107 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 }
6109 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006110 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 }
6112}
6113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006114void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6115 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118}
6119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006120void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6121 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123}
6124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006125void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6126 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128}
6129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006130void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132}
6133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006134void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136}
6137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006138void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6139 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141}
6142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006143void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145}
6146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006147void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149}
6150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006151void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153}
6154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006155void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157}
6158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006159void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161}
6162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006163void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6164 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166}
6167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006168void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170}
6171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006172void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174}
6175
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 addConfigurationProperty("touch.deviceType", "touchScreen");
6178 prepareDisplay(DISPLAY_ORIENTATION_0);
6179 prepareAxes(POSITION);
6180 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006181 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182
arthurhungdcef2dc2020-08-11 14:47:50 +08006183 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 NotifyMotionArgs motionArgs;
6186
6187 // Two fingers down at once.
6188 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6189 processPosition(mapper, x1, y1);
6190 processMTSync(mapper);
6191 processPosition(mapper, x2, y2);
6192 processMTSync(mapper);
6193 processSync(mapper);
6194
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6196 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6197 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6198 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6199 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6201 ASSERT_EQ(0, motionArgs.flags);
6202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6203 ASSERT_EQ(0, motionArgs.buttonState);
6204 ASSERT_EQ(0, motionArgs.edgeFlags);
6205 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6206 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6209 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6210 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6211 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6213
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6216 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6217 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6218 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6220 motionArgs.action);
6221 ASSERT_EQ(0, motionArgs.flags);
6222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6223 ASSERT_EQ(0, motionArgs.buttonState);
6224 ASSERT_EQ(0, motionArgs.edgeFlags);
6225 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6228 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6231 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6233 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6234 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6235 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6237
6238 // Move.
6239 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6240 processPosition(mapper, x1, y1);
6241 processMTSync(mapper);
6242 processPosition(mapper, x2, y2);
6243 processMTSync(mapper);
6244 processSync(mapper);
6245
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6248 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6249 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6250 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6252 ASSERT_EQ(0, motionArgs.flags);
6253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6254 ASSERT_EQ(0, motionArgs.buttonState);
6255 ASSERT_EQ(0, motionArgs.edgeFlags);
6256 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6257 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6259 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6262 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6264 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6265 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6266 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6268
6269 // First finger up.
6270 x2 += 15; y2 -= 20;
6271 processPosition(mapper, x2, y2);
6272 processMTSync(mapper);
6273 processSync(mapper);
6274
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6277 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6278 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6279 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6281 motionArgs.action);
6282 ASSERT_EQ(0, motionArgs.flags);
6283 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6284 ASSERT_EQ(0, motionArgs.buttonState);
6285 ASSERT_EQ(0, motionArgs.edgeFlags);
6286 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6287 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6289 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6292 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6294 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6295 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6296 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6301 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6302 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6303 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6305 ASSERT_EQ(0, motionArgs.flags);
6306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6307 ASSERT_EQ(0, motionArgs.buttonState);
6308 ASSERT_EQ(0, motionArgs.edgeFlags);
6309 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6310 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6313 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6314 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6315 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6317
6318 // Move.
6319 x2 += 20; y2 -= 25;
6320 processPosition(mapper, x2, y2);
6321 processMTSync(mapper);
6322 processSync(mapper);
6323
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6326 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6327 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6328 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6330 ASSERT_EQ(0, motionArgs.flags);
6331 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6332 ASSERT_EQ(0, motionArgs.buttonState);
6333 ASSERT_EQ(0, motionArgs.edgeFlags);
6334 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6335 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6339 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6340 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6342
6343 // New finger down.
6344 int32_t x3 = 700, y3 = 300;
6345 processPosition(mapper, x2, y2);
6346 processMTSync(mapper);
6347 processPosition(mapper, x3, y3);
6348 processMTSync(mapper);
6349 processSync(mapper);
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6353 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6355 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6357 motionArgs.action);
6358 ASSERT_EQ(0, motionArgs.flags);
6359 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6360 ASSERT_EQ(0, motionArgs.buttonState);
6361 ASSERT_EQ(0, motionArgs.edgeFlags);
6362 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6363 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6365 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6368 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6370 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6371 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6372 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6374
6375 // Second finger up.
6376 x3 += 30; y3 -= 20;
6377 processPosition(mapper, x3, y3);
6378 processMTSync(mapper);
6379 processSync(mapper);
6380
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6387 motionArgs.action);
6388 ASSERT_EQ(0, motionArgs.flags);
6389 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6390 ASSERT_EQ(0, motionArgs.buttonState);
6391 ASSERT_EQ(0, motionArgs.edgeFlags);
6392 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6393 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6395 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6398 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6400 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6401 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6402 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6404
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6407 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6411 ASSERT_EQ(0, motionArgs.flags);
6412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6413 ASSERT_EQ(0, motionArgs.buttonState);
6414 ASSERT_EQ(0, motionArgs.edgeFlags);
6415 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6419 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6420 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6421 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6423
6424 // Last finger up.
6425 processMTSync(mapper);
6426 processSync(mapper);
6427
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6434 ASSERT_EQ(0, motionArgs.flags);
6435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6436 ASSERT_EQ(0, motionArgs.buttonState);
6437 ASSERT_EQ(0, motionArgs.edgeFlags);
6438 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6443 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6444 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6446
6447 // Should not have sent any more keys or motions.
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6450}
6451
6452TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 addConfigurationProperty("touch.deviceType", "touchScreen");
6454 prepareDisplay(DISPLAY_ORIENTATION_0);
6455 prepareAxes(POSITION | ID);
6456 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006457 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458
arthurhungdcef2dc2020-08-11 14:47:50 +08006459 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 NotifyMotionArgs motionArgs;
6462
6463 // Two fingers down at once.
6464 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6465 processPosition(mapper, x1, y1);
6466 processId(mapper, 1);
6467 processMTSync(mapper);
6468 processPosition(mapper, x2, y2);
6469 processId(mapper, 2);
6470 processMTSync(mapper);
6471 processSync(mapper);
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6475 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6476 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6479 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6480
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6482 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6483 motionArgs.action);
6484 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6487 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6490 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6492 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6493
6494 // Move.
6495 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6496 processPosition(mapper, x1, y1);
6497 processId(mapper, 1);
6498 processMTSync(mapper);
6499 processPosition(mapper, x2, y2);
6500 processId(mapper, 2);
6501 processMTSync(mapper);
6502 processSync(mapper);
6503
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6506 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6509 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6512 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6514 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6515
6516 // First finger up.
6517 x2 += 15; y2 -= 20;
6518 processPosition(mapper, x2, y2);
6519 processId(mapper, 2);
6520 processMTSync(mapper);
6521 processSync(mapper);
6522
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6525 motionArgs.action);
6526 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6529 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6532 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6534 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6535
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6538 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6539 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6542 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6543
6544 // Move.
6545 x2 += 20; y2 -= 25;
6546 processPosition(mapper, x2, y2);
6547 processId(mapper, 2);
6548 processMTSync(mapper);
6549 processSync(mapper);
6550
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6553 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6554 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6558
6559 // New finger down.
6560 int32_t x3 = 700, y3 = 300;
6561 processPosition(mapper, x2, y2);
6562 processId(mapper, 2);
6563 processMTSync(mapper);
6564 processPosition(mapper, x3, y3);
6565 processId(mapper, 3);
6566 processMTSync(mapper);
6567 processSync(mapper);
6568
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6571 motionArgs.action);
6572 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6575 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6578 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6580 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6581
6582 // Second finger up.
6583 x3 += 30; y3 -= 20;
6584 processPosition(mapper, x3, y3);
6585 processId(mapper, 3);
6586 processMTSync(mapper);
6587 processSync(mapper);
6588
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6591 motionArgs.action);
6592 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6595 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6598 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6600 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6604 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6608 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6609
6610 // Last finger up.
6611 processMTSync(mapper);
6612 processSync(mapper);
6613
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6616 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6617 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6620 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6621
6622 // Should not have sent any more keys or motions.
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6625}
6626
6627TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006628 addConfigurationProperty("touch.deviceType", "touchScreen");
6629 prepareDisplay(DISPLAY_ORIENTATION_0);
6630 prepareAxes(POSITION | ID | SLOT);
6631 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006632 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006633
arthurhungdcef2dc2020-08-11 14:47:50 +08006634 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006635
6636 NotifyMotionArgs motionArgs;
6637
6638 // Two fingers down at once.
6639 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6640 processPosition(mapper, x1, y1);
6641 processId(mapper, 1);
6642 processSlot(mapper, 1);
6643 processPosition(mapper, x2, y2);
6644 processId(mapper, 2);
6645 processSync(mapper);
6646
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6648 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6649 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6654
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6656 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6657 motionArgs.action);
6658 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6659 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6661 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6664 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6666 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6667
6668 // Move.
6669 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6670 processSlot(mapper, 0);
6671 processPosition(mapper, x1, y1);
6672 processSlot(mapper, 1);
6673 processPosition(mapper, x2, y2);
6674 processSync(mapper);
6675
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6678 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6681 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6686 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6687
6688 // First finger up.
6689 x2 += 15; y2 -= 20;
6690 processSlot(mapper, 0);
6691 processId(mapper, -1);
6692 processSlot(mapper, 1);
6693 processPosition(mapper, x2, y2);
6694 processSync(mapper);
6695
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6697 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6698 motionArgs.action);
6699 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6700 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6702 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6705 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6707 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6708
6709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6711 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6712 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6715 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6716
6717 // Move.
6718 x2 += 20; y2 -= 25;
6719 processPosition(mapper, x2, y2);
6720 processSync(mapper);
6721
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6724 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6725 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6728 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6729
6730 // New finger down.
6731 int32_t x3 = 700, y3 = 300;
6732 processPosition(mapper, x2, y2);
6733 processSlot(mapper, 0);
6734 processId(mapper, 3);
6735 processPosition(mapper, x3, y3);
6736 processSync(mapper);
6737
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6740 motionArgs.action);
6741 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6742 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6744 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6747 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6749 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6750
6751 // Second finger up.
6752 x3 += 30; y3 -= 20;
6753 processSlot(mapper, 1);
6754 processId(mapper, -1);
6755 processSlot(mapper, 0);
6756 processPosition(mapper, x3, y3);
6757 processSync(mapper);
6758
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6761 motionArgs.action);
6762 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6763 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6765 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6770 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6771
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6774 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6778 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6779
6780 // Last finger up.
6781 processId(mapper, -1);
6782 processSync(mapper);
6783
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6786 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6790 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6791
6792 // Should not have sent any more keys or motions.
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6795}
6796
6797TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006798 addConfigurationProperty("touch.deviceType", "touchScreen");
6799 prepareDisplay(DISPLAY_ORIENTATION_0);
6800 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006802
6803 // These calculations are based on the input device calibration documentation.
6804 int32_t rawX = 100;
6805 int32_t rawY = 200;
6806 int32_t rawTouchMajor = 7;
6807 int32_t rawTouchMinor = 6;
6808 int32_t rawToolMajor = 9;
6809 int32_t rawToolMinor = 8;
6810 int32_t rawPressure = 11;
6811 int32_t rawDistance = 0;
6812 int32_t rawOrientation = 3;
6813 int32_t id = 5;
6814
6815 float x = toDisplayX(rawX);
6816 float y = toDisplayY(rawY);
6817 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6818 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6819 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6820 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6821 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6822 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6823 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6824 float distance = float(rawDistance);
6825
6826 processPosition(mapper, rawX, rawY);
6827 processTouchMajor(mapper, rawTouchMajor);
6828 processTouchMinor(mapper, rawTouchMinor);
6829 processToolMajor(mapper, rawToolMajor);
6830 processToolMinor(mapper, rawToolMinor);
6831 processPressure(mapper, rawPressure);
6832 processOrientation(mapper, rawOrientation);
6833 processDistance(mapper, rawDistance);
6834 processId(mapper, id);
6835 processMTSync(mapper);
6836 processSync(mapper);
6837
6838 NotifyMotionArgs args;
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6840 ASSERT_EQ(0, args.pointerProperties[0].id);
6841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6842 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6843 orientation, distance));
6844}
6845
6846TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847 addConfigurationProperty("touch.deviceType", "touchScreen");
6848 prepareDisplay(DISPLAY_ORIENTATION_0);
6849 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6850 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006851 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006852
6853 // These calculations are based on the input device calibration documentation.
6854 int32_t rawX = 100;
6855 int32_t rawY = 200;
6856 int32_t rawTouchMajor = 140;
6857 int32_t rawTouchMinor = 120;
6858 int32_t rawToolMajor = 180;
6859 int32_t rawToolMinor = 160;
6860
6861 float x = toDisplayX(rawX);
6862 float y = toDisplayY(rawY);
6863 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6864 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6865 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6866 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6867 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6868
6869 processPosition(mapper, rawX, rawY);
6870 processTouchMajor(mapper, rawTouchMajor);
6871 processTouchMinor(mapper, rawTouchMinor);
6872 processToolMajor(mapper, rawToolMajor);
6873 processToolMinor(mapper, rawToolMinor);
6874 processMTSync(mapper);
6875 processSync(mapper);
6876
6877 NotifyMotionArgs args;
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6880 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6881}
6882
6883TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006884 addConfigurationProperty("touch.deviceType", "touchScreen");
6885 prepareDisplay(DISPLAY_ORIENTATION_0);
6886 prepareAxes(POSITION | TOUCH | TOOL);
6887 addConfigurationProperty("touch.size.calibration", "diameter");
6888 addConfigurationProperty("touch.size.scale", "10");
6889 addConfigurationProperty("touch.size.bias", "160");
6890 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006891 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006892
6893 // These calculations are based on the input device calibration documentation.
6894 // Note: We only provide a single common touch/tool value because the device is assumed
6895 // not to emit separate values for each pointer (isSummed = 1).
6896 int32_t rawX = 100;
6897 int32_t rawY = 200;
6898 int32_t rawX2 = 150;
6899 int32_t rawY2 = 250;
6900 int32_t rawTouchMajor = 5;
6901 int32_t rawToolMajor = 8;
6902
6903 float x = toDisplayX(rawX);
6904 float y = toDisplayY(rawY);
6905 float x2 = toDisplayX(rawX2);
6906 float y2 = toDisplayY(rawY2);
6907 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6908 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6909 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6910
6911 processPosition(mapper, rawX, rawY);
6912 processTouchMajor(mapper, rawTouchMajor);
6913 processToolMajor(mapper, rawToolMajor);
6914 processMTSync(mapper);
6915 processPosition(mapper, rawX2, rawY2);
6916 processTouchMajor(mapper, rawTouchMajor);
6917 processToolMajor(mapper, rawToolMajor);
6918 processMTSync(mapper);
6919 processSync(mapper);
6920
6921 NotifyMotionArgs args;
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6924
6925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6926 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6927 args.action);
6928 ASSERT_EQ(size_t(2), args.pointerCount);
6929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6930 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6932 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6933}
6934
6935TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936 addConfigurationProperty("touch.deviceType", "touchScreen");
6937 prepareDisplay(DISPLAY_ORIENTATION_0);
6938 prepareAxes(POSITION | TOUCH | TOOL);
6939 addConfigurationProperty("touch.size.calibration", "area");
6940 addConfigurationProperty("touch.size.scale", "43");
6941 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006942 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006943
6944 // These calculations are based on the input device calibration documentation.
6945 int32_t rawX = 100;
6946 int32_t rawY = 200;
6947 int32_t rawTouchMajor = 5;
6948 int32_t rawToolMajor = 8;
6949
6950 float x = toDisplayX(rawX);
6951 float y = toDisplayY(rawY);
6952 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6953 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6954 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6955
6956 processPosition(mapper, rawX, rawY);
6957 processTouchMajor(mapper, rawTouchMajor);
6958 processToolMajor(mapper, rawToolMajor);
6959 processMTSync(mapper);
6960 processSync(mapper);
6961
6962 NotifyMotionArgs args;
6963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6965 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6966}
6967
6968TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006969 addConfigurationProperty("touch.deviceType", "touchScreen");
6970 prepareDisplay(DISPLAY_ORIENTATION_0);
6971 prepareAxes(POSITION | PRESSURE);
6972 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6973 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006974 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975
Michael Wrightaa449c92017-12-13 21:21:43 +00006976 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006977 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006978 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6979 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6980 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6981
Michael Wrightd02c5b62014-02-10 15:10:22 -08006982 // These calculations are based on the input device calibration documentation.
6983 int32_t rawX = 100;
6984 int32_t rawY = 200;
6985 int32_t rawPressure = 60;
6986
6987 float x = toDisplayX(rawX);
6988 float y = toDisplayY(rawY);
6989 float pressure = float(rawPressure) * 0.01f;
6990
6991 processPosition(mapper, rawX, rawY);
6992 processPressure(mapper, rawPressure);
6993 processMTSync(mapper);
6994 processSync(mapper);
6995
6996 NotifyMotionArgs args;
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6999 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7000}
7001
7002TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007003 addConfigurationProperty("touch.deviceType", "touchScreen");
7004 prepareDisplay(DISPLAY_ORIENTATION_0);
7005 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007007
7008 NotifyMotionArgs motionArgs;
7009 NotifyKeyArgs keyArgs;
7010
7011 processId(mapper, 1);
7012 processPosition(mapper, 100, 200);
7013 processSync(mapper);
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7015 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7016 ASSERT_EQ(0, motionArgs.buttonState);
7017
7018 // press BTN_LEFT, release BTN_LEFT
7019 processKey(mapper, BTN_LEFT, 1);
7020 processSync(mapper);
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7023 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7024
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7027 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7028
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029 processKey(mapper, BTN_LEFT, 0);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007034
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007037 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007038
7039 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7040 processKey(mapper, BTN_RIGHT, 1);
7041 processKey(mapper, BTN_MIDDLE, 1);
7042 processSync(mapper);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7045 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7046 motionArgs.buttonState);
7047
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7050 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7051
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7054 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7055 motionArgs.buttonState);
7056
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057 processKey(mapper, BTN_RIGHT, 0);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007061 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007062
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007065 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007066
7067 processKey(mapper, BTN_MIDDLE, 0);
7068 processSync(mapper);
7069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007070 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007072
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007075 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007076
7077 // press BTN_BACK, release BTN_BACK
7078 processKey(mapper, BTN_BACK, 1);
7079 processSync(mapper);
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7081 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7082 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007083
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007086 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7087
7088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7090 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091
7092 processKey(mapper, BTN_BACK, 0);
7093 processSync(mapper);
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007097
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007100 ASSERT_EQ(0, motionArgs.buttonState);
7101
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7103 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7104 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7105
7106 // press BTN_SIDE, release BTN_SIDE
7107 processKey(mapper, BTN_SIDE, 1);
7108 processSync(mapper);
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7110 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7111 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007112
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007115 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7116
7117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7119 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120
7121 processKey(mapper, BTN_SIDE, 0);
7122 processSync(mapper);
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007125 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007126
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007129 ASSERT_EQ(0, motionArgs.buttonState);
7130
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7132 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7133 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7134
7135 // press BTN_FORWARD, release BTN_FORWARD
7136 processKey(mapper, BTN_FORWARD, 1);
7137 processSync(mapper);
7138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7139 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7140 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007141
Michael Wrightd02c5b62014-02-10 15:10:22 -08007142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007144 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7145
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7148 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007149
7150 processKey(mapper, BTN_FORWARD, 0);
7151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007155
7156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007158 ASSERT_EQ(0, motionArgs.buttonState);
7159
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7161 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7162 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7163
7164 // press BTN_EXTRA, release BTN_EXTRA
7165 processKey(mapper, BTN_EXTRA, 1);
7166 processSync(mapper);
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7168 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7169 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007170
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007173 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7174
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7177 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007178
7179 processKey(mapper, BTN_EXTRA, 0);
7180 processSync(mapper);
7181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007184
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007187 ASSERT_EQ(0, motionArgs.buttonState);
7188
Michael Wrightd02c5b62014-02-10 15:10:22 -08007189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7190 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7191 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7192
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7194
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195 // press BTN_STYLUS, release BTN_STYLUS
7196 processKey(mapper, BTN_STYLUS, 1);
7197 processSync(mapper);
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007200 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7201
7202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7203 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7204 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007205
7206 processKey(mapper, BTN_STYLUS, 0);
7207 processSync(mapper);
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007211
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007214 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215
7216 // press BTN_STYLUS2, release BTN_STYLUS2
7217 processKey(mapper, BTN_STYLUS2, 1);
7218 processSync(mapper);
7219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007221 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7222
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7225 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226
7227 processKey(mapper, BTN_STYLUS2, 0);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007232
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007235 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007236
7237 // release touch
7238 processId(mapper, -1);
7239 processSync(mapper);
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7241 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7242 ASSERT_EQ(0, motionArgs.buttonState);
7243}
7244
7245TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246 addConfigurationProperty("touch.deviceType", "touchScreen");
7247 prepareDisplay(DISPLAY_ORIENTATION_0);
7248 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007249 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007250
7251 NotifyMotionArgs motionArgs;
7252
7253 // default tool type is finger
7254 processId(mapper, 1);
7255 processPosition(mapper, 100, 200);
7256 processSync(mapper);
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7258 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7260
7261 // eraser
7262 processKey(mapper, BTN_TOOL_RUBBER, 1);
7263 processSync(mapper);
7264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7267
7268 // stylus
7269 processKey(mapper, BTN_TOOL_RUBBER, 0);
7270 processKey(mapper, BTN_TOOL_PEN, 1);
7271 processSync(mapper);
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7275
7276 // brush
7277 processKey(mapper, BTN_TOOL_PEN, 0);
7278 processKey(mapper, BTN_TOOL_BRUSH, 1);
7279 processSync(mapper);
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7283
7284 // pencil
7285 processKey(mapper, BTN_TOOL_BRUSH, 0);
7286 processKey(mapper, BTN_TOOL_PENCIL, 1);
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7291
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007292 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007293 processKey(mapper, BTN_TOOL_PENCIL, 0);
7294 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7295 processSync(mapper);
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7299
7300 // mouse
7301 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7302 processKey(mapper, BTN_TOOL_MOUSE, 1);
7303 processSync(mapper);
7304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7307
7308 // lens
7309 processKey(mapper, BTN_TOOL_MOUSE, 0);
7310 processKey(mapper, BTN_TOOL_LENS, 1);
7311 processSync(mapper);
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7315
7316 // double-tap
7317 processKey(mapper, BTN_TOOL_LENS, 0);
7318 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7319 processSync(mapper);
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7323
7324 // triple-tap
7325 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7326 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7327 processSync(mapper);
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7331
7332 // quad-tap
7333 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7334 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7335 processSync(mapper);
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7339
7340 // finger
7341 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7342 processKey(mapper, BTN_TOOL_FINGER, 1);
7343 processSync(mapper);
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7347
7348 // stylus trumps finger
7349 processKey(mapper, BTN_TOOL_PEN, 1);
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7354
7355 // eraser trumps stylus
7356 processKey(mapper, BTN_TOOL_RUBBER, 1);
7357 processSync(mapper);
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7361
7362 // mouse trumps eraser
7363 processKey(mapper, BTN_TOOL_MOUSE, 1);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7368
7369 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7370 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7371 processSync(mapper);
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7375
7376 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7377 processToolType(mapper, MT_TOOL_PEN);
7378 processSync(mapper);
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7382
7383 // back to default tool type
7384 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7385 processKey(mapper, BTN_TOOL_MOUSE, 0);
7386 processKey(mapper, BTN_TOOL_RUBBER, 0);
7387 processKey(mapper, BTN_TOOL_PEN, 0);
7388 processKey(mapper, BTN_TOOL_FINGER, 0);
7389 processSync(mapper);
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7393}
7394
7395TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396 addConfigurationProperty("touch.deviceType", "touchScreen");
7397 prepareDisplay(DISPLAY_ORIENTATION_0);
7398 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007399 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007400 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007401
7402 NotifyMotionArgs motionArgs;
7403
7404 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7405 processId(mapper, 1);
7406 processPosition(mapper, 100, 200);
7407 processSync(mapper);
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7411 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7412
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7416 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7417
7418 // move a little
7419 processPosition(mapper, 150, 250);
7420 processSync(mapper);
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7424 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7425
7426 // down when BTN_TOUCH is pressed, pressure defaults to 1
7427 processKey(mapper, BTN_TOUCH, 1);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7430 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7432 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7437 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7438
7439 // up when BTN_TOUCH is released, hover restored
7440 processKey(mapper, BTN_TOUCH, 0);
7441 processSync(mapper);
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7443 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7445 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7446
7447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7450 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7451
7452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7455 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7456
7457 // exit hover when pointer goes away
7458 processId(mapper, -1);
7459 processSync(mapper);
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7463 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7464}
7465
7466TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007467 addConfigurationProperty("touch.deviceType", "touchScreen");
7468 prepareDisplay(DISPLAY_ORIENTATION_0);
7469 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007470 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007471
7472 NotifyMotionArgs motionArgs;
7473
7474 // initially hovering because pressure is 0
7475 processId(mapper, 1);
7476 processPosition(mapper, 100, 200);
7477 processPressure(mapper, 0);
7478 processSync(mapper);
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7482 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7483
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7487 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7488
7489 // move a little
7490 processPosition(mapper, 150, 250);
7491 processSync(mapper);
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7495 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7496
7497 // down when pressure becomes non-zero
7498 processPressure(mapper, RAW_PRESSURE_MAX);
7499 processSync(mapper);
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7503 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7504
7505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7506 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7508 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7509
7510 // up when pressure becomes 0, hover restored
7511 processPressure(mapper, 0);
7512 processSync(mapper);
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7514 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7516 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7517
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7522
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7526 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7527
7528 // exit hover when pointer goes away
7529 processId(mapper, -1);
7530 processSync(mapper);
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7534 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7535}
7536
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007537/**
7538 * Set the input device port <--> display port associations, and check that the
7539 * events are routed to the display that matches the display port.
7540 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7541 */
7542TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007543 const std::string usb2 = "USB2";
7544 const uint8_t hdmi1 = 0;
7545 const uint8_t hdmi2 = 1;
7546 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007547 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007548
7549 addConfigurationProperty("touch.deviceType", "touchScreen");
7550 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007551 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007552
7553 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7554 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7555
7556 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7557 // for this input device is specified, and the matching viewport is not present,
7558 // the input device should be disabled (at the mapper level).
7559
7560 // Add viewport for display 2 on hdmi2
7561 prepareSecondaryDisplay(type, hdmi2);
7562 // Send a touch event
7563 processPosition(mapper, 100, 100);
7564 processSync(mapper);
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7566
7567 // Add viewport for display 1 on hdmi1
7568 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7569 // Send a touch event again
7570 processPosition(mapper, 100, 100);
7571 processSync(mapper);
7572
7573 NotifyMotionArgs args;
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7575 ASSERT_EQ(DISPLAY_ID, args.displayId);
7576}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007577
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007578TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007579 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007580 std::shared_ptr<FakePointerController> fakePointerController =
7581 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007582 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007583 fakePointerController->setPosition(100, 200);
7584 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007585 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7586
Garfield Tan888a6a42020-01-09 11:39:16 -08007587 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007588 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007589
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007590 prepareDisplay(DISPLAY_ORIENTATION_0);
7591 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007592 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007593
7594 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007595 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007596
7597 NotifyMotionArgs motionArgs;
7598 processPosition(mapper, 100, 100);
7599 processSync(mapper);
7600
7601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7603 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7604}
7605
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007606/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007607 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7608 */
7609TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7610 addConfigurationProperty("touch.deviceType", "touchScreen");
7611 prepareAxes(POSITION);
7612 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7613
7614 prepareDisplay(DISPLAY_ORIENTATION_0);
7615 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7616 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7617 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7618 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7619
7620 NotifyMotionArgs args;
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7622 ASSERT_EQ(26, args.readTime);
7623
7624 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7625 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7626 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7627
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7629 ASSERT_EQ(33, args.readTime);
7630}
7631
7632/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007633 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7634 * events should not be delivered to the listener.
7635 */
7636TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7637 addConfigurationProperty("touch.deviceType", "touchScreen");
7638 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7639 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7640 ViewportType::INTERNAL);
7641 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7642 prepareAxes(POSITION);
7643 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7644
7645 NotifyMotionArgs motionArgs;
7646 processPosition(mapper, 100, 100);
7647 processSync(mapper);
7648
7649 mFakeListener->assertNotifyMotionWasNotCalled();
7650}
7651
Garfield Tanc734e4f2021-01-15 20:01:39 -08007652TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7653 addConfigurationProperty("touch.deviceType", "touchScreen");
7654 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7655 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7656 ViewportType::INTERNAL);
7657 std::optional<DisplayViewport> optionalDisplayViewport =
7658 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7659 ASSERT_TRUE(optionalDisplayViewport.has_value());
7660 DisplayViewport displayViewport = *optionalDisplayViewport;
7661
7662 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7663 prepareAxes(POSITION);
7664 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7665
7666 // Finger down
7667 int32_t x = 100, y = 100;
7668 processPosition(mapper, x, y);
7669 processSync(mapper);
7670
7671 NotifyMotionArgs motionArgs;
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7673 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7674
7675 // Deactivate display viewport
7676 displayViewport.isActive = false;
7677 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7678 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7679
7680 // Finger move
7681 x += 10, y += 10;
7682 processPosition(mapper, x, y);
7683 processSync(mapper);
7684
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7686 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7687
7688 // Reactivate display viewport
7689 displayViewport.isActive = true;
7690 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7691 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7692
7693 // Finger move again
7694 x += 10, y += 10;
7695 processPosition(mapper, x, y);
7696 processSync(mapper);
7697
7698 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7699 // no pointer on the touch device.
7700 mFakeListener->assertNotifyMotionWasNotCalled();
7701}
7702
Arthur Hung7c645402019-01-25 17:45:42 +08007703TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7704 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007705 prepareAxes(POSITION | ID | SLOT);
7706 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007707 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007708
7709 // Create the second touch screen device, and enable multi fingers.
7710 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007711 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007712 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007713 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007714 std::shared_ptr<InputDevice> device2 =
7715 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7716 Flags<InputDeviceClass>(0));
7717
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007718 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7719 0 /*flat*/, 0 /*fuzz*/);
7720 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7721 0 /*flat*/, 0 /*fuzz*/);
7722 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7723 0 /*flat*/, 0 /*fuzz*/);
7724 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7725 0 /*flat*/, 0 /*fuzz*/);
7726 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7727 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7728 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007729
7730 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007731 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007732 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7733 device2->reset(ARBITRARY_TIME);
7734
7735 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007736 std::shared_ptr<FakePointerController> fakePointerController =
7737 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007738 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7739 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7740
7741 // Setup policy for associated displays and show touches.
7742 const uint8_t hdmi1 = 0;
7743 const uint8_t hdmi2 = 1;
7744 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7745 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7746 mFakePolicy->setShowTouches(true);
7747
7748 // Create displays.
7749 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007750 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007751
7752 // Default device will reconfigure above, need additional reconfiguration for another device.
7753 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007754 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007755
7756 // Two fingers down at default display.
7757 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7758 processPosition(mapper, x1, y1);
7759 processId(mapper, 1);
7760 processSlot(mapper, 1);
7761 processPosition(mapper, x2, y2);
7762 processId(mapper, 2);
7763 processSync(mapper);
7764
7765 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7766 fakePointerController->getSpots().find(DISPLAY_ID);
7767 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7768 ASSERT_EQ(size_t(2), iter->second.size());
7769
7770 // Two fingers down at second display.
7771 processPosition(mapper2, x1, y1);
7772 processId(mapper2, 1);
7773 processSlot(mapper2, 1);
7774 processPosition(mapper2, x2, y2);
7775 processId(mapper2, 2);
7776 processSync(mapper2);
7777
7778 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7779 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7780 ASSERT_EQ(size_t(2), iter->second.size());
7781}
7782
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007783TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007784 prepareAxes(POSITION);
7785 addConfigurationProperty("touch.deviceType", "touchScreen");
7786 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007787 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007788
7789 NotifyMotionArgs motionArgs;
7790 // Unrotated video frame
7791 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7792 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007793 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007794 processPosition(mapper, 100, 200);
7795 processSync(mapper);
7796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7797 ASSERT_EQ(frames, motionArgs.videoFrames);
7798
7799 // Subsequent touch events should not have any videoframes
7800 // This is implemented separately in FakeEventHub,
7801 // but that should match the behaviour of TouchVideoDevice.
7802 processPosition(mapper, 200, 200);
7803 processSync(mapper);
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7805 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7806}
7807
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007808TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007809 prepareAxes(POSITION);
7810 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007811 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007812 // Unrotated video frame
7813 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7814 NotifyMotionArgs motionArgs;
7815
7816 // Test all 4 orientations
7817 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7818 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7819 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7820 clearViewports();
7821 prepareDisplay(orientation);
7822 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007823 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007824 processPosition(mapper, 100, 200);
7825 processSync(mapper);
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7827 frames[0].rotate(orientation);
7828 ASSERT_EQ(frames, motionArgs.videoFrames);
7829 }
7830}
7831
7832TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007833 prepareAxes(POSITION);
7834 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007835 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007836 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7837 // so mix these.
7838 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7839 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7840 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7841 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7842 NotifyMotionArgs motionArgs;
7843
7844 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007845 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007846 processPosition(mapper, 100, 200);
7847 processSync(mapper);
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7849 std::for_each(frames.begin(), frames.end(),
7850 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7851 ASSERT_EQ(frames, motionArgs.videoFrames);
7852}
7853
Arthur Hung9da14732019-09-02 16:16:58 +08007854/**
7855 * If we had defined port associations, but the viewport is not ready, the touch device would be
7856 * expected to be disabled, and it should be enabled after the viewport has found.
7857 */
7858TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007859 constexpr uint8_t hdmi2 = 1;
7860 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007861 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007862
7863 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7864
7865 addConfigurationProperty("touch.deviceType", "touchScreen");
7866 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007867 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007868
7869 ASSERT_EQ(mDevice->isEnabled(), false);
7870
7871 // Add display on hdmi2, the device should be enabled and can receive touch event.
7872 prepareSecondaryDisplay(type, hdmi2);
7873 ASSERT_EQ(mDevice->isEnabled(), true);
7874
7875 // Send a touch event.
7876 processPosition(mapper, 100, 100);
7877 processSync(mapper);
7878
7879 NotifyMotionArgs args;
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7881 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7882}
7883
Arthur Hung421eb1c2020-01-16 00:09:42 +08007884TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007885 addConfigurationProperty("touch.deviceType", "touchScreen");
7886 prepareDisplay(DISPLAY_ORIENTATION_0);
7887 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007888 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007889
7890 NotifyMotionArgs motionArgs;
7891
7892 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7893 // finger down
7894 processId(mapper, 1);
7895 processPosition(mapper, x1, y1);
7896 processSync(mapper);
7897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7898 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7900
7901 // finger move
7902 processId(mapper, 1);
7903 processPosition(mapper, x2, y2);
7904 processSync(mapper);
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7908
7909 // finger up.
7910 processId(mapper, -1);
7911 processSync(mapper);
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7913 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7915
7916 // new finger down
7917 processId(mapper, 1);
7918 processPosition(mapper, x3, y3);
7919 processSync(mapper);
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7921 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7923}
7924
7925/**
arthurhungcc7f9802020-04-30 17:55:40 +08007926 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7927 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007928 */
arthurhungcc7f9802020-04-30 17:55:40 +08007929TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007930 addConfigurationProperty("touch.deviceType", "touchScreen");
7931 prepareDisplay(DISPLAY_ORIENTATION_0);
7932 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007933 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007934
7935 NotifyMotionArgs motionArgs;
7936
7937 // default tool type is finger
7938 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007939 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007940 processPosition(mapper, x1, y1);
7941 processSync(mapper);
7942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7943 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7945
7946 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7947 processToolType(mapper, MT_TOOL_PALM);
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7951
7952 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007953 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007954 processPosition(mapper, x2, y2);
7955 processSync(mapper);
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7957
7958 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007959 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007960 processSync(mapper);
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7962
7963 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007964 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007965 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007966 processPosition(mapper, x3, y3);
7967 processSync(mapper);
7968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7969 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7971}
7972
arthurhungbf89a482020-04-17 17:37:55 +08007973/**
arthurhungcc7f9802020-04-30 17:55:40 +08007974 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7975 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007976 */
arthurhungcc7f9802020-04-30 17:55:40 +08007977TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007978 addConfigurationProperty("touch.deviceType", "touchScreen");
7979 prepareDisplay(DISPLAY_ORIENTATION_0);
7980 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7981 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7982
7983 NotifyMotionArgs motionArgs;
7984
7985 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007986 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7987 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007988 processPosition(mapper, x1, y1);
7989 processSync(mapper);
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7991 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7993
7994 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007995 processSlot(mapper, SECOND_SLOT);
7996 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007997 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007998 processSync(mapper);
7999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8000 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8001 motionArgs.action);
8002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8003
8004 // If the tool type of the first finger changes to MT_TOOL_PALM,
8005 // we expect to receive ACTION_POINTER_UP with cancel flag.
8006 processSlot(mapper, FIRST_SLOT);
8007 processId(mapper, FIRST_TRACKING_ID);
8008 processToolType(mapper, MT_TOOL_PALM);
8009 processSync(mapper);
8010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8011 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8012 motionArgs.action);
8013 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8014
8015 // The following MOVE events of second finger should be processed.
8016 processSlot(mapper, SECOND_SLOT);
8017 processId(mapper, SECOND_TRACKING_ID);
8018 processPosition(mapper, x2 + 1, y2 + 1);
8019 processSync(mapper);
8020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8022 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8023
8024 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8025 // it. Second finger receive move.
8026 processSlot(mapper, FIRST_SLOT);
8027 processId(mapper, INVALID_TRACKING_ID);
8028 processSync(mapper);
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8031 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8032
8033 // Second finger keeps moving.
8034 processSlot(mapper, SECOND_SLOT);
8035 processId(mapper, SECOND_TRACKING_ID);
8036 processPosition(mapper, x2 + 2, y2 + 2);
8037 processSync(mapper);
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8040 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8041
8042 // Second finger up.
8043 processId(mapper, INVALID_TRACKING_ID);
8044 processSync(mapper);
8045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8046 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8047 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8048}
8049
8050/**
8051 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8052 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8053 */
8054TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8055 addConfigurationProperty("touch.deviceType", "touchScreen");
8056 prepareDisplay(DISPLAY_ORIENTATION_0);
8057 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8058 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8059
8060 NotifyMotionArgs motionArgs;
8061
8062 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8063 // First finger down.
8064 processId(mapper, FIRST_TRACKING_ID);
8065 processPosition(mapper, x1, y1);
8066 processSync(mapper);
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8068 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8070
8071 // Second finger down.
8072 processSlot(mapper, SECOND_SLOT);
8073 processId(mapper, SECOND_TRACKING_ID);
8074 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008075 processSync(mapper);
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8078 motionArgs.action);
8079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8080
arthurhungcc7f9802020-04-30 17:55:40 +08008081 // If the tool type of the first finger changes to MT_TOOL_PALM,
8082 // we expect to receive ACTION_POINTER_UP with cancel flag.
8083 processSlot(mapper, FIRST_SLOT);
8084 processId(mapper, FIRST_TRACKING_ID);
8085 processToolType(mapper, MT_TOOL_PALM);
8086 processSync(mapper);
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8089 motionArgs.action);
8090 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8091
8092 // Second finger keeps moving.
8093 processSlot(mapper, SECOND_SLOT);
8094 processId(mapper, SECOND_TRACKING_ID);
8095 processPosition(mapper, x2 + 1, y2 + 1);
8096 processSync(mapper);
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8099
8100 // second finger becomes palm, receive cancel due to only 1 finger is active.
8101 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008102 processToolType(mapper, MT_TOOL_PALM);
8103 processSync(mapper);
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8106
arthurhungcc7f9802020-04-30 17:55:40 +08008107 // third finger down.
8108 processSlot(mapper, THIRD_SLOT);
8109 processId(mapper, THIRD_TRACKING_ID);
8110 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008111 processPosition(mapper, x3, y3);
8112 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008116 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8117
8118 // third finger move
8119 processId(mapper, THIRD_TRACKING_ID);
8120 processPosition(mapper, x3 + 1, y3 + 1);
8121 processSync(mapper);
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8124
8125 // first finger up, third finger receive move.
8126 processSlot(mapper, FIRST_SLOT);
8127 processId(mapper, INVALID_TRACKING_ID);
8128 processSync(mapper);
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8131 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8132
8133 // second finger up, third finger receive move.
8134 processSlot(mapper, SECOND_SLOT);
8135 processId(mapper, INVALID_TRACKING_ID);
8136 processSync(mapper);
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8139 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8140
8141 // third finger up.
8142 processSlot(mapper, THIRD_SLOT);
8143 processId(mapper, INVALID_TRACKING_ID);
8144 processSync(mapper);
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8146 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8147 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8148}
8149
8150/**
8151 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8152 * and the active finger could still be allowed to receive the events
8153 */
8154TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8155 addConfigurationProperty("touch.deviceType", "touchScreen");
8156 prepareDisplay(DISPLAY_ORIENTATION_0);
8157 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8158 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8159
8160 NotifyMotionArgs motionArgs;
8161
8162 // default tool type is finger
8163 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8164 processId(mapper, FIRST_TRACKING_ID);
8165 processPosition(mapper, x1, y1);
8166 processSync(mapper);
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8168 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8170
8171 // Second finger down.
8172 processSlot(mapper, SECOND_SLOT);
8173 processId(mapper, SECOND_TRACKING_ID);
8174 processPosition(mapper, x2, y2);
8175 processSync(mapper);
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8177 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8178 motionArgs.action);
8179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8180
8181 // If the tool type of the second finger changes to MT_TOOL_PALM,
8182 // we expect to receive ACTION_POINTER_UP with cancel flag.
8183 processId(mapper, SECOND_TRACKING_ID);
8184 processToolType(mapper, MT_TOOL_PALM);
8185 processSync(mapper);
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8188 motionArgs.action);
8189 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8190
8191 // The following MOVE event should be processed.
8192 processSlot(mapper, FIRST_SLOT);
8193 processId(mapper, FIRST_TRACKING_ID);
8194 processPosition(mapper, x1 + 1, y1 + 1);
8195 processSync(mapper);
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8198 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8199
8200 // second finger up.
8201 processSlot(mapper, SECOND_SLOT);
8202 processId(mapper, INVALID_TRACKING_ID);
8203 processSync(mapper);
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8206
8207 // first finger keep moving
8208 processSlot(mapper, FIRST_SLOT);
8209 processId(mapper, FIRST_TRACKING_ID);
8210 processPosition(mapper, x1 + 2, y1 + 2);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8214
8215 // first finger up.
8216 processId(mapper, INVALID_TRACKING_ID);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8219 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8220 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008221}
8222
Arthur Hung9ad18942021-06-19 02:04:46 +00008223/**
8224 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8225 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8226 * cause slot be valid again.
8227 */
8228TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8229 addConfigurationProperty("touch.deviceType", "touchScreen");
8230 prepareDisplay(DISPLAY_ORIENTATION_0);
8231 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8232 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8233
8234 NotifyMotionArgs motionArgs;
8235
8236 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8237 // First finger down.
8238 processId(mapper, FIRST_TRACKING_ID);
8239 processPosition(mapper, x1, y1);
8240 processPressure(mapper, RAW_PRESSURE_MAX);
8241 processSync(mapper);
8242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8243 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8244 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8245
8246 // First finger move.
8247 processId(mapper, FIRST_TRACKING_ID);
8248 processPosition(mapper, x1 + 1, y1 + 1);
8249 processPressure(mapper, RAW_PRESSURE_MAX);
8250 processSync(mapper);
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8253 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8254
8255 // Second finger down.
8256 processSlot(mapper, SECOND_SLOT);
8257 processId(mapper, SECOND_TRACKING_ID);
8258 processPosition(mapper, x2, y2);
8259 processPressure(mapper, RAW_PRESSURE_MAX);
8260 processSync(mapper);
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8262 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8263 motionArgs.action);
8264 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8265
8266 // second finger up with some unexpected data.
8267 processSlot(mapper, SECOND_SLOT);
8268 processId(mapper, INVALID_TRACKING_ID);
8269 processPosition(mapper, x2, y2);
8270 processSync(mapper);
8271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8272 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8273 motionArgs.action);
8274 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8275
8276 // first finger up with some unexpected data.
8277 processSlot(mapper, FIRST_SLOT);
8278 processId(mapper, INVALID_TRACKING_ID);
8279 processPosition(mapper, x2, y2);
8280 processPressure(mapper, RAW_PRESSURE_MAX);
8281 processSync(mapper);
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8283 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8284 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8285}
8286
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008287// --- MultiTouchInputMapperTest_ExternalDevice ---
8288
8289class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8290protected:
Chris Yea52ade12020-08-27 16:49:20 -07008291 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008292};
8293
8294/**
8295 * Expect fallback to internal viewport if device is external and external viewport is not present.
8296 */
8297TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8298 prepareAxes(POSITION);
8299 addConfigurationProperty("touch.deviceType", "touchScreen");
8300 prepareDisplay(DISPLAY_ORIENTATION_0);
8301 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8302
8303 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8304
8305 NotifyMotionArgs motionArgs;
8306
8307 // Expect the event to be sent to the internal viewport,
8308 // because an external viewport is not present.
8309 processPosition(mapper, 100, 100);
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8312 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8313
8314 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008315 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008316 processPosition(mapper, 100, 100);
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8319 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8320}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008321
8322/**
8323 * Test touch should not work if outside of surface.
8324 */
8325class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8326protected:
8327 void halfDisplayToCenterHorizontal(int32_t orientation) {
8328 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008329 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008330
8331 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8332 internalViewport->orientation = orientation;
8333 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8334 internalViewport->logicalLeft = 0;
8335 internalViewport->logicalTop = 0;
8336 internalViewport->logicalRight = DISPLAY_HEIGHT;
8337 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8338
8339 internalViewport->physicalLeft = 0;
8340 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8341 internalViewport->physicalRight = DISPLAY_HEIGHT;
8342 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8343
8344 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8345 internalViewport->deviceHeight = DISPLAY_WIDTH;
8346 } else {
8347 internalViewport->logicalLeft = 0;
8348 internalViewport->logicalTop = 0;
8349 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8350 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8351
8352 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8353 internalViewport->physicalTop = 0;
8354 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8355 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8356
8357 internalViewport->deviceWidth = DISPLAY_WIDTH;
8358 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8359 }
8360
8361 mFakePolicy->updateViewport(internalViewport.value());
8362 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8363 }
8364
arthurhung5d547942020-12-14 17:04:45 +08008365 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8366 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008367 int32_t yExpected) {
8368 // touch on outside area should not work.
8369 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8370 processSync(mapper);
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8372
8373 // touch on inside area should receive the event.
8374 NotifyMotionArgs args;
8375 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8376 processSync(mapper);
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8378 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8379 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8380
8381 // Reset.
8382 mapper.reset(ARBITRARY_TIME);
8383 }
8384};
8385
arthurhung5d547942020-12-14 17:04:45 +08008386TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008387 addConfigurationProperty("touch.deviceType", "touchScreen");
8388 prepareDisplay(DISPLAY_ORIENTATION_0);
8389 prepareAxes(POSITION);
8390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8391
8392 // Touch on center of normal display should work.
8393 const int32_t x = DISPLAY_WIDTH / 4;
8394 const int32_t y = DISPLAY_HEIGHT / 2;
8395 processPosition(mapper, toRawX(x), toRawY(y));
8396 processSync(mapper);
8397 NotifyMotionArgs args;
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8400 0.0f, 0.0f, 0.0f, 0.0f));
8401 // Reset.
8402 mapper.reset(ARBITRARY_TIME);
8403
8404 // Let physical display be different to device, and make surface and physical could be 1:1.
8405 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8406
8407 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8408 const int32_t yExpected = y;
8409 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8410}
8411
arthurhung5d547942020-12-14 17:04:45 +08008412TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008413 addConfigurationProperty("touch.deviceType", "touchScreen");
8414 prepareDisplay(DISPLAY_ORIENTATION_0);
8415 prepareAxes(POSITION);
8416 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8417
8418 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8419 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8420
8421 const int32_t x = DISPLAY_WIDTH / 4;
8422 const int32_t y = DISPLAY_HEIGHT / 2;
8423
8424 // expect x/y = swap x/y then reverse y.
8425 const int32_t xExpected = y;
8426 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8427 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8428}
8429
arthurhung5d547942020-12-14 17:04:45 +08008430TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008431 addConfigurationProperty("touch.deviceType", "touchScreen");
8432 prepareDisplay(DISPLAY_ORIENTATION_0);
8433 prepareAxes(POSITION);
8434 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8435
8436 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8437 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8438
8439 const int32_t x = DISPLAY_WIDTH / 4;
8440 const int32_t y = DISPLAY_HEIGHT / 2;
8441
8442 // expect x/y = swap x/y then reverse x.
8443 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8444 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8445 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8446}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008447
arthurhunga36b28e2020-12-29 20:28:15 +08008448TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8449 addConfigurationProperty("touch.deviceType", "touchScreen");
8450 prepareDisplay(DISPLAY_ORIENTATION_0);
8451 prepareAxes(POSITION);
8452 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8453
8454 const int32_t x = 0;
8455 const int32_t y = 0;
8456
8457 const int32_t xExpected = x;
8458 const int32_t yExpected = y;
8459 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8460
8461 clearViewports();
8462 prepareDisplay(DISPLAY_ORIENTATION_90);
8463 // expect x/y = swap x/y then reverse y.
8464 const int32_t xExpected90 = y;
8465 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8466 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8467
8468 clearViewports();
8469 prepareDisplay(DISPLAY_ORIENTATION_270);
8470 // expect x/y = swap x/y then reverse x.
8471 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8472 const int32_t yExpected270 = x;
8473 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8474}
8475
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008476TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8477 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8478 std::shared_ptr<FakePointerController> fakePointerController =
8479 std::make_shared<FakePointerController>();
8480 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8481 fakePointerController->setPosition(0, 0);
8482 fakePointerController->setButtonState(0);
8483
8484 // prepare device and capture
8485 prepareDisplay(DISPLAY_ORIENTATION_0);
8486 prepareAxes(POSITION | ID | SLOT);
8487 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8488 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8489 mFakePolicy->setPointerCapture(true);
8490 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8491 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8492
8493 // captured touchpad should be a touchpad source
8494 NotifyDeviceResetArgs resetArgs;
8495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8496 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8497
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008498 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008499
8500 const InputDeviceInfo::MotionRange* relRangeX =
8501 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8502 ASSERT_NE(relRangeX, nullptr);
8503 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8504 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8505 const InputDeviceInfo::MotionRange* relRangeY =
8506 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8507 ASSERT_NE(relRangeY, nullptr);
8508 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8509 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8510
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008511 // run captured pointer tests - note that this is unscaled, so input listener events should be
8512 // identical to what the hardware sends (accounting for any
8513 // calibration).
8514 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008515 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008516 processId(mapper, 1);
8517 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8518 processKey(mapper, BTN_TOUCH, 1);
8519 processSync(mapper);
8520
8521 // expect coord[0] to contain initial location of touch 0
8522 NotifyMotionArgs args;
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8525 ASSERT_EQ(1U, args.pointerCount);
8526 ASSERT_EQ(0, args.pointerProperties[0].id);
8527 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8528 ASSERT_NO_FATAL_FAILURE(
8529 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8530
8531 // FINGER 1 DOWN
8532 processSlot(mapper, 1);
8533 processId(mapper, 2);
8534 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8535 processSync(mapper);
8536
8537 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008539 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8540 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008541 ASSERT_EQ(2U, args.pointerCount);
8542 ASSERT_EQ(0, args.pointerProperties[0].id);
8543 ASSERT_EQ(1, args.pointerProperties[1].id);
8544 ASSERT_NO_FATAL_FAILURE(
8545 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8546 ASSERT_NO_FATAL_FAILURE(
8547 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8548
8549 // FINGER 1 MOVE
8550 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8551 processSync(mapper);
8552
8553 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8554 // from move
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8557 ASSERT_NO_FATAL_FAILURE(
8558 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8559 ASSERT_NO_FATAL_FAILURE(
8560 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8561
8562 // FINGER 0 MOVE
8563 processSlot(mapper, 0);
8564 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8565 processSync(mapper);
8566
8567 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8570 ASSERT_NO_FATAL_FAILURE(
8571 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8572 ASSERT_NO_FATAL_FAILURE(
8573 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8574
8575 // BUTTON DOWN
8576 processKey(mapper, BTN_LEFT, 1);
8577 processSync(mapper);
8578
8579 // touchinputmapper design sends a move before button press
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8584
8585 // BUTTON UP
8586 processKey(mapper, BTN_LEFT, 0);
8587 processSync(mapper);
8588
8589 // touchinputmapper design sends a move after button release
8590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8594
8595 // FINGER 0 UP
8596 processId(mapper, -1);
8597 processSync(mapper);
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8599 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8600
8601 // FINGER 1 MOVE
8602 processSlot(mapper, 1);
8603 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8604 processSync(mapper);
8605
8606 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8609 ASSERT_EQ(1U, args.pointerCount);
8610 ASSERT_EQ(1, args.pointerProperties[0].id);
8611 ASSERT_NO_FATAL_FAILURE(
8612 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8613
8614 // FINGER 1 UP
8615 processId(mapper, -1);
8616 processKey(mapper, BTN_TOUCH, 0);
8617 processSync(mapper);
8618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8620
8621 // non captured touchpad should be a mouse source
8622 mFakePolicy->setPointerCapture(false);
8623 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8625 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8626}
8627
8628TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8629 std::shared_ptr<FakePointerController> fakePointerController =
8630 std::make_shared<FakePointerController>();
8631 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8632 fakePointerController->setPosition(0, 0);
8633 fakePointerController->setButtonState(0);
8634
8635 // prepare device and capture
8636 prepareDisplay(DISPLAY_ORIENTATION_0);
8637 prepareAxes(POSITION | ID | SLOT);
8638 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8639 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8640 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8641 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8642 // run uncaptured pointer tests - pushes out generic events
8643 // FINGER 0 DOWN
8644 processId(mapper, 3);
8645 processPosition(mapper, 100, 100);
8646 processKey(mapper, BTN_TOUCH, 1);
8647 processSync(mapper);
8648
8649 // start at (100,100), cursor should be at (0,0) * scale
8650 NotifyMotionArgs args;
8651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8653 ASSERT_NO_FATAL_FAILURE(
8654 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8655
8656 // FINGER 0 MOVE
8657 processPosition(mapper, 200, 200);
8658 processSync(mapper);
8659
8660 // compute scaling to help with touch position checking
8661 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8662 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8663 float scale =
8664 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8665
8666 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8670 0, 0, 0, 0, 0, 0, 0));
8671}
8672
8673TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8674 std::shared_ptr<FakePointerController> fakePointerController =
8675 std::make_shared<FakePointerController>();
8676
8677 prepareDisplay(DISPLAY_ORIENTATION_0);
8678 prepareAxes(POSITION | ID | SLOT);
8679 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8680 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8681 mFakePolicy->setPointerCapture(false);
8682 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8683
8684 // uncaptured touchpad should be a pointer device
8685 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8686
8687 // captured touchpad should be a touchpad device
8688 mFakePolicy->setPointerCapture(true);
8689 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8690 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8691}
8692
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008693// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008694
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008695class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008696protected:
8697 static const char* DEVICE_NAME;
8698 static const char* DEVICE_LOCATION;
8699 static const int32_t DEVICE_ID;
8700 static const int32_t DEVICE_GENERATION;
8701 static const int32_t DEVICE_CONTROLLER_NUMBER;
8702 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8703 static const int32_t EVENTHUB_ID;
8704
8705 std::shared_ptr<FakeEventHub> mFakeEventHub;
8706 sp<FakeInputReaderPolicy> mFakePolicy;
8707 sp<TestInputListener> mFakeListener;
8708 std::unique_ptr<InstrumentedInputReader> mReader;
8709 std::shared_ptr<InputDevice> mDevice;
8710
8711 virtual void SetUp(Flags<InputDeviceClass> classes) {
8712 mFakeEventHub = std::make_unique<FakeEventHub>();
8713 mFakePolicy = new FakeInputReaderPolicy();
8714 mFakeListener = new TestInputListener();
8715 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8716 mFakeListener);
8717 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8718 }
8719
8720 void SetUp() override { SetUp(DEVICE_CLASSES); }
8721
8722 void TearDown() override {
8723 mFakeListener.clear();
8724 mFakePolicy.clear();
8725 }
8726
8727 void configureDevice(uint32_t changes) {
8728 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8729 mReader->requestRefreshConfiguration(changes);
8730 mReader->loopOnce();
8731 }
8732 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8733 }
8734
8735 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8736 const std::string& location, int32_t eventHubId,
8737 Flags<InputDeviceClass> classes) {
8738 InputDeviceIdentifier identifier;
8739 identifier.name = name;
8740 identifier.location = location;
8741 std::shared_ptr<InputDevice> device =
8742 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8743 identifier);
8744 mReader->pushNextDevice(device);
8745 mFakeEventHub->addDevice(eventHubId, name, classes);
8746 mReader->loopOnce();
8747 return device;
8748 }
8749
8750 template <class T, typename... Args>
8751 T& addControllerAndConfigure(Args... args) {
8752 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8753
8754 return controller;
8755 }
8756};
8757
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008758const char* PeripheralControllerTest::DEVICE_NAME = "device";
8759const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8760const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8761const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8762const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8763const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008764 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008765const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008766
8767// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008768class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008769protected:
8770 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008771 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008772 }
8773};
8774
8775TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008776 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008777
8778 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8779 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8780}
8781
8782TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008783 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008784
8785 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8786 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8787}
8788
8789// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008790class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008791protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008792 void SetUp() override {
8793 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8794 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008795};
8796
Chris Ye85758332021-05-16 23:05:17 -07008797TEST_F(LightControllerTest, MonoLight) {
8798 RawLightInfo infoMono = {.id = 1,
8799 .name = "Mono",
8800 .maxBrightness = 255,
8801 .flags = InputLightClass::BRIGHTNESS,
8802 .path = ""};
8803 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008804
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008805 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008806 InputDeviceInfo info;
8807 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008808 std::vector<InputDeviceLightInfo> lights = info.getLights();
8809 ASSERT_EQ(1U, lights.size());
8810 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008811
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008812 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8813 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008814}
8815
8816TEST_F(LightControllerTest, RGBLight) {
8817 RawLightInfo infoRed = {.id = 1,
8818 .name = "red",
8819 .maxBrightness = 255,
8820 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8821 .path = ""};
8822 RawLightInfo infoGreen = {.id = 2,
8823 .name = "green",
8824 .maxBrightness = 255,
8825 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8826 .path = ""};
8827 RawLightInfo infoBlue = {.id = 3,
8828 .name = "blue",
8829 .maxBrightness = 255,
8830 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8831 .path = ""};
8832 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8833 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8834 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8835
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008836 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008837 InputDeviceInfo info;
8838 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008839 std::vector<InputDeviceLightInfo> lights = info.getLights();
8840 ASSERT_EQ(1U, lights.size());
8841 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008842
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008843 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8844 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008845}
8846
8847TEST_F(LightControllerTest, MultiColorRGBLight) {
8848 RawLightInfo infoColor = {.id = 1,
8849 .name = "red",
8850 .maxBrightness = 255,
8851 .flags = InputLightClass::BRIGHTNESS |
8852 InputLightClass::MULTI_INTENSITY |
8853 InputLightClass::MULTI_INDEX,
8854 .path = ""};
8855
8856 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8857
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008858 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008859 InputDeviceInfo info;
8860 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008861 std::vector<InputDeviceLightInfo> lights = info.getLights();
8862 ASSERT_EQ(1U, lights.size());
8863 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008864
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008865 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8866 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008867}
8868
8869TEST_F(LightControllerTest, PlayerIdLight) {
8870 RawLightInfo info1 = {.id = 1,
8871 .name = "player1",
8872 .maxBrightness = 255,
8873 .flags = InputLightClass::BRIGHTNESS,
8874 .path = ""};
8875 RawLightInfo info2 = {.id = 2,
8876 .name = "player2",
8877 .maxBrightness = 255,
8878 .flags = InputLightClass::BRIGHTNESS,
8879 .path = ""};
8880 RawLightInfo info3 = {.id = 3,
8881 .name = "player3",
8882 .maxBrightness = 255,
8883 .flags = InputLightClass::BRIGHTNESS,
8884 .path = ""};
8885 RawLightInfo info4 = {.id = 4,
8886 .name = "player4",
8887 .maxBrightness = 255,
8888 .flags = InputLightClass::BRIGHTNESS,
8889 .path = ""};
8890 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8891 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8892 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8893 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8894
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008895 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008896 InputDeviceInfo info;
8897 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008898 std::vector<InputDeviceLightInfo> lights = info.getLights();
8899 ASSERT_EQ(1U, lights.size());
8900 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008901
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008902 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8903 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
8904 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008905}
8906
Michael Wrightd02c5b62014-02-10 15:10:22 -08008907} // namespace android