blob: 7a11ca73961ad235dbeaae1a2eb4eef5f6438794 [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, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001528 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
Chris Ye98d3f532020-10-01 21:48:59 -07001532 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001533 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001534 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001535 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1537 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1538 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001539}
1540
1541TEST_F(InputReaderTest, PolicyGetInputDevices) {
1542 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1543 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1544 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001545
1546 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001547 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001548 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001549 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001550 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001551 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1552 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1553 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1554}
1555
Chris Yee7310032020-09-22 15:36:28 -07001556TEST_F(InputReaderTest, GetMergedInputDevices) {
1557 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1558 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1559 // Add two subdevices to device
1560 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1561 // Must add at least one mapper or the device will be ignored!
1562 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1563 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1564
1565 // Push same device instance for next device to be added, so they'll have same identifier.
1566 mReader->pushNextDevice(device);
1567 mReader->pushNextDevice(device);
1568 ASSERT_NO_FATAL_FAILURE(
1569 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1570 ASSERT_NO_FATAL_FAILURE(
1571 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1572
1573 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001574 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001575}
1576
Chris Yee14523a2020-12-19 13:46:00 -08001577TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1578 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1579 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1580 // Add two subdevices to device
1581 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1582 // Must add at least one mapper or the device will be ignored!
1583 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1584 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1585
1586 // Push same device instance for next device to be added, so they'll have same identifier.
1587 mReader->pushNextDevice(device);
1588 mReader->pushNextDevice(device);
1589 // Sensor device is initially disabled
1590 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1591 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1592 nullptr));
1593 // Device is disabled because the only sub device is a sensor device and disabled initially.
1594 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1595 ASSERT_FALSE(device->isEnabled());
1596 ASSERT_NO_FATAL_FAILURE(
1597 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1598 // The merged device is enabled if any sub device is enabled
1599 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1600 ASSERT_TRUE(device->isEnabled());
1601}
1602
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001604 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001605 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 constexpr int32_t eventHubId = 1;
1607 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001608 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001609 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001610 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001612
Yi Kong9b14ac62018-07-17 13:48:38 -07001613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001614
1615 NotifyDeviceResetArgs resetArgs;
1616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001617 ASSERT_EQ(deviceId, resetArgs.deviceId);
1618
1619 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001620 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001621 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001622
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001624 ASSERT_EQ(deviceId, resetArgs.deviceId);
1625 ASSERT_EQ(device->isEnabled(), false);
1626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001631 ASSERT_EQ(device->isEnabled(), false);
1632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001633 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001634 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001636 ASSERT_EQ(deviceId, resetArgs.deviceId);
1637 ASSERT_EQ(device->isEnabled(), true);
1638}
1639
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001642 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001644 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001646 AINPUT_SOURCE_KEYBOARD, nullptr);
1647 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648
1649 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1650 AINPUT_SOURCE_ANY, AKEYCODE_A))
1651 << "Should return unknown when the device id is >= 0 but unknown.";
1652
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1654 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1655 << "Should return unknown when the device id is valid but the sources are not "
1656 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 ASSERT_EQ(AKEY_STATE_DOWN,
1659 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1660 AKEYCODE_A))
1661 << "Should return value provided by mapper when device id is valid and the device "
1662 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663
1664 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1665 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1666 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1667
1668 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1669 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1670 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1671}
1672
1673TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001674 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001675 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001677 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001679 AINPUT_SOURCE_KEYBOARD, nullptr);
1680 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681
1682 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1683 AINPUT_SOURCE_ANY, KEY_A))
1684 << "Should return unknown when the device id is >= 0 but unknown.";
1685
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1687 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1688 << "Should return unknown when the device id is valid but the sources are not "
1689 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 ASSERT_EQ(AKEY_STATE_DOWN,
1692 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1693 KEY_A))
1694 << "Should return value provided by mapper when device id is valid and the device "
1695 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696
1697 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1698 AINPUT_SOURCE_TRACKBALL, KEY_A))
1699 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1700
1701 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1702 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1703 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1704}
1705
1706TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001708 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001712 AINPUT_SOURCE_KEYBOARD, nullptr);
1713 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714
1715 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1716 AINPUT_SOURCE_ANY, SW_LID))
1717 << "Should return unknown when the device id is >= 0 but unknown.";
1718
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001719 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1720 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1721 << "Should return unknown when the device id is valid but the sources are not "
1722 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001723
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 ASSERT_EQ(AKEY_STATE_DOWN,
1725 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1726 SW_LID))
1727 << "Should return value provided by mapper when device id is valid and the device "
1728 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
1730 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1731 AINPUT_SOURCE_TRACKBALL, SW_LID))
1732 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1733
1734 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1735 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1736 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1737}
1738
1739TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001741 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001747 mapper.addSupportedKeyCode(AKEYCODE_A);
1748 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001749
1750 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1751 uint8_t flags[4] = { 0, 0, 0, 1 };
1752
1753 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1754 << "Should return false when device id is >= 0 but unknown.";
1755 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1756
1757 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001758 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1759 << "Should return false when device id is valid but the sources are not supported by "
1760 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001761 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1762
1763 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001764 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1765 keyCodes, flags))
1766 << "Should return value provided by mapper when device id is valid and the device "
1767 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1769
1770 flags[3] = 1;
1771 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1772 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1773 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1774
1775 flags[3] = 1;
1776 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1777 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1778 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1779}
1780
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001781TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001783 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784
1785 NotifyConfigurationChangedArgs args;
1786
1787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1788 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1789}
1790
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001791TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001792 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001793 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001794 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001795 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001796 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001797 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001799 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001800
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001801 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001802 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1804
1805 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001806 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001807 ASSERT_EQ(when, event.when);
1808 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001809 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810 ASSERT_EQ(EV_KEY, event.type);
1811 ASSERT_EQ(KEY_A, event.code);
1812 ASSERT_EQ(1, event.value);
1813}
1814
Garfield Tan1c7bc862020-01-28 13:24:04 -08001815TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001817 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 constexpr int32_t eventHubId = 1;
1819 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001820 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001822 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001824
1825 NotifyDeviceResetArgs resetArgs;
1826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001827 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001829 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001830 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001832 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001833 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001835 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001836 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001838 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001839 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001841 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001842 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001844 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001845 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001846}
1847
Garfield Tan1c7bc862020-01-28 13:24:04 -08001848TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1849 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001850 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001851 constexpr int32_t eventHubId = 1;
1852 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1853 // Must add at least one mapper or the device will be ignored!
1854 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001855 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001856 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1857
1858 NotifyDeviceResetArgs resetArgs;
1859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1860 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1861}
1862
Arthur Hungc23540e2018-11-29 20:42:11 +08001863TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001864 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001865 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001867 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1869 FakeInputMapper& mapper =
1870 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001871 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001872
1873 const uint8_t hdmi1 = 1;
1874
1875 // Associated touch screen with second display.
1876 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1877
1878 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001879 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001880 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001881 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001882 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001883 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001884 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001885 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001886 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001887 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001888
1889 // Add the device, and make sure all of the callbacks are triggered.
1890 // The device is added after the input port associations are processed since
1891 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001895 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001896
Arthur Hung2c9a3342019-07-23 14:18:59 +08001897 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001898 ASSERT_EQ(deviceId, device->getId());
1899 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1900 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001901
1902 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001903 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001904 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001905 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001906}
1907
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001908TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1909 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1910 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1911 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1912 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1913 // Must add at least one mapper or the device will be ignored!
1914 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1915 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1916 mReader->pushNextDevice(device);
1917 mReader->pushNextDevice(device);
1918 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1919 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1920
1921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1922
1923 NotifyDeviceResetArgs resetArgs;
1924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1925 ASSERT_EQ(deviceId, resetArgs.deviceId);
1926 ASSERT_TRUE(device->isEnabled());
1927 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1928 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1929
1930 disableDevice(deviceId);
1931 mReader->loopOnce();
1932
1933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1934 ASSERT_EQ(deviceId, resetArgs.deviceId);
1935 ASSERT_FALSE(device->isEnabled());
1936 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1937 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1938
1939 enableDevice(deviceId);
1940 mReader->loopOnce();
1941
1942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1943 ASSERT_EQ(deviceId, resetArgs.deviceId);
1944 ASSERT_TRUE(device->isEnabled());
1945 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1946 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1947}
1948
1949TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1951 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1952 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1953 // Add two subdevices to device
1954 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1955 FakeInputMapper& mapperDevice1 =
1956 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1957 FakeInputMapper& mapperDevice2 =
1958 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1959 mReader->pushNextDevice(device);
1960 mReader->pushNextDevice(device);
1961 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1962 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1963
1964 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1965 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1966
1967 ASSERT_EQ(AKEY_STATE_DOWN,
1968 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1969 ASSERT_EQ(AKEY_STATE_DOWN,
1970 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1971 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1972 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1973}
1974
Prabir Pradhan7e186182020-11-10 13:56:45 -08001975TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1976 NotifyPointerCaptureChangedArgs args;
1977
1978 mFakePolicy->setPointerCapture(true);
1979 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1980 mReader->loopOnce();
1981 mFakeListener->assertNotifyCaptureWasCalled(&args);
1982 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1983
1984 mFakePolicy->setPointerCapture(false);
1985 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1986 mReader->loopOnce();
1987 mFakeListener->assertNotifyCaptureWasCalled(&args);
1988 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1989
1990 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1991 // does not change.
1992 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1993 mReader->loopOnce();
1994 mFakeListener->assertNotifyCaptureWasCalled(&args);
1995 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1996}
1997
Chris Ye87143712020-11-10 05:05:58 +00001998class FakeVibratorInputMapper : public FakeInputMapper {
1999public:
2000 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2001 : FakeInputMapper(deviceContext, sources) {}
2002
2003 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2004};
2005
2006TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2007 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2008 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2009 constexpr int32_t eventHubId = 1;
2010 const char* DEVICE_LOCATION = "BLUETOOTH";
2011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2012 FakeVibratorInputMapper& mapper =
2013 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2014 mReader->pushNextDevice(device);
2015
2016 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2017 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2018
2019 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2020 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2021}
2022
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002023// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002024
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002025class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002026public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002027 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002028
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002029 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002030
2031 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2032
2033 void dump(std::string& dump) override {}
2034
2035 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2036 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002037 }
2038
Chris Yee2b1e5c2021-03-10 22:45:12 -08002039 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2040 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002041 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002042
2043 bool setLightColor(int32_t lightId, int32_t color) override {
2044 getDeviceContext().setLightBrightness(lightId, color >> 24);
2045 return true;
2046 }
2047
2048 std::optional<int32_t> getLightColor(int32_t lightId) override {
2049 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2050 if (!result.has_value()) {
2051 return std::nullopt;
2052 }
2053 return result.value() << 24;
2054 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002055
2056 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2057
2058 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2059
2060private:
2061 InputDeviceContext& mDeviceContext;
2062 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2063 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002064};
2065
Chris Yee2b1e5c2021-03-10 22:45:12 -08002066TEST_F(InputReaderTest, BatteryGetCapacity) {
2067 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2068 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2069 constexpr int32_t eventHubId = 1;
2070 const char* DEVICE_LOCATION = "BLUETOOTH";
2071 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002072 FakePeripheralController& controller =
2073 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002074 mReader->pushNextDevice(device);
2075
2076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2077
2078 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2079 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2080}
2081
2082TEST_F(InputReaderTest, BatteryGetStatus) {
2083 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2084 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2085 constexpr int32_t eventHubId = 1;
2086 const char* DEVICE_LOCATION = "BLUETOOTH";
2087 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002088 FakePeripheralController& controller =
2089 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002090 mReader->pushNextDevice(device);
2091
2092 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2093
2094 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2095 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2096}
2097
Chris Ye3fdbfef2021-01-06 18:45:18 -08002098TEST_F(InputReaderTest, LightGetColor) {
2099 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2100 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2101 constexpr int32_t eventHubId = 1;
2102 const char* DEVICE_LOCATION = "BLUETOOTH";
2103 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002104 FakePeripheralController& controller =
2105 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002106 mReader->pushNextDevice(device);
2107 RawLightInfo info = {.id = 1,
2108 .name = "Mono",
2109 .maxBrightness = 255,
2110 .flags = InputLightClass::BRIGHTNESS,
2111 .path = ""};
2112 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2113 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2114
2115 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002116
Chris Yee2b1e5c2021-03-10 22:45:12 -08002117 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2118 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002119 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2120 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2121}
2122
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002123// --- InputReaderIntegrationTest ---
2124
2125// These tests create and interact with the InputReader only through its interface.
2126// The InputReader is started during SetUp(), which starts its processing in its own
2127// thread. The tests use linux uinput to emulate input devices.
2128// NOTE: Interacting with the physical device while these tests are running may cause
2129// the tests to fail.
2130class InputReaderIntegrationTest : public testing::Test {
2131protected:
2132 sp<TestInputListener> mTestListener;
2133 sp<FakeInputReaderPolicy> mFakePolicy;
2134 sp<InputReaderInterface> mReader;
2135
Chris Yea52ade12020-08-27 16:49:20 -07002136 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002137 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002138 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2139 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002140
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002141 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002142 ASSERT_EQ(mReader->start(), OK);
2143
2144 // Since this test is run on a real device, all the input devices connected
2145 // to the test device will show up in mReader. We wait for those input devices to
2146 // show up before beginning the tests.
2147 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2148 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2149 }
2150
Chris Yea52ade12020-08-27 16:49:20 -07002151 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002152 ASSERT_EQ(mReader->stop(), OK);
2153 mTestListener.clear();
2154 mFakePolicy.clear();
2155 }
2156};
2157
2158TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2159 // An invalid input device that is only used for this test.
2160 class InvalidUinputDevice : public UinputDevice {
2161 public:
2162 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2163
2164 private:
2165 void configureDevice(int fd, uinput_user_dev* device) override {}
2166 };
2167
2168 const size_t numDevices = mFakePolicy->getInputDevices().size();
2169
2170 // UinputDevice does not set any event or key bits, so InputReader should not
2171 // consider it as a valid device.
2172 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2173 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2174 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2175 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2176
2177 invalidDevice.reset();
2178 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2179 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2180 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2181}
2182
2183TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2184 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2185
2186 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2187 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2188 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2189 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2190
2191 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002192 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2193 const auto& it =
2194 std::find_if(inputDevices.begin(), inputDevices.end(),
2195 [&keyboard](const InputDeviceInfo& info) {
2196 return info.getIdentifier().name == keyboard->getName();
2197 });
2198
2199 ASSERT_NE(it, inputDevices.end());
2200 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2201 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2202 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002203
2204 keyboard.reset();
2205 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2206 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2207 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2208}
2209
2210TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2211 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2212 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2213
2214 NotifyConfigurationChangedArgs configChangedArgs;
2215 ASSERT_NO_FATAL_FAILURE(
2216 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002217 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002218 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2219
2220 NotifyKeyArgs keyArgs;
2221 keyboard->pressAndReleaseHomeKey();
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2223 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002224 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002225 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002226 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002227 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002228 prevTimestamp = keyArgs.eventTime;
2229
2230 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2231 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002232 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002233 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002234 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002235}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002236
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002237/**
2238 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2239 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2240 * are passed to the listener.
2241 */
2242static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2243TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2244 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2245 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2246 NotifyKeyArgs keyArgs;
2247
2248 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2249 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2250 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2251 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2252
2253 controller->pressAndReleaseKey(BTN_GEAR_UP);
2254 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2255 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2256 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2257}
2258
Arthur Hungaab25622020-01-16 11:22:11 +08002259// --- TouchProcessTest ---
2260class TouchIntegrationTest : public InputReaderIntegrationTest {
2261protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002262 const std::string UNIQUE_ID = "local:0";
2263
Chris Yea52ade12020-08-27 16:49:20 -07002264 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002265 InputReaderIntegrationTest::SetUp();
2266 // At least add an internal display.
2267 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2268 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002269 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002270
2271 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2272 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2273 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2274 }
2275
2276 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2277 int32_t orientation, const std::string& uniqueId,
2278 std::optional<uint8_t> physicalPort,
2279 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002280 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2281 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002282 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2283 }
2284
2285 std::unique_ptr<UinputTouchScreen> mDevice;
2286};
2287
2288TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2289 NotifyMotionArgs args;
2290 const Point centerPoint = mDevice->getCenterPoint();
2291
2292 // ACTION_DOWN
2293 mDevice->sendDown(centerPoint);
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2296
2297 // ACTION_MOVE
2298 mDevice->sendMove(centerPoint + Point(1, 1));
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2301
2302 // ACTION_UP
2303 mDevice->sendUp();
2304 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2305 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2306}
2307
2308TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2309 NotifyMotionArgs args;
2310 const Point centerPoint = mDevice->getCenterPoint();
2311
2312 // ACTION_DOWN
2313 mDevice->sendDown(centerPoint);
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2315 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2316
2317 // ACTION_POINTER_DOWN (Second slot)
2318 const Point secondPoint = centerPoint + Point(100, 100);
2319 mDevice->sendSlot(SECOND_SLOT);
2320 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2321 mDevice->sendDown(secondPoint + Point(1, 1));
2322 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2323 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2324 args.action);
2325
2326 // ACTION_MOVE (Second slot)
2327 mDevice->sendMove(secondPoint);
2328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2330
2331 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002332 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002333 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002334 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002335 args.action);
2336
2337 // ACTION_UP
2338 mDevice->sendSlot(FIRST_SLOT);
2339 mDevice->sendUp();
2340 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2341 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2342}
2343
2344TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2345 NotifyMotionArgs args;
2346 const Point centerPoint = mDevice->getCenterPoint();
2347
2348 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002349 mDevice->sendSlot(FIRST_SLOT);
2350 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002351 mDevice->sendDown(centerPoint);
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2353 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2354
arthurhungcc7f9802020-04-30 17:55:40 +08002355 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002356 const Point secondPoint = centerPoint + Point(100, 100);
2357 mDevice->sendSlot(SECOND_SLOT);
2358 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2359 mDevice->sendDown(secondPoint);
2360 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2361 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2362 args.action);
2363
arthurhungcc7f9802020-04-30 17:55:40 +08002364 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002365 mDevice->sendMove(secondPoint + Point(1, 1));
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2368
arthurhungcc7f9802020-04-30 17:55:40 +08002369 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2370 // a palm event.
2371 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002372 mDevice->sendToolType(MT_TOOL_PALM);
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002374 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2375 args.action);
2376 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002377
arthurhungcc7f9802020-04-30 17:55:40 +08002378 // Send up to second slot, expect first slot send moving.
2379 mDevice->sendPointerUp();
2380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002382
arthurhungcc7f9802020-04-30 17:55:40 +08002383 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002384 mDevice->sendSlot(FIRST_SLOT);
2385 mDevice->sendUp();
2386
arthurhungcc7f9802020-04-30 17:55:40 +08002387 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2388 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002389}
2390
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392class InputDeviceTest : public testing::Test {
2393protected:
2394 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002395 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396 static const int32_t DEVICE_ID;
2397 static const int32_t DEVICE_GENERATION;
2398 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002399 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002400 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002402 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002404 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002405 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002406 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407
Chris Yea52ade12020-08-27 16:49:20 -07002408 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002409 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002411 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002412 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2413 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414 InputDeviceIdentifier identifier;
2415 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002416 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002417 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002418 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002419 mReader->pushNextDevice(mDevice);
2420 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2421 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422 }
2423
Chris Yea52ade12020-08-27 16:49:20 -07002424 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425 mFakeListener.clear();
2426 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 }
2428};
2429
2430const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002431const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002432const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2434const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002435const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2436 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002437const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438
2439TEST_F(InputDeviceTest, ImmutableProperties) {
2440 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002441 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002442 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443}
2444
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002445TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2446 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002447}
2448
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2450 // Configuration.
2451 InputReaderConfiguration config;
2452 mDevice->configure(ARBITRARY_TIME, &config, 0);
2453
2454 // Reset.
2455 mDevice->reset(ARBITRARY_TIME);
2456
2457 NotifyDeviceResetArgs resetArgs;
2458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2459 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2460 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2461
2462 // Metadata.
2463 ASSERT_TRUE(mDevice->isIgnored());
2464 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2465
2466 InputDeviceInfo info;
2467 mDevice->getDeviceInfo(&info);
2468 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002469 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2471 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2472
2473 // State queries.
2474 ASSERT_EQ(0, mDevice->getMetaState());
2475
2476 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2477 << "Ignored device should return unknown key code state.";
2478 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2479 << "Ignored device should return unknown scan code state.";
2480 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2481 << "Ignored device should return unknown switch state.";
2482
2483 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2484 uint8_t flags[2] = { 0, 1 };
2485 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2486 << "Ignored device should never mark any key codes.";
2487 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2488 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2489}
2490
2491TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2492 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002493 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002495 FakeInputMapper& mapper1 =
2496 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002497 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2498 mapper1.setMetaState(AMETA_ALT_ON);
2499 mapper1.addSupportedKeyCode(AKEYCODE_A);
2500 mapper1.addSupportedKeyCode(AKEYCODE_B);
2501 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2502 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2503 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2504 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2505 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002506
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002507 FakeInputMapper& mapper2 =
2508 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002509 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002510
2511 InputReaderConfiguration config;
2512 mDevice->configure(ARBITRARY_TIME, &config, 0);
2513
2514 String8 propertyValue;
2515 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2516 << "Device should have read configuration during configuration phase.";
2517 ASSERT_STREQ("value", propertyValue.string());
2518
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002519 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2520 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521
2522 // Reset
2523 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002524 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2525 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526
2527 NotifyDeviceResetArgs resetArgs;
2528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2529 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2530 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2531
2532 // Metadata.
2533 ASSERT_FALSE(mDevice->isIgnored());
2534 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2535
2536 InputDeviceInfo info;
2537 mDevice->getDeviceInfo(&info);
2538 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002539 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2541 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2542
2543 // State queries.
2544 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2545 << "Should query mappers and combine meta states.";
2546
2547 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2548 << "Should return unknown key code state when source not supported.";
2549 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2550 << "Should return unknown scan code state when source not supported.";
2551 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2552 << "Should return unknown switch state when source not supported.";
2553
2554 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2555 << "Should query mapper when source is supported.";
2556 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2557 << "Should query mapper when source is supported.";
2558 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2559 << "Should query mapper when source is supported.";
2560
2561 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2562 uint8_t flags[4] = { 0, 0, 0, 1 };
2563 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2564 << "Should do nothing when source is unsupported.";
2565 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2566 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2567 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2568 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2569
2570 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2571 << "Should query mapper when source is supported.";
2572 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2573 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2574 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2575 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2576
2577 // Event handling.
2578 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002579 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 mDevice->process(&event, 1);
2581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2583 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584}
2585
Arthur Hung2c9a3342019-07-23 14:18:59 +08002586// A single input device is associated with a specific display. Check that:
2587// 1. Device is disabled if the viewport corresponding to the associated display is not found
2588// 2. Device is disabled when setEnabled API is called
2589TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002590 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002591
2592 // First Configuration.
2593 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2594
2595 // Device should be enabled by default.
2596 ASSERT_TRUE(mDevice->isEnabled());
2597
2598 // Prepare associated info.
2599 constexpr uint8_t hdmi = 1;
2600 const std::string UNIQUE_ID = "local:1";
2601
2602 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2603 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2604 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2605 // Device should be disabled because it is associated with a specific display via
2606 // input port <-> display port association, but the corresponding display is not found
2607 ASSERT_FALSE(mDevice->isEnabled());
2608
2609 // Prepare displays.
2610 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002611 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2612 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002613 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2614 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2615 ASSERT_TRUE(mDevice->isEnabled());
2616
2617 // Device should be disabled after set disable.
2618 mFakePolicy->addDisabledDevice(mDevice->getId());
2619 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2620 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2621 ASSERT_FALSE(mDevice->isEnabled());
2622
2623 // Device should still be disabled even found the associated display.
2624 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2625 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2626 ASSERT_FALSE(mDevice->isEnabled());
2627}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628
Christine Franks1ba71cc2021-04-07 14:37:42 -07002629TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2630 // Device should be enabled by default.
2631 mFakePolicy->clearViewports();
2632 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2633 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2634 ASSERT_TRUE(mDevice->isEnabled());
2635
2636 // Device should be disabled because it is associated with a specific display, but the
2637 // corresponding display is not found.
2638 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2639 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2640 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2641 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2642 ASSERT_FALSE(mDevice->isEnabled());
2643
2644 // Device should be enabled when a display is found.
2645 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2646 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2647 NO_PORT, ViewportType::INTERNAL);
2648 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2649 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2650 ASSERT_TRUE(mDevice->isEnabled());
2651
2652 // Device should be disabled after set disable.
2653 mFakePolicy->addDisabledDevice(mDevice->getId());
2654 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2655 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2656 ASSERT_FALSE(mDevice->isEnabled());
2657
2658 // Device should still be disabled even found the associated display.
2659 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2660 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2661 ASSERT_FALSE(mDevice->isEnabled());
2662}
2663
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664// --- InputMapperTest ---
2665
2666class InputMapperTest : public testing::Test {
2667protected:
2668 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002669 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 static const int32_t DEVICE_ID;
2671 static const int32_t DEVICE_GENERATION;
2672 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002673 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002674 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002676 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002678 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002679 std::unique_ptr<InstrumentedInputReader> mReader;
2680 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681
Chris Ye1b0c7342020-07-28 21:57:03 -07002682 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002683 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002685 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002686 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2687 mFakeListener);
2688 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 }
2690
Chris Yea52ade12020-08-27 16:49:20 -07002691 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002692
Chris Yea52ade12020-08-27 16:49:20 -07002693 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 mFakeListener.clear();
2695 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 }
2697
2698 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002699 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 }
2701
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002702 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002703 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002704 mReader->requestRefreshConfiguration(changes);
2705 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002706 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002707 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2708 }
2709
arthurhungdcef2dc2020-08-11 14:47:50 +08002710 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2711 const std::string& location, int32_t eventHubId,
2712 Flags<InputDeviceClass> classes) {
2713 InputDeviceIdentifier identifier;
2714 identifier.name = name;
2715 identifier.location = location;
2716 std::shared_ptr<InputDevice> device =
2717 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2718 identifier);
2719 mReader->pushNextDevice(device);
2720 mFakeEventHub->addDevice(eventHubId, name, classes);
2721 mReader->loopOnce();
2722 return device;
2723 }
2724
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002725 template <class T, typename... Args>
2726 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002727 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002728 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002730 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002731 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732 }
2733
2734 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002735 int32_t orientation, const std::string& uniqueId,
2736 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002737 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2738 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002739 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2740 }
2741
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002742 void clearViewports() {
2743 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744 }
2745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002746 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2747 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748 RawEvent event;
2749 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002750 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002751 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752 event.type = type;
2753 event.code = code;
2754 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002755 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002756 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757 }
2758
2759 static void assertMotionRange(const InputDeviceInfo& info,
2760 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2761 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002762 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2764 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2765 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2766 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2767 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2768 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2769 }
2770
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002771 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2772 float size, float touchMajor, float touchMinor, float toolMajor,
2773 float toolMinor, float orientation, float distance,
2774 float scaledAxisEpsilon = 1.f) {
2775 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2776 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2778 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002779 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2780 scaledAxisEpsilon);
2781 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2782 scaledAxisEpsilon);
2783 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2784 scaledAxisEpsilon);
2785 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2786 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2788 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2789 }
2790
Michael Wright17db18e2020-06-26 20:51:44 +01002791 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002793 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794 ASSERT_NEAR(x, actualX, 1);
2795 ASSERT_NEAR(y, actualY, 1);
2796 }
2797};
2798
2799const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002800const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002801const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2803const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002804const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2805 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002806const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807
2808// --- SwitchInputMapperTest ---
2809
2810class SwitchInputMapperTest : public InputMapperTest {
2811protected:
2812};
2813
2814TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002815 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002817 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818}
2819
2820TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002821 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002824 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002826 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002827 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828}
2829
2830TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002831 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837
2838 NotifySwitchArgs args;
2839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2840 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002841 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2842 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843 args.switchMask);
2844 ASSERT_EQ(uint32_t(0), args.policyFlags);
2845}
2846
Chris Ye87143712020-11-10 05:05:58 +00002847// --- VibratorInputMapperTest ---
2848class VibratorInputMapperTest : public InputMapperTest {
2849protected:
2850 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2851};
2852
2853TEST_F(VibratorInputMapperTest, GetSources) {
2854 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2855
2856 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2857}
2858
2859TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2860 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2861
2862 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2863}
2864
2865TEST_F(VibratorInputMapperTest, Vibrate) {
2866 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002867 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002868 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2869
2870 VibrationElement pattern(2);
2871 VibrationSequence sequence(2);
2872 pattern.duration = std::chrono::milliseconds(200);
2873 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2874 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2875 sequence.addElement(pattern);
2876 pattern.duration = std::chrono::milliseconds(500);
2877 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2878 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2879 sequence.addElement(pattern);
2880
2881 std::vector<int64_t> timings = {0, 1};
2882 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2883
2884 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002885 // Start vibrating
2886 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002887 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002888 // Verify vibrator state listener was notified.
2889 mReader->loopOnce();
2890 NotifyVibratorStateArgs args;
2891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2892 ASSERT_EQ(DEVICE_ID, args.deviceId);
2893 ASSERT_TRUE(args.isOn);
2894 // Stop vibrating
2895 mapper.cancelVibrate(VIBRATION_TOKEN);
2896 ASSERT_FALSE(mapper.isVibrating());
2897 // Verify vibrator state listener was notified.
2898 mReader->loopOnce();
2899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2900 ASSERT_EQ(DEVICE_ID, args.deviceId);
2901 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002902}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903
Chris Yef59a2f42020-10-16 12:55:26 -07002904// --- SensorInputMapperTest ---
2905
2906class SensorInputMapperTest : public InputMapperTest {
2907protected:
2908 static const int32_t ACCEL_RAW_MIN;
2909 static const int32_t ACCEL_RAW_MAX;
2910 static const int32_t ACCEL_RAW_FUZZ;
2911 static const int32_t ACCEL_RAW_FLAT;
2912 static const int32_t ACCEL_RAW_RESOLUTION;
2913
2914 static const int32_t GYRO_RAW_MIN;
2915 static const int32_t GYRO_RAW_MAX;
2916 static const int32_t GYRO_RAW_FUZZ;
2917 static const int32_t GYRO_RAW_FLAT;
2918 static const int32_t GYRO_RAW_RESOLUTION;
2919
2920 static const float GRAVITY_MS2_UNIT;
2921 static const float DEGREE_RADIAN_UNIT;
2922
2923 void prepareAccelAxes();
2924 void prepareGyroAxes();
2925 void setAccelProperties();
2926 void setGyroProperties();
2927 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2928};
2929
2930const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2931const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2932const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2933const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2934const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2935
2936const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2937const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2938const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2939const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2940const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2941
2942const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2943const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2944
2945void SensorInputMapperTest::prepareAccelAxes() {
2946 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2947 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2948 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2949 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2950 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2951 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2952}
2953
2954void SensorInputMapperTest::prepareGyroAxes() {
2955 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2956 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2958 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2959 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2960 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2961}
2962
2963void SensorInputMapperTest::setAccelProperties() {
2964 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2965 /* sensorDataIndex */ 0);
2966 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2967 /* sensorDataIndex */ 1);
2968 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2969 /* sensorDataIndex */ 2);
2970 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2971 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2972 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2973 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2974 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2975}
2976
2977void SensorInputMapperTest::setGyroProperties() {
2978 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2979 /* sensorDataIndex */ 0);
2980 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2981 /* sensorDataIndex */ 1);
2982 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2983 /* sensorDataIndex */ 2);
2984 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2985 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2986 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2987 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2988 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2989}
2990
2991TEST_F(SensorInputMapperTest, GetSources) {
2992 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2993
2994 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2995}
2996
2997TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2998 setAccelProperties();
2999 prepareAccelAxes();
3000 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3001
3002 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3003 std::chrono::microseconds(10000),
3004 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003005 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003011
3012 NotifySensorArgs args;
3013 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3014 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3015 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3016
3017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3018 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3019 ASSERT_EQ(args.deviceId, DEVICE_ID);
3020 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3021 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3022 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3023 ASSERT_EQ(args.values, values);
3024 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3025}
3026
3027TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3028 setGyroProperties();
3029 prepareGyroAxes();
3030 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3031
3032 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3033 std::chrono::microseconds(10000),
3034 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003035 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003041
3042 NotifySensorArgs args;
3043 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3044 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3045 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3046
3047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3048 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3049 ASSERT_EQ(args.deviceId, DEVICE_ID);
3050 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3051 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3052 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3053 ASSERT_EQ(args.values, values);
3054 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3055}
3056
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057// --- KeyboardInputMapperTest ---
3058
3059class KeyboardInputMapperTest : public InputMapperTest {
3060protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003061 const std::string UNIQUE_ID = "local:0";
3062
3063 void prepareDisplay(int32_t orientation);
3064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003065 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003066 int32_t originalKeyCode, int32_t rotatedKeyCode,
3067 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068};
3069
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003070/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3071 * orientation.
3072 */
3073void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003074 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3075 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003076}
3077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003079 int32_t originalScanCode, int32_t originalKeyCode,
3080 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 NotifyKeyArgs args;
3082
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3085 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3086 ASSERT_EQ(originalScanCode, args.scanCode);
3087 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003088 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3092 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3093 ASSERT_EQ(originalScanCode, args.scanCode);
3094 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003095 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096}
3097
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 KeyboardInputMapper& mapper =
3100 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3101 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003103 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104}
3105
3106TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3107 const int32_t USAGE_A = 0x070004;
3108 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003109 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3110 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003111 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3112 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3113 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003115 KeyboardInputMapper& mapper =
3116 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3117 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003118 // Initial metastate to AMETA_NONE.
3119 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3120 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121
3122 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 NotifyKeyArgs args;
3125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3126 ASSERT_EQ(DEVICE_ID, args.deviceId);
3127 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3128 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3129 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3130 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3131 ASSERT_EQ(KEY_HOME, args.scanCode);
3132 ASSERT_EQ(AMETA_NONE, args.metaState);
3133 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3134 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3135 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3136
3137 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003138 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(DEVICE_ID, args.deviceId);
3141 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3142 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3143 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3144 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3145 ASSERT_EQ(KEY_HOME, args.scanCode);
3146 ASSERT_EQ(AMETA_NONE, args.metaState);
3147 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3148 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3149 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3150
3151 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3155 ASSERT_EQ(DEVICE_ID, args.deviceId);
3156 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3157 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3158 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3159 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3160 ASSERT_EQ(0, args.scanCode);
3161 ASSERT_EQ(AMETA_NONE, args.metaState);
3162 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3164 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3165
3166 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3168 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3170 ASSERT_EQ(DEVICE_ID, args.deviceId);
3171 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3172 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3173 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3174 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3175 ASSERT_EQ(0, args.scanCode);
3176 ASSERT_EQ(AMETA_NONE, args.metaState);
3177 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3178 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3179 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3180
3181 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3185 ASSERT_EQ(DEVICE_ID, args.deviceId);
3186 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3187 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3188 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3189 ASSERT_EQ(0, args.keyCode);
3190 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3191 ASSERT_EQ(AMETA_NONE, args.metaState);
3192 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3193 ASSERT_EQ(0U, args.policyFlags);
3194 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3195
3196 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3198 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3200 ASSERT_EQ(DEVICE_ID, args.deviceId);
3201 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3202 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3203 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3204 ASSERT_EQ(0, args.keyCode);
3205 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3206 ASSERT_EQ(AMETA_NONE, args.metaState);
3207 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3208 ASSERT_EQ(0U, args.policyFlags);
3209 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3210}
3211
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003212/**
3213 * Ensure that the readTime is set to the time when the EV_KEY is received.
3214 */
3215TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3216 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3217
3218 KeyboardInputMapper& mapper =
3219 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3220 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3221 NotifyKeyArgs args;
3222
3223 // Key down
3224 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3226 ASSERT_EQ(12, args.readTime);
3227
3228 // Key up
3229 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3231 ASSERT_EQ(15, args.readTime);
3232}
3233
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003235 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3236 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003237 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3238 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3239 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003241 KeyboardInputMapper& mapper =
3242 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3243 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244
arthurhungc903df12020-08-11 15:08:42 +08003245 // Initial metastate to AMETA_NONE.
3246 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3247 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
3249 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 NotifyKeyArgs args;
3252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003255 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256
3257 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003258 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3260 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262
3263 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003264 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3266 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268
3269 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3272 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003274 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275}
3276
3277TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003278 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3279 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3280 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3281 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003283 KeyboardInputMapper& mapper =
3284 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3285 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003287 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3289 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3290 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3291 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3292 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3293 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3294 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3295 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3296}
3297
3298TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3300 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3301 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3302 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003305 KeyboardInputMapper& mapper =
3306 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3307 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003309 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003310 ASSERT_NO_FATAL_FAILURE(
3311 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3312 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3313 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3314 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3315 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3316 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3317 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003319 clearViewports();
3320 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003321 ASSERT_NO_FATAL_FAILURE(
3322 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3324 AKEYCODE_DPAD_UP, DISPLAY_ID));
3325 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3326 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3327 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3328 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003330 clearViewports();
3331 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003332 ASSERT_NO_FATAL_FAILURE(
3333 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3335 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3336 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3337 AKEYCODE_DPAD_UP, DISPLAY_ID));
3338 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3339 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003341 clearViewports();
3342 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003343 ASSERT_NO_FATAL_FAILURE(
3344 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3345 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3346 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3347 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3348 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3349 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3350 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
3352 // Special case: if orientation changes while key is down, we still emit the same keycode
3353 // in the key up as we did in the key down.
3354 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003355 clearViewports();
3356 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3359 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3360 ASSERT_EQ(KEY_UP, args.scanCode);
3361 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3362
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003363 clearViewports();
3364 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3367 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3368 ASSERT_EQ(KEY_UP, args.scanCode);
3369 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3370}
3371
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003372TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3373 // If the keyboard is not orientation aware,
3374 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003375 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003377 KeyboardInputMapper& mapper =
3378 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3379 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003380 NotifyKeyArgs args;
3381
3382 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3387 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3388
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003389 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3394 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3395}
3396
3397TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3398 // If the keyboard is orientation aware,
3399 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003400 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003401
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003402 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 KeyboardInputMapper& mapper =
3404 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3405 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003406 NotifyKeyArgs args;
3407
3408 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3409 // ^--- already checked by the previous test
3410
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003411 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003412 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3417 ASSERT_EQ(DISPLAY_ID, args.displayId);
3418
3419 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003420 clearViewports();
3421 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003422 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3427 ASSERT_EQ(newDisplayId, args.displayId);
3428}
3429
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003431 KeyboardInputMapper& mapper =
3432 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3433 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003435 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003438 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003439 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440}
3441
3442TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003443 KeyboardInputMapper& mapper =
3444 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3445 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003447 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003448 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003450 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003451 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452}
3453
3454TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003455 KeyboardInputMapper& mapper =
3456 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3457 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460
3461 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3462 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464 ASSERT_TRUE(flags[0]);
3465 ASSERT_FALSE(flags[1]);
3466}
3467
3468TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003469 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3470 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3471 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3474 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003476 KeyboardInputMapper& mapper =
3477 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3478 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003479 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003480 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3481 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482
3483 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3485 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3486 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487
3488 // Toggle caps lock on.
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_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3492 ASSERT_FALSE(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_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495
3496 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003499 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3500 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3501 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003502 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
3504 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003507 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3508 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3509 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Toggle scroll lock on.
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_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3517 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519
3520 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3524 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3525 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
3528 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003531 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3532 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3533 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003534 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535}
3536
Chris Yea52ade12020-08-27 16:49:20 -07003537TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3538 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3540 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3541 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3542
3543 KeyboardInputMapper& mapper =
3544 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3545 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3546
3547 // Initial metastate should be AMETA_NONE as no meta keys added.
3548 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3549 // Meta state should be AMETA_NONE after reset
3550 mapper.reset(ARBITRARY_TIME);
3551 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3552 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3553 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3554 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3555
3556 NotifyKeyArgs args;
3557 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3560 ASSERT_EQ(AMETA_NONE, args.metaState);
3561 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3562 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3563 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3564
3565 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003566 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3568 ASSERT_EQ(AMETA_NONE, args.metaState);
3569 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3570 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3571 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3572}
3573
Arthur Hung2c9a3342019-07-23 14:18:59 +08003574TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3575 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3577 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003580
3581 // keyboard 2.
3582 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003583 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003584 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003586 std::shared_ptr<InputDevice> device2 =
3587 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3588 Flags<InputDeviceClass>(0));
3589
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003590 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3591 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3592 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3593 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003595 KeyboardInputMapper& mapper =
3596 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003598
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003599 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003600 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003601 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003602 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3603 device2->reset(ARBITRARY_TIME);
3604
3605 // Prepared displays and associated info.
3606 constexpr uint8_t hdmi1 = 0;
3607 constexpr uint8_t hdmi2 = 1;
3608 const std::string SECONDARY_UNIQUE_ID = "local:1";
3609
3610 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3611 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3612
3613 // No associated display viewport found, should disable the device.
3614 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3615 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3616 ASSERT_FALSE(device2->isEnabled());
3617
3618 // Prepare second display.
3619 constexpr int32_t newDisplayId = 2;
3620 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003621 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003622 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003623 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003624 // Default device will reconfigure above, need additional reconfiguration for another device.
3625 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3626 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3627
3628 // Device should be enabled after the associated display is found.
3629 ASSERT_TRUE(mDevice->isEnabled());
3630 ASSERT_TRUE(device2->isEnabled());
3631
3632 // Test pad key events
3633 ASSERT_NO_FATAL_FAILURE(
3634 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3635 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3636 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3637 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3638 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3639 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3640 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3641
3642 ASSERT_NO_FATAL_FAILURE(
3643 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3644 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3645 AKEYCODE_DPAD_RIGHT, newDisplayId));
3646 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3647 AKEYCODE_DPAD_DOWN, newDisplayId));
3648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3649 AKEYCODE_DPAD_LEFT, newDisplayId));
3650}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651
arthurhungc903df12020-08-11 15:08:42 +08003652TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3653 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3654 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3655 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3656 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3657 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3658 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3659
3660 KeyboardInputMapper& mapper =
3661 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3662 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3663 // Initial metastate to AMETA_NONE.
3664 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3665 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3666
3667 // Initialization should have turned all of the lights off.
3668 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3669 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3670 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3671
3672 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003675 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3676 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3677
3678 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003681 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3682 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3683
3684 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003687 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3688 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3689
3690 mFakeEventHub->removeDevice(EVENTHUB_ID);
3691 mReader->loopOnce();
3692
3693 // keyboard 2 should default toggle keys.
3694 const std::string USB2 = "USB2";
3695 const std::string DEVICE_NAME2 = "KEYBOARD2";
3696 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3697 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3698 std::shared_ptr<InputDevice> device2 =
3699 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3700 Flags<InputDeviceClass>(0));
3701 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3702 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3703 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3704 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3705 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3706 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3707
arthurhung6fe95782020-10-05 22:41:16 +08003708 KeyboardInputMapper& mapper2 =
3709 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3710 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003711 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3712 device2->reset(ARBITRARY_TIME);
3713
3714 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3715 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3716 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003717 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3718 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003719}
3720
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721// --- KeyboardInputMapperTest_ExternalDevice ---
3722
3723class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3724protected:
Chris Yea52ade12020-08-27 16:49:20 -07003725 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003726};
3727
3728TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003729 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3730 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3733 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3735 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003736
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 KeyboardInputMapper& mapper =
3738 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3739 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003742 NotifyKeyArgs args;
3743 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_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3748 ASSERT_EQ(uint32_t(0), args.policyFlags);
3749
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3752 ASSERT_EQ(uint32_t(0), args.policyFlags);
3753
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003754 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3756 ASSERT_EQ(uint32_t(0), args.policyFlags);
3757
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3760 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3761
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3764 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3765}
3766
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003767TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003768 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3771 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3772 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003773
Powei Fengd041c5d2019-05-03 17:11:33 -07003774 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775 KeyboardInputMapper& mapper =
3776 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3777 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003778
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003780 NotifyKeyArgs args;
3781 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_HOME, 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
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3790 ASSERT_EQ(uint32_t(0), args.policyFlags);
3791
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003792 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3794 ASSERT_EQ(uint32_t(0), args.policyFlags);
3795
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3798 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003800 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3802 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3803}
3804
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805// --- CursorInputMapperTest ---
3806
3807class CursorInputMapperTest : public InputMapperTest {
3808protected:
3809 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3810
Michael Wright17db18e2020-06-26 20:51:44 +01003811 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812
Chris Yea52ade12020-08-27 16:49:20 -07003813 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814 InputMapperTest::SetUp();
3815
Michael Wright17db18e2020-06-26 20:51:44 +01003816 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003817 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818 }
3819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003820 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3821 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003822
3823 void prepareDisplay(int32_t orientation) {
3824 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003825 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003826 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3827 orientation, uniqueId, NO_PORT, viewportType);
3828 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003829
3830 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3831 float pressure) {
3832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3833 0.0f, 0.0f, 0.0f, EPSILON));
3834 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835};
3836
3837const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3838
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003839void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3840 int32_t originalY, int32_t rotatedX,
3841 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842 NotifyMotionArgs args;
3843
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003849 ASSERT_NO_FATAL_FAILURE(
3850 assertCursorPointerCoords(args.pointerCoords[0],
3851 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3852 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853}
3854
3855TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003857 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003859 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860}
3861
3862TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867}
3868
3869TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872
3873 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003874 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875
3876 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003877 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3878 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3880 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3881
3882 // When the bounds are set, then there should be a valid motion range.
3883 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3884
3885 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003886 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887
3888 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3889 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3890 1, 800 - 1, 0.0f, 0.0f));
3891 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3892 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3893 2, 480 - 1, 0.0f, 0.0f));
3894 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3895 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3896 0.0f, 1.0f, 0.0f, 0.0f));
3897}
3898
3899TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003901 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902
3903 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003904 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
3906 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3907 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3908 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3909 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3910 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3911 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3912 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3913 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3914 0.0f, 1.0f, 0.0f, 0.0f));
3915}
3916
3917TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920
arthurhungdcef2dc2020-08-11 14:47:50 +08003921 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922
3923 NotifyMotionArgs args;
3924
3925 // Button press.
3926 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3930 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3931 ASSERT_EQ(DEVICE_ID, args.deviceId);
3932 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3933 ASSERT_EQ(uint32_t(0), args.policyFlags);
3934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3935 ASSERT_EQ(0, args.flags);
3936 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3937 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3938 ASSERT_EQ(0, args.edgeFlags);
3939 ASSERT_EQ(uint32_t(1), args.pointerCount);
3940 ASSERT_EQ(0, args.pointerProperties[0].id);
3941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003942 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3944 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3945 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3946
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3948 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3949 ASSERT_EQ(DEVICE_ID, args.deviceId);
3950 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3951 ASSERT_EQ(uint32_t(0), args.policyFlags);
3952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3953 ASSERT_EQ(0, args.flags);
3954 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3955 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3956 ASSERT_EQ(0, args.edgeFlags);
3957 ASSERT_EQ(uint32_t(1), args.pointerCount);
3958 ASSERT_EQ(0, args.pointerProperties[0].id);
3959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003960 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003961 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3962 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3963 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3964
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003966 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3967 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3969 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3970 ASSERT_EQ(DEVICE_ID, args.deviceId);
3971 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3972 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003973 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3974 ASSERT_EQ(0, args.flags);
3975 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3976 ASSERT_EQ(0, args.buttonState);
3977 ASSERT_EQ(0, args.edgeFlags);
3978 ASSERT_EQ(uint32_t(1), args.pointerCount);
3979 ASSERT_EQ(0, args.pointerProperties[0].id);
3980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003981 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003982 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3983 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3984 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3985
3986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3987 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3988 ASSERT_EQ(DEVICE_ID, args.deviceId);
3989 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3990 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3992 ASSERT_EQ(0, args.flags);
3993 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3994 ASSERT_EQ(0, args.buttonState);
3995 ASSERT_EQ(0, args.edgeFlags);
3996 ASSERT_EQ(uint32_t(1), args.pointerCount);
3997 ASSERT_EQ(0, args.pointerProperties[0].id);
3998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003999 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004000 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4001 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4002 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4003}
4004
4005TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004007 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008
4009 NotifyMotionArgs args;
4010
4011 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004016 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4017 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4018 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019
4020 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004025 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4026 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027}
4028
4029TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004031 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032
4033 NotifyMotionArgs args;
4034
4035 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004040 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004044 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004045
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004051 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004052
4053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004055 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056}
4057
4058TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004060 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061
4062 NotifyMotionArgs args;
4063
4064 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 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_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004071 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4072 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4073 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004077 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4078 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4079 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004080
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004087 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4088 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4089 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090
4091 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004096 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004097
4098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004100 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101}
4102
4103TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104 addConfigurationProperty("cursor.mode", "navigation");
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_90);
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}
4117
4118TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119 addConfigurationProperty("cursor.mode", "navigation");
4120 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004121 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004123 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4128 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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004133 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4138 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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004143 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4152
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004153 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4162}
4163
4164TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004166 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167
4168 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4169 mFakePointerController->setPosition(100, 200);
4170 mFakePointerController->setButtonState(0);
4171
4172 NotifyMotionArgs motionArgs;
4173 NotifyKeyArgs keyArgs;
4174
4175 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
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));
4179 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4180 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4181 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004182 ASSERT_NO_FATAL_FAILURE(
4183 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4187 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4188 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004189 ASSERT_NO_FATAL_FAILURE(
4190 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004191
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_EQ(0, motionArgs.buttonState);
4197 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004198 ASSERT_NO_FATAL_FAILURE(
4199 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 ASSERT_EQ(0, motionArgs.buttonState);
4204 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004205 ASSERT_NO_FATAL_FAILURE(
4206 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207
4208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(0, motionArgs.buttonState);
4211 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004212 ASSERT_NO_FATAL_FAILURE(
4213 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214
4215 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4220 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4221 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4222 motionArgs.buttonState);
4223 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4224 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004225 ASSERT_NO_FATAL_FAILURE(
4226 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4230 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4231 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4232 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004233 ASSERT_NO_FATAL_FAILURE(
4234 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004235
4236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4238 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4239 motionArgs.buttonState);
4240 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4241 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004244
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4250 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004251 ASSERT_NO_FATAL_FAILURE(
4252 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004253
4254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4257 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004258 ASSERT_NO_FATAL_FAILURE(
4259 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4265 ASSERT_EQ(0, motionArgs.buttonState);
4266 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004267 ASSERT_NO_FATAL_FAILURE(
4268 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271
4272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_EQ(0, motionArgs.buttonState);
4274 ASSERT_EQ(0, mFakePointerController->getButtonState());
4275 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004276 ASSERT_NO_FATAL_FAILURE(
4277 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004278
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4280 ASSERT_EQ(0, motionArgs.buttonState);
4281 ASSERT_EQ(0, mFakePointerController->getButtonState());
4282 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004283 ASSERT_NO_FATAL_FAILURE(
4284 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285
4286 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4290 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4291 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004292
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_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4296 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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));
4301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4302 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004304 ASSERT_NO_FATAL_FAILURE(
4305 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 ASSERT_EQ(0, motionArgs.buttonState);
4312 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004313 ASSERT_NO_FATAL_FAILURE(
4314 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004315
4316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318 ASSERT_EQ(0, motionArgs.buttonState);
4319 ASSERT_EQ(0, mFakePointerController->getButtonState());
4320
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(
4322 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4324 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4325 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4326
4327 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4331 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4332 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333
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_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4337 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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_BUTTON_PRESS, motionArgs.action);
4343 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4344 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_EQ(0, motionArgs.buttonState);
4353 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(
4355 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356
4357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4359 ASSERT_EQ(0, motionArgs.buttonState);
4360 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004361 ASSERT_NO_FATAL_FAILURE(
4362 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4365 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4366 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4367
4368 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4372 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4373 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
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_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4378 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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_BUTTON_PRESS, motionArgs.action);
4384 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4385 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_EQ(0, motionArgs.buttonState);
4394 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(
4396 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4400 ASSERT_EQ(0, motionArgs.buttonState);
4401 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004402 ASSERT_NO_FATAL_FAILURE(
4403 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004404
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4406 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4407 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4408
4409 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4413 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4414 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415
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_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4419 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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_BUTTON_PRESS, motionArgs.action);
4425 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4426 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_EQ(0, motionArgs.buttonState);
4435 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(
4437 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438
4439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4441 ASSERT_EQ(0, motionArgs.buttonState);
4442 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(
4444 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4447 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4448 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4449}
4450
4451TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004453 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454
4455 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4456 mFakePointerController->setPosition(100, 200);
4457 mFakePointerController->setButtonState(0);
4458
4459 NotifyMotionArgs args;
4460
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004465 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4466 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4468 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 +01004469 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004470}
4471
4472TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004473 addConfigurationProperty("cursor.mode", "pointer");
4474 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004475 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004476
4477 NotifyDeviceResetArgs resetArgs;
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4479 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4480 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4481
4482 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4483 mFakePointerController->setPosition(100, 200);
4484 mFakePointerController->setButtonState(0);
4485
4486 NotifyMotionArgs args;
4487
4488 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4493 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4496 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 +01004497 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004498
4499 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4503 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4506 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4508 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4511 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4512
4513 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004514 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4515 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4517 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4518 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4520 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4522 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4523 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4525 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4526
4527 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4532 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4535 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 +01004536 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004537
4538 // Disable pointer capture and check that the device generation got bumped
4539 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004540 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004541 mFakePolicy->setPointerCapture(false);
4542 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004543 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004544
4545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4546 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4547 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4548
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4553 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4556 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 +01004557 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558}
4559
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004560TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004561 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004562
Garfield Tan888a6a42020-01-09 11:39:16 -08004563 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004564 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004565 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4566 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004567 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4568 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004569 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4570 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4571
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004572 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4573 mFakePointerController->setPosition(100, 200);
4574 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004575
4576 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4581 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4584 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 +01004585 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004586 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4587}
4588
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589// --- TouchInputMapperTest ---
4590
4591class TouchInputMapperTest : public InputMapperTest {
4592protected:
4593 static const int32_t RAW_X_MIN;
4594 static const int32_t RAW_X_MAX;
4595 static const int32_t RAW_Y_MIN;
4596 static const int32_t RAW_Y_MAX;
4597 static const int32_t RAW_TOUCH_MIN;
4598 static const int32_t RAW_TOUCH_MAX;
4599 static const int32_t RAW_TOOL_MIN;
4600 static const int32_t RAW_TOOL_MAX;
4601 static const int32_t RAW_PRESSURE_MIN;
4602 static const int32_t RAW_PRESSURE_MAX;
4603 static const int32_t RAW_ORIENTATION_MIN;
4604 static const int32_t RAW_ORIENTATION_MAX;
4605 static const int32_t RAW_DISTANCE_MIN;
4606 static const int32_t RAW_DISTANCE_MAX;
4607 static const int32_t RAW_TILT_MIN;
4608 static const int32_t RAW_TILT_MAX;
4609 static const int32_t RAW_ID_MIN;
4610 static const int32_t RAW_ID_MAX;
4611 static const int32_t RAW_SLOT_MIN;
4612 static const int32_t RAW_SLOT_MAX;
4613 static const float X_PRECISION;
4614 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004615 static const float X_PRECISION_VIRTUAL;
4616 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617
4618 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004619 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620
4621 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4622
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004623 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004624 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004625
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 enum Axes {
4627 POSITION = 1 << 0,
4628 TOUCH = 1 << 1,
4629 TOOL = 1 << 2,
4630 PRESSURE = 1 << 3,
4631 ORIENTATION = 1 << 4,
4632 MINOR = 1 << 5,
4633 ID = 1 << 6,
4634 DISTANCE = 1 << 7,
4635 TILT = 1 << 8,
4636 SLOT = 1 << 9,
4637 TOOL_TYPE = 1 << 10,
4638 };
4639
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004640 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4641 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004642 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004644 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 int32_t toRawX(float displayX);
4646 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004647 float toCookedX(float rawX, float rawY);
4648 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004650 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004652 float toDisplayY(int32_t rawY, int32_t displayHeight);
4653
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654};
4655
4656const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4657const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4658const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4659const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4660const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4661const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4662const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4663const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004664const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4665const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4667const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4668const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4669const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4670const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4671const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4672const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4673const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4674const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4675const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4676const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4677const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004678const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4679 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4680const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4681 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004682const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4683 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684
4685const float TouchInputMapperTest::GEOMETRIC_SCALE =
4686 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4687 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4688
4689const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4690 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4691 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4692};
4693
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004694void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004695 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4696 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004697}
4698
4699void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4700 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4701 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702}
4703
Santos Cordonfa5cf462017-04-05 10:37:00 -07004704void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004705 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4706 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4707 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004708}
4709
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004711 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4712 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4713 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4714 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715}
4716
Jason Gerecke489fda82012-09-07 17:19:40 -07004717void TouchInputMapperTest::prepareLocationCalibration() {
4718 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4719}
4720
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721int32_t TouchInputMapperTest::toRawX(float displayX) {
4722 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4723}
4724
4725int32_t TouchInputMapperTest::toRawY(float displayY) {
4726 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4727}
4728
Jason Gerecke489fda82012-09-07 17:19:40 -07004729float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4730 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4731 return rawX;
4732}
4733
4734float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4735 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4736 return rawY;
4737}
4738
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004740 return toDisplayX(rawX, DISPLAY_WIDTH);
4741}
4742
4743float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4744 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745}
4746
4747float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004748 return toDisplayY(rawY, DISPLAY_HEIGHT);
4749}
4750
4751float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4752 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753}
4754
4755
4756// --- SingleTouchInputMapperTest ---
4757
4758class SingleTouchInputMapperTest : public TouchInputMapperTest {
4759protected:
4760 void prepareButtons();
4761 void prepareAxes(int axes);
4762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004763 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4764 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4765 void processUp(SingleTouchInputMapper& mappery);
4766 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4767 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4768 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4769 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4770 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4771 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772};
4773
4774void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004775 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776}
4777
4778void SingleTouchInputMapperTest::prepareAxes(int axes) {
4779 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004780 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4781 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 }
4783 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004784 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4785 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 }
4787 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004788 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4789 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 }
4791 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004792 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4793 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 }
4795 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004796 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4797 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 }
4799}
4800
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004801void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805}
4806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004807void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810}
4811
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004812void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814}
4815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004816void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818}
4819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004820void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4821 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823}
4824
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004825void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827}
4828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004829void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4830 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833}
4834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004835void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4836 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838}
4839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004840void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842}
4843
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 prepareButtons();
4846 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004847 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004849 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850}
4851
4852TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004853 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4854 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 prepareButtons();
4856 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004857 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004859 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860}
4861
4862TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 prepareButtons();
4864 prepareAxes(POSITION);
4865 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004866 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004868 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869}
4870
4871TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 prepareButtons();
4873 prepareAxes(POSITION);
4874 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004875 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878}
4879
4880TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 addConfigurationProperty("touch.deviceType", "touchScreen");
4882 prepareDisplay(DISPLAY_ORIENTATION_0);
4883 prepareButtons();
4884 prepareAxes(POSITION);
4885 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004886 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887
4888 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004889 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890
4891 // Virtual key is down.
4892 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4893 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4894 processDown(mapper, x, y);
4895 processSync(mapper);
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004898 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899
4900 // Virtual key is up.
4901 processUp(mapper);
4902 processSync(mapper);
4903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004905 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906}
4907
4908TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909 addConfigurationProperty("touch.deviceType", "touchScreen");
4910 prepareDisplay(DISPLAY_ORIENTATION_0);
4911 prepareButtons();
4912 prepareAxes(POSITION);
4913 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915
4916 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004917 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918
4919 // Virtual key is down.
4920 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4921 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4922 processDown(mapper, x, y);
4923 processSync(mapper);
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004926 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927
4928 // Virtual key is up.
4929 processUp(mapper);
4930 processSync(mapper);
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4932
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004933 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934}
4935
4936TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 addConfigurationProperty("touch.deviceType", "touchScreen");
4938 prepareDisplay(DISPLAY_ORIENTATION_0);
4939 prepareButtons();
4940 prepareAxes(POSITION);
4941 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
4944 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4945 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004946 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 ASSERT_TRUE(flags[0]);
4948 ASSERT_FALSE(flags[1]);
4949}
4950
4951TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 addConfigurationProperty("touch.deviceType", "touchScreen");
4953 prepareDisplay(DISPLAY_ORIENTATION_0);
4954 prepareButtons();
4955 prepareAxes(POSITION);
4956 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004957 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958
arthurhungdcef2dc2020-08-11 14:47:50 +08004959 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960
4961 NotifyKeyArgs args;
4962
4963 // Press virtual key.
4964 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4965 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4966 processDown(mapper, x, y);
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_DOWN, 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 // Release virtual key.
4982 processUp(mapper);
4983 processSync(mapper);
4984
4985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4986 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4987 ASSERT_EQ(DEVICE_ID, args.deviceId);
4988 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4989 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4990 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4991 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4992 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4993 ASSERT_EQ(KEY_HOME, args.scanCode);
4994 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4995 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4996
4997 // Should not have sent any motions.
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4999}
5000
5001TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 addConfigurationProperty("touch.deviceType", "touchScreen");
5003 prepareDisplay(DISPLAY_ORIENTATION_0);
5004 prepareButtons();
5005 prepareAxes(POSITION);
5006 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005007 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008
arthurhungdcef2dc2020-08-11 14:47:50 +08005009 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010
5011 NotifyKeyArgs keyArgs;
5012
5013 // Press virtual key.
5014 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5015 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5016 processDown(mapper, x, y);
5017 processSync(mapper);
5018
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5020 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5021 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5022 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5023 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5024 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5025 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5026 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5027 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5028 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5029 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5030
5031 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5032 // into the display area.
5033 y -= 100;
5034 processMove(mapper, x, y);
5035 processSync(mapper);
5036
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5038 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5039 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5040 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5041 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5043 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5044 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5045 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5046 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5047 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5048 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5049
5050 NotifyMotionArgs motionArgs;
5051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5052 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5053 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5054 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5055 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5056 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5057 ASSERT_EQ(0, motionArgs.flags);
5058 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5059 ASSERT_EQ(0, motionArgs.buttonState);
5060 ASSERT_EQ(0, motionArgs.edgeFlags);
5061 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5062 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5065 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5066 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5067 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5068 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5069
5070 // Keep moving out of bounds. Should generate a pointer move.
5071 y -= 50;
5072 processMove(mapper, x, y);
5073 processSync(mapper);
5074
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5077 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5078 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5079 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5081 ASSERT_EQ(0, motionArgs.flags);
5082 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5083 ASSERT_EQ(0, motionArgs.buttonState);
5084 ASSERT_EQ(0, motionArgs.edgeFlags);
5085 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5089 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5090 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5091 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5092 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5093
5094 // Release out of bounds. Should generate a pointer up.
5095 processUp(mapper);
5096 processSync(mapper);
5097
5098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5099 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5100 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5101 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5102 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5103 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5104 ASSERT_EQ(0, motionArgs.flags);
5105 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5106 ASSERT_EQ(0, motionArgs.buttonState);
5107 ASSERT_EQ(0, motionArgs.edgeFlags);
5108 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5109 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5112 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5113 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5114 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5115 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5116
5117 // Should not have sent any more keys or motions.
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5120}
5121
5122TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 addConfigurationProperty("touch.deviceType", "touchScreen");
5124 prepareDisplay(DISPLAY_ORIENTATION_0);
5125 prepareButtons();
5126 prepareAxes(POSITION);
5127 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005128 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129
arthurhungdcef2dc2020-08-11 14:47:50 +08005130 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131
5132 NotifyMotionArgs motionArgs;
5133
5134 // Initially go down out of bounds.
5135 int32_t x = -10;
5136 int32_t y = -10;
5137 processDown(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5141
5142 // Move into the display area. Should generate a pointer down.
5143 x = 50;
5144 y = 75;
5145 processMove(mapper, x, y);
5146 processSync(mapper);
5147
5148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5149 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5150 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5151 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5152 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5153 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5154 ASSERT_EQ(0, motionArgs.flags);
5155 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5156 ASSERT_EQ(0, motionArgs.buttonState);
5157 ASSERT_EQ(0, motionArgs.edgeFlags);
5158 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5159 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5160 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5162 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5163 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5164 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5165 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5166
5167 // Release. Should generate a pointer up.
5168 processUp(mapper);
5169 processSync(mapper);
5170
5171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5173 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5174 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5175 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5176 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5177 ASSERT_EQ(0, motionArgs.flags);
5178 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5179 ASSERT_EQ(0, motionArgs.buttonState);
5180 ASSERT_EQ(0, motionArgs.edgeFlags);
5181 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5182 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5185 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5186 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5187 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5188 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5189
5190 // Should not have sent any more keys or motions.
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5193}
5194
Santos Cordonfa5cf462017-04-05 10:37:00 -07005195TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005196 addConfigurationProperty("touch.deviceType", "touchScreen");
5197 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5198
5199 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5200 prepareButtons();
5201 prepareAxes(POSITION);
5202 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005203 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005204
arthurhungdcef2dc2020-08-11 14:47:50 +08005205 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005206
5207 NotifyMotionArgs motionArgs;
5208
5209 // Down.
5210 int32_t x = 100;
5211 int32_t y = 125;
5212 processDown(mapper, x, y);
5213 processSync(mapper);
5214
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5218 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5219 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5220 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5221 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5222 ASSERT_EQ(0, motionArgs.flags);
5223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5224 ASSERT_EQ(0, motionArgs.buttonState);
5225 ASSERT_EQ(0, motionArgs.edgeFlags);
5226 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5230 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5231 1, 0, 0, 0, 0, 0, 0, 0));
5232 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5233 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5235
5236 // Move.
5237 x += 50;
5238 y += 75;
5239 processMove(mapper, x, y);
5240 processSync(mapper);
5241
5242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5244 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5245 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5246 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5247 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5249 ASSERT_EQ(0, motionArgs.flags);
5250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5251 ASSERT_EQ(0, motionArgs.buttonState);
5252 ASSERT_EQ(0, motionArgs.edgeFlags);
5253 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5254 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5257 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5258 1, 0, 0, 0, 0, 0, 0, 0));
5259 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5260 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5262
5263 // Up.
5264 processUp(mapper);
5265 processSync(mapper);
5266
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5269 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5270 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5273 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5274 ASSERT_EQ(0, motionArgs.flags);
5275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5276 ASSERT_EQ(0, motionArgs.buttonState);
5277 ASSERT_EQ(0, motionArgs.edgeFlags);
5278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5282 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5283 1, 0, 0, 0, 0, 0, 0, 0));
5284 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5285 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5287
5288 // Should not have sent any more keys or motions.
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5291}
5292
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 addConfigurationProperty("touch.deviceType", "touchScreen");
5295 prepareDisplay(DISPLAY_ORIENTATION_0);
5296 prepareButtons();
5297 prepareAxes(POSITION);
5298 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300
arthurhungdcef2dc2020-08-11 14:47:50 +08005301 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302
5303 NotifyMotionArgs motionArgs;
5304
5305 // Down.
5306 int32_t x = 100;
5307 int32_t y = 125;
5308 processDown(mapper, x, y);
5309 processSync(mapper);
5310
5311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5313 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5314 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5315 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5316 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5317 ASSERT_EQ(0, motionArgs.flags);
5318 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5319 ASSERT_EQ(0, motionArgs.buttonState);
5320 ASSERT_EQ(0, motionArgs.edgeFlags);
5321 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5322 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5325 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5326 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5327 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5329
5330 // Move.
5331 x += 50;
5332 y += 75;
5333 processMove(mapper, x, y);
5334 processSync(mapper);
5335
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5339 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5340 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5342 ASSERT_EQ(0, motionArgs.flags);
5343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5344 ASSERT_EQ(0, motionArgs.buttonState);
5345 ASSERT_EQ(0, motionArgs.edgeFlags);
5346 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5347 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5350 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5351 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5352 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5354
5355 // Up.
5356 processUp(mapper);
5357 processSync(mapper);
5358
5359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5360 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5361 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5362 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5363 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5365 ASSERT_EQ(0, motionArgs.flags);
5366 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5367 ASSERT_EQ(0, motionArgs.buttonState);
5368 ASSERT_EQ(0, motionArgs.edgeFlags);
5369 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5370 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5373 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5374 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5375 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5377
5378 // Should not have sent any more keys or motions.
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5381}
5382
5383TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384 addConfigurationProperty("touch.deviceType", "touchScreen");
5385 prepareButtons();
5386 prepareAxes(POSITION);
5387 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389
5390 NotifyMotionArgs args;
5391
5392 // Rotation 90.
5393 prepareDisplay(DISPLAY_ORIENTATION_90);
5394 processDown(mapper, toRawX(50), toRawY(75));
5395 processSync(mapper);
5396
5397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5398 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5399 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5400
5401 processUp(mapper);
5402 processSync(mapper);
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5404}
5405
5406TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 addConfigurationProperty("touch.deviceType", "touchScreen");
5408 prepareButtons();
5409 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005410 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411
5412 NotifyMotionArgs args;
5413
5414 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005415 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 prepareDisplay(DISPLAY_ORIENTATION_0);
5417 processDown(mapper, toRawX(50), toRawY(75));
5418 processSync(mapper);
5419
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5421 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5422 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5423
5424 processUp(mapper);
5425 processSync(mapper);
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5427
5428 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005429 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430 prepareDisplay(DISPLAY_ORIENTATION_90);
5431 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5432 processSync(mapper);
5433
5434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5435 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5436 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5437
5438 processUp(mapper);
5439 processSync(mapper);
5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5441
5442 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005443 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 prepareDisplay(DISPLAY_ORIENTATION_180);
5445 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5446 processSync(mapper);
5447
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5449 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5450 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5451
5452 processUp(mapper);
5453 processSync(mapper);
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5455
5456 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005457 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458 prepareDisplay(DISPLAY_ORIENTATION_270);
5459 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5460 processSync(mapper);
5461
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5463 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5464 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5465
5466 processUp(mapper);
5467 processSync(mapper);
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5469}
5470
5471TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 addConfigurationProperty("touch.deviceType", "touchScreen");
5473 prepareDisplay(DISPLAY_ORIENTATION_0);
5474 prepareButtons();
5475 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005476 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477
5478 // These calculations are based on the input device calibration documentation.
5479 int32_t rawX = 100;
5480 int32_t rawY = 200;
5481 int32_t rawPressure = 10;
5482 int32_t rawToolMajor = 12;
5483 int32_t rawDistance = 2;
5484 int32_t rawTiltX = 30;
5485 int32_t rawTiltY = 110;
5486
5487 float x = toDisplayX(rawX);
5488 float y = toDisplayY(rawY);
5489 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5490 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5491 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5492 float distance = float(rawDistance);
5493
5494 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5495 float tiltScale = M_PI / 180;
5496 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5497 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5498 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5499 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5500
5501 processDown(mapper, rawX, rawY);
5502 processPressure(mapper, rawPressure);
5503 processToolMajor(mapper, rawToolMajor);
5504 processDistance(mapper, rawDistance);
5505 processTilt(mapper, rawTiltX, rawTiltY);
5506 processSync(mapper);
5507
5508 NotifyMotionArgs args;
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5511 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5512 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5513}
5514
Jason Gerecke489fda82012-09-07 17:19:40 -07005515TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005516 addConfigurationProperty("touch.deviceType", "touchScreen");
5517 prepareDisplay(DISPLAY_ORIENTATION_0);
5518 prepareLocationCalibration();
5519 prepareButtons();
5520 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005521 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005522
5523 int32_t rawX = 100;
5524 int32_t rawY = 200;
5525
5526 float x = toDisplayX(toCookedX(rawX, rawY));
5527 float y = toDisplayY(toCookedY(rawX, rawY));
5528
5529 processDown(mapper, rawX, rawY);
5530 processSync(mapper);
5531
5532 NotifyMotionArgs args;
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5535 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5536}
5537
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 addConfigurationProperty("touch.deviceType", "touchScreen");
5540 prepareDisplay(DISPLAY_ORIENTATION_0);
5541 prepareButtons();
5542 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005543 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544
5545 NotifyMotionArgs motionArgs;
5546 NotifyKeyArgs keyArgs;
5547
5548 processDown(mapper, 100, 200);
5549 processSync(mapper);
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5552 ASSERT_EQ(0, motionArgs.buttonState);
5553
5554 // press BTN_LEFT, release BTN_LEFT
5555 processKey(mapper, BTN_LEFT, 1);
5556 processSync(mapper);
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5559 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5560
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5562 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5563 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5564
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565 processKey(mapper, BTN_LEFT, 0);
5566 processSync(mapper);
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005568 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005570
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005573 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574
5575 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5576 processKey(mapper, BTN_RIGHT, 1);
5577 processKey(mapper, BTN_MIDDLE, 1);
5578 processSync(mapper);
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5581 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5582 motionArgs.buttonState);
5583
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5586 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5587
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5589 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5590 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5591 motionArgs.buttonState);
5592
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 processKey(mapper, BTN_RIGHT, 0);
5594 processSync(mapper);
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005598
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005601 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602
5603 processKey(mapper, BTN_MIDDLE, 0);
5604 processSync(mapper);
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005611 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612
5613 // press BTN_BACK, release BTN_BACK
5614 processKey(mapper, BTN_BACK, 1);
5615 processSync(mapper);
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5617 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5618 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005619
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005622 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5623
5624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5626 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627
5628 processKey(mapper, BTN_BACK, 0);
5629 processSync(mapper);
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005633
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005636 ASSERT_EQ(0, motionArgs.buttonState);
5637
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5639 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5640 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5641
5642 // press BTN_SIDE, release BTN_SIDE
5643 processKey(mapper, BTN_SIDE, 1);
5644 processSync(mapper);
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5646 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5647 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005648
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005651 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5655 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656
5657 processKey(mapper, BTN_SIDE, 0);
5658 processSync(mapper);
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005662
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005665 ASSERT_EQ(0, motionArgs.buttonState);
5666
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5668 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5669 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5670
5671 // press BTN_FORWARD, release BTN_FORWARD
5672 processKey(mapper, BTN_FORWARD, 1);
5673 processSync(mapper);
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5675 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5676 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005677
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005680 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5681
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5683 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5684 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685
5686 processKey(mapper, BTN_FORWARD, 0);
5687 processSync(mapper);
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005691
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005694 ASSERT_EQ(0, motionArgs.buttonState);
5695
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5697 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5698 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5699
5700 // press BTN_EXTRA, release BTN_EXTRA
5701 processKey(mapper, BTN_EXTRA, 1);
5702 processSync(mapper);
5703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5704 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5705 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005709 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5710
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5713 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005714
5715 processKey(mapper, BTN_EXTRA, 0);
5716 processSync(mapper);
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005719 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005720
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005723 ASSERT_EQ(0, motionArgs.buttonState);
5724
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5726 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5727 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5728
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5730
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 // press BTN_STYLUS, release BTN_STYLUS
5732 processKey(mapper, BTN_STYLUS, 1);
5733 processSync(mapper);
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005736 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5737
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5740 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741
5742 processKey(mapper, BTN_STYLUS, 0);
5743 processSync(mapper);
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005745 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005747
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005750 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005751
5752 // press BTN_STYLUS2, release BTN_STYLUS2
5753 processKey(mapper, BTN_STYLUS2, 1);
5754 processSync(mapper);
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005757 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5758
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5761 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762
5763 processKey(mapper, BTN_STYLUS2, 0);
5764 processSync(mapper);
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005766 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005768
5769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005771 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 // release touch
5774 processUp(mapper);
5775 processSync(mapper);
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5777 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5778 ASSERT_EQ(0, motionArgs.buttonState);
5779}
5780
5781TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782 addConfigurationProperty("touch.deviceType", "touchScreen");
5783 prepareDisplay(DISPLAY_ORIENTATION_0);
5784 prepareButtons();
5785 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005786 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787
5788 NotifyMotionArgs motionArgs;
5789
5790 // default tool type is finger
5791 processDown(mapper, 100, 200);
5792 processSync(mapper);
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5794 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5796
5797 // eraser
5798 processKey(mapper, BTN_TOOL_RUBBER, 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_ERASER, motionArgs.pointerProperties[0].toolType);
5803
5804 // stylus
5805 processKey(mapper, BTN_TOOL_RUBBER, 0);
5806 processKey(mapper, BTN_TOOL_PEN, 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
5812 // brush
5813 processKey(mapper, BTN_TOOL_PEN, 0);
5814 processKey(mapper, BTN_TOOL_BRUSH, 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 // pencil
5821 processKey(mapper, BTN_TOOL_BRUSH, 0);
5822 processKey(mapper, BTN_TOOL_PENCIL, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
5827
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005828 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829 processKey(mapper, BTN_TOOL_PENCIL, 0);
5830 processKey(mapper, BTN_TOOL_AIRBRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
5835
5836 // mouse
5837 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5838 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
5843
5844 // lens
5845 processKey(mapper, BTN_TOOL_MOUSE, 0);
5846 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
5851
5852 // double-tap
5853 processKey(mapper, BTN_TOOL_LENS, 0);
5854 processKey(mapper, BTN_TOOL_DOUBLETAP, 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 // triple-tap
5861 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5862 processKey(mapper, BTN_TOOL_TRIPLETAP, 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 // quad-tap
5869 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5870 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5875
5876 // finger
5877 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5878 processKey(mapper, BTN_TOOL_FINGER, 1);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5883
5884 // stylus trumps finger
5885 processKey(mapper, BTN_TOOL_PEN, 1);
5886 processSync(mapper);
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5890
5891 // eraser trumps stylus
5892 processKey(mapper, BTN_TOOL_RUBBER, 1);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5897
5898 // mouse trumps eraser
5899 processKey(mapper, BTN_TOOL_MOUSE, 1);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5904
5905 // back to default tool type
5906 processKey(mapper, BTN_TOOL_MOUSE, 0);
5907 processKey(mapper, BTN_TOOL_RUBBER, 0);
5908 processKey(mapper, BTN_TOOL_PEN, 0);
5909 processKey(mapper, BTN_TOOL_FINGER, 0);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5914}
5915
5916TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 addConfigurationProperty("touch.deviceType", "touchScreen");
5918 prepareDisplay(DISPLAY_ORIENTATION_0);
5919 prepareButtons();
5920 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005921 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005922 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923
5924 NotifyMotionArgs motionArgs;
5925
5926 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5927 processKey(mapper, BTN_TOOL_FINGER, 1);
5928 processMove(mapper, 100, 200);
5929 processSync(mapper);
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5934
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5938 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5939
5940 // move a little
5941 processMove(mapper, 150, 250);
5942 processSync(mapper);
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5946 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5947
5948 // down when BTN_TOUCH is pressed, pressure defaults to 1
5949 processKey(mapper, BTN_TOUCH, 1);
5950 processSync(mapper);
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5954 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5959 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5960
5961 // up when BTN_TOUCH is released, hover restored
5962 processKey(mapper, BTN_TOUCH, 0);
5963 processSync(mapper);
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5965 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5967 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5968
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5972 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5973
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5977 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5978
5979 // exit hover when pointer goes away
5980 processKey(mapper, BTN_TOOL_FINGER, 0);
5981 processSync(mapper);
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5985 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5986}
5987
5988TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 addConfigurationProperty("touch.deviceType", "touchScreen");
5990 prepareDisplay(DISPLAY_ORIENTATION_0);
5991 prepareButtons();
5992 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005993 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994
5995 NotifyMotionArgs motionArgs;
5996
5997 // initially hovering because pressure is 0
5998 processDown(mapper, 100, 200);
5999 processPressure(mapper, 0);
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6004 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6009 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6010
6011 // move a little
6012 processMove(mapper, 150, 250);
6013 processSync(mapper);
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6015 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6017 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6018
6019 // down when pressure is non-zero
6020 processPressure(mapper, RAW_PRESSURE_MAX);
6021 processSync(mapper);
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6025 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6026
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6030 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6031
6032 // up when pressure becomes 0, hover restored
6033 processPressure(mapper, 0);
6034 processSync(mapper);
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6038 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6039
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6041 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6043 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6044
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6046 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6048 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6049
6050 // exit hover when pointer goes away
6051 processUp(mapper);
6052 processSync(mapper);
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6056 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6057}
6058
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059// --- MultiTouchInputMapperTest ---
6060
6061class MultiTouchInputMapperTest : public TouchInputMapperTest {
6062protected:
6063 void prepareAxes(int axes);
6064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006065 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6066 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6067 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6068 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6069 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6070 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6071 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6072 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6073 void processId(MultiTouchInputMapper& mapper, int32_t id);
6074 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6075 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6076 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6077 void processMTSync(MultiTouchInputMapper& mapper);
6078 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079};
6080
6081void MultiTouchInputMapperTest::prepareAxes(int axes) {
6082 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6084 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 }
6086 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006087 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6088 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6091 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 }
6093 }
6094 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6096 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6099 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 }
6101 }
6102 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6104 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 }
6106 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6108 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 }
6110 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6112 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113 }
6114 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006115 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6116 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117 }
6118 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006119 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6120 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 }
6122 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006123 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 }
6125}
6126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006127void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6128 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131}
6132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006133void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6134 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136}
6137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006138void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6139 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141}
6142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006143void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145}
6146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006147void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149}
6150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006151void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6152 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154}
6155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006156void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158}
6159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006160void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162}
6163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006164void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166}
6167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006168void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170}
6171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006172void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174}
6175
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006176void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6177 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179}
6180
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006181void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183}
6184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006185void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187}
6188
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 addConfigurationProperty("touch.deviceType", "touchScreen");
6191 prepareDisplay(DISPLAY_ORIENTATION_0);
6192 prepareAxes(POSITION);
6193 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006194 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195
arthurhungdcef2dc2020-08-11 14:47:50 +08006196 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197
6198 NotifyMotionArgs motionArgs;
6199
6200 // Two fingers down at once.
6201 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6202 processPosition(mapper, x1, y1);
6203 processMTSync(mapper);
6204 processPosition(mapper, x2, y2);
6205 processMTSync(mapper);
6206 processSync(mapper);
6207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6210 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6211 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6212 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6214 ASSERT_EQ(0, motionArgs.flags);
6215 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6216 ASSERT_EQ(0, motionArgs.buttonState);
6217 ASSERT_EQ(0, motionArgs.edgeFlags);
6218 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6219 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6222 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6223 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6224 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6225 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6226
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6229 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6231 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6233 motionArgs.action);
6234 ASSERT_EQ(0, motionArgs.flags);
6235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6236 ASSERT_EQ(0, motionArgs.buttonState);
6237 ASSERT_EQ(0, motionArgs.edgeFlags);
6238 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6241 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6244 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6246 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6247 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6248 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6250
6251 // Move.
6252 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6253 processPosition(mapper, x1, y1);
6254 processMTSync(mapper);
6255 processPosition(mapper, x2, y2);
6256 processMTSync(mapper);
6257 processSync(mapper);
6258
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6261 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6262 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6263 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6265 ASSERT_EQ(0, motionArgs.flags);
6266 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6267 ASSERT_EQ(0, motionArgs.buttonState);
6268 ASSERT_EQ(0, motionArgs.edgeFlags);
6269 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6270 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6272 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6275 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6277 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6278 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6279 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6280 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6281
6282 // First finger up.
6283 x2 += 15; y2 -= 20;
6284 processPosition(mapper, x2, y2);
6285 processMTSync(mapper);
6286 processSync(mapper);
6287
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6290 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6291 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6292 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6294 motionArgs.action);
6295 ASSERT_EQ(0, motionArgs.flags);
6296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6297 ASSERT_EQ(0, motionArgs.buttonState);
6298 ASSERT_EQ(0, motionArgs.edgeFlags);
6299 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6300 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6302 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6305 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6307 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6308 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6309 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6311
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6314 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6315 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6316 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6318 ASSERT_EQ(0, motionArgs.flags);
6319 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6320 ASSERT_EQ(0, motionArgs.buttonState);
6321 ASSERT_EQ(0, motionArgs.edgeFlags);
6322 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6323 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6326 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6327 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6328 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6330
6331 // Move.
6332 x2 += 20; y2 -= 25;
6333 processPosition(mapper, x2, y2);
6334 processMTSync(mapper);
6335 processSync(mapper);
6336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6339 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6341 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6343 ASSERT_EQ(0, motionArgs.flags);
6344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6345 ASSERT_EQ(0, motionArgs.buttonState);
6346 ASSERT_EQ(0, motionArgs.edgeFlags);
6347 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6348 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6351 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6352 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6353 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6355
6356 // New finger down.
6357 int32_t x3 = 700, y3 = 300;
6358 processPosition(mapper, x2, y2);
6359 processMTSync(mapper);
6360 processPosition(mapper, x3, y3);
6361 processMTSync(mapper);
6362 processSync(mapper);
6363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6366 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6367 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6368 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6370 motionArgs.action);
6371 ASSERT_EQ(0, motionArgs.flags);
6372 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6373 ASSERT_EQ(0, motionArgs.buttonState);
6374 ASSERT_EQ(0, motionArgs.edgeFlags);
6375 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6376 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6378 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6381 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6383 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6384 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6385 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6387
6388 // Second finger up.
6389 x3 += 30; y3 -= 20;
6390 processPosition(mapper, x3, y3);
6391 processMTSync(mapper);
6392 processSync(mapper);
6393
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6396 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6397 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6398 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6400 motionArgs.action);
6401 ASSERT_EQ(0, motionArgs.flags);
6402 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6403 ASSERT_EQ(0, motionArgs.buttonState);
6404 ASSERT_EQ(0, motionArgs.edgeFlags);
6405 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6406 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6408 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6411 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6413 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6414 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6415 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6416 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6420 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6421 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6422 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6424 ASSERT_EQ(0, motionArgs.flags);
6425 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6426 ASSERT_EQ(0, motionArgs.buttonState);
6427 ASSERT_EQ(0, motionArgs.edgeFlags);
6428 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6429 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6432 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6433 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6434 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6435 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6436
6437 // Last finger up.
6438 processMTSync(mapper);
6439 processSync(mapper);
6440
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6443 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6444 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6445 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6447 ASSERT_EQ(0, motionArgs.flags);
6448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6449 ASSERT_EQ(0, motionArgs.buttonState);
6450 ASSERT_EQ(0, motionArgs.edgeFlags);
6451 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6452 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6455 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6456 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6457 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6459
6460 // Should not have sent any more keys or motions.
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6463}
6464
6465TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006466 addConfigurationProperty("touch.deviceType", "touchScreen");
6467 prepareDisplay(DISPLAY_ORIENTATION_0);
6468 prepareAxes(POSITION | ID);
6469 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006470 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471
arthurhungdcef2dc2020-08-11 14:47:50 +08006472 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473
6474 NotifyMotionArgs motionArgs;
6475
6476 // Two fingers down at once.
6477 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6478 processPosition(mapper, x1, y1);
6479 processId(mapper, 1);
6480 processMTSync(mapper);
6481 processPosition(mapper, x2, y2);
6482 processId(mapper, 2);
6483 processMTSync(mapper);
6484 processSync(mapper);
6485
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6488 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6489 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6492 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6493
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6496 motionArgs.action);
6497 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6498 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6500 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6503 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6505 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6506
6507 // Move.
6508 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6509 processPosition(mapper, x1, y1);
6510 processId(mapper, 1);
6511 processMTSync(mapper);
6512 processPosition(mapper, x2, y2);
6513 processId(mapper, 2);
6514 processMTSync(mapper);
6515 processSync(mapper);
6516
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6519 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6522 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6527 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6528
6529 // First finger up.
6530 x2 += 15; y2 -= 20;
6531 processPosition(mapper, x2, y2);
6532 processId(mapper, 2);
6533 processMTSync(mapper);
6534 processSync(mapper);
6535
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6538 motionArgs.action);
6539 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6542 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6545 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6547 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6548
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6551 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6552 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6555 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6556
6557 // Move.
6558 x2 += 20; y2 -= 25;
6559 processPosition(mapper, x2, y2);
6560 processId(mapper, 2);
6561 processMTSync(mapper);
6562 processSync(mapper);
6563
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6566 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6567 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6570 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6571
6572 // New finger down.
6573 int32_t x3 = 700, y3 = 300;
6574 processPosition(mapper, x2, y2);
6575 processId(mapper, 2);
6576 processMTSync(mapper);
6577 processPosition(mapper, x3, y3);
6578 processId(mapper, 3);
6579 processMTSync(mapper);
6580 processSync(mapper);
6581
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6584 motionArgs.action);
6585 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6588 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6591 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6593 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6594
6595 // Second finger up.
6596 x3 += 30; y3 -= 20;
6597 processPosition(mapper, x3, y3);
6598 processId(mapper, 3);
6599 processMTSync(mapper);
6600 processSync(mapper);
6601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6604 motionArgs.action);
6605 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6608 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6614
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6617 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6618 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6621 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6622
6623 // Last finger up.
6624 processMTSync(mapper);
6625 processSync(mapper);
6626
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6628 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6629 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6633 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6634
6635 // Should not have sent any more keys or motions.
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6638}
6639
6640TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006641 addConfigurationProperty("touch.deviceType", "touchScreen");
6642 prepareDisplay(DISPLAY_ORIENTATION_0);
6643 prepareAxes(POSITION | ID | SLOT);
6644 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006645 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646
arthurhungdcef2dc2020-08-11 14:47:50 +08006647 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006648
6649 NotifyMotionArgs motionArgs;
6650
6651 // Two fingers down at once.
6652 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6653 processPosition(mapper, x1, y1);
6654 processId(mapper, 1);
6655 processSlot(mapper, 1);
6656 processPosition(mapper, x2, y2);
6657 processId(mapper, 2);
6658 processSync(mapper);
6659
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6661 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6662 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6667
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6670 motionArgs.action);
6671 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6672 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6674 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6679 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6680
6681 // Move.
6682 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6683 processSlot(mapper, 0);
6684 processPosition(mapper, x1, y1);
6685 processSlot(mapper, 1);
6686 processPosition(mapper, x2, y2);
6687 processSync(mapper);
6688
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6691 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6700
6701 // First finger up.
6702 x2 += 15; y2 -= 20;
6703 processSlot(mapper, 0);
6704 processId(mapper, -1);
6705 processSlot(mapper, 1);
6706 processPosition(mapper, x2, y2);
6707 processSync(mapper);
6708
6709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6711 motionArgs.action);
6712 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6715 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6718 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6720 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6721
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6724 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6725 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6728 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6729
6730 // Move.
6731 x2 += 20; y2 -= 25;
6732 processPosition(mapper, x2, y2);
6733 processSync(mapper);
6734
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6737 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6738 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6742
6743 // New finger down.
6744 int32_t x3 = 700, y3 = 300;
6745 processPosition(mapper, x2, y2);
6746 processSlot(mapper, 0);
6747 processId(mapper, 3);
6748 processPosition(mapper, x3, y3);
6749 processSync(mapper);
6750
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6753 motionArgs.action);
6754 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6755 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6757 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6760 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6762 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6763
6764 // Second finger up.
6765 x3 += 30; y3 -= 20;
6766 processSlot(mapper, 1);
6767 processId(mapper, -1);
6768 processSlot(mapper, 0);
6769 processPosition(mapper, x3, y3);
6770 processSync(mapper);
6771
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6773 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6774 motionArgs.action);
6775 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6776 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6778 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6781 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6783 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6784
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6787 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6791 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6792
6793 // Last finger up.
6794 processId(mapper, -1);
6795 processSync(mapper);
6796
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6798 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6799 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6803 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6804
6805 // Should not have sent any more keys or motions.
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6808}
6809
6810TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006811 addConfigurationProperty("touch.deviceType", "touchScreen");
6812 prepareDisplay(DISPLAY_ORIENTATION_0);
6813 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006814 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815
6816 // These calculations are based on the input device calibration documentation.
6817 int32_t rawX = 100;
6818 int32_t rawY = 200;
6819 int32_t rawTouchMajor = 7;
6820 int32_t rawTouchMinor = 6;
6821 int32_t rawToolMajor = 9;
6822 int32_t rawToolMinor = 8;
6823 int32_t rawPressure = 11;
6824 int32_t rawDistance = 0;
6825 int32_t rawOrientation = 3;
6826 int32_t id = 5;
6827
6828 float x = toDisplayX(rawX);
6829 float y = toDisplayY(rawY);
6830 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6831 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6832 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6833 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6834 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6835 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6836 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6837 float distance = float(rawDistance);
6838
6839 processPosition(mapper, rawX, rawY);
6840 processTouchMajor(mapper, rawTouchMajor);
6841 processTouchMinor(mapper, rawTouchMinor);
6842 processToolMajor(mapper, rawToolMajor);
6843 processToolMinor(mapper, rawToolMinor);
6844 processPressure(mapper, rawPressure);
6845 processOrientation(mapper, rawOrientation);
6846 processDistance(mapper, rawDistance);
6847 processId(mapper, id);
6848 processMTSync(mapper);
6849 processSync(mapper);
6850
6851 NotifyMotionArgs args;
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6853 ASSERT_EQ(0, args.pointerProperties[0].id);
6854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6855 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6856 orientation, distance));
6857}
6858
6859TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006860 addConfigurationProperty("touch.deviceType", "touchScreen");
6861 prepareDisplay(DISPLAY_ORIENTATION_0);
6862 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6863 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006865
6866 // These calculations are based on the input device calibration documentation.
6867 int32_t rawX = 100;
6868 int32_t rawY = 200;
6869 int32_t rawTouchMajor = 140;
6870 int32_t rawTouchMinor = 120;
6871 int32_t rawToolMajor = 180;
6872 int32_t rawToolMinor = 160;
6873
6874 float x = toDisplayX(rawX);
6875 float y = toDisplayY(rawY);
6876 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6877 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6878 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6879 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6880 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6881
6882 processPosition(mapper, rawX, rawY);
6883 processTouchMajor(mapper, rawTouchMajor);
6884 processTouchMinor(mapper, rawTouchMinor);
6885 processToolMajor(mapper, rawToolMajor);
6886 processToolMinor(mapper, rawToolMinor);
6887 processMTSync(mapper);
6888 processSync(mapper);
6889
6890 NotifyMotionArgs args;
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6893 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6894}
6895
6896TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897 addConfigurationProperty("touch.deviceType", "touchScreen");
6898 prepareDisplay(DISPLAY_ORIENTATION_0);
6899 prepareAxes(POSITION | TOUCH | TOOL);
6900 addConfigurationProperty("touch.size.calibration", "diameter");
6901 addConfigurationProperty("touch.size.scale", "10");
6902 addConfigurationProperty("touch.size.bias", "160");
6903 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006904 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905
6906 // These calculations are based on the input device calibration documentation.
6907 // Note: We only provide a single common touch/tool value because the device is assumed
6908 // not to emit separate values for each pointer (isSummed = 1).
6909 int32_t rawX = 100;
6910 int32_t rawY = 200;
6911 int32_t rawX2 = 150;
6912 int32_t rawY2 = 250;
6913 int32_t rawTouchMajor = 5;
6914 int32_t rawToolMajor = 8;
6915
6916 float x = toDisplayX(rawX);
6917 float y = toDisplayY(rawY);
6918 float x2 = toDisplayX(rawX2);
6919 float y2 = toDisplayY(rawY2);
6920 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6921 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6922 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6923
6924 processPosition(mapper, rawX, rawY);
6925 processTouchMajor(mapper, rawTouchMajor);
6926 processToolMajor(mapper, rawToolMajor);
6927 processMTSync(mapper);
6928 processPosition(mapper, rawX2, rawY2);
6929 processTouchMajor(mapper, rawTouchMajor);
6930 processToolMajor(mapper, rawToolMajor);
6931 processMTSync(mapper);
6932 processSync(mapper);
6933
6934 NotifyMotionArgs args;
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6936 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6937
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6939 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6940 args.action);
6941 ASSERT_EQ(size_t(2), args.pointerCount);
6942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6943 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6945 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6946}
6947
6948TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949 addConfigurationProperty("touch.deviceType", "touchScreen");
6950 prepareDisplay(DISPLAY_ORIENTATION_0);
6951 prepareAxes(POSITION | TOUCH | TOOL);
6952 addConfigurationProperty("touch.size.calibration", "area");
6953 addConfigurationProperty("touch.size.scale", "43");
6954 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006955 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006956
6957 // These calculations are based on the input device calibration documentation.
6958 int32_t rawX = 100;
6959 int32_t rawY = 200;
6960 int32_t rawTouchMajor = 5;
6961 int32_t rawToolMajor = 8;
6962
6963 float x = toDisplayX(rawX);
6964 float y = toDisplayY(rawY);
6965 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6966 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6967 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6968
6969 processPosition(mapper, rawX, rawY);
6970 processTouchMajor(mapper, rawTouchMajor);
6971 processToolMajor(mapper, rawToolMajor);
6972 processMTSync(mapper);
6973 processSync(mapper);
6974
6975 NotifyMotionArgs args;
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6978 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6979}
6980
6981TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006982 addConfigurationProperty("touch.deviceType", "touchScreen");
6983 prepareDisplay(DISPLAY_ORIENTATION_0);
6984 prepareAxes(POSITION | PRESSURE);
6985 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6986 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006987 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006988
Michael Wrightaa449c92017-12-13 21:21:43 +00006989 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006990 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006991 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6992 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6993 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6994
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995 // These calculations are based on the input device calibration documentation.
6996 int32_t rawX = 100;
6997 int32_t rawY = 200;
6998 int32_t rawPressure = 60;
6999
7000 float x = toDisplayX(rawX);
7001 float y = toDisplayY(rawY);
7002 float pressure = float(rawPressure) * 0.01f;
7003
7004 processPosition(mapper, rawX, rawY);
7005 processPressure(mapper, rawPressure);
7006 processMTSync(mapper);
7007 processSync(mapper);
7008
7009 NotifyMotionArgs args;
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7012 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7013}
7014
7015TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007016 addConfigurationProperty("touch.deviceType", "touchScreen");
7017 prepareDisplay(DISPLAY_ORIENTATION_0);
7018 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007020
7021 NotifyMotionArgs motionArgs;
7022 NotifyKeyArgs keyArgs;
7023
7024 processId(mapper, 1);
7025 processPosition(mapper, 100, 200);
7026 processSync(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7028 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7029 ASSERT_EQ(0, motionArgs.buttonState);
7030
7031 // press BTN_LEFT, release BTN_LEFT
7032 processKey(mapper, BTN_LEFT, 1);
7033 processSync(mapper);
7034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7036 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7037
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7040 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7041
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042 processKey(mapper, BTN_LEFT, 0);
7043 processSync(mapper);
7044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007046 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007047
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007050 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007051
7052 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7053 processKey(mapper, BTN_RIGHT, 1);
7054 processKey(mapper, BTN_MIDDLE, 1);
7055 processSync(mapper);
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7058 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7059 motionArgs.buttonState);
7060
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7063 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7064
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7067 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7068 motionArgs.buttonState);
7069
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070 processKey(mapper, BTN_RIGHT, 0);
7071 processSync(mapper);
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007075
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007078 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079
7080 processKey(mapper, BTN_MIDDLE, 0);
7081 processSync(mapper);
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007085
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007088 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007089
7090 // press BTN_BACK, release BTN_BACK
7091 processKey(mapper, BTN_BACK, 1);
7092 processSync(mapper);
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7094 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7095 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007096
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007099 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7100
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7103 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007104
7105 processKey(mapper, BTN_BACK, 0);
7106 processSync(mapper);
7107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007110
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007113 ASSERT_EQ(0, motionArgs.buttonState);
7114
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7117 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7118
7119 // press BTN_SIDE, release BTN_SIDE
7120 processKey(mapper, BTN_SIDE, 1);
7121 processSync(mapper);
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7123 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7124 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007125
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007128 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7129
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7132 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007133
7134 processKey(mapper, BTN_SIDE, 0);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007139
7140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007142 ASSERT_EQ(0, motionArgs.buttonState);
7143
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7147
7148 // press BTN_FORWARD, release BTN_FORWARD
7149 processKey(mapper, BTN_FORWARD, 1);
7150 processSync(mapper);
7151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7152 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7153 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007154
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007157 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7158
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7161 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162
7163 processKey(mapper, BTN_FORWARD, 0);
7164 processSync(mapper);
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007168
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007171 ASSERT_EQ(0, motionArgs.buttonState);
7172
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7174 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7175 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7176
7177 // press BTN_EXTRA, release BTN_EXTRA
7178 processKey(mapper, BTN_EXTRA, 1);
7179 processSync(mapper);
7180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7181 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7182 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007183
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007186 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7187
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7190 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191
7192 processKey(mapper, BTN_EXTRA, 0);
7193 processSync(mapper);
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007197
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007200 ASSERT_EQ(0, motionArgs.buttonState);
7201
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7203 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7204 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7205
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7207
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208 // press BTN_STYLUS, release BTN_STYLUS
7209 processKey(mapper, BTN_STYLUS, 1);
7210 processSync(mapper);
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007213 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7214
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7217 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218
7219 processKey(mapper, BTN_STYLUS, 0);
7220 processSync(mapper);
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007224
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007227 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228
7229 // press BTN_STYLUS2, release BTN_STYLUS2
7230 processKey(mapper, BTN_STYLUS2, 1);
7231 processSync(mapper);
7232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007234 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7235
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7238 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007239
7240 processKey(mapper, BTN_STYLUS2, 0);
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007244 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007245
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007248 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007249
7250 // release touch
7251 processId(mapper, -1);
7252 processSync(mapper);
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7254 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7255 ASSERT_EQ(0, motionArgs.buttonState);
7256}
7257
7258TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007259 addConfigurationProperty("touch.deviceType", "touchScreen");
7260 prepareDisplay(DISPLAY_ORIENTATION_0);
7261 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007262 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263
7264 NotifyMotionArgs motionArgs;
7265
7266 // default tool type is finger
7267 processId(mapper, 1);
7268 processPosition(mapper, 100, 200);
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7273
7274 // eraser
7275 processKey(mapper, BTN_TOOL_RUBBER, 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_ERASER, motionArgs.pointerProperties[0].toolType);
7280
7281 // stylus
7282 processKey(mapper, BTN_TOOL_RUBBER, 0);
7283 processKey(mapper, BTN_TOOL_PEN, 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
7289 // brush
7290 processKey(mapper, BTN_TOOL_PEN, 0);
7291 processKey(mapper, BTN_TOOL_BRUSH, 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 // pencil
7298 processKey(mapper, BTN_TOOL_BRUSH, 0);
7299 processKey(mapper, BTN_TOOL_PENCIL, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
7304
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007305 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306 processKey(mapper, BTN_TOOL_PENCIL, 0);
7307 processKey(mapper, BTN_TOOL_AIRBRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
7312
7313 // mouse
7314 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7315 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
7320
7321 // lens
7322 processKey(mapper, BTN_TOOL_MOUSE, 0);
7323 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
7328
7329 // double-tap
7330 processKey(mapper, BTN_TOOL_LENS, 0);
7331 processKey(mapper, BTN_TOOL_DOUBLETAP, 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 // triple-tap
7338 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7339 processKey(mapper, BTN_TOOL_TRIPLETAP, 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 // quad-tap
7346 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7347 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7352
7353 // finger
7354 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7355 processKey(mapper, BTN_TOOL_FINGER, 1);
7356 processSync(mapper);
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7360
7361 // stylus trumps finger
7362 processKey(mapper, BTN_TOOL_PEN, 1);
7363 processSync(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7367
7368 // eraser trumps stylus
7369 processKey(mapper, BTN_TOOL_RUBBER, 1);
7370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7374
7375 // mouse trumps eraser
7376 processKey(mapper, BTN_TOOL_MOUSE, 1);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7381
7382 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7383 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7384 processSync(mapper);
7385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7388
7389 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7390 processToolType(mapper, MT_TOOL_PEN);
7391 processSync(mapper);
7392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7395
7396 // back to default tool type
7397 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7398 processKey(mapper, BTN_TOOL_MOUSE, 0);
7399 processKey(mapper, BTN_TOOL_RUBBER, 0);
7400 processKey(mapper, BTN_TOOL_PEN, 0);
7401 processKey(mapper, BTN_TOOL_FINGER, 0);
7402 processSync(mapper);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7406}
7407
7408TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409 addConfigurationProperty("touch.deviceType", "touchScreen");
7410 prepareDisplay(DISPLAY_ORIENTATION_0);
7411 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007412 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007413 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007414
7415 NotifyMotionArgs motionArgs;
7416
7417 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7418 processId(mapper, 1);
7419 processPosition(mapper, 100, 200);
7420 processSync(mapper);
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7424 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7425
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7429 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7430
7431 // move a little
7432 processPosition(mapper, 150, 250);
7433 processSync(mapper);
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7437 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7438
7439 // down when BTN_TOUCH is pressed, pressure defaults to 1
7440 processKey(mapper, BTN_TOUCH, 1);
7441 processSync(mapper);
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7443 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7445 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7446
7447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7450 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7451
7452 // up when BTN_TOUCH is released, hover restored
7453 processKey(mapper, BTN_TOUCH, 0);
7454 processSync(mapper);
7455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7458 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7459
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7463 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7464
7465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7466 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7468 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7469
7470 // exit hover when pointer goes away
7471 processId(mapper, -1);
7472 processSync(mapper);
7473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7476 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7477}
7478
7479TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007480 addConfigurationProperty("touch.deviceType", "touchScreen");
7481 prepareDisplay(DISPLAY_ORIENTATION_0);
7482 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007483 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007484
7485 NotifyMotionArgs motionArgs;
7486
7487 // initially hovering because pressure is 0
7488 processId(mapper, 1);
7489 processPosition(mapper, 100, 200);
7490 processPressure(mapper, 0);
7491 processSync(mapper);
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7495 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7496
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7500 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7501
7502 // move a little
7503 processPosition(mapper, 150, 250);
7504 processSync(mapper);
7505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7508 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7509
7510 // down when pressure becomes non-zero
7511 processPressure(mapper, RAW_PRESSURE_MAX);
7512 processSync(mapper);
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7514 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7516 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7517
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7521 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7522
7523 // up when pressure becomes 0, hover restored
7524 processPressure(mapper, 0);
7525 processSync(mapper);
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7527 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7529 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7530
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7534 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7535
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7539 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7540
7541 // exit hover when pointer goes away
7542 processId(mapper, -1);
7543 processSync(mapper);
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7545 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7547 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7548}
7549
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007550/**
7551 * Set the input device port <--> display port associations, and check that the
7552 * events are routed to the display that matches the display port.
7553 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7554 */
7555TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007556 const std::string usb2 = "USB2";
7557 const uint8_t hdmi1 = 0;
7558 const uint8_t hdmi2 = 1;
7559 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007560 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007561
7562 addConfigurationProperty("touch.deviceType", "touchScreen");
7563 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007564 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007565
7566 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7567 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7568
7569 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7570 // for this input device is specified, and the matching viewport is not present,
7571 // the input device should be disabled (at the mapper level).
7572
7573 // Add viewport for display 2 on hdmi2
7574 prepareSecondaryDisplay(type, hdmi2);
7575 // Send a touch event
7576 processPosition(mapper, 100, 100);
7577 processSync(mapper);
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7579
7580 // Add viewport for display 1 on hdmi1
7581 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7582 // Send a touch event again
7583 processPosition(mapper, 100, 100);
7584 processSync(mapper);
7585
7586 NotifyMotionArgs args;
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7588 ASSERT_EQ(DISPLAY_ID, args.displayId);
7589}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007590
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007591TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007592 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007593 std::shared_ptr<FakePointerController> fakePointerController =
7594 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007595 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007596 fakePointerController->setPosition(100, 200);
7597 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007598 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7599
Garfield Tan888a6a42020-01-09 11:39:16 -08007600 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007601 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007602
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007603 prepareDisplay(DISPLAY_ORIENTATION_0);
7604 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007605 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007606
7607 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007608 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007609
7610 NotifyMotionArgs motionArgs;
7611 processPosition(mapper, 100, 100);
7612 processSync(mapper);
7613
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7616 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7617}
7618
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007619/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007620 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7621 */
7622TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7623 addConfigurationProperty("touch.deviceType", "touchScreen");
7624 prepareAxes(POSITION);
7625 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7626
7627 prepareDisplay(DISPLAY_ORIENTATION_0);
7628 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7629 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7630 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7631 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7632
7633 NotifyMotionArgs args;
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7635 ASSERT_EQ(26, args.readTime);
7636
7637 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7638 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7639 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7640
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7642 ASSERT_EQ(33, args.readTime);
7643}
7644
7645/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007646 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7647 * events should not be delivered to the listener.
7648 */
7649TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7650 addConfigurationProperty("touch.deviceType", "touchScreen");
7651 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7652 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7653 ViewportType::INTERNAL);
7654 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7655 prepareAxes(POSITION);
7656 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7657
7658 NotifyMotionArgs motionArgs;
7659 processPosition(mapper, 100, 100);
7660 processSync(mapper);
7661
7662 mFakeListener->assertNotifyMotionWasNotCalled();
7663}
7664
Garfield Tanc734e4f2021-01-15 20:01:39 -08007665TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7666 addConfigurationProperty("touch.deviceType", "touchScreen");
7667 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7668 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7669 ViewportType::INTERNAL);
7670 std::optional<DisplayViewport> optionalDisplayViewport =
7671 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7672 ASSERT_TRUE(optionalDisplayViewport.has_value());
7673 DisplayViewport displayViewport = *optionalDisplayViewport;
7674
7675 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7676 prepareAxes(POSITION);
7677 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7678
7679 // Finger down
7680 int32_t x = 100, y = 100;
7681 processPosition(mapper, x, y);
7682 processSync(mapper);
7683
7684 NotifyMotionArgs motionArgs;
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7686 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7687
7688 // Deactivate display viewport
7689 displayViewport.isActive = false;
7690 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7691 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7692
7693 // Finger move
7694 x += 10, y += 10;
7695 processPosition(mapper, x, y);
7696 processSync(mapper);
7697
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7699 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7700
7701 // Reactivate display viewport
7702 displayViewport.isActive = true;
7703 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7704 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7705
7706 // Finger move again
7707 x += 10, y += 10;
7708 processPosition(mapper, x, y);
7709 processSync(mapper);
7710
7711 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7712 // no pointer on the touch device.
7713 mFakeListener->assertNotifyMotionWasNotCalled();
7714}
7715
Arthur Hung7c645402019-01-25 17:45:42 +08007716TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7717 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007718 prepareAxes(POSITION | ID | SLOT);
7719 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007720 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007721
7722 // Create the second touch screen device, and enable multi fingers.
7723 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007724 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007725 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007726 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007727 std::shared_ptr<InputDevice> device2 =
7728 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7729 Flags<InputDeviceClass>(0));
7730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007731 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7732 0 /*flat*/, 0 /*fuzz*/);
7733 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7734 0 /*flat*/, 0 /*fuzz*/);
7735 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7736 0 /*flat*/, 0 /*fuzz*/);
7737 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7738 0 /*flat*/, 0 /*fuzz*/);
7739 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7740 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7741 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007742
7743 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007744 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007745 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7746 device2->reset(ARBITRARY_TIME);
7747
7748 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007749 std::shared_ptr<FakePointerController> fakePointerController =
7750 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007751 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7752 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7753
7754 // Setup policy for associated displays and show touches.
7755 const uint8_t hdmi1 = 0;
7756 const uint8_t hdmi2 = 1;
7757 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7758 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7759 mFakePolicy->setShowTouches(true);
7760
7761 // Create displays.
7762 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007763 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007764
7765 // Default device will reconfigure above, need additional reconfiguration for another device.
7766 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007767 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007768
7769 // Two fingers down at default display.
7770 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7771 processPosition(mapper, x1, y1);
7772 processId(mapper, 1);
7773 processSlot(mapper, 1);
7774 processPosition(mapper, x2, y2);
7775 processId(mapper, 2);
7776 processSync(mapper);
7777
7778 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7779 fakePointerController->getSpots().find(DISPLAY_ID);
7780 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7781 ASSERT_EQ(size_t(2), iter->second.size());
7782
7783 // Two fingers down at second display.
7784 processPosition(mapper2, x1, y1);
7785 processId(mapper2, 1);
7786 processSlot(mapper2, 1);
7787 processPosition(mapper2, x2, y2);
7788 processId(mapper2, 2);
7789 processSync(mapper2);
7790
7791 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7792 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7793 ASSERT_EQ(size_t(2), iter->second.size());
7794}
7795
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007796TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007797 prepareAxes(POSITION);
7798 addConfigurationProperty("touch.deviceType", "touchScreen");
7799 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007801
7802 NotifyMotionArgs motionArgs;
7803 // Unrotated video frame
7804 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7805 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007806 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007807 processPosition(mapper, 100, 200);
7808 processSync(mapper);
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7810 ASSERT_EQ(frames, motionArgs.videoFrames);
7811
7812 // Subsequent touch events should not have any videoframes
7813 // This is implemented separately in FakeEventHub,
7814 // but that should match the behaviour of TouchVideoDevice.
7815 processPosition(mapper, 200, 200);
7816 processSync(mapper);
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7818 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7819}
7820
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007821TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007822 prepareAxes(POSITION);
7823 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007824 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007825 // Unrotated video frame
7826 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7827 NotifyMotionArgs motionArgs;
7828
7829 // Test all 4 orientations
7830 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7831 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7832 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7833 clearViewports();
7834 prepareDisplay(orientation);
7835 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007836 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007837 processPosition(mapper, 100, 200);
7838 processSync(mapper);
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7840 frames[0].rotate(orientation);
7841 ASSERT_EQ(frames, motionArgs.videoFrames);
7842 }
7843}
7844
7845TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007846 prepareAxes(POSITION);
7847 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007848 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007849 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7850 // so mix these.
7851 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7852 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7853 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7854 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7855 NotifyMotionArgs motionArgs;
7856
7857 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007858 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007859 processPosition(mapper, 100, 200);
7860 processSync(mapper);
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7862 std::for_each(frames.begin(), frames.end(),
7863 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7864 ASSERT_EQ(frames, motionArgs.videoFrames);
7865}
7866
Arthur Hung9da14732019-09-02 16:16:58 +08007867/**
7868 * If we had defined port associations, but the viewport is not ready, the touch device would be
7869 * expected to be disabled, and it should be enabled after the viewport has found.
7870 */
7871TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007872 constexpr uint8_t hdmi2 = 1;
7873 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007874 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007875
7876 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7877
7878 addConfigurationProperty("touch.deviceType", "touchScreen");
7879 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007880 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007881
7882 ASSERT_EQ(mDevice->isEnabled(), false);
7883
7884 // Add display on hdmi2, the device should be enabled and can receive touch event.
7885 prepareSecondaryDisplay(type, hdmi2);
7886 ASSERT_EQ(mDevice->isEnabled(), true);
7887
7888 // Send a touch event.
7889 processPosition(mapper, 100, 100);
7890 processSync(mapper);
7891
7892 NotifyMotionArgs args;
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7894 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7895}
7896
Arthur Hung421eb1c2020-01-16 00:09:42 +08007897TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007898 addConfigurationProperty("touch.deviceType", "touchScreen");
7899 prepareDisplay(DISPLAY_ORIENTATION_0);
7900 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007902
7903 NotifyMotionArgs motionArgs;
7904
7905 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7906 // finger down
7907 processId(mapper, 1);
7908 processPosition(mapper, x1, y1);
7909 processSync(mapper);
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7913
7914 // finger move
7915 processId(mapper, 1);
7916 processPosition(mapper, x2, y2);
7917 processSync(mapper);
7918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7921
7922 // finger up.
7923 processId(mapper, -1);
7924 processSync(mapper);
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7926 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7928
7929 // new finger down
7930 processId(mapper, 1);
7931 processPosition(mapper, x3, y3);
7932 processSync(mapper);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7936}
7937
7938/**
arthurhungcc7f9802020-04-30 17:55:40 +08007939 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7940 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007941 */
arthurhungcc7f9802020-04-30 17:55:40 +08007942TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007943 addConfigurationProperty("touch.deviceType", "touchScreen");
7944 prepareDisplay(DISPLAY_ORIENTATION_0);
7945 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007946 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007947
7948 NotifyMotionArgs motionArgs;
7949
7950 // default tool type is finger
7951 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007952 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007953 processPosition(mapper, x1, y1);
7954 processSync(mapper);
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7956 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7958
7959 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7960 processToolType(mapper, MT_TOOL_PALM);
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7963 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7964
7965 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007966 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007967 processPosition(mapper, x2, y2);
7968 processSync(mapper);
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7970
7971 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007972 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007973 processSync(mapper);
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7975
7976 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007977 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007978 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007979 processPosition(mapper, x3, y3);
7980 processSync(mapper);
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7984}
7985
arthurhungbf89a482020-04-17 17:37:55 +08007986/**
arthurhungcc7f9802020-04-30 17:55:40 +08007987 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7988 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007989 */
arthurhungcc7f9802020-04-30 17:55:40 +08007990TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007991 addConfigurationProperty("touch.deviceType", "touchScreen");
7992 prepareDisplay(DISPLAY_ORIENTATION_0);
7993 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7995
7996 NotifyMotionArgs motionArgs;
7997
7998 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007999 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8000 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008001 processPosition(mapper, x1, y1);
8002 processSync(mapper);
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8004 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8006
8007 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008008 processSlot(mapper, SECOND_SLOT);
8009 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008010 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008011 processSync(mapper);
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8013 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8014 motionArgs.action);
8015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8016
8017 // If the tool type of the first finger changes to MT_TOOL_PALM,
8018 // we expect to receive ACTION_POINTER_UP with cancel flag.
8019 processSlot(mapper, FIRST_SLOT);
8020 processId(mapper, FIRST_TRACKING_ID);
8021 processToolType(mapper, MT_TOOL_PALM);
8022 processSync(mapper);
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8024 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8025 motionArgs.action);
8026 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8027
8028 // The following MOVE events of second finger should be processed.
8029 processSlot(mapper, SECOND_SLOT);
8030 processId(mapper, SECOND_TRACKING_ID);
8031 processPosition(mapper, x2 + 1, y2 + 1);
8032 processSync(mapper);
8033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8035 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8036
8037 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8038 // it. Second finger receive move.
8039 processSlot(mapper, FIRST_SLOT);
8040 processId(mapper, INVALID_TRACKING_ID);
8041 processSync(mapper);
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8044 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8045
8046 // Second finger keeps moving.
8047 processSlot(mapper, SECOND_SLOT);
8048 processId(mapper, SECOND_TRACKING_ID);
8049 processPosition(mapper, x2 + 2, y2 + 2);
8050 processSync(mapper);
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8053 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8054
8055 // Second finger up.
8056 processId(mapper, INVALID_TRACKING_ID);
8057 processSync(mapper);
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8060 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8061}
8062
8063/**
8064 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8065 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8066 */
8067TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8068 addConfigurationProperty("touch.deviceType", "touchScreen");
8069 prepareDisplay(DISPLAY_ORIENTATION_0);
8070 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8071 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8072
8073 NotifyMotionArgs motionArgs;
8074
8075 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8076 // First finger down.
8077 processId(mapper, FIRST_TRACKING_ID);
8078 processPosition(mapper, x1, y1);
8079 processSync(mapper);
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8083
8084 // Second finger down.
8085 processSlot(mapper, SECOND_SLOT);
8086 processId(mapper, SECOND_TRACKING_ID);
8087 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8091 motionArgs.action);
8092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8093
arthurhungcc7f9802020-04-30 17:55:40 +08008094 // If the tool type of the first finger changes to MT_TOOL_PALM,
8095 // we expect to receive ACTION_POINTER_UP with cancel flag.
8096 processSlot(mapper, FIRST_SLOT);
8097 processId(mapper, FIRST_TRACKING_ID);
8098 processToolType(mapper, MT_TOOL_PALM);
8099 processSync(mapper);
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8101 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8102 motionArgs.action);
8103 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8104
8105 // Second finger keeps moving.
8106 processSlot(mapper, SECOND_SLOT);
8107 processId(mapper, SECOND_TRACKING_ID);
8108 processPosition(mapper, x2 + 1, y2 + 1);
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8112
8113 // second finger becomes palm, receive cancel due to only 1 finger is active.
8114 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008115 processToolType(mapper, MT_TOOL_PALM);
8116 processSync(mapper);
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8119
arthurhungcc7f9802020-04-30 17:55:40 +08008120 // third finger down.
8121 processSlot(mapper, THIRD_SLOT);
8122 processId(mapper, THIRD_TRACKING_ID);
8123 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008124 processPosition(mapper, x3, y3);
8125 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008129 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8130
8131 // third finger move
8132 processId(mapper, THIRD_TRACKING_ID);
8133 processPosition(mapper, x3 + 1, y3 + 1);
8134 processSync(mapper);
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8137
8138 // first finger up, third finger receive move.
8139 processSlot(mapper, FIRST_SLOT);
8140 processId(mapper, INVALID_TRACKING_ID);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8144 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8145
8146 // second finger up, third finger receive move.
8147 processSlot(mapper, SECOND_SLOT);
8148 processId(mapper, INVALID_TRACKING_ID);
8149 processSync(mapper);
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8152 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8153
8154 // third finger up.
8155 processSlot(mapper, THIRD_SLOT);
8156 processId(mapper, INVALID_TRACKING_ID);
8157 processSync(mapper);
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8159 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8160 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8161}
8162
8163/**
8164 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8165 * and the active finger could still be allowed to receive the events
8166 */
8167TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8168 addConfigurationProperty("touch.deviceType", "touchScreen");
8169 prepareDisplay(DISPLAY_ORIENTATION_0);
8170 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8172
8173 NotifyMotionArgs motionArgs;
8174
8175 // default tool type is finger
8176 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8177 processId(mapper, FIRST_TRACKING_ID);
8178 processPosition(mapper, x1, y1);
8179 processSync(mapper);
8180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8183
8184 // Second finger down.
8185 processSlot(mapper, SECOND_SLOT);
8186 processId(mapper, SECOND_TRACKING_ID);
8187 processPosition(mapper, x2, y2);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8191 motionArgs.action);
8192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8193
8194 // If the tool type of the second finger changes to MT_TOOL_PALM,
8195 // we expect to receive ACTION_POINTER_UP with cancel flag.
8196 processId(mapper, SECOND_TRACKING_ID);
8197 processToolType(mapper, MT_TOOL_PALM);
8198 processSync(mapper);
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8200 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8201 motionArgs.action);
8202 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8203
8204 // The following MOVE event should be processed.
8205 processSlot(mapper, FIRST_SLOT);
8206 processId(mapper, FIRST_TRACKING_ID);
8207 processPosition(mapper, x1 + 1, y1 + 1);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8211 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8212
8213 // second finger up.
8214 processSlot(mapper, SECOND_SLOT);
8215 processId(mapper, INVALID_TRACKING_ID);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8219
8220 // first finger keep moving
8221 processSlot(mapper, FIRST_SLOT);
8222 processId(mapper, FIRST_TRACKING_ID);
8223 processPosition(mapper, x1 + 2, y1 + 2);
8224 processSync(mapper);
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8227
8228 // first finger up.
8229 processId(mapper, INVALID_TRACKING_ID);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8233 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008234}
8235
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008236// --- MultiTouchInputMapperTest_ExternalDevice ---
8237
8238class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8239protected:
Chris Yea52ade12020-08-27 16:49:20 -07008240 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008241};
8242
8243/**
8244 * Expect fallback to internal viewport if device is external and external viewport is not present.
8245 */
8246TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8247 prepareAxes(POSITION);
8248 addConfigurationProperty("touch.deviceType", "touchScreen");
8249 prepareDisplay(DISPLAY_ORIENTATION_0);
8250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8251
8252 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8253
8254 NotifyMotionArgs motionArgs;
8255
8256 // Expect the event to be sent to the internal viewport,
8257 // because an external viewport is not present.
8258 processPosition(mapper, 100, 100);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8262
8263 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008264 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008265 processPosition(mapper, 100, 100);
8266 processSync(mapper);
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8268 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8269}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008270
8271/**
8272 * Test touch should not work if outside of surface.
8273 */
8274class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8275protected:
8276 void halfDisplayToCenterHorizontal(int32_t orientation) {
8277 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008278 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008279
8280 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8281 internalViewport->orientation = orientation;
8282 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8283 internalViewport->logicalLeft = 0;
8284 internalViewport->logicalTop = 0;
8285 internalViewport->logicalRight = DISPLAY_HEIGHT;
8286 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8287
8288 internalViewport->physicalLeft = 0;
8289 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8290 internalViewport->physicalRight = DISPLAY_HEIGHT;
8291 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8292
8293 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8294 internalViewport->deviceHeight = DISPLAY_WIDTH;
8295 } else {
8296 internalViewport->logicalLeft = 0;
8297 internalViewport->logicalTop = 0;
8298 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8299 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8300
8301 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8302 internalViewport->physicalTop = 0;
8303 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8304 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8305
8306 internalViewport->deviceWidth = DISPLAY_WIDTH;
8307 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8308 }
8309
8310 mFakePolicy->updateViewport(internalViewport.value());
8311 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8312 }
8313
arthurhung5d547942020-12-14 17:04:45 +08008314 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8315 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008316 int32_t yExpected) {
8317 // touch on outside area should not work.
8318 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8319 processSync(mapper);
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8321
8322 // touch on inside area should receive the event.
8323 NotifyMotionArgs args;
8324 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8325 processSync(mapper);
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8327 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8328 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8329
8330 // Reset.
8331 mapper.reset(ARBITRARY_TIME);
8332 }
8333};
8334
arthurhung5d547942020-12-14 17:04:45 +08008335TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008336 addConfigurationProperty("touch.deviceType", "touchScreen");
8337 prepareDisplay(DISPLAY_ORIENTATION_0);
8338 prepareAxes(POSITION);
8339 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8340
8341 // Touch on center of normal display should work.
8342 const int32_t x = DISPLAY_WIDTH / 4;
8343 const int32_t y = DISPLAY_HEIGHT / 2;
8344 processPosition(mapper, toRawX(x), toRawY(y));
8345 processSync(mapper);
8346 NotifyMotionArgs args;
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8349 0.0f, 0.0f, 0.0f, 0.0f));
8350 // Reset.
8351 mapper.reset(ARBITRARY_TIME);
8352
8353 // Let physical display be different to device, and make surface and physical could be 1:1.
8354 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8355
8356 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8357 const int32_t yExpected = y;
8358 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8359}
8360
arthurhung5d547942020-12-14 17:04:45 +08008361TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008362 addConfigurationProperty("touch.deviceType", "touchScreen");
8363 prepareDisplay(DISPLAY_ORIENTATION_0);
8364 prepareAxes(POSITION);
8365 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8366
8367 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8368 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8369
8370 const int32_t x = DISPLAY_WIDTH / 4;
8371 const int32_t y = DISPLAY_HEIGHT / 2;
8372
8373 // expect x/y = swap x/y then reverse y.
8374 const int32_t xExpected = y;
8375 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8376 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8377}
8378
arthurhung5d547942020-12-14 17:04:45 +08008379TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008380 addConfigurationProperty("touch.deviceType", "touchScreen");
8381 prepareDisplay(DISPLAY_ORIENTATION_0);
8382 prepareAxes(POSITION);
8383 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8384
8385 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8386 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8387
8388 const int32_t x = DISPLAY_WIDTH / 4;
8389 const int32_t y = DISPLAY_HEIGHT / 2;
8390
8391 // expect x/y = swap x/y then reverse x.
8392 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8393 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8394 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8395}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008396
arthurhunga36b28e2020-12-29 20:28:15 +08008397TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8398 addConfigurationProperty("touch.deviceType", "touchScreen");
8399 prepareDisplay(DISPLAY_ORIENTATION_0);
8400 prepareAxes(POSITION);
8401 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8402
8403 const int32_t x = 0;
8404 const int32_t y = 0;
8405
8406 const int32_t xExpected = x;
8407 const int32_t yExpected = y;
8408 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8409
8410 clearViewports();
8411 prepareDisplay(DISPLAY_ORIENTATION_90);
8412 // expect x/y = swap x/y then reverse y.
8413 const int32_t xExpected90 = y;
8414 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8415 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8416
8417 clearViewports();
8418 prepareDisplay(DISPLAY_ORIENTATION_270);
8419 // expect x/y = swap x/y then reverse x.
8420 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8421 const int32_t yExpected270 = x;
8422 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8423}
8424
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008425TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8426 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8427 std::shared_ptr<FakePointerController> fakePointerController =
8428 std::make_shared<FakePointerController>();
8429 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8430 fakePointerController->setPosition(0, 0);
8431 fakePointerController->setButtonState(0);
8432
8433 // prepare device and capture
8434 prepareDisplay(DISPLAY_ORIENTATION_0);
8435 prepareAxes(POSITION | ID | SLOT);
8436 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8437 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8438 mFakePolicy->setPointerCapture(true);
8439 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8440 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8441
8442 // captured touchpad should be a touchpad source
8443 NotifyDeviceResetArgs resetArgs;
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8445 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8446
Chris Yef74dc422020-09-02 22:41:50 -07008447 InputDeviceInfo deviceInfo;
8448 mDevice->getDeviceInfo(&deviceInfo);
8449
8450 const InputDeviceInfo::MotionRange* relRangeX =
8451 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8452 ASSERT_NE(relRangeX, nullptr);
8453 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8454 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8455 const InputDeviceInfo::MotionRange* relRangeY =
8456 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8457 ASSERT_NE(relRangeY, nullptr);
8458 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8459 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8460
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008461 // run captured pointer tests - note that this is unscaled, so input listener events should be
8462 // identical to what the hardware sends (accounting for any
8463 // calibration).
8464 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008465 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008466 processId(mapper, 1);
8467 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8468 processKey(mapper, BTN_TOUCH, 1);
8469 processSync(mapper);
8470
8471 // expect coord[0] to contain initial location of touch 0
8472 NotifyMotionArgs args;
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8475 ASSERT_EQ(1U, args.pointerCount);
8476 ASSERT_EQ(0, args.pointerProperties[0].id);
8477 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8478 ASSERT_NO_FATAL_FAILURE(
8479 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8480
8481 // FINGER 1 DOWN
8482 processSlot(mapper, 1);
8483 processId(mapper, 2);
8484 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8485 processSync(mapper);
8486
8487 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008489 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8490 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008491 ASSERT_EQ(2U, args.pointerCount);
8492 ASSERT_EQ(0, args.pointerProperties[0].id);
8493 ASSERT_EQ(1, args.pointerProperties[1].id);
8494 ASSERT_NO_FATAL_FAILURE(
8495 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8496 ASSERT_NO_FATAL_FAILURE(
8497 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8498
8499 // FINGER 1 MOVE
8500 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8501 processSync(mapper);
8502
8503 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8504 // from move
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8507 ASSERT_NO_FATAL_FAILURE(
8508 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8509 ASSERT_NO_FATAL_FAILURE(
8510 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8511
8512 // FINGER 0 MOVE
8513 processSlot(mapper, 0);
8514 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8515 processSync(mapper);
8516
8517 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8520 ASSERT_NO_FATAL_FAILURE(
8521 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8522 ASSERT_NO_FATAL_FAILURE(
8523 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8524
8525 // BUTTON DOWN
8526 processKey(mapper, BTN_LEFT, 1);
8527 processSync(mapper);
8528
8529 // touchinputmapper design sends a move before button press
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8534
8535 // BUTTON UP
8536 processKey(mapper, BTN_LEFT, 0);
8537 processSync(mapper);
8538
8539 // touchinputmapper design sends a move after button release
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8544
8545 // FINGER 0 UP
8546 processId(mapper, -1);
8547 processSync(mapper);
8548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8549 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8550
8551 // FINGER 1 MOVE
8552 processSlot(mapper, 1);
8553 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8554 processSync(mapper);
8555
8556 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8559 ASSERT_EQ(1U, args.pointerCount);
8560 ASSERT_EQ(1, args.pointerProperties[0].id);
8561 ASSERT_NO_FATAL_FAILURE(
8562 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8563
8564 // FINGER 1 UP
8565 processId(mapper, -1);
8566 processKey(mapper, BTN_TOUCH, 0);
8567 processSync(mapper);
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8570
8571 // non captured touchpad should be a mouse source
8572 mFakePolicy->setPointerCapture(false);
8573 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8575 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8576}
8577
8578TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8579 std::shared_ptr<FakePointerController> fakePointerController =
8580 std::make_shared<FakePointerController>();
8581 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8582 fakePointerController->setPosition(0, 0);
8583 fakePointerController->setButtonState(0);
8584
8585 // prepare device and capture
8586 prepareDisplay(DISPLAY_ORIENTATION_0);
8587 prepareAxes(POSITION | ID | SLOT);
8588 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8589 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8590 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8591 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8592 // run uncaptured pointer tests - pushes out generic events
8593 // FINGER 0 DOWN
8594 processId(mapper, 3);
8595 processPosition(mapper, 100, 100);
8596 processKey(mapper, BTN_TOUCH, 1);
8597 processSync(mapper);
8598
8599 // start at (100,100), cursor should be at (0,0) * scale
8600 NotifyMotionArgs args;
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8603 ASSERT_NO_FATAL_FAILURE(
8604 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8605
8606 // FINGER 0 MOVE
8607 processPosition(mapper, 200, 200);
8608 processSync(mapper);
8609
8610 // compute scaling to help with touch position checking
8611 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8612 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8613 float scale =
8614 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8615
8616 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8620 0, 0, 0, 0, 0, 0, 0));
8621}
8622
8623TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8624 std::shared_ptr<FakePointerController> fakePointerController =
8625 std::make_shared<FakePointerController>();
8626
8627 prepareDisplay(DISPLAY_ORIENTATION_0);
8628 prepareAxes(POSITION | ID | SLOT);
8629 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8630 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8631 mFakePolicy->setPointerCapture(false);
8632 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8633
8634 // uncaptured touchpad should be a pointer device
8635 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8636
8637 // captured touchpad should be a touchpad device
8638 mFakePolicy->setPointerCapture(true);
8639 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8640 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8641}
8642
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008643// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008644
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008645class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008646protected:
8647 static const char* DEVICE_NAME;
8648 static const char* DEVICE_LOCATION;
8649 static const int32_t DEVICE_ID;
8650 static const int32_t DEVICE_GENERATION;
8651 static const int32_t DEVICE_CONTROLLER_NUMBER;
8652 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8653 static const int32_t EVENTHUB_ID;
8654
8655 std::shared_ptr<FakeEventHub> mFakeEventHub;
8656 sp<FakeInputReaderPolicy> mFakePolicy;
8657 sp<TestInputListener> mFakeListener;
8658 std::unique_ptr<InstrumentedInputReader> mReader;
8659 std::shared_ptr<InputDevice> mDevice;
8660
8661 virtual void SetUp(Flags<InputDeviceClass> classes) {
8662 mFakeEventHub = std::make_unique<FakeEventHub>();
8663 mFakePolicy = new FakeInputReaderPolicy();
8664 mFakeListener = new TestInputListener();
8665 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8666 mFakeListener);
8667 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8668 }
8669
8670 void SetUp() override { SetUp(DEVICE_CLASSES); }
8671
8672 void TearDown() override {
8673 mFakeListener.clear();
8674 mFakePolicy.clear();
8675 }
8676
8677 void configureDevice(uint32_t changes) {
8678 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8679 mReader->requestRefreshConfiguration(changes);
8680 mReader->loopOnce();
8681 }
8682 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8683 }
8684
8685 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8686 const std::string& location, int32_t eventHubId,
8687 Flags<InputDeviceClass> classes) {
8688 InputDeviceIdentifier identifier;
8689 identifier.name = name;
8690 identifier.location = location;
8691 std::shared_ptr<InputDevice> device =
8692 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8693 identifier);
8694 mReader->pushNextDevice(device);
8695 mFakeEventHub->addDevice(eventHubId, name, classes);
8696 mReader->loopOnce();
8697 return device;
8698 }
8699
8700 template <class T, typename... Args>
8701 T& addControllerAndConfigure(Args... args) {
8702 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8703
8704 return controller;
8705 }
8706};
8707
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008708const char* PeripheralControllerTest::DEVICE_NAME = "device";
8709const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8710const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8711const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8712const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8713const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008714 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008715const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008716
8717// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008718class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008719protected:
8720 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008721 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008722 }
8723};
8724
8725TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008726 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008727
8728 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8729 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8730}
8731
8732TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008733 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008734
8735 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8736 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8737}
8738
8739// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008740class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008741protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008742 void SetUp() override {
8743 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8744 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008745};
8746
Chris Ye85758332021-05-16 23:05:17 -07008747TEST_F(LightControllerTest, MonoLight) {
8748 RawLightInfo infoMono = {.id = 1,
8749 .name = "Mono",
8750 .maxBrightness = 255,
8751 .flags = InputLightClass::BRIGHTNESS,
8752 .path = ""};
8753 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008754
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008755 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008756 InputDeviceInfo info;
8757 controller.populateDeviceInfo(&info);
8758 const auto& ids = info.getLightIds();
8759 ASSERT_EQ(1UL, ids.size());
Chris Ye85758332021-05-16 23:05:17 -07008760 ASSERT_EQ(InputDeviceLightType::MONO, info.getLightInfo(ids[0])->type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008761
8762 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_BRIGHTNESS));
8763 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_BRIGHTNESS);
8764}
8765
8766TEST_F(LightControllerTest, RGBLight) {
8767 RawLightInfo infoRed = {.id = 1,
8768 .name = "red",
8769 .maxBrightness = 255,
8770 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8771 .path = ""};
8772 RawLightInfo infoGreen = {.id = 2,
8773 .name = "green",
8774 .maxBrightness = 255,
8775 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8776 .path = ""};
8777 RawLightInfo infoBlue = {.id = 3,
8778 .name = "blue",
8779 .maxBrightness = 255,
8780 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8781 .path = ""};
8782 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8783 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8784 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8785
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008786 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008787 InputDeviceInfo info;
8788 controller.populateDeviceInfo(&info);
8789 const auto& ids = info.getLightIds();
8790 ASSERT_EQ(1UL, ids.size());
8791 ASSERT_EQ(InputDeviceLightType::RGB, info.getLightInfo(ids[0])->type);
8792
8793 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_COLOR));
8794 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
8795}
8796
8797TEST_F(LightControllerTest, MultiColorRGBLight) {
8798 RawLightInfo infoColor = {.id = 1,
8799 .name = "red",
8800 .maxBrightness = 255,
8801 .flags = InputLightClass::BRIGHTNESS |
8802 InputLightClass::MULTI_INTENSITY |
8803 InputLightClass::MULTI_INDEX,
8804 .path = ""};
8805
8806 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8807
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008808 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008809 InputDeviceInfo info;
8810 controller.populateDeviceInfo(&info);
8811 const auto& ids = info.getLightIds();
8812 ASSERT_EQ(1UL, ids.size());
8813 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, info.getLightInfo(ids[0])->type);
8814
8815 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_COLOR));
8816 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
8817}
8818
8819TEST_F(LightControllerTest, PlayerIdLight) {
8820 RawLightInfo info1 = {.id = 1,
8821 .name = "player1",
8822 .maxBrightness = 255,
8823 .flags = InputLightClass::BRIGHTNESS,
8824 .path = ""};
8825 RawLightInfo info2 = {.id = 2,
8826 .name = "player2",
8827 .maxBrightness = 255,
8828 .flags = InputLightClass::BRIGHTNESS,
8829 .path = ""};
8830 RawLightInfo info3 = {.id = 3,
8831 .name = "player3",
8832 .maxBrightness = 255,
8833 .flags = InputLightClass::BRIGHTNESS,
8834 .path = ""};
8835 RawLightInfo info4 = {.id = 4,
8836 .name = "player4",
8837 .maxBrightness = 255,
8838 .flags = InputLightClass::BRIGHTNESS,
8839 .path = ""};
8840 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8841 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8842 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8843 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8844
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008845 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008846 InputDeviceInfo info;
8847 controller.populateDeviceInfo(&info);
8848 const auto& ids = info.getLightIds();
8849 ASSERT_EQ(1UL, ids.size());
8850 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, info.getLightInfo(ids[0])->type);
8851
8852 ASSERT_FALSE(controller.setLightColor(ids[0], LIGHT_COLOR));
8853 ASSERT_TRUE(controller.setLightPlayerId(ids[0], LIGHT_PLAYER_ID));
8854 ASSERT_EQ(controller.getLightPlayerId(ids[0]).value_or(-1), LIGHT_PLAYER_ID);
8855}
8856
Michael Wrightd02c5b62014-02-10 15:10:22 -08008857} // namespace android