blob: 73198bc5a1c7de6f5d53177af60905b0b399a52a [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
2279 mDevice->sendDown(centerPoint);
2280 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2281 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2282
2283 // ACTION_MOVE
2284 mDevice->sendMove(centerPoint + Point(1, 1));
2285 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2287
2288 // ACTION_UP
2289 mDevice->sendUp();
2290 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2291 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2292}
2293
2294TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2295 NotifyMotionArgs args;
2296 const Point centerPoint = mDevice->getCenterPoint();
2297
2298 // ACTION_DOWN
2299 mDevice->sendDown(centerPoint);
2300 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2301 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2302
2303 // ACTION_POINTER_DOWN (Second slot)
2304 const Point secondPoint = centerPoint + Point(100, 100);
2305 mDevice->sendSlot(SECOND_SLOT);
2306 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2307 mDevice->sendDown(secondPoint + Point(1, 1));
2308 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2309 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2310 args.action);
2311
2312 // ACTION_MOVE (Second slot)
2313 mDevice->sendMove(secondPoint);
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2316
2317 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002318 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002319 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002320 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002321 args.action);
2322
2323 // ACTION_UP
2324 mDevice->sendSlot(FIRST_SLOT);
2325 mDevice->sendUp();
2326 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2327 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2328}
2329
2330TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2331 NotifyMotionArgs args;
2332 const Point centerPoint = mDevice->getCenterPoint();
2333
2334 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002335 mDevice->sendSlot(FIRST_SLOT);
2336 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002337 mDevice->sendDown(centerPoint);
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2339 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2340
arthurhungcc7f9802020-04-30 17:55:40 +08002341 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002342 const Point secondPoint = centerPoint + Point(100, 100);
2343 mDevice->sendSlot(SECOND_SLOT);
2344 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2345 mDevice->sendDown(secondPoint);
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2347 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2348 args.action);
2349
arthurhungcc7f9802020-04-30 17:55:40 +08002350 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002351 mDevice->sendMove(secondPoint + Point(1, 1));
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2354
arthurhungcc7f9802020-04-30 17:55:40 +08002355 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2356 // a palm event.
2357 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002358 mDevice->sendToolType(MT_TOOL_PALM);
2359 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002360 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2361 args.action);
2362 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002363
arthurhungcc7f9802020-04-30 17:55:40 +08002364 // Send up to second slot, expect first slot send moving.
2365 mDevice->sendPointerUp();
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002368
arthurhungcc7f9802020-04-30 17:55:40 +08002369 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002370 mDevice->sendSlot(FIRST_SLOT);
2371 mDevice->sendUp();
2372
arthurhungcc7f9802020-04-30 17:55:40 +08002373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2374 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002375}
2376
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378class InputDeviceTest : public testing::Test {
2379protected:
2380 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002381 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 static const int32_t DEVICE_ID;
2383 static const int32_t DEVICE_GENERATION;
2384 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002385 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002386 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002388 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002390 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002391 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002392 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393
Chris Yea52ade12020-08-27 16:49:20 -07002394 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002395 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002397 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002398 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2399 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400 InputDeviceIdentifier identifier;
2401 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002402 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002403 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002404 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002405 mReader->pushNextDevice(mDevice);
2406 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2407 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 }
2409
Chris Yea52ade12020-08-27 16:49:20 -07002410 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411 mFakeListener.clear();
2412 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413 }
2414};
2415
2416const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002417const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002418const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2420const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002421const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2422 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002423const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424
2425TEST_F(InputDeviceTest, ImmutableProperties) {
2426 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002427 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002428 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429}
2430
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002431TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2432 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002433}
2434
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2436 // Configuration.
2437 InputReaderConfiguration config;
2438 mDevice->configure(ARBITRARY_TIME, &config, 0);
2439
2440 // Reset.
2441 mDevice->reset(ARBITRARY_TIME);
2442
2443 NotifyDeviceResetArgs resetArgs;
2444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2445 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2446 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2447
2448 // Metadata.
2449 ASSERT_TRUE(mDevice->isIgnored());
2450 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2451
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002452 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002453 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002454 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2456 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2457
2458 // State queries.
2459 ASSERT_EQ(0, mDevice->getMetaState());
2460
2461 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2462 << "Ignored device should return unknown key code state.";
2463 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2464 << "Ignored device should return unknown scan code state.";
2465 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2466 << "Ignored device should return unknown switch state.";
2467
2468 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2469 uint8_t flags[2] = { 0, 1 };
2470 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2471 << "Ignored device should never mark any key codes.";
2472 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2473 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2474}
2475
2476TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2477 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002478 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002479
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002480 FakeInputMapper& mapper1 =
2481 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002482 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2483 mapper1.setMetaState(AMETA_ALT_ON);
2484 mapper1.addSupportedKeyCode(AKEYCODE_A);
2485 mapper1.addSupportedKeyCode(AKEYCODE_B);
2486 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2487 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2488 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2489 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2490 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002492 FakeInputMapper& mapper2 =
2493 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002494 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495
2496 InputReaderConfiguration config;
2497 mDevice->configure(ARBITRARY_TIME, &config, 0);
2498
2499 String8 propertyValue;
2500 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2501 << "Device should have read configuration during configuration phase.";
2502 ASSERT_STREQ("value", propertyValue.string());
2503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002504 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2505 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002506
2507 // Reset
2508 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002509 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2510 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511
2512 NotifyDeviceResetArgs resetArgs;
2513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2514 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2515 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2516
2517 // Metadata.
2518 ASSERT_FALSE(mDevice->isIgnored());
2519 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2520
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002521 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002523 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2525 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2526
2527 // State queries.
2528 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2529 << "Should query mappers and combine meta states.";
2530
2531 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2532 << "Should return unknown key code state when source not supported.";
2533 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2534 << "Should return unknown scan code state when source not supported.";
2535 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2536 << "Should return unknown switch state when source not supported.";
2537
2538 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2539 << "Should query mapper when source is supported.";
2540 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2541 << "Should query mapper when source is supported.";
2542 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2543 << "Should query mapper when source is supported.";
2544
2545 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2546 uint8_t flags[4] = { 0, 0, 0, 1 };
2547 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2548 << "Should do nothing when source is unsupported.";
2549 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2550 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2551 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2552 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2553
2554 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2555 << "Should query mapper when source is supported.";
2556 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2557 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2558 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2559 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2560
2561 // Event handling.
2562 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002563 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564 mDevice->process(&event, 1);
2565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002566 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2567 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568}
2569
Arthur Hung2c9a3342019-07-23 14:18:59 +08002570// A single input device is associated with a specific display. Check that:
2571// 1. Device is disabled if the viewport corresponding to the associated display is not found
2572// 2. Device is disabled when setEnabled API is called
2573TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002574 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002575
2576 // First Configuration.
2577 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2578
2579 // Device should be enabled by default.
2580 ASSERT_TRUE(mDevice->isEnabled());
2581
2582 // Prepare associated info.
2583 constexpr uint8_t hdmi = 1;
2584 const std::string UNIQUE_ID = "local:1";
2585
2586 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2587 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2588 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2589 // Device should be disabled because it is associated with a specific display via
2590 // input port <-> display port association, but the corresponding display is not found
2591 ASSERT_FALSE(mDevice->isEnabled());
2592
2593 // Prepare displays.
2594 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002595 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2596 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002597 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2598 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2599 ASSERT_TRUE(mDevice->isEnabled());
2600
2601 // Device should be disabled after set disable.
2602 mFakePolicy->addDisabledDevice(mDevice->getId());
2603 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2604 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2605 ASSERT_FALSE(mDevice->isEnabled());
2606
2607 // Device should still be disabled even found the associated display.
2608 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2609 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2610 ASSERT_FALSE(mDevice->isEnabled());
2611}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002612
Christine Franks1ba71cc2021-04-07 14:37:42 -07002613TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2614 // Device should be enabled by default.
2615 mFakePolicy->clearViewports();
2616 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2617 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2618 ASSERT_TRUE(mDevice->isEnabled());
2619
2620 // Device should be disabled because it is associated with a specific display, but the
2621 // corresponding display is not found.
2622 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2623 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2624 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2625 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2626 ASSERT_FALSE(mDevice->isEnabled());
2627
2628 // Device should be enabled when a display is found.
2629 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2630 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2631 NO_PORT, ViewportType::INTERNAL);
2632 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2633 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2634 ASSERT_TRUE(mDevice->isEnabled());
2635
2636 // Device should be disabled after set disable.
2637 mFakePolicy->addDisabledDevice(mDevice->getId());
2638 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2639 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2640 ASSERT_FALSE(mDevice->isEnabled());
2641
2642 // Device should still be disabled even found the associated display.
2643 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2644 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2645 ASSERT_FALSE(mDevice->isEnabled());
2646}
2647
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648// --- InputMapperTest ---
2649
2650class InputMapperTest : public testing::Test {
2651protected:
2652 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002653 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 static const int32_t DEVICE_ID;
2655 static const int32_t DEVICE_GENERATION;
2656 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002657 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002658 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002660 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002662 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002663 std::unique_ptr<InstrumentedInputReader> mReader;
2664 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
Chris Ye1b0c7342020-07-28 21:57:03 -07002666 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002667 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002669 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002670 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2671 mFakeListener);
2672 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 }
2674
Chris Yea52ade12020-08-27 16:49:20 -07002675 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002676
Chris Yea52ade12020-08-27 16:49:20 -07002677 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678 mFakeListener.clear();
2679 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680 }
2681
2682 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002683 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 }
2685
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002686 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002687 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002688 mReader->requestRefreshConfiguration(changes);
2689 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002690 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002691 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2692 }
2693
arthurhungdcef2dc2020-08-11 14:47:50 +08002694 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2695 const std::string& location, int32_t eventHubId,
2696 Flags<InputDeviceClass> classes) {
2697 InputDeviceIdentifier identifier;
2698 identifier.name = name;
2699 identifier.location = location;
2700 std::shared_ptr<InputDevice> device =
2701 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2702 identifier);
2703 mReader->pushNextDevice(device);
2704 mFakeEventHub->addDevice(eventHubId, name, classes);
2705 mReader->loopOnce();
2706 return device;
2707 }
2708
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002709 template <class T, typename... Args>
2710 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002711 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002712 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002714 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002715 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716 }
2717
2718 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002719 int32_t orientation, const std::string& uniqueId,
2720 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002721 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2722 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002723 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2724 }
2725
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002726 void clearViewports() {
2727 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 }
2729
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002730 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2731 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732 RawEvent event;
2733 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002734 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002735 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002736 event.type = type;
2737 event.code = code;
2738 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002739 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002740 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 }
2742
2743 static void assertMotionRange(const InputDeviceInfo& info,
2744 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2745 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002746 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2748 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2749 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2750 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2751 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2752 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2753 }
2754
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002755 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2756 float size, float touchMajor, float touchMinor, float toolMajor,
2757 float toolMinor, float orientation, float distance,
2758 float scaledAxisEpsilon = 1.f) {
2759 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2760 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2762 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002763 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2764 scaledAxisEpsilon);
2765 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2766 scaledAxisEpsilon);
2767 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2768 scaledAxisEpsilon);
2769 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2770 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2772 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2773 }
2774
Michael Wright17db18e2020-06-26 20:51:44 +01002775 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002777 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778 ASSERT_NEAR(x, actualX, 1);
2779 ASSERT_NEAR(y, actualY, 1);
2780 }
2781};
2782
2783const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002784const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002785const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2787const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002788const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2789 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002790const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791
2792// --- SwitchInputMapperTest ---
2793
2794class SwitchInputMapperTest : public InputMapperTest {
2795protected:
2796};
2797
2798TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002799 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002801 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802}
2803
2804TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002807 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002808 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002810 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002811 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812}
2813
2814TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002815 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821
2822 NotifySwitchArgs args;
2823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2824 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002825 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2826 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827 args.switchMask);
2828 ASSERT_EQ(uint32_t(0), args.policyFlags);
2829}
2830
Chris Ye87143712020-11-10 05:05:58 +00002831// --- VibratorInputMapperTest ---
2832class VibratorInputMapperTest : public InputMapperTest {
2833protected:
2834 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2835};
2836
2837TEST_F(VibratorInputMapperTest, GetSources) {
2838 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2839
2840 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2841}
2842
2843TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2844 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2845
2846 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2847}
2848
2849TEST_F(VibratorInputMapperTest, Vibrate) {
2850 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002851 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002852 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2853
2854 VibrationElement pattern(2);
2855 VibrationSequence sequence(2);
2856 pattern.duration = std::chrono::milliseconds(200);
2857 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2858 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2859 sequence.addElement(pattern);
2860 pattern.duration = std::chrono::milliseconds(500);
2861 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2862 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2863 sequence.addElement(pattern);
2864
2865 std::vector<int64_t> timings = {0, 1};
2866 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2867
2868 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002869 // Start vibrating
2870 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002871 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002872 // Verify vibrator state listener was notified.
2873 mReader->loopOnce();
2874 NotifyVibratorStateArgs args;
2875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2876 ASSERT_EQ(DEVICE_ID, args.deviceId);
2877 ASSERT_TRUE(args.isOn);
2878 // Stop vibrating
2879 mapper.cancelVibrate(VIBRATION_TOKEN);
2880 ASSERT_FALSE(mapper.isVibrating());
2881 // Verify vibrator state listener was notified.
2882 mReader->loopOnce();
2883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2884 ASSERT_EQ(DEVICE_ID, args.deviceId);
2885 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002886}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887
Chris Yef59a2f42020-10-16 12:55:26 -07002888// --- SensorInputMapperTest ---
2889
2890class SensorInputMapperTest : public InputMapperTest {
2891protected:
2892 static const int32_t ACCEL_RAW_MIN;
2893 static const int32_t ACCEL_RAW_MAX;
2894 static const int32_t ACCEL_RAW_FUZZ;
2895 static const int32_t ACCEL_RAW_FLAT;
2896 static const int32_t ACCEL_RAW_RESOLUTION;
2897
2898 static const int32_t GYRO_RAW_MIN;
2899 static const int32_t GYRO_RAW_MAX;
2900 static const int32_t GYRO_RAW_FUZZ;
2901 static const int32_t GYRO_RAW_FLAT;
2902 static const int32_t GYRO_RAW_RESOLUTION;
2903
2904 static const float GRAVITY_MS2_UNIT;
2905 static const float DEGREE_RADIAN_UNIT;
2906
2907 void prepareAccelAxes();
2908 void prepareGyroAxes();
2909 void setAccelProperties();
2910 void setGyroProperties();
2911 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2912};
2913
2914const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2915const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2916const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2917const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2918const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2919
2920const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2921const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2922const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2923const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2924const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2925
2926const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2927const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2928
2929void SensorInputMapperTest::prepareAccelAxes() {
2930 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2931 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2932 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2933 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2934 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2935 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2936}
2937
2938void SensorInputMapperTest::prepareGyroAxes() {
2939 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2940 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2941 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2942 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2943 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2944 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2945}
2946
2947void SensorInputMapperTest::setAccelProperties() {
2948 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2949 /* sensorDataIndex */ 0);
2950 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2951 /* sensorDataIndex */ 1);
2952 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2953 /* sensorDataIndex */ 2);
2954 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2955 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2956 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2957 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2958 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2959}
2960
2961void SensorInputMapperTest::setGyroProperties() {
2962 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2963 /* sensorDataIndex */ 0);
2964 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2965 /* sensorDataIndex */ 1);
2966 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2967 /* sensorDataIndex */ 2);
2968 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2969 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2970 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2971 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2972 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2973}
2974
2975TEST_F(SensorInputMapperTest, GetSources) {
2976 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2977
2978 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2979}
2980
2981TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2982 setAccelProperties();
2983 prepareAccelAxes();
2984 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2985
2986 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2987 std::chrono::microseconds(10000),
2988 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002989 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002995
2996 NotifySensorArgs args;
2997 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2998 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2999 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3000
3001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3002 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3003 ASSERT_EQ(args.deviceId, DEVICE_ID);
3004 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3005 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3006 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3007 ASSERT_EQ(args.values, values);
3008 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3009}
3010
3011TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3012 setGyroProperties();
3013 prepareGyroAxes();
3014 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3015
3016 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3017 std::chrono::microseconds(10000),
3018 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003019 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003025
3026 NotifySensorArgs args;
3027 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3028 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3029 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3030
3031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3032 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3033 ASSERT_EQ(args.deviceId, DEVICE_ID);
3034 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3035 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3036 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3037 ASSERT_EQ(args.values, values);
3038 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3039}
3040
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041// --- KeyboardInputMapperTest ---
3042
3043class KeyboardInputMapperTest : public InputMapperTest {
3044protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003045 const std::string UNIQUE_ID = "local:0";
3046
3047 void prepareDisplay(int32_t orientation);
3048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003049 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003050 int32_t originalKeyCode, int32_t rotatedKeyCode,
3051 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052};
3053
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003054/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3055 * orientation.
3056 */
3057void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003058 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3059 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003060}
3061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003062void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003063 int32_t originalScanCode, int32_t originalKeyCode,
3064 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 NotifyKeyArgs args;
3066
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3069 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3070 ASSERT_EQ(originalScanCode, args.scanCode);
3071 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003072 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3076 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3077 ASSERT_EQ(originalScanCode, args.scanCode);
3078 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003079 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080}
3081
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083 KeyboardInputMapper& mapper =
3084 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3085 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088}
3089
3090TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3091 const int32_t USAGE_A = 0x070004;
3092 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3094 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003095 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3096 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3097 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 KeyboardInputMapper& mapper =
3100 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3101 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003102 // Initial metastate to AMETA_NONE.
3103 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3104 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
3106 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108 NotifyKeyArgs args;
3109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3110 ASSERT_EQ(DEVICE_ID, args.deviceId);
3111 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3112 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3113 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3114 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3115 ASSERT_EQ(KEY_HOME, args.scanCode);
3116 ASSERT_EQ(AMETA_NONE, args.metaState);
3117 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3118 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3119 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3120
3121 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003122 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3124 ASSERT_EQ(DEVICE_ID, args.deviceId);
3125 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3126 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3127 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3128 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3129 ASSERT_EQ(KEY_HOME, args.scanCode);
3130 ASSERT_EQ(AMETA_NONE, args.metaState);
3131 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3132 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3133 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3134
3135 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3139 ASSERT_EQ(DEVICE_ID, args.deviceId);
3140 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3141 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3142 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3143 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3144 ASSERT_EQ(0, args.scanCode);
3145 ASSERT_EQ(AMETA_NONE, args.metaState);
3146 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3147 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3148 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3149
3150 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3152 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3154 ASSERT_EQ(DEVICE_ID, args.deviceId);
3155 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3156 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3157 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3158 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3159 ASSERT_EQ(0, args.scanCode);
3160 ASSERT_EQ(AMETA_NONE, args.metaState);
3161 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3162 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3163 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3164
3165 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3169 ASSERT_EQ(DEVICE_ID, args.deviceId);
3170 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3171 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3172 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3173 ASSERT_EQ(0, args.keyCode);
3174 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3175 ASSERT_EQ(AMETA_NONE, args.metaState);
3176 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3177 ASSERT_EQ(0U, args.policyFlags);
3178 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3179
3180 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3182 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3184 ASSERT_EQ(DEVICE_ID, args.deviceId);
3185 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3186 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3187 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3188 ASSERT_EQ(0, args.keyCode);
3189 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3190 ASSERT_EQ(AMETA_NONE, args.metaState);
3191 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3192 ASSERT_EQ(0U, args.policyFlags);
3193 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3194}
3195
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003196/**
3197 * Ensure that the readTime is set to the time when the EV_KEY is received.
3198 */
3199TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3200 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3201
3202 KeyboardInputMapper& mapper =
3203 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3204 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3205 NotifyKeyArgs args;
3206
3207 // Key down
3208 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3210 ASSERT_EQ(12, args.readTime);
3211
3212 // Key up
3213 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3215 ASSERT_EQ(15, args.readTime);
3216}
3217
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003219 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3220 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003221 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3222 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3223 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003225 KeyboardInputMapper& mapper =
3226 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3227 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228
arthurhungc903df12020-08-11 15:08:42 +08003229 // Initial metastate to AMETA_NONE.
3230 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3231 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232
3233 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 NotifyKeyArgs args;
3236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003239 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240
3241 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003242 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3244 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246
3247 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003248 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
3253 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003254 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3256 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003258 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259}
3260
3261TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003262 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3263 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3264 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3265 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003267 KeyboardInputMapper& mapper =
3268 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3269 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003271 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3273 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3274 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3275 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3276 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3277 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3279 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3280}
3281
3282TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003283 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3284 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3285 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003289 KeyboardInputMapper& mapper =
3290 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3291 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003293 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003294 ASSERT_NO_FATAL_FAILURE(
3295 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3296 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3297 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3298 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3299 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3300 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3301 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003303 clearViewports();
3304 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003305 ASSERT_NO_FATAL_FAILURE(
3306 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3307 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3308 AKEYCODE_DPAD_UP, DISPLAY_ID));
3309 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3310 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3311 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3312 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003314 clearViewports();
3315 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003316 ASSERT_NO_FATAL_FAILURE(
3317 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3318 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3319 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3320 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3321 AKEYCODE_DPAD_UP, DISPLAY_ID));
3322 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3323 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003325 clearViewports();
3326 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003327 ASSERT_NO_FATAL_FAILURE(
3328 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3329 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3330 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3331 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3332 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3333 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3334 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335
3336 // Special case: if orientation changes while key is down, we still emit the same keycode
3337 // in the key up as we did in the key down.
3338 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003339 clearViewports();
3340 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3343 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3344 ASSERT_EQ(KEY_UP, args.scanCode);
3345 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3346
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003347 clearViewports();
3348 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3351 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3352 ASSERT_EQ(KEY_UP, args.scanCode);
3353 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3354}
3355
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003356TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3357 // If the keyboard is not orientation aware,
3358 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003359 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003360
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003361 KeyboardInputMapper& mapper =
3362 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3363 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003364 NotifyKeyArgs args;
3365
3366 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3371 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3372
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003373 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3378 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3379}
3380
3381TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3382 // If the keyboard is orientation aware,
3383 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003384 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003385
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003386 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003387 KeyboardInputMapper& mapper =
3388 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3389 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003390 NotifyKeyArgs args;
3391
3392 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3393 // ^--- already checked by the previous test
3394
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003395 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003396 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(DISPLAY_ID, args.displayId);
3402
3403 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003404 clearViewports();
3405 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003406 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(newDisplayId, args.displayId);
3412}
3413
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003415 KeyboardInputMapper& mapper =
3416 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3417 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003419 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003420 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003422 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424}
3425
3426TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003427 KeyboardInputMapper& mapper =
3428 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3429 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003431 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003432 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003434 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003435 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436}
3437
3438TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003439 KeyboardInputMapper& mapper =
3440 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3441 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003443 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444
3445 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3446 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 ASSERT_TRUE(flags[0]);
3449 ASSERT_FALSE(flags[1]);
3450}
3451
3452TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003453 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3454 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3455 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3456 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3457 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3458 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 KeyboardInputMapper& mapper =
3461 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3462 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003463 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003464 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3465 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466
3467 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003468 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3469 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3470 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
3472 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003475 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3476 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3477 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479
3480 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003483 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3484 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3485 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003486 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487
3488 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003491 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3492 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3493 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003494 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495
3496 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003499 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3500 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3501 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003502 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
3504 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003507 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3508 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3509 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003515 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3516 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3517 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519}
3520
Chris Yea52ade12020-08-27 16:49:20 -07003521TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3522 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3523 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3524 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3525 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3526
3527 KeyboardInputMapper& mapper =
3528 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3529 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3530
3531 // Initial metastate should be AMETA_NONE as no meta keys added.
3532 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3533 // Meta state should be AMETA_NONE after reset
3534 mapper.reset(ARBITRARY_TIME);
3535 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3536 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3537 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3538 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3539
3540 NotifyKeyArgs args;
3541 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3544 ASSERT_EQ(AMETA_NONE, args.metaState);
3545 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3546 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3547 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3548
3549 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003550 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3552 ASSERT_EQ(AMETA_NONE, args.metaState);
3553 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3554 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3555 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3556}
3557
Arthur Hung2c9a3342019-07-23 14:18:59 +08003558TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3559 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003560 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003564
3565 // keyboard 2.
3566 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003567 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003568 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003569 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003570 std::shared_ptr<InputDevice> device2 =
3571 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3572 Flags<InputDeviceClass>(0));
3573
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003574 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3575 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3576 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3577 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003579 KeyboardInputMapper& mapper =
3580 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3581 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003584 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003586 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3587 device2->reset(ARBITRARY_TIME);
3588
3589 // Prepared displays and associated info.
3590 constexpr uint8_t hdmi1 = 0;
3591 constexpr uint8_t hdmi2 = 1;
3592 const std::string SECONDARY_UNIQUE_ID = "local:1";
3593
3594 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3595 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3596
3597 // No associated display viewport found, should disable the device.
3598 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3599 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3600 ASSERT_FALSE(device2->isEnabled());
3601
3602 // Prepare second display.
3603 constexpr int32_t newDisplayId = 2;
3604 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003605 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003606 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003607 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608 // Default device will reconfigure above, need additional reconfiguration for another device.
3609 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3610 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3611
3612 // Device should be enabled after the associated display is found.
3613 ASSERT_TRUE(mDevice->isEnabled());
3614 ASSERT_TRUE(device2->isEnabled());
3615
3616 // Test pad key events
3617 ASSERT_NO_FATAL_FAILURE(
3618 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3620 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3622 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3624 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3625
3626 ASSERT_NO_FATAL_FAILURE(
3627 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3628 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3629 AKEYCODE_DPAD_RIGHT, newDisplayId));
3630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3631 AKEYCODE_DPAD_DOWN, newDisplayId));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3633 AKEYCODE_DPAD_LEFT, newDisplayId));
3634}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635
arthurhungc903df12020-08-11 15:08:42 +08003636TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3637 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3638 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3639 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3640 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3641 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3642 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3643
3644 KeyboardInputMapper& mapper =
3645 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3646 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3647 // Initial metastate to AMETA_NONE.
3648 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3649 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3650
3651 // Initialization should have turned all of the lights off.
3652 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3653 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3654 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3655
3656 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003659 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3660 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3661
3662 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003665 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3666 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3667
3668 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003671 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3672 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3673
3674 mFakeEventHub->removeDevice(EVENTHUB_ID);
3675 mReader->loopOnce();
3676
3677 // keyboard 2 should default toggle keys.
3678 const std::string USB2 = "USB2";
3679 const std::string DEVICE_NAME2 = "KEYBOARD2";
3680 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3681 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3682 std::shared_ptr<InputDevice> device2 =
3683 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3684 Flags<InputDeviceClass>(0));
3685 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3686 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3687 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3688 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3689 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3690 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3691
arthurhung6fe95782020-10-05 22:41:16 +08003692 KeyboardInputMapper& mapper2 =
3693 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3694 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003695 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3696 device2->reset(ARBITRARY_TIME);
3697
3698 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3699 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3700 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003701 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3702 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003703}
3704
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003705// --- KeyboardInputMapperTest_ExternalDevice ---
3706
3707class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3708protected:
Chris Yea52ade12020-08-27 16:49:20 -07003709 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003710};
3711
3712TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003713 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3714 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003715
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003716 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3717 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3718 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3719 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003720
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003721 KeyboardInputMapper& mapper =
3722 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3723 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003724
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003726 NotifyKeyArgs args;
3727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3728 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3729
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3732 ASSERT_EQ(uint32_t(0), args.policyFlags);
3733
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3736 ASSERT_EQ(uint32_t(0), args.policyFlags);
3737
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3740 ASSERT_EQ(uint32_t(0), args.policyFlags);
3741
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3744 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3748 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3749}
3750
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003751TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003752 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003753
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003757
Powei Fengd041c5d2019-05-03 17:11:33 -07003758 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003759 KeyboardInputMapper& mapper =
3760 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3761 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003762
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003764 NotifyKeyArgs args;
3765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3766 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3770 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3771
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3774 ASSERT_EQ(uint32_t(0), args.policyFlags);
3775
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003776 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3778 ASSERT_EQ(uint32_t(0), args.policyFlags);
3779
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3782 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3783
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003784 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3786 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3787}
3788
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789// --- CursorInputMapperTest ---
3790
3791class CursorInputMapperTest : public InputMapperTest {
3792protected:
3793 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3794
Michael Wright17db18e2020-06-26 20:51:44 +01003795 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796
Chris Yea52ade12020-08-27 16:49:20 -07003797 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003798 InputMapperTest::SetUp();
3799
Michael Wright17db18e2020-06-26 20:51:44 +01003800 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003801 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802 }
3803
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3805 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003806
3807 void prepareDisplay(int32_t orientation) {
3808 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003809 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003810 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3811 orientation, uniqueId, NO_PORT, viewportType);
3812 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003813
3814 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3815 float pressure) {
3816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3817 0.0f, 0.0f, 0.0f, EPSILON));
3818 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819};
3820
3821const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3822
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3824 int32_t originalY, int32_t rotatedX,
3825 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826 NotifyMotionArgs args;
3827
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003833 ASSERT_NO_FATAL_FAILURE(
3834 assertCursorPointerCoords(args.pointerCoords[0],
3835 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3836 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837}
3838
3839TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003841 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003843 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003844}
3845
3846TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003848 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003850 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851}
3852
3853TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856
3857 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859
3860 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003861 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3862 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3864 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3865
3866 // When the bounds are set, then there should be a valid motion range.
3867 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3868
3869 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871
3872 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3873 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3874 1, 800 - 1, 0.0f, 0.0f));
3875 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3876 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3877 2, 480 - 1, 0.0f, 0.0f));
3878 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3879 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3880 0.0f, 1.0f, 0.0f, 0.0f));
3881}
3882
3883TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886
3887 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003888 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889
3890 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3891 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3892 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3893 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3894 AINPUT_MOTION_RANGE_Y, 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_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3898 0.0f, 1.0f, 0.0f, 0.0f));
3899}
3900
3901TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003903 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904
arthurhungdcef2dc2020-08-11 14:47:50 +08003905 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906
3907 NotifyMotionArgs args;
3908
3909 // Button press.
3910 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3914 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3915 ASSERT_EQ(DEVICE_ID, args.deviceId);
3916 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3917 ASSERT_EQ(uint32_t(0), args.policyFlags);
3918 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3919 ASSERT_EQ(0, args.flags);
3920 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3921 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3922 ASSERT_EQ(0, args.edgeFlags);
3923 ASSERT_EQ(uint32_t(1), args.pointerCount);
3924 ASSERT_EQ(0, args.pointerProperties[0].id);
3925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003926 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3928 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3929 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3930
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3932 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3933 ASSERT_EQ(DEVICE_ID, args.deviceId);
3934 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3935 ASSERT_EQ(uint32_t(0), args.policyFlags);
3936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3937 ASSERT_EQ(0, args.flags);
3938 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3939 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3940 ASSERT_EQ(0, args.edgeFlags);
3941 ASSERT_EQ(uint32_t(1), args.pointerCount);
3942 ASSERT_EQ(0, args.pointerProperties[0].id);
3943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003944 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003945 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3946 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3947 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3948
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003950 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3951 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3953 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3954 ASSERT_EQ(DEVICE_ID, args.deviceId);
3955 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3956 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3958 ASSERT_EQ(0, args.flags);
3959 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3960 ASSERT_EQ(0, args.buttonState);
3961 ASSERT_EQ(0, args.edgeFlags);
3962 ASSERT_EQ(uint32_t(1), args.pointerCount);
3963 ASSERT_EQ(0, args.pointerProperties[0].id);
3964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003965 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003966 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3967 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3968 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3969
3970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3971 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3972 ASSERT_EQ(DEVICE_ID, args.deviceId);
3973 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3974 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3976 ASSERT_EQ(0, args.flags);
3977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3978 ASSERT_EQ(0, args.buttonState);
3979 ASSERT_EQ(0, args.edgeFlags);
3980 ASSERT_EQ(uint32_t(1), args.pointerCount);
3981 ASSERT_EQ(0, args.pointerProperties[0].id);
3982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003983 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3985 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3986 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3987}
3988
3989TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003991 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992
3993 NotifyMotionArgs args;
3994
3995 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004000 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4001 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4002 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003
4004 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004009 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4010 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011}
4012
4013TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004015 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016
4017 NotifyMotionArgs args;
4018
4019 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4023 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004024 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004028 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004029
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004035 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004036
4037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004039 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040}
4041
4042TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004044 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045
4046 NotifyMotionArgs args;
4047
4048 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4054 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004055 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4056 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4057 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004061 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4062 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4063 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004064
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004071 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4072 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4073 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074
4075 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004080 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004081
4082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004084 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085}
4086
4087TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004089 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004091 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4100}
4101
4102TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 addConfigurationProperty("cursor.mode", "navigation");
4104 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004105 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004107 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4116
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004117 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4119 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4120 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4121 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4126
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004127 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4136
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004137 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4146}
4147
4148TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004150 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151
4152 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4153 mFakePointerController->setPosition(100, 200);
4154 mFakePointerController->setButtonState(0);
4155
4156 NotifyMotionArgs motionArgs;
4157 NotifyKeyArgs keyArgs;
4158
4159 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4163 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4164 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4165 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004166 ASSERT_NO_FATAL_FAILURE(
4167 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4170 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4171 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4172 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004173 ASSERT_NO_FATAL_FAILURE(
4174 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004175
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_EQ(0, motionArgs.buttonState);
4181 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004182 ASSERT_NO_FATAL_FAILURE(
4183 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 ASSERT_EQ(0, motionArgs.buttonState);
4188 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004189 ASSERT_NO_FATAL_FAILURE(
4190 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004191
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_EQ(0, motionArgs.buttonState);
4195 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004196 ASSERT_NO_FATAL_FAILURE(
4197 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198
4199 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4205 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4206 motionArgs.buttonState);
4207 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4208 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(
4210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4214 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4215 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4216 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004217 ASSERT_NO_FATAL_FAILURE(
4218 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004219
4220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4222 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4223 motionArgs.buttonState);
4224 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4225 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004226 ASSERT_NO_FATAL_FAILURE(
4227 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4234 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004235 ASSERT_NO_FATAL_FAILURE(
4236 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004237
4238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4241 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004242 ASSERT_NO_FATAL_FAILURE(
4243 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4249 ASSERT_EQ(0, motionArgs.buttonState);
4250 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004251 ASSERT_NO_FATAL_FAILURE(
4252 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255
4256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_EQ(0, motionArgs.buttonState);
4258 ASSERT_EQ(0, mFakePointerController->getButtonState());
4259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004260 ASSERT_NO_FATAL_FAILURE(
4261 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4264 ASSERT_EQ(0, motionArgs.buttonState);
4265 ASSERT_EQ(0, mFakePointerController->getButtonState());
4266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004267 ASSERT_NO_FATAL_FAILURE(
4268 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
4270 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4274 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4275 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004276
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004278 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4280 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004281 ASSERT_NO_FATAL_FAILURE(
4282 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004283
4284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4285 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4286 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4287 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004288 ASSERT_NO_FATAL_FAILURE(
4289 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_EQ(0, motionArgs.buttonState);
4296 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004297 ASSERT_NO_FATAL_FAILURE(
4298 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004299
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302 ASSERT_EQ(0, motionArgs.buttonState);
4303 ASSERT_EQ(0, mFakePointerController->getButtonState());
4304
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(
4306 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4308 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4310
4311 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4316 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4321 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004322 ASSERT_NO_FATAL_FAILURE(
4323 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004324
4325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4327 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4328 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004329 ASSERT_NO_FATAL_FAILURE(
4330 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_EQ(0, motionArgs.buttonState);
4337 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004338 ASSERT_NO_FATAL_FAILURE(
4339 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004340
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4343 ASSERT_EQ(0, motionArgs.buttonState);
4344 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004345 ASSERT_NO_FATAL_FAILURE(
4346 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4350 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4351
4352 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4357 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4362 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004363 ASSERT_NO_FATAL_FAILURE(
4364 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365
4366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4368 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4369 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(
4371 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_EQ(0, motionArgs.buttonState);
4378 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004379 ASSERT_NO_FATAL_FAILURE(
4380 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004381
4382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4383 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4384 ASSERT_EQ(0, motionArgs.buttonState);
4385 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004386 ASSERT_NO_FATAL_FAILURE(
4387 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4390 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4391 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4392
4393 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4397 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4398 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004399
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4403 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004404 ASSERT_NO_FATAL_FAILURE(
4405 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004406
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4409 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4410 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004411 ASSERT_NO_FATAL_FAILURE(
4412 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 ASSERT_EQ(0, motionArgs.buttonState);
4419 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004420 ASSERT_NO_FATAL_FAILURE(
4421 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004422
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4424 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4425 ASSERT_EQ(0, motionArgs.buttonState);
4426 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004427 ASSERT_NO_FATAL_FAILURE(
4428 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004429
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4431 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4432 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4433}
4434
4435TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004437 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438
4439 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4440 mFakePointerController->setPosition(100, 200);
4441 mFakePointerController->setButtonState(0);
4442
4443 NotifyMotionArgs args;
4444
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004449 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4452 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 +01004453 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004454}
4455
4456TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004457 addConfigurationProperty("cursor.mode", "pointer");
4458 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004459 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004460
4461 NotifyDeviceResetArgs resetArgs;
4462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4463 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4464 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4465
4466 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4467 mFakePointerController->setPosition(100, 200);
4468 mFakePointerController->setButtonState(0);
4469
4470 NotifyMotionArgs args;
4471
4472 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4477 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4480 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 +01004481 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004482
4483 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4487 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4488 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4490 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4492 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4495 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4496
4497 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4499 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4501 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4504 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4506 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4509 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4510
4511 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4516 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4519 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 +01004520 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004521
4522 // Disable pointer capture and check that the device generation got bumped
4523 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004524 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004525 mFakePolicy->setPointerCapture(false);
4526 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004527 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004528
4529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4530 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4531 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4532
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4537 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4540 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 +01004541 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542}
4543
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004544TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004545 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004546
Garfield Tan888a6a42020-01-09 11:39:16 -08004547 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004548 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004549 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4550 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004551 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4552 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004553 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4554 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4555
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004556 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4557 mFakePointerController->setPosition(100, 200);
4558 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004559
4560 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4565 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4568 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 +01004569 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004570 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4571}
4572
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573// --- TouchInputMapperTest ---
4574
4575class TouchInputMapperTest : public InputMapperTest {
4576protected:
4577 static const int32_t RAW_X_MIN;
4578 static const int32_t RAW_X_MAX;
4579 static const int32_t RAW_Y_MIN;
4580 static const int32_t RAW_Y_MAX;
4581 static const int32_t RAW_TOUCH_MIN;
4582 static const int32_t RAW_TOUCH_MAX;
4583 static const int32_t RAW_TOOL_MIN;
4584 static const int32_t RAW_TOOL_MAX;
4585 static const int32_t RAW_PRESSURE_MIN;
4586 static const int32_t RAW_PRESSURE_MAX;
4587 static const int32_t RAW_ORIENTATION_MIN;
4588 static const int32_t RAW_ORIENTATION_MAX;
4589 static const int32_t RAW_DISTANCE_MIN;
4590 static const int32_t RAW_DISTANCE_MAX;
4591 static const int32_t RAW_TILT_MIN;
4592 static const int32_t RAW_TILT_MAX;
4593 static const int32_t RAW_ID_MIN;
4594 static const int32_t RAW_ID_MAX;
4595 static const int32_t RAW_SLOT_MIN;
4596 static const int32_t RAW_SLOT_MAX;
4597 static const float X_PRECISION;
4598 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004599 static const float X_PRECISION_VIRTUAL;
4600 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601
4602 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004603 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604
4605 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4606
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004607 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004608 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004609
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 enum Axes {
4611 POSITION = 1 << 0,
4612 TOUCH = 1 << 1,
4613 TOOL = 1 << 2,
4614 PRESSURE = 1 << 3,
4615 ORIENTATION = 1 << 4,
4616 MINOR = 1 << 5,
4617 ID = 1 << 6,
4618 DISTANCE = 1 << 7,
4619 TILT = 1 << 8,
4620 SLOT = 1 << 9,
4621 TOOL_TYPE = 1 << 10,
4622 };
4623
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004624 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4625 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004626 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004628 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 int32_t toRawX(float displayX);
4630 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004631 float toCookedX(float rawX, float rawY);
4632 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004634 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004636 float toDisplayY(int32_t rawY, int32_t displayHeight);
4637
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638};
4639
4640const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4641const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4642const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4643const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4644const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4645const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4646const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4647const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004648const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4649const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4651const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4652const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4653const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4654const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4655const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4656const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4657const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4658const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4659const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4660const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4661const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004662const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4663 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4664const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4665 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004666const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4667 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668
4669const float TouchInputMapperTest::GEOMETRIC_SCALE =
4670 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4671 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4672
4673const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4674 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4675 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4676};
4677
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004678void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004679 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4680 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004681}
4682
4683void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4684 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4685 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686}
4687
Santos Cordonfa5cf462017-04-05 10:37:00 -07004688void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004689 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4690 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4691 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004692}
4693
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004695 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4696 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4697 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699}
4700
Jason Gerecke489fda82012-09-07 17:19:40 -07004701void TouchInputMapperTest::prepareLocationCalibration() {
4702 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4703}
4704
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705int32_t TouchInputMapperTest::toRawX(float displayX) {
4706 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4707}
4708
4709int32_t TouchInputMapperTest::toRawY(float displayY) {
4710 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4711}
4712
Jason Gerecke489fda82012-09-07 17:19:40 -07004713float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4714 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4715 return rawX;
4716}
4717
4718float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4719 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4720 return rawY;
4721}
4722
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004724 return toDisplayX(rawX, DISPLAY_WIDTH);
4725}
4726
4727float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4728 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729}
4730
4731float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004732 return toDisplayY(rawY, DISPLAY_HEIGHT);
4733}
4734
4735float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4736 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737}
4738
4739
4740// --- SingleTouchInputMapperTest ---
4741
4742class SingleTouchInputMapperTest : public TouchInputMapperTest {
4743protected:
4744 void prepareButtons();
4745 void prepareAxes(int axes);
4746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004747 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4748 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4749 void processUp(SingleTouchInputMapper& mappery);
4750 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4751 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4752 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4753 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4754 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4755 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756};
4757
4758void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004759 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760}
4761
4762void SingleTouchInputMapperTest::prepareAxes(int axes) {
4763 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004764 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4765 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 }
4767 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004768 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4769 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 }
4771 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004772 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4773 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 }
4775 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004776 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4777 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778 }
4779 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004780 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4781 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 }
4783}
4784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004785void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789}
4790
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004791void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794}
4795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004796void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798}
4799
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004800void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802}
4803
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004804void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4805 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807}
4808
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004809void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811}
4812
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004813void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4814 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817}
4818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004819void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4820 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822}
4823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004824void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826}
4827
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 prepareButtons();
4830 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004831 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004833 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834}
4835
4836TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004837 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4838 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 prepareButtons();
4840 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004841 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004843 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844}
4845
4846TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 prepareButtons();
4848 prepareAxes(POSITION);
4849 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004850 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004852 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853}
4854
4855TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856 prepareButtons();
4857 prepareAxes(POSITION);
4858 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004859 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004861 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862}
4863
4864TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 addConfigurationProperty("touch.deviceType", "touchScreen");
4866 prepareDisplay(DISPLAY_ORIENTATION_0);
4867 prepareButtons();
4868 prepareAxes(POSITION);
4869 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004870 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871
4872 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004873 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874
4875 // Virtual key is down.
4876 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4877 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4878 processDown(mapper, x, y);
4879 processSync(mapper);
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004882 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
4884 // Virtual key is up.
4885 processUp(mapper);
4886 processSync(mapper);
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004889 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890}
4891
4892TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 addConfigurationProperty("touch.deviceType", "touchScreen");
4894 prepareDisplay(DISPLAY_ORIENTATION_0);
4895 prepareButtons();
4896 prepareAxes(POSITION);
4897 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004898 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899
4900 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004901 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902
4903 // Virtual key is down.
4904 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4905 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4906 processDown(mapper, x, y);
4907 processSync(mapper);
4908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004910 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911
4912 // Virtual key is up.
4913 processUp(mapper);
4914 processSync(mapper);
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4916
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004917 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918}
4919
4920TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 addConfigurationProperty("touch.deviceType", "touchScreen");
4922 prepareDisplay(DISPLAY_ORIENTATION_0);
4923 prepareButtons();
4924 prepareAxes(POSITION);
4925 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004926 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927
4928 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4929 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004930 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 ASSERT_TRUE(flags[0]);
4932 ASSERT_FALSE(flags[1]);
4933}
4934
4935TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 addConfigurationProperty("touch.deviceType", "touchScreen");
4937 prepareDisplay(DISPLAY_ORIENTATION_0);
4938 prepareButtons();
4939 prepareAxes(POSITION);
4940 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
arthurhungdcef2dc2020-08-11 14:47:50 +08004943 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944
4945 NotifyKeyArgs args;
4946
4947 // Press virtual key.
4948 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4949 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4950 processDown(mapper, x, y);
4951 processSync(mapper);
4952
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4954 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4955 ASSERT_EQ(DEVICE_ID, args.deviceId);
4956 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4957 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4958 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4959 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4960 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4961 ASSERT_EQ(KEY_HOME, args.scanCode);
4962 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4963 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4964
4965 // Release virtual key.
4966 processUp(mapper);
4967 processSync(mapper);
4968
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4970 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4971 ASSERT_EQ(DEVICE_ID, args.deviceId);
4972 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4973 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4974 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4975 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4976 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4977 ASSERT_EQ(KEY_HOME, args.scanCode);
4978 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4979 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4980
4981 // Should not have sent any motions.
4982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4983}
4984
4985TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 addConfigurationProperty("touch.deviceType", "touchScreen");
4987 prepareDisplay(DISPLAY_ORIENTATION_0);
4988 prepareButtons();
4989 prepareAxes(POSITION);
4990 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004991 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992
arthurhungdcef2dc2020-08-11 14:47:50 +08004993 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994
4995 NotifyKeyArgs keyArgs;
4996
4997 // Press virtual key.
4998 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4999 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5000 processDown(mapper, x, y);
5001 processSync(mapper);
5002
5003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5004 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5005 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5006 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5007 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5008 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5009 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5010 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5011 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5012 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5013 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5014
5015 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5016 // into the display area.
5017 y -= 100;
5018 processMove(mapper, x, y);
5019 processSync(mapper);
5020
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5022 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5023 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5024 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5025 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5026 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5027 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5028 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5029 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5030 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5031 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5032 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5033
5034 NotifyMotionArgs motionArgs;
5035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5036 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5037 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5038 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5039 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5040 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5041 ASSERT_EQ(0, motionArgs.flags);
5042 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5043 ASSERT_EQ(0, motionArgs.buttonState);
5044 ASSERT_EQ(0, motionArgs.edgeFlags);
5045 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5046 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5049 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5050 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5051 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5052 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5053
5054 // Keep moving out of bounds. Should generate a pointer move.
5055 y -= 50;
5056 processMove(mapper, x, y);
5057 processSync(mapper);
5058
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5061 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5062 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5063 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5065 ASSERT_EQ(0, motionArgs.flags);
5066 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5067 ASSERT_EQ(0, motionArgs.buttonState);
5068 ASSERT_EQ(0, motionArgs.edgeFlags);
5069 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5073 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5074 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5075 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5077
5078 // Release out of bounds. Should generate a pointer up.
5079 processUp(mapper);
5080 processSync(mapper);
5081
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5083 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5084 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5085 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5086 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5087 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5088 ASSERT_EQ(0, motionArgs.flags);
5089 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5090 ASSERT_EQ(0, motionArgs.buttonState);
5091 ASSERT_EQ(0, motionArgs.edgeFlags);
5092 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5093 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5096 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5097 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5098 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5099 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5100
5101 // Should not have sent any more keys or motions.
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5104}
5105
5106TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 addConfigurationProperty("touch.deviceType", "touchScreen");
5108 prepareDisplay(DISPLAY_ORIENTATION_0);
5109 prepareButtons();
5110 prepareAxes(POSITION);
5111 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005112 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113
arthurhungdcef2dc2020-08-11 14:47:50 +08005114 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115
5116 NotifyMotionArgs motionArgs;
5117
5118 // Initially go down out of bounds.
5119 int32_t x = -10;
5120 int32_t y = -10;
5121 processDown(mapper, x, y);
5122 processSync(mapper);
5123
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5125
5126 // Move into the display area. Should generate a pointer down.
5127 x = 50;
5128 y = 75;
5129 processMove(mapper, x, y);
5130 processSync(mapper);
5131
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5134 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5135 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5136 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5137 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5138 ASSERT_EQ(0, motionArgs.flags);
5139 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5140 ASSERT_EQ(0, motionArgs.buttonState);
5141 ASSERT_EQ(0, motionArgs.edgeFlags);
5142 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5143 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5146 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5147 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5148 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5149 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5150
5151 // Release. Should generate a pointer up.
5152 processUp(mapper);
5153 processSync(mapper);
5154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5157 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5158 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5159 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5161 ASSERT_EQ(0, motionArgs.flags);
5162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5163 ASSERT_EQ(0, motionArgs.buttonState);
5164 ASSERT_EQ(0, motionArgs.edgeFlags);
5165 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5166 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5169 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5170 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5171 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5173
5174 // Should not have sent any more keys or motions.
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5177}
5178
Santos Cordonfa5cf462017-04-05 10:37:00 -07005179TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005180 addConfigurationProperty("touch.deviceType", "touchScreen");
5181 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5182
5183 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5184 prepareButtons();
5185 prepareAxes(POSITION);
5186 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005187 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005188
arthurhungdcef2dc2020-08-11 14:47:50 +08005189 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005190
5191 NotifyMotionArgs motionArgs;
5192
5193 // Down.
5194 int32_t x = 100;
5195 int32_t y = 125;
5196 processDown(mapper, x, y);
5197 processSync(mapper);
5198
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5201 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5202 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5203 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5204 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5205 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5206 ASSERT_EQ(0, motionArgs.flags);
5207 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5208 ASSERT_EQ(0, motionArgs.buttonState);
5209 ASSERT_EQ(0, motionArgs.edgeFlags);
5210 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5211 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5214 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5215 1, 0, 0, 0, 0, 0, 0, 0));
5216 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5217 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5218 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5219
5220 // Move.
5221 x += 50;
5222 y += 75;
5223 processMove(mapper, x, y);
5224 processSync(mapper);
5225
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5227 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5228 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5229 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5231 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5233 ASSERT_EQ(0, motionArgs.flags);
5234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5235 ASSERT_EQ(0, motionArgs.buttonState);
5236 ASSERT_EQ(0, motionArgs.edgeFlags);
5237 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5241 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5242 1, 0, 0, 0, 0, 0, 0, 0));
5243 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5244 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5246
5247 // Up.
5248 processUp(mapper);
5249 processSync(mapper);
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5253 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5254 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5255 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5256 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5258 ASSERT_EQ(0, motionArgs.flags);
5259 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5260 ASSERT_EQ(0, motionArgs.buttonState);
5261 ASSERT_EQ(0, motionArgs.edgeFlags);
5262 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5263 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5266 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5267 1, 0, 0, 0, 0, 0, 0, 0));
5268 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5269 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5271
5272 // Should not have sent any more keys or motions.
5273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5275}
5276
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278 addConfigurationProperty("touch.deviceType", "touchScreen");
5279 prepareDisplay(DISPLAY_ORIENTATION_0);
5280 prepareButtons();
5281 prepareAxes(POSITION);
5282 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005283 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284
arthurhungdcef2dc2020-08-11 14:47:50 +08005285 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286
5287 NotifyMotionArgs motionArgs;
5288
5289 // Down.
5290 int32_t x = 100;
5291 int32_t y = 125;
5292 processDown(mapper, x, y);
5293 processSync(mapper);
5294
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5300 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5301 ASSERT_EQ(0, motionArgs.flags);
5302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5303 ASSERT_EQ(0, motionArgs.buttonState);
5304 ASSERT_EQ(0, motionArgs.edgeFlags);
5305 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5306 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5309 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5310 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5311 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5313
5314 // Move.
5315 x += 50;
5316 y += 75;
5317 processMove(mapper, x, y);
5318 processSync(mapper);
5319
5320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5326 ASSERT_EQ(0, motionArgs.flags);
5327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5328 ASSERT_EQ(0, motionArgs.buttonState);
5329 ASSERT_EQ(0, motionArgs.edgeFlags);
5330 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5334 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5336 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5338
5339 // Up.
5340 processUp(mapper);
5341 processSync(mapper);
5342
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5345 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5346 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5347 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5348 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5349 ASSERT_EQ(0, motionArgs.flags);
5350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5351 ASSERT_EQ(0, motionArgs.buttonState);
5352 ASSERT_EQ(0, motionArgs.edgeFlags);
5353 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5354 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5357 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5358 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5359 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5360 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5361
5362 // Should not have sent any more keys or motions.
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5365}
5366
5367TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368 addConfigurationProperty("touch.deviceType", "touchScreen");
5369 prepareButtons();
5370 prepareAxes(POSITION);
5371 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005372 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373
5374 NotifyMotionArgs args;
5375
5376 // Rotation 90.
5377 prepareDisplay(DISPLAY_ORIENTATION_90);
5378 processDown(mapper, toRawX(50), toRawY(75));
5379 processSync(mapper);
5380
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5382 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5383 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5384
5385 processUp(mapper);
5386 processSync(mapper);
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5388}
5389
5390TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391 addConfigurationProperty("touch.deviceType", "touchScreen");
5392 prepareButtons();
5393 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005394 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395
5396 NotifyMotionArgs args;
5397
5398 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005399 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 prepareDisplay(DISPLAY_ORIENTATION_0);
5401 processDown(mapper, toRawX(50), toRawY(75));
5402 processSync(mapper);
5403
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5405 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5406 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5407
5408 processUp(mapper);
5409 processSync(mapper);
5410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5411
5412 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005413 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 prepareDisplay(DISPLAY_ORIENTATION_90);
5415 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5416 processSync(mapper);
5417
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5419 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5420 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5421
5422 processUp(mapper);
5423 processSync(mapper);
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5425
5426 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005427 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 prepareDisplay(DISPLAY_ORIENTATION_180);
5429 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5430 processSync(mapper);
5431
5432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5433 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5434 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5435
5436 processUp(mapper);
5437 processSync(mapper);
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5439
5440 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005441 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442 prepareDisplay(DISPLAY_ORIENTATION_270);
5443 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5444 processSync(mapper);
5445
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5447 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5448 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5449
5450 processUp(mapper);
5451 processSync(mapper);
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5453}
5454
5455TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456 addConfigurationProperty("touch.deviceType", "touchScreen");
5457 prepareDisplay(DISPLAY_ORIENTATION_0);
5458 prepareButtons();
5459 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005460 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
5462 // These calculations are based on the input device calibration documentation.
5463 int32_t rawX = 100;
5464 int32_t rawY = 200;
5465 int32_t rawPressure = 10;
5466 int32_t rawToolMajor = 12;
5467 int32_t rawDistance = 2;
5468 int32_t rawTiltX = 30;
5469 int32_t rawTiltY = 110;
5470
5471 float x = toDisplayX(rawX);
5472 float y = toDisplayY(rawY);
5473 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5474 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5475 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5476 float distance = float(rawDistance);
5477
5478 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5479 float tiltScale = M_PI / 180;
5480 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5481 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5482 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5483 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5484
5485 processDown(mapper, rawX, rawY);
5486 processPressure(mapper, rawPressure);
5487 processToolMajor(mapper, rawToolMajor);
5488 processDistance(mapper, rawDistance);
5489 processTilt(mapper, rawTiltX, rawTiltY);
5490 processSync(mapper);
5491
5492 NotifyMotionArgs args;
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5495 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5496 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5497}
5498
Jason Gerecke489fda82012-09-07 17:19:40 -07005499TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005500 addConfigurationProperty("touch.deviceType", "touchScreen");
5501 prepareDisplay(DISPLAY_ORIENTATION_0);
5502 prepareLocationCalibration();
5503 prepareButtons();
5504 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005505 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005506
5507 int32_t rawX = 100;
5508 int32_t rawY = 200;
5509
5510 float x = toDisplayX(toCookedX(rawX, rawY));
5511 float y = toDisplayY(toCookedY(rawX, rawY));
5512
5513 processDown(mapper, rawX, rawY);
5514 processSync(mapper);
5515
5516 NotifyMotionArgs args;
5517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5519 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5520}
5521
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523 addConfigurationProperty("touch.deviceType", "touchScreen");
5524 prepareDisplay(DISPLAY_ORIENTATION_0);
5525 prepareButtons();
5526 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005527 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528
5529 NotifyMotionArgs motionArgs;
5530 NotifyKeyArgs keyArgs;
5531
5532 processDown(mapper, 100, 200);
5533 processSync(mapper);
5534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5535 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5536 ASSERT_EQ(0, motionArgs.buttonState);
5537
5538 // press BTN_LEFT, release BTN_LEFT
5539 processKey(mapper, BTN_LEFT, 1);
5540 processSync(mapper);
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5543 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5544
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5547 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5548
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549 processKey(mapper, BTN_LEFT, 0);
5550 processSync(mapper);
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005554
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558
5559 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5560 processKey(mapper, BTN_RIGHT, 1);
5561 processKey(mapper, BTN_MIDDLE, 1);
5562 processSync(mapper);
5563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5565 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5566 motionArgs.buttonState);
5567
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5570 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5571
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5574 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5575 motionArgs.buttonState);
5576
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577 processKey(mapper, BTN_RIGHT, 0);
5578 processSync(mapper);
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005585 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586
5587 processKey(mapper, BTN_MIDDLE, 0);
5588 processSync(mapper);
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005590 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005592
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005595 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596
5597 // press BTN_BACK, release BTN_BACK
5598 processKey(mapper, BTN_BACK, 1);
5599 processSync(mapper);
5600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5601 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5602 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005603
Michael Wrightd02c5b62014-02-10 15:10:22 -08005604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005606 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5607
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5610 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611
5612 processKey(mapper, BTN_BACK, 0);
5613 processSync(mapper);
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005620 ASSERT_EQ(0, motionArgs.buttonState);
5621
Michael Wrightd02c5b62014-02-10 15:10:22 -08005622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5623 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5624 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5625
5626 // press BTN_SIDE, release BTN_SIDE
5627 processKey(mapper, BTN_SIDE, 1);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5630 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5631 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005632
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005635 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5639 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640
5641 processKey(mapper, BTN_SIDE, 0);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005646
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005649 ASSERT_EQ(0, motionArgs.buttonState);
5650
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5652 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5653 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5654
5655 // press BTN_FORWARD, release BTN_FORWARD
5656 processKey(mapper, BTN_FORWARD, 1);
5657 processSync(mapper);
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5659 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5660 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005661
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005664 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5668 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669
5670 processKey(mapper, BTN_FORWARD, 0);
5671 processSync(mapper);
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005673 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005675
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005678 ASSERT_EQ(0, motionArgs.buttonState);
5679
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5681 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5682 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5683
5684 // press BTN_EXTRA, release BTN_EXTRA
5685 processKey(mapper, BTN_EXTRA, 1);
5686 processSync(mapper);
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5688 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5689 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005690
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005693 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5697 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698
5699 processKey(mapper, BTN_EXTRA, 0);
5700 processSync(mapper);
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005704
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005707 ASSERT_EQ(0, motionArgs.buttonState);
5708
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5710 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5711 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5712
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5714
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715 // press BTN_STYLUS, release BTN_STYLUS
5716 processKey(mapper, BTN_STYLUS, 1);
5717 processSync(mapper);
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005720 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5721
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5724 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725
5726 processKey(mapper, BTN_STYLUS, 0);
5727 processSync(mapper);
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005730 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005731
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005734 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735
5736 // press BTN_STYLUS2, release BTN_STYLUS2
5737 processKey(mapper, BTN_STYLUS2, 1);
5738 processSync(mapper);
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005741 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5745 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746
5747 processKey(mapper, BTN_STYLUS2, 0);
5748 processSync(mapper);
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005751 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005752
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005755 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756
5757 // release touch
5758 processUp(mapper);
5759 processSync(mapper);
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5761 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5762 ASSERT_EQ(0, motionArgs.buttonState);
5763}
5764
5765TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766 addConfigurationProperty("touch.deviceType", "touchScreen");
5767 prepareDisplay(DISPLAY_ORIENTATION_0);
5768 prepareButtons();
5769 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005770 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771
5772 NotifyMotionArgs motionArgs;
5773
5774 // default tool type is finger
5775 processDown(mapper, 100, 200);
5776 processSync(mapper);
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5780
5781 // eraser
5782 processKey(mapper, BTN_TOOL_RUBBER, 1);
5783 processSync(mapper);
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5787
5788 // stylus
5789 processKey(mapper, BTN_TOOL_RUBBER, 0);
5790 processKey(mapper, BTN_TOOL_PEN, 1);
5791 processSync(mapper);
5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5795
5796 // brush
5797 processKey(mapper, BTN_TOOL_PEN, 0);
5798 processKey(mapper, BTN_TOOL_BRUSH, 1);
5799 processSync(mapper);
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5803
5804 // pencil
5805 processKey(mapper, BTN_TOOL_BRUSH, 0);
5806 processKey(mapper, BTN_TOOL_PENCIL, 1);
5807 processSync(mapper);
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5811
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005812 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813 processKey(mapper, BTN_TOOL_PENCIL, 0);
5814 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5815 processSync(mapper);
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5819
5820 // mouse
5821 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5822 processKey(mapper, BTN_TOOL_MOUSE, 1);
5823 processSync(mapper);
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5827
5828 // lens
5829 processKey(mapper, BTN_TOOL_MOUSE, 0);
5830 processKey(mapper, BTN_TOOL_LENS, 1);
5831 processSync(mapper);
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5835
5836 // double-tap
5837 processKey(mapper, BTN_TOOL_LENS, 0);
5838 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5843
5844 // triple-tap
5845 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5846 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5847 processSync(mapper);
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5851
5852 // quad-tap
5853 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5854 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5855 processSync(mapper);
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5859
5860 // finger
5861 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5862 processKey(mapper, BTN_TOOL_FINGER, 1);
5863 processSync(mapper);
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5867
5868 // stylus trumps finger
5869 processKey(mapper, BTN_TOOL_PEN, 1);
5870 processSync(mapper);
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5874
5875 // eraser trumps stylus
5876 processKey(mapper, BTN_TOOL_RUBBER, 1);
5877 processSync(mapper);
5878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5881
5882 // mouse trumps eraser
5883 processKey(mapper, BTN_TOOL_MOUSE, 1);
5884 processSync(mapper);
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5888
5889 // back to default tool type
5890 processKey(mapper, BTN_TOOL_MOUSE, 0);
5891 processKey(mapper, BTN_TOOL_RUBBER, 0);
5892 processKey(mapper, BTN_TOOL_PEN, 0);
5893 processKey(mapper, BTN_TOOL_FINGER, 0);
5894 processSync(mapper);
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5898}
5899
5900TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901 addConfigurationProperty("touch.deviceType", "touchScreen");
5902 prepareDisplay(DISPLAY_ORIENTATION_0);
5903 prepareButtons();
5904 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005905 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005906 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907
5908 NotifyMotionArgs motionArgs;
5909
5910 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5911 processKey(mapper, BTN_TOOL_FINGER, 1);
5912 processMove(mapper, 100, 200);
5913 processSync(mapper);
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5915 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5917 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5922 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5923
5924 // move a little
5925 processMove(mapper, 150, 250);
5926 processSync(mapper);
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5928 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5930 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5931
5932 // down when BTN_TOUCH is pressed, pressure defaults to 1
5933 processKey(mapper, BTN_TOUCH, 1);
5934 processSync(mapper);
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5938 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5939
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5943 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5944
5945 // up when BTN_TOUCH is released, hover restored
5946 processKey(mapper, BTN_TOUCH, 0);
5947 processSync(mapper);
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5949 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5951 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5952
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5956 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5957
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5961 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5962
5963 // exit hover when pointer goes away
5964 processKey(mapper, BTN_TOOL_FINGER, 0);
5965 processSync(mapper);
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5969 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5970}
5971
5972TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973 addConfigurationProperty("touch.deviceType", "touchScreen");
5974 prepareDisplay(DISPLAY_ORIENTATION_0);
5975 prepareButtons();
5976 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005977 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978
5979 NotifyMotionArgs motionArgs;
5980
5981 // initially hovering because pressure is 0
5982 processDown(mapper, 100, 200);
5983 processPressure(mapper, 0);
5984 processSync(mapper);
5985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5986 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5988 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5989
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5993 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5994
5995 // move a little
5996 processMove(mapper, 150, 250);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6001 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6002
6003 // down when pressure is non-zero
6004 processPressure(mapper, RAW_PRESSURE_MAX);
6005 processSync(mapper);
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6009 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6010
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6014 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6015
6016 // up when pressure becomes 0, hover restored
6017 processPressure(mapper, 0);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6022 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6023
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6025 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6027 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6028
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6032 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6033
6034 // exit hover when pointer goes away
6035 processUp(mapper);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6040 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6041}
6042
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043// --- MultiTouchInputMapperTest ---
6044
6045class MultiTouchInputMapperTest : public TouchInputMapperTest {
6046protected:
6047 void prepareAxes(int axes);
6048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006049 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6050 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6051 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6052 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6053 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6054 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6055 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6056 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6057 void processId(MultiTouchInputMapper& mapper, int32_t id);
6058 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6059 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6060 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6061 void processMTSync(MultiTouchInputMapper& mapper);
6062 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063};
6064
6065void MultiTouchInputMapperTest::prepareAxes(int axes) {
6066 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006067 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6068 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069 }
6070 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6072 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006074 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6075 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 }
6077 }
6078 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6080 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006082 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6083 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084 }
6085 }
6086 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006087 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6088 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089 }
6090 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6092 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 }
6094 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6096 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 }
6098 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6100 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 }
6102 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6104 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 }
6106 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 }
6109}
6110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006111void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6112 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115}
6116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006117void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6118 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120}
6121
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006122void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6123 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006125}
6126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006127void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129}
6130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006131void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133}
6134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006135void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6136 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138}
6139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006140void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142}
6143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006144void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146}
6147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006148void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150}
6151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006152void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154}
6155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006156void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158}
6159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006160void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6161 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163}
6164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006165void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167}
6168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006169void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171}
6172
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174 addConfigurationProperty("touch.deviceType", "touchScreen");
6175 prepareDisplay(DISPLAY_ORIENTATION_0);
6176 prepareAxes(POSITION);
6177 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006178 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179
arthurhungdcef2dc2020-08-11 14:47:50 +08006180 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181
6182 NotifyMotionArgs motionArgs;
6183
6184 // Two fingers down at once.
6185 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6186 processPosition(mapper, x1, y1);
6187 processMTSync(mapper);
6188 processPosition(mapper, x2, y2);
6189 processMTSync(mapper);
6190 processSync(mapper);
6191
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6193 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6194 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6195 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6196 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6198 ASSERT_EQ(0, motionArgs.flags);
6199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6200 ASSERT_EQ(0, motionArgs.buttonState);
6201 ASSERT_EQ(0, motionArgs.edgeFlags);
6202 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6203 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6206 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6207 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6208 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6209 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6210
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6213 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6214 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6215 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6217 motionArgs.action);
6218 ASSERT_EQ(0, motionArgs.flags);
6219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6220 ASSERT_EQ(0, motionArgs.buttonState);
6221 ASSERT_EQ(0, motionArgs.edgeFlags);
6222 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6223 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6225 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6228 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6230 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6231 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6232 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6234
6235 // Move.
6236 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6237 processPosition(mapper, x1, y1);
6238 processMTSync(mapper);
6239 processPosition(mapper, x2, y2);
6240 processMTSync(mapper);
6241 processSync(mapper);
6242
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6245 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6246 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6247 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6249 ASSERT_EQ(0, motionArgs.flags);
6250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6251 ASSERT_EQ(0, motionArgs.buttonState);
6252 ASSERT_EQ(0, motionArgs.edgeFlags);
6253 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6254 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6256 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6259 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6261 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6262 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6263 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6264 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6265
6266 // First finger up.
6267 x2 += 15; y2 -= 20;
6268 processPosition(mapper, x2, y2);
6269 processMTSync(mapper);
6270 processSync(mapper);
6271
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6278 motionArgs.action);
6279 ASSERT_EQ(0, motionArgs.flags);
6280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6281 ASSERT_EQ(0, motionArgs.buttonState);
6282 ASSERT_EQ(0, motionArgs.edgeFlags);
6283 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6286 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6289 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6292 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6293 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6295
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6298 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6300 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6302 ASSERT_EQ(0, motionArgs.flags);
6303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6304 ASSERT_EQ(0, motionArgs.buttonState);
6305 ASSERT_EQ(0, motionArgs.edgeFlags);
6306 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6307 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6314
6315 // Move.
6316 x2 += 20; y2 -= 25;
6317 processPosition(mapper, x2, y2);
6318 processMTSync(mapper);
6319 processSync(mapper);
6320
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6323 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6324 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6325 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6327 ASSERT_EQ(0, motionArgs.flags);
6328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6329 ASSERT_EQ(0, motionArgs.buttonState);
6330 ASSERT_EQ(0, motionArgs.edgeFlags);
6331 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6332 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6335 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6336 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6337 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6339
6340 // New finger down.
6341 int32_t x3 = 700, y3 = 300;
6342 processPosition(mapper, x2, y2);
6343 processMTSync(mapper);
6344 processPosition(mapper, x3, y3);
6345 processMTSync(mapper);
6346 processSync(mapper);
6347
6348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6349 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6350 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6351 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6352 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6354 motionArgs.action);
6355 ASSERT_EQ(0, motionArgs.flags);
6356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6357 ASSERT_EQ(0, motionArgs.buttonState);
6358 ASSERT_EQ(0, motionArgs.edgeFlags);
6359 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6362 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6365 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6367 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6368 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6369 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6370 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6371
6372 // Second finger up.
6373 x3 += 30; y3 -= 20;
6374 processPosition(mapper, x3, y3);
6375 processMTSync(mapper);
6376 processSync(mapper);
6377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6380 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6381 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6382 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6384 motionArgs.action);
6385 ASSERT_EQ(0, motionArgs.flags);
6386 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6387 ASSERT_EQ(0, motionArgs.buttonState);
6388 ASSERT_EQ(0, motionArgs.edgeFlags);
6389 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6390 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6392 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6395 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6397 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6398 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6399 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6401
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6408 ASSERT_EQ(0, motionArgs.flags);
6409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6410 ASSERT_EQ(0, motionArgs.buttonState);
6411 ASSERT_EQ(0, motionArgs.edgeFlags);
6412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6417 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6418 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6420
6421 // Last finger up.
6422 processMTSync(mapper);
6423 processSync(mapper);
6424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6427 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6428 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6429 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6431 ASSERT_EQ(0, motionArgs.flags);
6432 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6433 ASSERT_EQ(0, motionArgs.buttonState);
6434 ASSERT_EQ(0, motionArgs.edgeFlags);
6435 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6436 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6439 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6440 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6441 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6443
6444 // Should not have sent any more keys or motions.
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6447}
6448
6449TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450 addConfigurationProperty("touch.deviceType", "touchScreen");
6451 prepareDisplay(DISPLAY_ORIENTATION_0);
6452 prepareAxes(POSITION | ID);
6453 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006454 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455
arthurhungdcef2dc2020-08-11 14:47:50 +08006456 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457
6458 NotifyMotionArgs motionArgs;
6459
6460 // Two fingers down at once.
6461 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6462 processPosition(mapper, x1, y1);
6463 processId(mapper, 1);
6464 processMTSync(mapper);
6465 processPosition(mapper, x2, y2);
6466 processId(mapper, 2);
6467 processMTSync(mapper);
6468 processSync(mapper);
6469
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6476 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6477
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6480 motionArgs.action);
6481 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6484 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6487 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6489 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6490
6491 // Move.
6492 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6493 processPosition(mapper, x1, y1);
6494 processId(mapper, 1);
6495 processMTSync(mapper);
6496 processPosition(mapper, x2, y2);
6497 processId(mapper, 2);
6498 processMTSync(mapper);
6499 processSync(mapper);
6500
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6503 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6504 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6506 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6509 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6511 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6512
6513 // First finger up.
6514 x2 += 15; y2 -= 20;
6515 processPosition(mapper, x2, y2);
6516 processId(mapper, 2);
6517 processMTSync(mapper);
6518 processSync(mapper);
6519
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6522 motionArgs.action);
6523 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6524 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6526 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6529 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6531 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6532
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6535 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6536 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6539 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6540
6541 // Move.
6542 x2 += 20; y2 -= 25;
6543 processPosition(mapper, x2, y2);
6544 processId(mapper, 2);
6545 processMTSync(mapper);
6546 processSync(mapper);
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6550 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6551 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6554 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6555
6556 // New finger down.
6557 int32_t x3 = 700, y3 = 300;
6558 processPosition(mapper, x2, y2);
6559 processId(mapper, 2);
6560 processMTSync(mapper);
6561 processPosition(mapper, x3, y3);
6562 processId(mapper, 3);
6563 processMTSync(mapper);
6564 processSync(mapper);
6565
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6568 motionArgs.action);
6569 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6572 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6575 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6577 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6578
6579 // Second finger up.
6580 x3 += 30; y3 -= 20;
6581 processPosition(mapper, x3, y3);
6582 processId(mapper, 3);
6583 processMTSync(mapper);
6584 processSync(mapper);
6585
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6588 motionArgs.action);
6589 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6590 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6592 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6595 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6597 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6598
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6601 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6602 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6605 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6606
6607 // Last finger up.
6608 processMTSync(mapper);
6609 processSync(mapper);
6610
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6613 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6614 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6617 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6618
6619 // Should not have sent any more keys or motions.
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6622}
6623
6624TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625 addConfigurationProperty("touch.deviceType", "touchScreen");
6626 prepareDisplay(DISPLAY_ORIENTATION_0);
6627 prepareAxes(POSITION | ID | SLOT);
6628 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630
arthurhungdcef2dc2020-08-11 14:47:50 +08006631 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006632
6633 NotifyMotionArgs motionArgs;
6634
6635 // Two fingers down at once.
6636 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6637 processPosition(mapper, x1, y1);
6638 processId(mapper, 1);
6639 processSlot(mapper, 1);
6640 processPosition(mapper, x2, y2);
6641 processId(mapper, 2);
6642 processSync(mapper);
6643
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6645 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6646 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6650 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6651
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6654 motionArgs.action);
6655 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6658 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6661 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6663 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6664
6665 // Move.
6666 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6667 processSlot(mapper, 0);
6668 processPosition(mapper, x1, y1);
6669 processSlot(mapper, 1);
6670 processPosition(mapper, x2, y2);
6671 processSync(mapper);
6672
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6675 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6678 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6681 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6683 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6684
6685 // First finger up.
6686 x2 += 15; y2 -= 20;
6687 processSlot(mapper, 0);
6688 processId(mapper, -1);
6689 processSlot(mapper, 1);
6690 processPosition(mapper, x2, y2);
6691 processSync(mapper);
6692
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6695 motionArgs.action);
6696 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6699 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6702 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6704 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6705
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6708 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6709 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6712 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6713
6714 // Move.
6715 x2 += 20; y2 -= 25;
6716 processPosition(mapper, x2, y2);
6717 processSync(mapper);
6718
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6721 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6722 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6725 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6726
6727 // New finger down.
6728 int32_t x3 = 700, y3 = 300;
6729 processPosition(mapper, x2, y2);
6730 processSlot(mapper, 0);
6731 processId(mapper, 3);
6732 processPosition(mapper, x3, y3);
6733 processSync(mapper);
6734
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6736 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6737 motionArgs.action);
6738 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6739 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6741 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6744 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6746 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6747
6748 // Second finger up.
6749 x3 += 30; y3 -= 20;
6750 processSlot(mapper, 1);
6751 processId(mapper, -1);
6752 processSlot(mapper, 0);
6753 processPosition(mapper, x3, y3);
6754 processSync(mapper);
6755
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6758 motionArgs.action);
6759 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6760 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6762 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6765 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6767 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6768
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6771 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6772 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6775 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6776
6777 // Last finger up.
6778 processId(mapper, -1);
6779 processSync(mapper);
6780
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6782 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6783 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6784 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6787 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6788
6789 // Should not have sent any more keys or motions.
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6792}
6793
6794TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006795 addConfigurationProperty("touch.deviceType", "touchScreen");
6796 prepareDisplay(DISPLAY_ORIENTATION_0);
6797 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006798 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006799
6800 // These calculations are based on the input device calibration documentation.
6801 int32_t rawX = 100;
6802 int32_t rawY = 200;
6803 int32_t rawTouchMajor = 7;
6804 int32_t rawTouchMinor = 6;
6805 int32_t rawToolMajor = 9;
6806 int32_t rawToolMinor = 8;
6807 int32_t rawPressure = 11;
6808 int32_t rawDistance = 0;
6809 int32_t rawOrientation = 3;
6810 int32_t id = 5;
6811
6812 float x = toDisplayX(rawX);
6813 float y = toDisplayY(rawY);
6814 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6815 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6816 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6817 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6818 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6819 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6820 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6821 float distance = float(rawDistance);
6822
6823 processPosition(mapper, rawX, rawY);
6824 processTouchMajor(mapper, rawTouchMajor);
6825 processTouchMinor(mapper, rawTouchMinor);
6826 processToolMajor(mapper, rawToolMajor);
6827 processToolMinor(mapper, rawToolMinor);
6828 processPressure(mapper, rawPressure);
6829 processOrientation(mapper, rawOrientation);
6830 processDistance(mapper, rawDistance);
6831 processId(mapper, id);
6832 processMTSync(mapper);
6833 processSync(mapper);
6834
6835 NotifyMotionArgs args;
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6837 ASSERT_EQ(0, args.pointerProperties[0].id);
6838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6839 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6840 orientation, distance));
6841}
6842
6843TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006844 addConfigurationProperty("touch.deviceType", "touchScreen");
6845 prepareDisplay(DISPLAY_ORIENTATION_0);
6846 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6847 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006848 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006849
6850 // These calculations are based on the input device calibration documentation.
6851 int32_t rawX = 100;
6852 int32_t rawY = 200;
6853 int32_t rawTouchMajor = 140;
6854 int32_t rawTouchMinor = 120;
6855 int32_t rawToolMajor = 180;
6856 int32_t rawToolMinor = 160;
6857
6858 float x = toDisplayX(rawX);
6859 float y = toDisplayY(rawY);
6860 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6861 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6862 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6863 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6864 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6865
6866 processPosition(mapper, rawX, rawY);
6867 processTouchMajor(mapper, rawTouchMajor);
6868 processTouchMinor(mapper, rawTouchMinor);
6869 processToolMajor(mapper, rawToolMajor);
6870 processToolMinor(mapper, rawToolMinor);
6871 processMTSync(mapper);
6872 processSync(mapper);
6873
6874 NotifyMotionArgs args;
6875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6877 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6878}
6879
6880TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006881 addConfigurationProperty("touch.deviceType", "touchScreen");
6882 prepareDisplay(DISPLAY_ORIENTATION_0);
6883 prepareAxes(POSITION | TOUCH | TOOL);
6884 addConfigurationProperty("touch.size.calibration", "diameter");
6885 addConfigurationProperty("touch.size.scale", "10");
6886 addConfigurationProperty("touch.size.bias", "160");
6887 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006888 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006889
6890 // These calculations are based on the input device calibration documentation.
6891 // Note: We only provide a single common touch/tool value because the device is assumed
6892 // not to emit separate values for each pointer (isSummed = 1).
6893 int32_t rawX = 100;
6894 int32_t rawY = 200;
6895 int32_t rawX2 = 150;
6896 int32_t rawY2 = 250;
6897 int32_t rawTouchMajor = 5;
6898 int32_t rawToolMajor = 8;
6899
6900 float x = toDisplayX(rawX);
6901 float y = toDisplayY(rawY);
6902 float x2 = toDisplayX(rawX2);
6903 float y2 = toDisplayY(rawY2);
6904 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6905 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6906 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6907
6908 processPosition(mapper, rawX, rawY);
6909 processTouchMajor(mapper, rawTouchMajor);
6910 processToolMajor(mapper, rawToolMajor);
6911 processMTSync(mapper);
6912 processPosition(mapper, rawX2, rawY2);
6913 processTouchMajor(mapper, rawTouchMajor);
6914 processToolMajor(mapper, rawToolMajor);
6915 processMTSync(mapper);
6916 processSync(mapper);
6917
6918 NotifyMotionArgs args;
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6920 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6921
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6923 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6924 args.action);
6925 ASSERT_EQ(size_t(2), args.pointerCount);
6926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6927 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6929 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6930}
6931
6932TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933 addConfigurationProperty("touch.deviceType", "touchScreen");
6934 prepareDisplay(DISPLAY_ORIENTATION_0);
6935 prepareAxes(POSITION | TOUCH | TOOL);
6936 addConfigurationProperty("touch.size.calibration", "area");
6937 addConfigurationProperty("touch.size.scale", "43");
6938 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006939 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940
6941 // These calculations are based on the input device calibration documentation.
6942 int32_t rawX = 100;
6943 int32_t rawY = 200;
6944 int32_t rawTouchMajor = 5;
6945 int32_t rawToolMajor = 8;
6946
6947 float x = toDisplayX(rawX);
6948 float y = toDisplayY(rawY);
6949 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6950 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6951 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6952
6953 processPosition(mapper, rawX, rawY);
6954 processTouchMajor(mapper, rawTouchMajor);
6955 processToolMajor(mapper, rawToolMajor);
6956 processMTSync(mapper);
6957 processSync(mapper);
6958
6959 NotifyMotionArgs args;
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6962 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6963}
6964
6965TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966 addConfigurationProperty("touch.deviceType", "touchScreen");
6967 prepareDisplay(DISPLAY_ORIENTATION_0);
6968 prepareAxes(POSITION | PRESSURE);
6969 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6970 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006971 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972
Michael Wrightaa449c92017-12-13 21:21:43 +00006973 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006974 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006975 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6976 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6977 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6978
Michael Wrightd02c5b62014-02-10 15:10:22 -08006979 // These calculations are based on the input device calibration documentation.
6980 int32_t rawX = 100;
6981 int32_t rawY = 200;
6982 int32_t rawPressure = 60;
6983
6984 float x = toDisplayX(rawX);
6985 float y = toDisplayY(rawY);
6986 float pressure = float(rawPressure) * 0.01f;
6987
6988 processPosition(mapper, rawX, rawY);
6989 processPressure(mapper, rawPressure);
6990 processMTSync(mapper);
6991 processSync(mapper);
6992
6993 NotifyMotionArgs args;
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6996 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6997}
6998
6999TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007000 addConfigurationProperty("touch.deviceType", "touchScreen");
7001 prepareDisplay(DISPLAY_ORIENTATION_0);
7002 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007003 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004
7005 NotifyMotionArgs motionArgs;
7006 NotifyKeyArgs keyArgs;
7007
7008 processId(mapper, 1);
7009 processPosition(mapper, 100, 200);
7010 processSync(mapper);
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7013 ASSERT_EQ(0, motionArgs.buttonState);
7014
7015 // press BTN_LEFT, release BTN_LEFT
7016 processKey(mapper, BTN_LEFT, 1);
7017 processSync(mapper);
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7020 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7021
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7024 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7025
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026 processKey(mapper, BTN_LEFT, 0);
7027 processSync(mapper);
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007030 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007031
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007034 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035
7036 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7037 processKey(mapper, BTN_RIGHT, 1);
7038 processKey(mapper, BTN_MIDDLE, 1);
7039 processSync(mapper);
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7042 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7043 motionArgs.buttonState);
7044
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7047 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7048
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7051 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7052 motionArgs.buttonState);
7053
Michael Wrightd02c5b62014-02-10 15:10:22 -08007054 processKey(mapper, BTN_RIGHT, 0);
7055 processSync(mapper);
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007058 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007059
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007062 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063
7064 processKey(mapper, BTN_MIDDLE, 0);
7065 processSync(mapper);
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007069
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007072 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073
7074 // press BTN_BACK, release BTN_BACK
7075 processKey(mapper, BTN_BACK, 1);
7076 processSync(mapper);
7077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7078 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7079 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007080
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007083 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7084
7085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7087 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088
7089 processKey(mapper, BTN_BACK, 0);
7090 processSync(mapper);
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007094
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007097 ASSERT_EQ(0, motionArgs.buttonState);
7098
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7100 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7101 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7102
7103 // press BTN_SIDE, release BTN_SIDE
7104 processKey(mapper, BTN_SIDE, 1);
7105 processSync(mapper);
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7107 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7108 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007109
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007112 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7113
7114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7116 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117
7118 processKey(mapper, BTN_SIDE, 0);
7119 processSync(mapper);
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007123
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007126 ASSERT_EQ(0, motionArgs.buttonState);
7127
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7129 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7130 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7131
7132 // press BTN_FORWARD, release BTN_FORWARD
7133 processKey(mapper, BTN_FORWARD, 1);
7134 processSync(mapper);
7135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7136 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7137 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007138
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007141 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7142
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7145 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146
7147 processKey(mapper, BTN_FORWARD, 0);
7148 processSync(mapper);
7149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007152
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007155 ASSERT_EQ(0, motionArgs.buttonState);
7156
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7158 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7159 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7160
7161 // press BTN_EXTRA, release BTN_EXTRA
7162 processKey(mapper, BTN_EXTRA, 1);
7163 processSync(mapper);
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7165 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7166 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007167
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007170 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7171
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7173 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7174 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175
7176 processKey(mapper, BTN_EXTRA, 0);
7177 processSync(mapper);
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007181
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007184 ASSERT_EQ(0, motionArgs.buttonState);
7185
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7187 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7188 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7189
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7191
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192 // press BTN_STYLUS, release BTN_STYLUS
7193 processKey(mapper, BTN_STYLUS, 1);
7194 processSync(mapper);
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007197 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7198
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7201 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202
7203 processKey(mapper, BTN_STYLUS, 0);
7204 processSync(mapper);
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007207 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007208
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007211 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212
7213 // press BTN_STYLUS2, release BTN_STYLUS2
7214 processKey(mapper, BTN_STYLUS2, 1);
7215 processSync(mapper);
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007218 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7219
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7222 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223
7224 processKey(mapper, BTN_STYLUS2, 0);
7225 processSync(mapper);
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007229
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007232 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233
7234 // release touch
7235 processId(mapper, -1);
7236 processSync(mapper);
7237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7238 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7239 ASSERT_EQ(0, motionArgs.buttonState);
7240}
7241
7242TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007243 addConfigurationProperty("touch.deviceType", "touchScreen");
7244 prepareDisplay(DISPLAY_ORIENTATION_0);
7245 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007246 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247
7248 NotifyMotionArgs motionArgs;
7249
7250 // default tool type is finger
7251 processId(mapper, 1);
7252 processPosition(mapper, 100, 200);
7253 processSync(mapper);
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7257
7258 // eraser
7259 processKey(mapper, BTN_TOOL_RUBBER, 1);
7260 processSync(mapper);
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7264
7265 // stylus
7266 processKey(mapper, BTN_TOOL_RUBBER, 0);
7267 processKey(mapper, BTN_TOOL_PEN, 1);
7268 processSync(mapper);
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7272
7273 // brush
7274 processKey(mapper, BTN_TOOL_PEN, 0);
7275 processKey(mapper, BTN_TOOL_BRUSH, 1);
7276 processSync(mapper);
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7280
7281 // pencil
7282 processKey(mapper, BTN_TOOL_BRUSH, 0);
7283 processKey(mapper, BTN_TOOL_PENCIL, 1);
7284 processSync(mapper);
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7288
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007289 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007290 processKey(mapper, BTN_TOOL_PENCIL, 0);
7291 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7292 processSync(mapper);
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7296
7297 // mouse
7298 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7299 processKey(mapper, BTN_TOOL_MOUSE, 1);
7300 processSync(mapper);
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7304
7305 // lens
7306 processKey(mapper, BTN_TOOL_MOUSE, 0);
7307 processKey(mapper, BTN_TOOL_LENS, 1);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7312
7313 // double-tap
7314 processKey(mapper, BTN_TOOL_LENS, 0);
7315 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7316 processSync(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7320
7321 // triple-tap
7322 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7323 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7324 processSync(mapper);
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7328
7329 // quad-tap
7330 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7331 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7332 processSync(mapper);
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7336
7337 // finger
7338 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7339 processKey(mapper, BTN_TOOL_FINGER, 1);
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7344
7345 // stylus trumps finger
7346 processKey(mapper, BTN_TOOL_PEN, 1);
7347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7351
7352 // eraser trumps stylus
7353 processKey(mapper, BTN_TOOL_RUBBER, 1);
7354 processSync(mapper);
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7358
7359 // mouse trumps eraser
7360 processKey(mapper, BTN_TOOL_MOUSE, 1);
7361 processSync(mapper);
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7365
7366 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7367 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7368 processSync(mapper);
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7372
7373 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7374 processToolType(mapper, MT_TOOL_PEN);
7375 processSync(mapper);
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7379
7380 // back to default tool type
7381 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7382 processKey(mapper, BTN_TOOL_MOUSE, 0);
7383 processKey(mapper, BTN_TOOL_RUBBER, 0);
7384 processKey(mapper, BTN_TOOL_PEN, 0);
7385 processKey(mapper, BTN_TOOL_FINGER, 0);
7386 processSync(mapper);
7387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7390}
7391
7392TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007393 addConfigurationProperty("touch.deviceType", "touchScreen");
7394 prepareDisplay(DISPLAY_ORIENTATION_0);
7395 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007396 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007397 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007398
7399 NotifyMotionArgs motionArgs;
7400
7401 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7402 processId(mapper, 1);
7403 processPosition(mapper, 100, 200);
7404 processSync(mapper);
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7408 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7409
7410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7411 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7413 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7414
7415 // move a little
7416 processPosition(mapper, 150, 250);
7417 processSync(mapper);
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7421 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7422
7423 // down when BTN_TOUCH is pressed, pressure defaults to 1
7424 processKey(mapper, BTN_TOUCH, 1);
7425 processSync(mapper);
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7429 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7430
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7434 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7435
7436 // up when BTN_TOUCH is released, hover restored
7437 processKey(mapper, BTN_TOUCH, 0);
7438 processSync(mapper);
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7440 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7442 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7443
7444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7445 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7447 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7448
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7452 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7453
7454 // exit hover when pointer goes away
7455 processId(mapper, -1);
7456 processSync(mapper);
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7458 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7460 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7461}
7462
7463TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464 addConfigurationProperty("touch.deviceType", "touchScreen");
7465 prepareDisplay(DISPLAY_ORIENTATION_0);
7466 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007467 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007468
7469 NotifyMotionArgs motionArgs;
7470
7471 // initially hovering because pressure is 0
7472 processId(mapper, 1);
7473 processPosition(mapper, 100, 200);
7474 processPressure(mapper, 0);
7475 processSync(mapper);
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7477 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7479 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7480
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7482 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7484 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7485
7486 // move a little
7487 processPosition(mapper, 150, 250);
7488 processSync(mapper);
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7492 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7493
7494 // down when pressure becomes non-zero
7495 processPressure(mapper, RAW_PRESSURE_MAX);
7496 processSync(mapper);
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7500 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7501
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7503 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7505 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7506
7507 // up when pressure becomes 0, hover restored
7508 processPressure(mapper, 0);
7509 processSync(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7511 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7513 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7514
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7518 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7519
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7523 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7524
7525 // exit hover when pointer goes away
7526 processId(mapper, -1);
7527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7531 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7532}
7533
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007534/**
7535 * Set the input device port <--> display port associations, and check that the
7536 * events are routed to the display that matches the display port.
7537 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7538 */
7539TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007540 const std::string usb2 = "USB2";
7541 const uint8_t hdmi1 = 0;
7542 const uint8_t hdmi2 = 1;
7543 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007544 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007545
7546 addConfigurationProperty("touch.deviceType", "touchScreen");
7547 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007549
7550 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7551 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7552
7553 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7554 // for this input device is specified, and the matching viewport is not present,
7555 // the input device should be disabled (at the mapper level).
7556
7557 // Add viewport for display 2 on hdmi2
7558 prepareSecondaryDisplay(type, hdmi2);
7559 // Send a touch event
7560 processPosition(mapper, 100, 100);
7561 processSync(mapper);
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7563
7564 // Add viewport for display 1 on hdmi1
7565 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7566 // Send a touch event again
7567 processPosition(mapper, 100, 100);
7568 processSync(mapper);
7569
7570 NotifyMotionArgs args;
7571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7572 ASSERT_EQ(DISPLAY_ID, args.displayId);
7573}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007574
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007575TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007576 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007577 std::shared_ptr<FakePointerController> fakePointerController =
7578 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007579 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007580 fakePointerController->setPosition(100, 200);
7581 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007582 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7583
Garfield Tan888a6a42020-01-09 11:39:16 -08007584 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007585 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007586
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007587 prepareDisplay(DISPLAY_ORIENTATION_0);
7588 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007589 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007590
7591 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007592 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007593
7594 NotifyMotionArgs motionArgs;
7595 processPosition(mapper, 100, 100);
7596 processSync(mapper);
7597
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7600 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7601}
7602
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007603/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007604 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7605 */
7606TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7607 addConfigurationProperty("touch.deviceType", "touchScreen");
7608 prepareAxes(POSITION);
7609 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7610
7611 prepareDisplay(DISPLAY_ORIENTATION_0);
7612 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7613 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7614 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7615 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7616
7617 NotifyMotionArgs args;
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7619 ASSERT_EQ(26, args.readTime);
7620
7621 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7622 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7623 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7624
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7626 ASSERT_EQ(33, args.readTime);
7627}
7628
7629/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007630 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7631 * events should not be delivered to the listener.
7632 */
7633TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7634 addConfigurationProperty("touch.deviceType", "touchScreen");
7635 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7636 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7637 ViewportType::INTERNAL);
7638 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7639 prepareAxes(POSITION);
7640 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7641
7642 NotifyMotionArgs motionArgs;
7643 processPosition(mapper, 100, 100);
7644 processSync(mapper);
7645
7646 mFakeListener->assertNotifyMotionWasNotCalled();
7647}
7648
Garfield Tanc734e4f2021-01-15 20:01:39 -08007649TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7650 addConfigurationProperty("touch.deviceType", "touchScreen");
7651 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7652 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7653 ViewportType::INTERNAL);
7654 std::optional<DisplayViewport> optionalDisplayViewport =
7655 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7656 ASSERT_TRUE(optionalDisplayViewport.has_value());
7657 DisplayViewport displayViewport = *optionalDisplayViewport;
7658
7659 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7660 prepareAxes(POSITION);
7661 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7662
7663 // Finger down
7664 int32_t x = 100, y = 100;
7665 processPosition(mapper, x, y);
7666 processSync(mapper);
7667
7668 NotifyMotionArgs motionArgs;
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7670 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7671
7672 // Deactivate display viewport
7673 displayViewport.isActive = false;
7674 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7675 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7676
7677 // Finger move
7678 x += 10, y += 10;
7679 processPosition(mapper, x, y);
7680 processSync(mapper);
7681
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7683 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7684
7685 // Reactivate display viewport
7686 displayViewport.isActive = true;
7687 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7688 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7689
7690 // Finger move again
7691 x += 10, y += 10;
7692 processPosition(mapper, x, y);
7693 processSync(mapper);
7694
7695 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7696 // no pointer on the touch device.
7697 mFakeListener->assertNotifyMotionWasNotCalled();
7698}
7699
Arthur Hung7c645402019-01-25 17:45:42 +08007700TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7701 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007702 prepareAxes(POSITION | ID | SLOT);
7703 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007705
7706 // Create the second touch screen device, and enable multi fingers.
7707 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007708 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007709 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007710 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007711 std::shared_ptr<InputDevice> device2 =
7712 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7713 Flags<InputDeviceClass>(0));
7714
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007715 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7716 0 /*flat*/, 0 /*fuzz*/);
7717 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7718 0 /*flat*/, 0 /*fuzz*/);
7719 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7720 0 /*flat*/, 0 /*fuzz*/);
7721 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7722 0 /*flat*/, 0 /*fuzz*/);
7723 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7724 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7725 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007726
7727 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007728 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007729 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7730 device2->reset(ARBITRARY_TIME);
7731
7732 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007733 std::shared_ptr<FakePointerController> fakePointerController =
7734 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007735 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7736 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7737
7738 // Setup policy for associated displays and show touches.
7739 const uint8_t hdmi1 = 0;
7740 const uint8_t hdmi2 = 1;
7741 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7742 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7743 mFakePolicy->setShowTouches(true);
7744
7745 // Create displays.
7746 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007747 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007748
7749 // Default device will reconfigure above, need additional reconfiguration for another device.
7750 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007751 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007752
7753 // Two fingers down at default display.
7754 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7755 processPosition(mapper, x1, y1);
7756 processId(mapper, 1);
7757 processSlot(mapper, 1);
7758 processPosition(mapper, x2, y2);
7759 processId(mapper, 2);
7760 processSync(mapper);
7761
7762 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7763 fakePointerController->getSpots().find(DISPLAY_ID);
7764 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7765 ASSERT_EQ(size_t(2), iter->second.size());
7766
7767 // Two fingers down at second display.
7768 processPosition(mapper2, x1, y1);
7769 processId(mapper2, 1);
7770 processSlot(mapper2, 1);
7771 processPosition(mapper2, x2, y2);
7772 processId(mapper2, 2);
7773 processSync(mapper2);
7774
7775 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7776 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7777 ASSERT_EQ(size_t(2), iter->second.size());
7778}
7779
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007780TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007781 prepareAxes(POSITION);
7782 addConfigurationProperty("touch.deviceType", "touchScreen");
7783 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007784 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007785
7786 NotifyMotionArgs motionArgs;
7787 // Unrotated video frame
7788 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7789 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007790 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007791 processPosition(mapper, 100, 200);
7792 processSync(mapper);
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7794 ASSERT_EQ(frames, motionArgs.videoFrames);
7795
7796 // Subsequent touch events should not have any videoframes
7797 // This is implemented separately in FakeEventHub,
7798 // but that should match the behaviour of TouchVideoDevice.
7799 processPosition(mapper, 200, 200);
7800 processSync(mapper);
7801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7802 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7803}
7804
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007805TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007806 prepareAxes(POSITION);
7807 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007808 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007809 // Unrotated video frame
7810 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7811 NotifyMotionArgs motionArgs;
7812
7813 // Test all 4 orientations
7814 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7815 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7816 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7817 clearViewports();
7818 prepareDisplay(orientation);
7819 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007820 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007821 processPosition(mapper, 100, 200);
7822 processSync(mapper);
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7824 frames[0].rotate(orientation);
7825 ASSERT_EQ(frames, motionArgs.videoFrames);
7826 }
7827}
7828
7829TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007830 prepareAxes(POSITION);
7831 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007832 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007833 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7834 // so mix these.
7835 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7836 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7837 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7838 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7839 NotifyMotionArgs motionArgs;
7840
7841 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007842 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007843 processPosition(mapper, 100, 200);
7844 processSync(mapper);
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7846 std::for_each(frames.begin(), frames.end(),
7847 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7848 ASSERT_EQ(frames, motionArgs.videoFrames);
7849}
7850
Arthur Hung9da14732019-09-02 16:16:58 +08007851/**
7852 * If we had defined port associations, but the viewport is not ready, the touch device would be
7853 * expected to be disabled, and it should be enabled after the viewport has found.
7854 */
7855TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007856 constexpr uint8_t hdmi2 = 1;
7857 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007858 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007859
7860 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7861
7862 addConfigurationProperty("touch.deviceType", "touchScreen");
7863 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007865
7866 ASSERT_EQ(mDevice->isEnabled(), false);
7867
7868 // Add display on hdmi2, the device should be enabled and can receive touch event.
7869 prepareSecondaryDisplay(type, hdmi2);
7870 ASSERT_EQ(mDevice->isEnabled(), true);
7871
7872 // Send a touch event.
7873 processPosition(mapper, 100, 100);
7874 processSync(mapper);
7875
7876 NotifyMotionArgs args;
7877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7878 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7879}
7880
Arthur Hung421eb1c2020-01-16 00:09:42 +08007881TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007882 addConfigurationProperty("touch.deviceType", "touchScreen");
7883 prepareDisplay(DISPLAY_ORIENTATION_0);
7884 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007885 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007886
7887 NotifyMotionArgs motionArgs;
7888
7889 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7890 // finger down
7891 processId(mapper, 1);
7892 processPosition(mapper, x1, y1);
7893 processSync(mapper);
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7897
7898 // finger move
7899 processId(mapper, 1);
7900 processPosition(mapper, x2, y2);
7901 processSync(mapper);
7902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7905
7906 // finger up.
7907 processId(mapper, -1);
7908 processSync(mapper);
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7910 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7912
7913 // new finger down
7914 processId(mapper, 1);
7915 processPosition(mapper, x3, y3);
7916 processSync(mapper);
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7918 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7920}
7921
7922/**
arthurhungcc7f9802020-04-30 17:55:40 +08007923 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7924 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007925 */
arthurhungcc7f9802020-04-30 17:55:40 +08007926TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007927 addConfigurationProperty("touch.deviceType", "touchScreen");
7928 prepareDisplay(DISPLAY_ORIENTATION_0);
7929 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007930 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007931
7932 NotifyMotionArgs motionArgs;
7933
7934 // default tool type is finger
7935 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007936 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007937 processPosition(mapper, x1, y1);
7938 processSync(mapper);
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7942
7943 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7944 processToolType(mapper, MT_TOOL_PALM);
7945 processSync(mapper);
7946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7947 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7948
7949 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007950 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007951 processPosition(mapper, x2, y2);
7952 processSync(mapper);
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7954
7955 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007956 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007957 processSync(mapper);
7958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7959
7960 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007961 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007962 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007963 processPosition(mapper, x3, y3);
7964 processSync(mapper);
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7968}
7969
arthurhungbf89a482020-04-17 17:37:55 +08007970/**
arthurhungcc7f9802020-04-30 17:55:40 +08007971 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7972 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007973 */
arthurhungcc7f9802020-04-30 17:55:40 +08007974TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007975 addConfigurationProperty("touch.deviceType", "touchScreen");
7976 prepareDisplay(DISPLAY_ORIENTATION_0);
7977 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7978 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7979
7980 NotifyMotionArgs motionArgs;
7981
7982 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007983 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7984 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007985 processPosition(mapper, x1, y1);
7986 processSync(mapper);
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7990
7991 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007992 processSlot(mapper, SECOND_SLOT);
7993 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007994 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007995 processSync(mapper);
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7997 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7998 motionArgs.action);
7999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8000
8001 // If the tool type of the first finger changes to MT_TOOL_PALM,
8002 // we expect to receive ACTION_POINTER_UP with cancel flag.
8003 processSlot(mapper, FIRST_SLOT);
8004 processId(mapper, FIRST_TRACKING_ID);
8005 processToolType(mapper, MT_TOOL_PALM);
8006 processSync(mapper);
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8008 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8009 motionArgs.action);
8010 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8011
8012 // The following MOVE events of second finger should be processed.
8013 processSlot(mapper, SECOND_SLOT);
8014 processId(mapper, SECOND_TRACKING_ID);
8015 processPosition(mapper, x2 + 1, y2 + 1);
8016 processSync(mapper);
8017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8019 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8020
8021 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8022 // it. Second finger receive move.
8023 processSlot(mapper, FIRST_SLOT);
8024 processId(mapper, INVALID_TRACKING_ID);
8025 processSync(mapper);
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8028 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8029
8030 // Second finger keeps moving.
8031 processSlot(mapper, SECOND_SLOT);
8032 processId(mapper, SECOND_TRACKING_ID);
8033 processPosition(mapper, x2 + 2, y2 + 2);
8034 processSync(mapper);
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8037 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8038
8039 // Second finger up.
8040 processId(mapper, INVALID_TRACKING_ID);
8041 processSync(mapper);
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8043 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8044 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8045}
8046
8047/**
8048 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8049 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8050 */
8051TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8052 addConfigurationProperty("touch.deviceType", "touchScreen");
8053 prepareDisplay(DISPLAY_ORIENTATION_0);
8054 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8055 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8056
8057 NotifyMotionArgs motionArgs;
8058
8059 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8060 // First finger down.
8061 processId(mapper, FIRST_TRACKING_ID);
8062 processPosition(mapper, x1, y1);
8063 processSync(mapper);
8064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8065 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8067
8068 // Second finger down.
8069 processSlot(mapper, SECOND_SLOT);
8070 processId(mapper, SECOND_TRACKING_ID);
8071 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008072 processSync(mapper);
8073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8074 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8075 motionArgs.action);
8076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8077
arthurhungcc7f9802020-04-30 17:55:40 +08008078 // If the tool type of the first finger changes to MT_TOOL_PALM,
8079 // we expect to receive ACTION_POINTER_UP with cancel flag.
8080 processSlot(mapper, FIRST_SLOT);
8081 processId(mapper, FIRST_TRACKING_ID);
8082 processToolType(mapper, MT_TOOL_PALM);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8086 motionArgs.action);
8087 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8088
8089 // Second finger keeps moving.
8090 processSlot(mapper, SECOND_SLOT);
8091 processId(mapper, SECOND_TRACKING_ID);
8092 processPosition(mapper, x2 + 1, y2 + 1);
8093 processSync(mapper);
8094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8096
8097 // second finger becomes palm, receive cancel due to only 1 finger is active.
8098 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008099 processToolType(mapper, MT_TOOL_PALM);
8100 processSync(mapper);
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8102 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8103
arthurhungcc7f9802020-04-30 17:55:40 +08008104 // third finger down.
8105 processSlot(mapper, THIRD_SLOT);
8106 processId(mapper, THIRD_TRACKING_ID);
8107 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008108 processPosition(mapper, x3, y3);
8109 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008113 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8114
8115 // third finger move
8116 processId(mapper, THIRD_TRACKING_ID);
8117 processPosition(mapper, x3 + 1, y3 + 1);
8118 processSync(mapper);
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8121
8122 // first finger up, third finger receive move.
8123 processSlot(mapper, FIRST_SLOT);
8124 processId(mapper, INVALID_TRACKING_ID);
8125 processSync(mapper);
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8128 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8129
8130 // second finger up, third finger receive move.
8131 processSlot(mapper, SECOND_SLOT);
8132 processId(mapper, INVALID_TRACKING_ID);
8133 processSync(mapper);
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8136 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8137
8138 // third finger up.
8139 processSlot(mapper, THIRD_SLOT);
8140 processId(mapper, INVALID_TRACKING_ID);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8144 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8145}
8146
8147/**
8148 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8149 * and the active finger could still be allowed to receive the events
8150 */
8151TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8152 addConfigurationProperty("touch.deviceType", "touchScreen");
8153 prepareDisplay(DISPLAY_ORIENTATION_0);
8154 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8155 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8156
8157 NotifyMotionArgs motionArgs;
8158
8159 // default tool type is finger
8160 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8161 processId(mapper, FIRST_TRACKING_ID);
8162 processPosition(mapper, x1, y1);
8163 processSync(mapper);
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8165 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8167
8168 // Second finger down.
8169 processSlot(mapper, SECOND_SLOT);
8170 processId(mapper, SECOND_TRACKING_ID);
8171 processPosition(mapper, x2, y2);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8175 motionArgs.action);
8176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8177
8178 // If the tool type of the second finger changes to MT_TOOL_PALM,
8179 // we expect to receive ACTION_POINTER_UP with cancel flag.
8180 processId(mapper, SECOND_TRACKING_ID);
8181 processToolType(mapper, MT_TOOL_PALM);
8182 processSync(mapper);
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8185 motionArgs.action);
8186 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8187
8188 // The following MOVE event should be processed.
8189 processSlot(mapper, FIRST_SLOT);
8190 processId(mapper, FIRST_TRACKING_ID);
8191 processPosition(mapper, x1 + 1, y1 + 1);
8192 processSync(mapper);
8193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8195 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8196
8197 // second finger up.
8198 processSlot(mapper, SECOND_SLOT);
8199 processId(mapper, INVALID_TRACKING_ID);
8200 processSync(mapper);
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8203
8204 // first finger keep moving
8205 processSlot(mapper, FIRST_SLOT);
8206 processId(mapper, FIRST_TRACKING_ID);
8207 processPosition(mapper, x1 + 2, y1 + 2);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8211
8212 // first finger up.
8213 processId(mapper, INVALID_TRACKING_ID);
8214 processSync(mapper);
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8216 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8217 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008218}
8219
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008220// --- MultiTouchInputMapperTest_ExternalDevice ---
8221
8222class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8223protected:
Chris Yea52ade12020-08-27 16:49:20 -07008224 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008225};
8226
8227/**
8228 * Expect fallback to internal viewport if device is external and external viewport is not present.
8229 */
8230TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8231 prepareAxes(POSITION);
8232 addConfigurationProperty("touch.deviceType", "touchScreen");
8233 prepareDisplay(DISPLAY_ORIENTATION_0);
8234 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8235
8236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8237
8238 NotifyMotionArgs motionArgs;
8239
8240 // Expect the event to be sent to the internal viewport,
8241 // because an external viewport is not present.
8242 processPosition(mapper, 100, 100);
8243 processSync(mapper);
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8245 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8246
8247 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008248 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008249 processPosition(mapper, 100, 100);
8250 processSync(mapper);
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8252 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8253}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008254
8255/**
8256 * Test touch should not work if outside of surface.
8257 */
8258class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8259protected:
8260 void halfDisplayToCenterHorizontal(int32_t orientation) {
8261 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008262 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008263
8264 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8265 internalViewport->orientation = orientation;
8266 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8267 internalViewport->logicalLeft = 0;
8268 internalViewport->logicalTop = 0;
8269 internalViewport->logicalRight = DISPLAY_HEIGHT;
8270 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8271
8272 internalViewport->physicalLeft = 0;
8273 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8274 internalViewport->physicalRight = DISPLAY_HEIGHT;
8275 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8276
8277 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8278 internalViewport->deviceHeight = DISPLAY_WIDTH;
8279 } else {
8280 internalViewport->logicalLeft = 0;
8281 internalViewport->logicalTop = 0;
8282 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8283 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8284
8285 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8286 internalViewport->physicalTop = 0;
8287 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8288 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8289
8290 internalViewport->deviceWidth = DISPLAY_WIDTH;
8291 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8292 }
8293
8294 mFakePolicy->updateViewport(internalViewport.value());
8295 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8296 }
8297
arthurhung5d547942020-12-14 17:04:45 +08008298 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8299 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008300 int32_t yExpected) {
8301 // touch on outside area should not work.
8302 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8305
8306 // touch on inside area should receive the event.
8307 NotifyMotionArgs args;
8308 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8309 processSync(mapper);
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8311 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8312 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8313
8314 // Reset.
8315 mapper.reset(ARBITRARY_TIME);
8316 }
8317};
8318
arthurhung5d547942020-12-14 17:04:45 +08008319TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008320 addConfigurationProperty("touch.deviceType", "touchScreen");
8321 prepareDisplay(DISPLAY_ORIENTATION_0);
8322 prepareAxes(POSITION);
8323 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8324
8325 // Touch on center of normal display should work.
8326 const int32_t x = DISPLAY_WIDTH / 4;
8327 const int32_t y = DISPLAY_HEIGHT / 2;
8328 processPosition(mapper, toRawX(x), toRawY(y));
8329 processSync(mapper);
8330 NotifyMotionArgs args;
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8333 0.0f, 0.0f, 0.0f, 0.0f));
8334 // Reset.
8335 mapper.reset(ARBITRARY_TIME);
8336
8337 // Let physical display be different to device, and make surface and physical could be 1:1.
8338 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8339
8340 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8341 const int32_t yExpected = y;
8342 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8343}
8344
arthurhung5d547942020-12-14 17:04:45 +08008345TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008346 addConfigurationProperty("touch.deviceType", "touchScreen");
8347 prepareDisplay(DISPLAY_ORIENTATION_0);
8348 prepareAxes(POSITION);
8349 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8350
8351 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8352 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8353
8354 const int32_t x = DISPLAY_WIDTH / 4;
8355 const int32_t y = DISPLAY_HEIGHT / 2;
8356
8357 // expect x/y = swap x/y then reverse y.
8358 const int32_t xExpected = y;
8359 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8360 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8361}
8362
arthurhung5d547942020-12-14 17:04:45 +08008363TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008364 addConfigurationProperty("touch.deviceType", "touchScreen");
8365 prepareDisplay(DISPLAY_ORIENTATION_0);
8366 prepareAxes(POSITION);
8367 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8368
8369 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8370 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8371
8372 const int32_t x = DISPLAY_WIDTH / 4;
8373 const int32_t y = DISPLAY_HEIGHT / 2;
8374
8375 // expect x/y = swap x/y then reverse x.
8376 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8377 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8378 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8379}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008380
arthurhunga36b28e2020-12-29 20:28:15 +08008381TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8382 addConfigurationProperty("touch.deviceType", "touchScreen");
8383 prepareDisplay(DISPLAY_ORIENTATION_0);
8384 prepareAxes(POSITION);
8385 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8386
8387 const int32_t x = 0;
8388 const int32_t y = 0;
8389
8390 const int32_t xExpected = x;
8391 const int32_t yExpected = y;
8392 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8393
8394 clearViewports();
8395 prepareDisplay(DISPLAY_ORIENTATION_90);
8396 // expect x/y = swap x/y then reverse y.
8397 const int32_t xExpected90 = y;
8398 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8399 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8400
8401 clearViewports();
8402 prepareDisplay(DISPLAY_ORIENTATION_270);
8403 // expect x/y = swap x/y then reverse x.
8404 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8405 const int32_t yExpected270 = x;
8406 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8407}
8408
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008409TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8410 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8411 std::shared_ptr<FakePointerController> fakePointerController =
8412 std::make_shared<FakePointerController>();
8413 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8414 fakePointerController->setPosition(0, 0);
8415 fakePointerController->setButtonState(0);
8416
8417 // prepare device and capture
8418 prepareDisplay(DISPLAY_ORIENTATION_0);
8419 prepareAxes(POSITION | ID | SLOT);
8420 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8421 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8422 mFakePolicy->setPointerCapture(true);
8423 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8424 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8425
8426 // captured touchpad should be a touchpad source
8427 NotifyDeviceResetArgs resetArgs;
8428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8429 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8430
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008431 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008432
8433 const InputDeviceInfo::MotionRange* relRangeX =
8434 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8435 ASSERT_NE(relRangeX, nullptr);
8436 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8437 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8438 const InputDeviceInfo::MotionRange* relRangeY =
8439 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8440 ASSERT_NE(relRangeY, nullptr);
8441 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8442 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8443
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008444 // run captured pointer tests - note that this is unscaled, so input listener events should be
8445 // identical to what the hardware sends (accounting for any
8446 // calibration).
8447 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008448 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008449 processId(mapper, 1);
8450 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8451 processKey(mapper, BTN_TOUCH, 1);
8452 processSync(mapper);
8453
8454 // expect coord[0] to contain initial location of touch 0
8455 NotifyMotionArgs args;
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8457 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8458 ASSERT_EQ(1U, args.pointerCount);
8459 ASSERT_EQ(0, args.pointerProperties[0].id);
8460 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8461 ASSERT_NO_FATAL_FAILURE(
8462 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8463
8464 // FINGER 1 DOWN
8465 processSlot(mapper, 1);
8466 processId(mapper, 2);
8467 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8468 processSync(mapper);
8469
8470 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008472 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8473 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008474 ASSERT_EQ(2U, args.pointerCount);
8475 ASSERT_EQ(0, args.pointerProperties[0].id);
8476 ASSERT_EQ(1, args.pointerProperties[1].id);
8477 ASSERT_NO_FATAL_FAILURE(
8478 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8479 ASSERT_NO_FATAL_FAILURE(
8480 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8481
8482 // FINGER 1 MOVE
8483 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8484 processSync(mapper);
8485
8486 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8487 // from move
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8490 ASSERT_NO_FATAL_FAILURE(
8491 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8492 ASSERT_NO_FATAL_FAILURE(
8493 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8494
8495 // FINGER 0 MOVE
8496 processSlot(mapper, 0);
8497 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8498 processSync(mapper);
8499
8500 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8503 ASSERT_NO_FATAL_FAILURE(
8504 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8505 ASSERT_NO_FATAL_FAILURE(
8506 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8507
8508 // BUTTON DOWN
8509 processKey(mapper, BTN_LEFT, 1);
8510 processSync(mapper);
8511
8512 // touchinputmapper design sends a move before button press
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8517
8518 // BUTTON UP
8519 processKey(mapper, BTN_LEFT, 0);
8520 processSync(mapper);
8521
8522 // touchinputmapper design sends a move after button release
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8527
8528 // FINGER 0 UP
8529 processId(mapper, -1);
8530 processSync(mapper);
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8532 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8533
8534 // FINGER 1 MOVE
8535 processSlot(mapper, 1);
8536 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8537 processSync(mapper);
8538
8539 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8542 ASSERT_EQ(1U, args.pointerCount);
8543 ASSERT_EQ(1, args.pointerProperties[0].id);
8544 ASSERT_NO_FATAL_FAILURE(
8545 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8546
8547 // FINGER 1 UP
8548 processId(mapper, -1);
8549 processKey(mapper, BTN_TOUCH, 0);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8553
8554 // non captured touchpad should be a mouse source
8555 mFakePolicy->setPointerCapture(false);
8556 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8558 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8559}
8560
8561TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8562 std::shared_ptr<FakePointerController> fakePointerController =
8563 std::make_shared<FakePointerController>();
8564 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8565 fakePointerController->setPosition(0, 0);
8566 fakePointerController->setButtonState(0);
8567
8568 // prepare device and capture
8569 prepareDisplay(DISPLAY_ORIENTATION_0);
8570 prepareAxes(POSITION | ID | SLOT);
8571 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8572 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8573 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8574 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8575 // run uncaptured pointer tests - pushes out generic events
8576 // FINGER 0 DOWN
8577 processId(mapper, 3);
8578 processPosition(mapper, 100, 100);
8579 processKey(mapper, BTN_TOUCH, 1);
8580 processSync(mapper);
8581
8582 // start at (100,100), cursor should be at (0,0) * scale
8583 NotifyMotionArgs args;
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8586 ASSERT_NO_FATAL_FAILURE(
8587 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8588
8589 // FINGER 0 MOVE
8590 processPosition(mapper, 200, 200);
8591 processSync(mapper);
8592
8593 // compute scaling to help with touch position checking
8594 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8595 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8596 float scale =
8597 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8598
8599 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8603 0, 0, 0, 0, 0, 0, 0));
8604}
8605
8606TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8607 std::shared_ptr<FakePointerController> fakePointerController =
8608 std::make_shared<FakePointerController>();
8609
8610 prepareDisplay(DISPLAY_ORIENTATION_0);
8611 prepareAxes(POSITION | ID | SLOT);
8612 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8613 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8614 mFakePolicy->setPointerCapture(false);
8615 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8616
8617 // uncaptured touchpad should be a pointer device
8618 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8619
8620 // captured touchpad should be a touchpad device
8621 mFakePolicy->setPointerCapture(true);
8622 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8623 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8624}
8625
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008626// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008627
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008628class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008629protected:
8630 static const char* DEVICE_NAME;
8631 static const char* DEVICE_LOCATION;
8632 static const int32_t DEVICE_ID;
8633 static const int32_t DEVICE_GENERATION;
8634 static const int32_t DEVICE_CONTROLLER_NUMBER;
8635 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8636 static const int32_t EVENTHUB_ID;
8637
8638 std::shared_ptr<FakeEventHub> mFakeEventHub;
8639 sp<FakeInputReaderPolicy> mFakePolicy;
8640 sp<TestInputListener> mFakeListener;
8641 std::unique_ptr<InstrumentedInputReader> mReader;
8642 std::shared_ptr<InputDevice> mDevice;
8643
8644 virtual void SetUp(Flags<InputDeviceClass> classes) {
8645 mFakeEventHub = std::make_unique<FakeEventHub>();
8646 mFakePolicy = new FakeInputReaderPolicy();
8647 mFakeListener = new TestInputListener();
8648 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8649 mFakeListener);
8650 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8651 }
8652
8653 void SetUp() override { SetUp(DEVICE_CLASSES); }
8654
8655 void TearDown() override {
8656 mFakeListener.clear();
8657 mFakePolicy.clear();
8658 }
8659
8660 void configureDevice(uint32_t changes) {
8661 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8662 mReader->requestRefreshConfiguration(changes);
8663 mReader->loopOnce();
8664 }
8665 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8666 }
8667
8668 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8669 const std::string& location, int32_t eventHubId,
8670 Flags<InputDeviceClass> classes) {
8671 InputDeviceIdentifier identifier;
8672 identifier.name = name;
8673 identifier.location = location;
8674 std::shared_ptr<InputDevice> device =
8675 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8676 identifier);
8677 mReader->pushNextDevice(device);
8678 mFakeEventHub->addDevice(eventHubId, name, classes);
8679 mReader->loopOnce();
8680 return device;
8681 }
8682
8683 template <class T, typename... Args>
8684 T& addControllerAndConfigure(Args... args) {
8685 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8686
8687 return controller;
8688 }
8689};
8690
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008691const char* PeripheralControllerTest::DEVICE_NAME = "device";
8692const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8693const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8694const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8695const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8696const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008697 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008698const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008699
8700// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008701class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008702protected:
8703 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008704 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008705 }
8706};
8707
8708TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008709 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008710
8711 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8712 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8713}
8714
8715TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008716 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008717
8718 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8719 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8720}
8721
8722// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008723class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008724protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008725 void SetUp() override {
8726 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8727 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008728};
8729
Chris Ye85758332021-05-16 23:05:17 -07008730TEST_F(LightControllerTest, MonoLight) {
8731 RawLightInfo infoMono = {.id = 1,
8732 .name = "Mono",
8733 .maxBrightness = 255,
8734 .flags = InputLightClass::BRIGHTNESS,
8735 .path = ""};
8736 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008737
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008738 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008739 InputDeviceInfo info;
8740 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008741 std::vector<InputDeviceLightInfo> lights = info.getLights();
8742 ASSERT_EQ(1U, lights.size());
8743 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008744
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008745 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8746 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008747}
8748
8749TEST_F(LightControllerTest, RGBLight) {
8750 RawLightInfo infoRed = {.id = 1,
8751 .name = "red",
8752 .maxBrightness = 255,
8753 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8754 .path = ""};
8755 RawLightInfo infoGreen = {.id = 2,
8756 .name = "green",
8757 .maxBrightness = 255,
8758 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8759 .path = ""};
8760 RawLightInfo infoBlue = {.id = 3,
8761 .name = "blue",
8762 .maxBrightness = 255,
8763 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8764 .path = ""};
8765 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8766 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8767 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8768
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008769 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008770 InputDeviceInfo info;
8771 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008772 std::vector<InputDeviceLightInfo> lights = info.getLights();
8773 ASSERT_EQ(1U, lights.size());
8774 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008775
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008776 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8777 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008778}
8779
8780TEST_F(LightControllerTest, MultiColorRGBLight) {
8781 RawLightInfo infoColor = {.id = 1,
8782 .name = "red",
8783 .maxBrightness = 255,
8784 .flags = InputLightClass::BRIGHTNESS |
8785 InputLightClass::MULTI_INTENSITY |
8786 InputLightClass::MULTI_INDEX,
8787 .path = ""};
8788
8789 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8790
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008791 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008792 InputDeviceInfo info;
8793 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008794 std::vector<InputDeviceLightInfo> lights = info.getLights();
8795 ASSERT_EQ(1U, lights.size());
8796 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008797
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008798 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8799 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008800}
8801
8802TEST_F(LightControllerTest, PlayerIdLight) {
8803 RawLightInfo info1 = {.id = 1,
8804 .name = "player1",
8805 .maxBrightness = 255,
8806 .flags = InputLightClass::BRIGHTNESS,
8807 .path = ""};
8808 RawLightInfo info2 = {.id = 2,
8809 .name = "player2",
8810 .maxBrightness = 255,
8811 .flags = InputLightClass::BRIGHTNESS,
8812 .path = ""};
8813 RawLightInfo info3 = {.id = 3,
8814 .name = "player3",
8815 .maxBrightness = 255,
8816 .flags = InputLightClass::BRIGHTNESS,
8817 .path = ""};
8818 RawLightInfo info4 = {.id = 4,
8819 .name = "player4",
8820 .maxBrightness = 255,
8821 .flags = InputLightClass::BRIGHTNESS,
8822 .path = ""};
8823 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8824 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8825 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8826 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8827
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008828 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008829 InputDeviceInfo info;
8830 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008831 std::vector<InputDeviceLightInfo> lights = info.getLights();
8832 ASSERT_EQ(1U, lights.size());
8833 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008834
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008835 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8836 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
8837 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008838}
8839
Michael Wrightd02c5b62014-02-10 15:10:22 -08008840} // namespace android