blob: 46923caccf0f3d13a430d451bb8f68b1299c8fd9 [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
2771 static void assertPointerCoords(const PointerCoords& coords,
2772 float x, float y, float pressure, float size,
2773 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2774 float orientation, float distance) {
2775 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2776 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2777 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2778 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2779 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2780 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2781 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2782 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2783 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2784 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2785 }
2786
Michael Wright17db18e2020-06-26 20:51:44 +01002787 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002789 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790 ASSERT_NEAR(x, actualX, 1);
2791 ASSERT_NEAR(y, actualY, 1);
2792 }
2793};
2794
2795const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002796const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002797const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2799const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002800const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2801 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002802const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803
2804// --- SwitchInputMapperTest ---
2805
2806class SwitchInputMapperTest : public InputMapperTest {
2807protected:
2808};
2809
2810TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002811 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002813 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814}
2815
2816TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002817 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002819 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002820 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002822 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824}
2825
2826TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002827 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833
2834 NotifySwitchArgs args;
2835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2836 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002837 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2838 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839 args.switchMask);
2840 ASSERT_EQ(uint32_t(0), args.policyFlags);
2841}
2842
Chris Ye87143712020-11-10 05:05:58 +00002843// --- VibratorInputMapperTest ---
2844class VibratorInputMapperTest : public InputMapperTest {
2845protected:
2846 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2847};
2848
2849TEST_F(VibratorInputMapperTest, GetSources) {
2850 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2851
2852 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2853}
2854
2855TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2856 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2857
2858 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2859}
2860
2861TEST_F(VibratorInputMapperTest, Vibrate) {
2862 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002863 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002864 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2865
2866 VibrationElement pattern(2);
2867 VibrationSequence sequence(2);
2868 pattern.duration = std::chrono::milliseconds(200);
2869 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2870 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2871 sequence.addElement(pattern);
2872 pattern.duration = std::chrono::milliseconds(500);
2873 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2874 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2875 sequence.addElement(pattern);
2876
2877 std::vector<int64_t> timings = {0, 1};
2878 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2879
2880 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002881 // Start vibrating
2882 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002883 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002884 // Verify vibrator state listener was notified.
2885 mReader->loopOnce();
2886 NotifyVibratorStateArgs args;
2887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2888 ASSERT_EQ(DEVICE_ID, args.deviceId);
2889 ASSERT_TRUE(args.isOn);
2890 // Stop vibrating
2891 mapper.cancelVibrate(VIBRATION_TOKEN);
2892 ASSERT_FALSE(mapper.isVibrating());
2893 // Verify vibrator state listener was notified.
2894 mReader->loopOnce();
2895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2896 ASSERT_EQ(DEVICE_ID, args.deviceId);
2897 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002898}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002899
Chris Yef59a2f42020-10-16 12:55:26 -07002900// --- SensorInputMapperTest ---
2901
2902class SensorInputMapperTest : public InputMapperTest {
2903protected:
2904 static const int32_t ACCEL_RAW_MIN;
2905 static const int32_t ACCEL_RAW_MAX;
2906 static const int32_t ACCEL_RAW_FUZZ;
2907 static const int32_t ACCEL_RAW_FLAT;
2908 static const int32_t ACCEL_RAW_RESOLUTION;
2909
2910 static const int32_t GYRO_RAW_MIN;
2911 static const int32_t GYRO_RAW_MAX;
2912 static const int32_t GYRO_RAW_FUZZ;
2913 static const int32_t GYRO_RAW_FLAT;
2914 static const int32_t GYRO_RAW_RESOLUTION;
2915
2916 static const float GRAVITY_MS2_UNIT;
2917 static const float DEGREE_RADIAN_UNIT;
2918
2919 void prepareAccelAxes();
2920 void prepareGyroAxes();
2921 void setAccelProperties();
2922 void setGyroProperties();
2923 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2924};
2925
2926const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2927const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2928const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2929const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2930const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2931
2932const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2933const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2934const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2935const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2936const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2937
2938const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2939const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2940
2941void SensorInputMapperTest::prepareAccelAxes() {
2942 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2943 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2944 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2945 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2946 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2947 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2948}
2949
2950void SensorInputMapperTest::prepareGyroAxes() {
2951 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2952 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2953 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2954 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2955 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2956 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2957}
2958
2959void SensorInputMapperTest::setAccelProperties() {
2960 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2961 /* sensorDataIndex */ 0);
2962 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2963 /* sensorDataIndex */ 1);
2964 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2965 /* sensorDataIndex */ 2);
2966 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2967 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2968 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2969 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2970 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2971}
2972
2973void SensorInputMapperTest::setGyroProperties() {
2974 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2975 /* sensorDataIndex */ 0);
2976 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2977 /* sensorDataIndex */ 1);
2978 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2979 /* sensorDataIndex */ 2);
2980 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2981 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2982 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2983 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2984 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2985}
2986
2987TEST_F(SensorInputMapperTest, GetSources) {
2988 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2989
2990 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2991}
2992
2993TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2994 setAccelProperties();
2995 prepareAccelAxes();
2996 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2997
2998 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2999 std::chrono::microseconds(10000),
3000 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003001 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003007
3008 NotifySensorArgs args;
3009 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3010 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3011 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3012
3013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3014 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3015 ASSERT_EQ(args.deviceId, DEVICE_ID);
3016 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3017 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3018 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3019 ASSERT_EQ(args.values, values);
3020 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3021}
3022
3023TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3024 setGyroProperties();
3025 prepareGyroAxes();
3026 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3027
3028 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3029 std::chrono::microseconds(10000),
3030 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003031 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003037
3038 NotifySensorArgs args;
3039 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3040 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3041 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3042
3043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3044 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3045 ASSERT_EQ(args.deviceId, DEVICE_ID);
3046 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3047 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3048 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3049 ASSERT_EQ(args.values, values);
3050 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3051}
3052
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053// --- KeyboardInputMapperTest ---
3054
3055class KeyboardInputMapperTest : public InputMapperTest {
3056protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003057 const std::string UNIQUE_ID = "local:0";
3058
3059 void prepareDisplay(int32_t orientation);
3060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003061 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003062 int32_t originalKeyCode, int32_t rotatedKeyCode,
3063 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064};
3065
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003066/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3067 * orientation.
3068 */
3069void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003070 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3071 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003072}
3073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003075 int32_t originalScanCode, int32_t originalKeyCode,
3076 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 NotifyKeyArgs args;
3078
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3081 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3082 ASSERT_EQ(originalScanCode, args.scanCode);
3083 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003084 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3088 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3089 ASSERT_EQ(originalScanCode, args.scanCode);
3090 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003091 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 KeyboardInputMapper& mapper =
3096 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3097 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100}
3101
3102TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3103 const int32_t USAGE_A = 0x070004;
3104 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003105 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3106 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003107 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3108 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3109 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003111 KeyboardInputMapper& mapper =
3112 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3113 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003114 // Initial metastate to AMETA_NONE.
3115 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3116 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117
3118 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120 NotifyKeyArgs args;
3121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3122 ASSERT_EQ(DEVICE_ID, args.deviceId);
3123 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3124 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3125 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3126 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3127 ASSERT_EQ(KEY_HOME, args.scanCode);
3128 ASSERT_EQ(AMETA_NONE, args.metaState);
3129 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3130 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3131 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3132
3133 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003134 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3136 ASSERT_EQ(DEVICE_ID, args.deviceId);
3137 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3138 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3139 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3140 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3141 ASSERT_EQ(KEY_HOME, args.scanCode);
3142 ASSERT_EQ(AMETA_NONE, args.metaState);
3143 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3144 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3145 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3146
3147 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3151 ASSERT_EQ(DEVICE_ID, args.deviceId);
3152 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3153 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3154 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3155 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3156 ASSERT_EQ(0, args.scanCode);
3157 ASSERT_EQ(AMETA_NONE, args.metaState);
3158 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3159 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3160 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3161
3162 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3164 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3166 ASSERT_EQ(DEVICE_ID, args.deviceId);
3167 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3168 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3169 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3170 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3171 ASSERT_EQ(0, args.scanCode);
3172 ASSERT_EQ(AMETA_NONE, args.metaState);
3173 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3174 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3175 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3176
3177 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3181 ASSERT_EQ(DEVICE_ID, args.deviceId);
3182 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3183 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3184 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3185 ASSERT_EQ(0, args.keyCode);
3186 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3187 ASSERT_EQ(AMETA_NONE, args.metaState);
3188 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3189 ASSERT_EQ(0U, args.policyFlags);
3190 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3191
3192 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3194 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3196 ASSERT_EQ(DEVICE_ID, args.deviceId);
3197 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3198 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3199 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3200 ASSERT_EQ(0, args.keyCode);
3201 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3202 ASSERT_EQ(AMETA_NONE, args.metaState);
3203 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3204 ASSERT_EQ(0U, args.policyFlags);
3205 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3206}
3207
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003208/**
3209 * Ensure that the readTime is set to the time when the EV_KEY is received.
3210 */
3211TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3212 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3213
3214 KeyboardInputMapper& mapper =
3215 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3216 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3217 NotifyKeyArgs args;
3218
3219 // Key down
3220 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3222 ASSERT_EQ(12, args.readTime);
3223
3224 // Key up
3225 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3227 ASSERT_EQ(15, args.readTime);
3228}
3229
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003231 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003233 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3234 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3235 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003237 KeyboardInputMapper& mapper =
3238 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3239 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240
arthurhungc903df12020-08-11 15:08:42 +08003241 // Initial metastate to AMETA_NONE.
3242 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3243 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244
3245 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 NotifyKeyArgs args;
3248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003251 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
3253 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003254 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3256 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258
3259 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003260 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
3265 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003266 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3268 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003269 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003270 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271}
3272
3273TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003274 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3275 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3276 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3277 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 KeyboardInputMapper& mapper =
3280 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3281 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003283 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3285 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3286 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3287 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3288 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3289 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3290 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3291 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3292}
3293
3294TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003295 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3296 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3297 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3298 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003301 KeyboardInputMapper& mapper =
3302 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3303 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003305 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003306 ASSERT_NO_FATAL_FAILURE(
3307 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3309 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3310 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3311 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3312 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3313 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003315 clearViewports();
3316 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003317 ASSERT_NO_FATAL_FAILURE(
3318 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3320 AKEYCODE_DPAD_UP, DISPLAY_ID));
3321 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3322 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3324 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003326 clearViewports();
3327 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003328 ASSERT_NO_FATAL_FAILURE(
3329 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3331 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3332 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3333 AKEYCODE_DPAD_UP, DISPLAY_ID));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3335 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003337 clearViewports();
3338 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003339 ASSERT_NO_FATAL_FAILURE(
3340 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3342 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3343 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3344 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3345 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3346 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
3348 // Special case: if orientation changes while key is down, we still emit the same keycode
3349 // in the key up as we did in the key down.
3350 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003351 clearViewports();
3352 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3355 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3356 ASSERT_EQ(KEY_UP, args.scanCode);
3357 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3358
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003359 clearViewports();
3360 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3363 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3364 ASSERT_EQ(KEY_UP, args.scanCode);
3365 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3366}
3367
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003368TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3369 // If the keyboard is not orientation aware,
3370 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003371 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003373 KeyboardInputMapper& mapper =
3374 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3375 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003376 NotifyKeyArgs args;
3377
3378 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3384
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003385 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3390 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3391}
3392
3393TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3394 // If the keyboard is orientation aware,
3395 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003396 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003397
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003398 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003399 KeyboardInputMapper& mapper =
3400 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3401 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003402 NotifyKeyArgs args;
3403
3404 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3405 // ^--- already checked by the previous test
3406
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003407 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003408 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3413 ASSERT_EQ(DISPLAY_ID, args.displayId);
3414
3415 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003416 clearViewports();
3417 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003418 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3423 ASSERT_EQ(newDisplayId, args.displayId);
3424}
3425
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003427 KeyboardInputMapper& mapper =
3428 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3429 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003431 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003432 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003434 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003435 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436}
3437
3438TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003439 KeyboardInputMapper& mapper =
3440 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3441 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003443 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003444 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003446 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448}
3449
3450TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003451 KeyboardInputMapper& mapper =
3452 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3453 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003455 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456
3457 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3458 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460 ASSERT_TRUE(flags[0]);
3461 ASSERT_FALSE(flags[1]);
3462}
3463
3464TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003465 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3466 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3467 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3468 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3469 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003472 KeyboardInputMapper& mapper =
3473 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3474 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003475 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003476 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3477 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478
3479 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003480 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3481 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3482 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
3484 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003487 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3488 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3489 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491
3492 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003495 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3496 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
3500 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003503 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3504 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3505 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003506 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
3508 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003511 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3512 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3513 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003514 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515
3516 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003519 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3520 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3521 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
3524 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3528 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3529 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531}
3532
Chris Yea52ade12020-08-27 16:49:20 -07003533TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3534 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3535 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3536 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3537 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3538
3539 KeyboardInputMapper& mapper =
3540 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3541 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3542
3543 // Initial metastate should be AMETA_NONE as no meta keys added.
3544 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3545 // Meta state should be AMETA_NONE after reset
3546 mapper.reset(ARBITRARY_TIME);
3547 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3548 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3549 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3550 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3551
3552 NotifyKeyArgs args;
3553 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3556 ASSERT_EQ(AMETA_NONE, args.metaState);
3557 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3558 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3559 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3560
3561 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003562 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3564 ASSERT_EQ(AMETA_NONE, args.metaState);
3565 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3566 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3567 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3568}
3569
Arthur Hung2c9a3342019-07-23 14:18:59 +08003570TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3571 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003572 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003576
3577 // keyboard 2.
3578 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003579 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003580 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003581 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003582 std::shared_ptr<InputDevice> device2 =
3583 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3584 Flags<InputDeviceClass>(0));
3585
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003586 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3587 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3588 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3589 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003591 KeyboardInputMapper& mapper =
3592 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3593 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003595 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003596 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003598 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3599 device2->reset(ARBITRARY_TIME);
3600
3601 // Prepared displays and associated info.
3602 constexpr uint8_t hdmi1 = 0;
3603 constexpr uint8_t hdmi2 = 1;
3604 const std::string SECONDARY_UNIQUE_ID = "local:1";
3605
3606 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3607 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3608
3609 // No associated display viewport found, should disable the device.
3610 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3611 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3612 ASSERT_FALSE(device2->isEnabled());
3613
3614 // Prepare second display.
3615 constexpr int32_t newDisplayId = 2;
3616 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003617 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003618 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003619 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003620 // Default device will reconfigure above, need additional reconfiguration for another device.
3621 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3622 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3623
3624 // Device should be enabled after the associated display is found.
3625 ASSERT_TRUE(mDevice->isEnabled());
3626 ASSERT_TRUE(device2->isEnabled());
3627
3628 // Test pad key events
3629 ASSERT_NO_FATAL_FAILURE(
3630 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3631 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3632 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3633 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3634 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3635 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3636 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3637
3638 ASSERT_NO_FATAL_FAILURE(
3639 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3640 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3641 AKEYCODE_DPAD_RIGHT, newDisplayId));
3642 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3643 AKEYCODE_DPAD_DOWN, newDisplayId));
3644 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3645 AKEYCODE_DPAD_LEFT, newDisplayId));
3646}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647
arthurhungc903df12020-08-11 15:08:42 +08003648TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3649 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3650 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3651 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3652 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3653 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3654 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3655
3656 KeyboardInputMapper& mapper =
3657 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3658 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3659 // Initial metastate to AMETA_NONE.
3660 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3661 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3662
3663 // Initialization should have turned all of the lights off.
3664 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3665 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3666 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3667
3668 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003671 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3672 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3673
3674 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003677 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3678 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3679
3680 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003683 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3684 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3685
3686 mFakeEventHub->removeDevice(EVENTHUB_ID);
3687 mReader->loopOnce();
3688
3689 // keyboard 2 should default toggle keys.
3690 const std::string USB2 = "USB2";
3691 const std::string DEVICE_NAME2 = "KEYBOARD2";
3692 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3693 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3694 std::shared_ptr<InputDevice> device2 =
3695 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3696 Flags<InputDeviceClass>(0));
3697 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3698 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3699 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3700 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3701 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3702 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3703
arthurhung6fe95782020-10-05 22:41:16 +08003704 KeyboardInputMapper& mapper2 =
3705 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3706 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003707 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3708 device2->reset(ARBITRARY_TIME);
3709
3710 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3711 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3712 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003713 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3714 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003715}
3716
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003717// --- KeyboardInputMapperTest_ExternalDevice ---
3718
3719class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3720protected:
Chris Yea52ade12020-08-27 16:49:20 -07003721 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003722};
3723
3724TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003725 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3726 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003727
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003728 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3730 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3731 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003732
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003733 KeyboardInputMapper& mapper =
3734 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3735 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003736
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003738 NotifyKeyArgs args;
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3740 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3741
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003742 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3744 ASSERT_EQ(uint32_t(0), args.policyFlags);
3745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
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 + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
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, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3756 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3757
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003758 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3760 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3761}
3762
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003763TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003764 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003765
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003766 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3767 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3768 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003769
Powei Fengd041c5d2019-05-03 17:11:33 -07003770 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 KeyboardInputMapper& mapper =
3772 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3773 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003774
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003776 NotifyKeyArgs args;
3777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3778 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3779
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3782 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3783
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3786 ASSERT_EQ(uint32_t(0), args.policyFlags);
3787
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003788 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
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, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3794 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3795
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003796 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3798 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3799}
3800
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801// --- CursorInputMapperTest ---
3802
3803class CursorInputMapperTest : public InputMapperTest {
3804protected:
3805 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3806
Michael Wright17db18e2020-06-26 20:51:44 +01003807 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
Chris Yea52ade12020-08-27 16:49:20 -07003809 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810 InputMapperTest::SetUp();
3811
Michael Wright17db18e2020-06-26 20:51:44 +01003812 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003813 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814 }
3815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003816 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3817 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003818
3819 void prepareDisplay(int32_t orientation) {
3820 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003821 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003822 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3823 orientation, uniqueId, NO_PORT, viewportType);
3824 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003825};
3826
3827const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003829void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3830 int32_t originalY, int32_t rotatedX,
3831 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832 NotifyMotionArgs args;
3833
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3840 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3841 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3842 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3843}
3844
3845TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003847 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003849 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850}
3851
3852TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003854 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857}
3858
3859TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003861 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862
3863 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
3866 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003867 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3868 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3870 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3871
3872 // When the bounds are set, then there should be a valid motion range.
3873 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3874
3875 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877
3878 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3879 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3880 1, 800 - 1, 0.0f, 0.0f));
3881 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3882 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3883 2, 480 - 1, 0.0f, 0.0f));
3884 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3885 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3886 0.0f, 1.0f, 0.0f, 0.0f));
3887}
3888
3889TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003891 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892
3893 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003894 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895
3896 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3897 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3898 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3899 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3900 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3901 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3902 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3903 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3904 0.0f, 1.0f, 0.0f, 0.0f));
3905}
3906
3907TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003909 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910
arthurhungdcef2dc2020-08-11 14:47:50 +08003911 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912
3913 NotifyMotionArgs args;
3914
3915 // Button press.
3916 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3920 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3921 ASSERT_EQ(DEVICE_ID, args.deviceId);
3922 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3923 ASSERT_EQ(uint32_t(0), args.policyFlags);
3924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3925 ASSERT_EQ(0, args.flags);
3926 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3927 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3928 ASSERT_EQ(0, args.edgeFlags);
3929 ASSERT_EQ(uint32_t(1), args.pointerCount);
3930 ASSERT_EQ(0, args.pointerProperties[0].id);
3931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3933 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3934 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3935 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3936 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3937
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3939 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3940 ASSERT_EQ(DEVICE_ID, args.deviceId);
3941 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3942 ASSERT_EQ(uint32_t(0), args.policyFlags);
3943 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3944 ASSERT_EQ(0, args.flags);
3945 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3946 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3947 ASSERT_EQ(0, args.edgeFlags);
3948 ASSERT_EQ(uint32_t(1), args.pointerCount);
3949 ASSERT_EQ(0, args.pointerProperties[0].id);
3950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3952 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3953 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3954 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3955 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3956
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003958 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3959 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3961 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3962 ASSERT_EQ(DEVICE_ID, args.deviceId);
3963 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3964 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3966 ASSERT_EQ(0, args.flags);
3967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3968 ASSERT_EQ(0, args.buttonState);
3969 ASSERT_EQ(0, args.edgeFlags);
3970 ASSERT_EQ(uint32_t(1), args.pointerCount);
3971 ASSERT_EQ(0, args.pointerProperties[0].id);
3972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3974 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3975 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3976 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3977 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3978
3979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3981 ASSERT_EQ(DEVICE_ID, args.deviceId);
3982 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3983 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3985 ASSERT_EQ(0, args.flags);
3986 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3987 ASSERT_EQ(0, args.buttonState);
3988 ASSERT_EQ(0, args.edgeFlags);
3989 ASSERT_EQ(uint32_t(1), args.pointerCount);
3990 ASSERT_EQ(0, args.pointerProperties[0].id);
3991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3993 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3994 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3995 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3996 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3997}
3998
3999TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004000 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004001 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002
4003 NotifyMotionArgs args;
4004
4005 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4011 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4012
4013 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4019 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4020}
4021
4022TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004024 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025
4026 NotifyMotionArgs args;
4027
4028 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4034 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4035
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4039 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4040
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4047 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4048
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4052 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4053}
4054
4055TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004057 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058
4059 NotifyMotionArgs args;
4060
4061 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4067 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4069 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4070 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4071
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4075 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4076 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4077
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4085 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4086 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4087
4088 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4094 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4095
4096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4099 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4100}
4101
4102TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004104 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004106 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4115}
4116
4117TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 addConfigurationProperty("cursor.mode", "navigation");
4119 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004120 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004122 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4131
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004132 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4141
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004142 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4151
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004152 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4161}
4162
4163TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004165 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166
4167 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4168 mFakePointerController->setPosition(100, 200);
4169 mFakePointerController->setButtonState(0);
4170
4171 NotifyMotionArgs motionArgs;
4172 NotifyKeyArgs keyArgs;
4173
4174 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4178 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4179 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4180 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4182 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4183
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4186 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4187 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4189 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4190
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(0, motionArgs.buttonState);
4196 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4198 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4199
4200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 ASSERT_EQ(0, motionArgs.buttonState);
4203 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4205 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4206
4207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004209 ASSERT_EQ(0, motionArgs.buttonState);
4210 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4212 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4213
4214 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4220 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4221 motionArgs.buttonState);
4222 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4223 mFakePointerController->getButtonState());
4224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4225 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4226
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4229 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4230 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4231 mFakePointerController->getButtonState());
4232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4233 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4234
4235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4237 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4238 motionArgs.buttonState);
4239 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4240 mFakePointerController->getButtonState());
4241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4242 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4243
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4249 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4251 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4252
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4256 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4258 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4259
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004263 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4264 ASSERT_EQ(0, motionArgs.buttonState);
4265 ASSERT_EQ(0, mFakePointerController->getButtonState());
4266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4267 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270
4271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 ASSERT_EQ(0, motionArgs.buttonState);
4273 ASSERT_EQ(0, mFakePointerController->getButtonState());
4274 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4276 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004277
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4279 ASSERT_EQ(0, motionArgs.buttonState);
4280 ASSERT_EQ(0, mFakePointerController->getButtonState());
4281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4283 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4284
4285 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4289 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4290 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004291
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4295 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4297 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4298
4299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4301 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4302 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4304 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4305
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_EQ(0, motionArgs.buttonState);
4311 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4313 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4314
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_EQ(0, motionArgs.buttonState);
4318 ASSERT_EQ(0, mFakePointerController->getButtonState());
4319
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4321 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4323 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4324 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4325
4326 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4330 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4331 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004332
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4336 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4338 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4339
4340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4342 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4343 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4345 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_EQ(0, motionArgs.buttonState);
4352 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4354 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4358 ASSERT_EQ(0, motionArgs.buttonState);
4359 ASSERT_EQ(0, mFakePointerController->getButtonState());
4360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4361 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4362
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4364 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4365 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4366
4367 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4371 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4372 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4377 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4379 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4380
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4383 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4384 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4386 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4387
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_EQ(0, motionArgs.buttonState);
4393 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4395 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396
4397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4398 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4399 ASSERT_EQ(0, motionArgs.buttonState);
4400 ASSERT_EQ(0, mFakePointerController->getButtonState());
4401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4402 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4403
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4405 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4406 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4407
4408 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4412 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4413 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004414
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4418 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4420 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4421
4422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4424 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4425 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4427 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4428
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 ASSERT_EQ(0, motionArgs.buttonState);
4434 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4436 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437
4438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4439 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4440 ASSERT_EQ(0, motionArgs.buttonState);
4441 ASSERT_EQ(0, mFakePointerController->getButtonState());
4442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4443 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4444
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4446 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4447 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4448}
4449
4450TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004452 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453
4454 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4455 mFakePointerController->setPosition(100, 200);
4456 mFakePointerController->setButtonState(0);
4457
4458 NotifyMotionArgs args;
4459
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004464 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4467 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 +01004468 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004469}
4470
4471TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004472 addConfigurationProperty("cursor.mode", "pointer");
4473 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004474 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004475
4476 NotifyDeviceResetArgs resetArgs;
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4478 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4479 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4480
4481 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4482 mFakePointerController->setPosition(100, 200);
4483 mFakePointerController->setButtonState(0);
4484
4485 NotifyMotionArgs args;
4486
4487 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4492 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4495 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 +01004496 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004497
4498 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4502 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4503 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4505 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4507 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4508 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4510 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4511
4512 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004513 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4514 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4516 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4519 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4521 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4524 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4525
4526 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4531 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4534 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 +01004535 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004536
4537 // Disable pointer capture and check that the device generation got bumped
4538 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004539 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004540 mFakePolicy->setPointerCapture(false);
4541 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004542 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004543
4544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4545 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4546 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4547
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4552 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4555 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 +01004556 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557}
4558
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004559TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004560 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004561
Garfield Tan888a6a42020-01-09 11:39:16 -08004562 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004563 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004564 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4565 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004566 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4567 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004568 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4569 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4570
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004571 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4572 mFakePointerController->setPosition(100, 200);
4573 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004574
4575 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4580 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4583 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 +01004584 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004585 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4586}
4587
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588// --- TouchInputMapperTest ---
4589
4590class TouchInputMapperTest : public InputMapperTest {
4591protected:
4592 static const int32_t RAW_X_MIN;
4593 static const int32_t RAW_X_MAX;
4594 static const int32_t RAW_Y_MIN;
4595 static const int32_t RAW_Y_MAX;
4596 static const int32_t RAW_TOUCH_MIN;
4597 static const int32_t RAW_TOUCH_MAX;
4598 static const int32_t RAW_TOOL_MIN;
4599 static const int32_t RAW_TOOL_MAX;
4600 static const int32_t RAW_PRESSURE_MIN;
4601 static const int32_t RAW_PRESSURE_MAX;
4602 static const int32_t RAW_ORIENTATION_MIN;
4603 static const int32_t RAW_ORIENTATION_MAX;
4604 static const int32_t RAW_DISTANCE_MIN;
4605 static const int32_t RAW_DISTANCE_MAX;
4606 static const int32_t RAW_TILT_MIN;
4607 static const int32_t RAW_TILT_MAX;
4608 static const int32_t RAW_ID_MIN;
4609 static const int32_t RAW_ID_MAX;
4610 static const int32_t RAW_SLOT_MIN;
4611 static const int32_t RAW_SLOT_MAX;
4612 static const float X_PRECISION;
4613 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004614 static const float X_PRECISION_VIRTUAL;
4615 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
4617 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004618 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619
4620 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4621
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004622 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004623 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004624
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 enum Axes {
4626 POSITION = 1 << 0,
4627 TOUCH = 1 << 1,
4628 TOOL = 1 << 2,
4629 PRESSURE = 1 << 3,
4630 ORIENTATION = 1 << 4,
4631 MINOR = 1 << 5,
4632 ID = 1 << 6,
4633 DISTANCE = 1 << 7,
4634 TILT = 1 << 8,
4635 SLOT = 1 << 9,
4636 TOOL_TYPE = 1 << 10,
4637 };
4638
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004639 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4640 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004641 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004643 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 int32_t toRawX(float displayX);
4645 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004646 float toCookedX(float rawX, float rawY);
4647 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004649 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004651 float toDisplayY(int32_t rawY, int32_t displayHeight);
4652
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653};
4654
4655const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4656const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4657const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4658const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4659const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4660const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4661const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4662const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004663const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4664const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4666const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4667const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4668const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4669const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4670const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4671const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4672const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4673const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4674const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4675const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4676const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004677const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4678 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4679const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4680 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004681const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4682 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683
4684const float TouchInputMapperTest::GEOMETRIC_SCALE =
4685 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4686 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4687
4688const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4689 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4690 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4691};
4692
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004693void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004694 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4695 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004696}
4697
4698void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4699 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4700 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701}
4702
Santos Cordonfa5cf462017-04-05 10:37:00 -07004703void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004704 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4705 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4706 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004707}
4708
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004710 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4711 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4712 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4713 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714}
4715
Jason Gerecke489fda82012-09-07 17:19:40 -07004716void TouchInputMapperTest::prepareLocationCalibration() {
4717 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4718}
4719
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720int32_t TouchInputMapperTest::toRawX(float displayX) {
4721 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4722}
4723
4724int32_t TouchInputMapperTest::toRawY(float displayY) {
4725 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4726}
4727
Jason Gerecke489fda82012-09-07 17:19:40 -07004728float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4729 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4730 return rawX;
4731}
4732
4733float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4734 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4735 return rawY;
4736}
4737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004739 return toDisplayX(rawX, DISPLAY_WIDTH);
4740}
4741
4742float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4743 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744}
4745
4746float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004747 return toDisplayY(rawY, DISPLAY_HEIGHT);
4748}
4749
4750float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4751 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752}
4753
4754
4755// --- SingleTouchInputMapperTest ---
4756
4757class SingleTouchInputMapperTest : public TouchInputMapperTest {
4758protected:
4759 void prepareButtons();
4760 void prepareAxes(int axes);
4761
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004762 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4763 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4764 void processUp(SingleTouchInputMapper& mappery);
4765 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4766 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4767 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4768 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4769 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4770 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771};
4772
4773void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004774 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775}
4776
4777void SingleTouchInputMapperTest::prepareAxes(int axes) {
4778 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004779 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4780 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 }
4782 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004783 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4784 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 }
4786 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004787 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4788 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 }
4790 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004791 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4792 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 }
4794 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004795 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4796 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 }
4798}
4799
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004800void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804}
4805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004806void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809}
4810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004811void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813}
4814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004815void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817}
4818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004819void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4820 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822}
4823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004824void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826}
4827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004828void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4829 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832}
4833
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004834void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4835 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837}
4838
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004839void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841}
4842
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 prepareButtons();
4845 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004846 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004848 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849}
4850
4851TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004852 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4853 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 prepareButtons();
4855 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004856 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004858 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859}
4860
4861TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 prepareButtons();
4863 prepareAxes(POSITION);
4864 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004865 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004867 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868}
4869
4870TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 prepareButtons();
4872 prepareAxes(POSITION);
4873 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004874 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004876 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877}
4878
4879TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 addConfigurationProperty("touch.deviceType", "touchScreen");
4881 prepareDisplay(DISPLAY_ORIENTATION_0);
4882 prepareButtons();
4883 prepareAxes(POSITION);
4884 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886
4887 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004888 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889
4890 // Virtual key is down.
4891 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4892 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4893 processDown(mapper, x, y);
4894 processSync(mapper);
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004897 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898
4899 // Virtual key is up.
4900 processUp(mapper);
4901 processSync(mapper);
4902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4903
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004904 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905}
4906
4907TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 addConfigurationProperty("touch.deviceType", "touchScreen");
4909 prepareDisplay(DISPLAY_ORIENTATION_0);
4910 prepareButtons();
4911 prepareAxes(POSITION);
4912 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004913 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914
4915 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004916 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917
4918 // Virtual key is down.
4919 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4920 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4921 processDown(mapper, x, y);
4922 processSync(mapper);
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004925 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926
4927 // Virtual key is up.
4928 processUp(mapper);
4929 processSync(mapper);
4930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004932 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933}
4934
4935TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 addConfigurationProperty("touch.deviceType", "touchScreen");
4937 prepareDisplay(DISPLAY_ORIENTATION_0);
4938 prepareButtons();
4939 prepareAxes(POSITION);
4940 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
4943 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4944 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004945 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 ASSERT_TRUE(flags[0]);
4947 ASSERT_FALSE(flags[1]);
4948}
4949
4950TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 addConfigurationProperty("touch.deviceType", "touchScreen");
4952 prepareDisplay(DISPLAY_ORIENTATION_0);
4953 prepareButtons();
4954 prepareAxes(POSITION);
4955 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004956 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957
arthurhungdcef2dc2020-08-11 14:47:50 +08004958 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959
4960 NotifyKeyArgs args;
4961
4962 // Press virtual key.
4963 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4964 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4965 processDown(mapper, x, y);
4966 processSync(mapper);
4967
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4969 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4970 ASSERT_EQ(DEVICE_ID, args.deviceId);
4971 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4972 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4973 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4974 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4975 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4976 ASSERT_EQ(KEY_HOME, args.scanCode);
4977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4978 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4979
4980 // Release virtual key.
4981 processUp(mapper);
4982 processSync(mapper);
4983
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4985 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4986 ASSERT_EQ(DEVICE_ID, args.deviceId);
4987 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4988 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4989 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4990 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4991 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4992 ASSERT_EQ(KEY_HOME, args.scanCode);
4993 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4994 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4995
4996 // Should not have sent any motions.
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4998}
4999
5000TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001 addConfigurationProperty("touch.deviceType", "touchScreen");
5002 prepareDisplay(DISPLAY_ORIENTATION_0);
5003 prepareButtons();
5004 prepareAxes(POSITION);
5005 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007
arthurhungdcef2dc2020-08-11 14:47:50 +08005008 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009
5010 NotifyKeyArgs keyArgs;
5011
5012 // Press virtual key.
5013 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5014 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5015 processDown(mapper, x, y);
5016 processSync(mapper);
5017
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5019 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5020 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5021 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5022 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5023 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5024 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5025 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5026 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5027 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5028 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5029
5030 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5031 // into the display area.
5032 y -= 100;
5033 processMove(mapper, x, y);
5034 processSync(mapper);
5035
5036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5037 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5038 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5039 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5040 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5041 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5042 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5043 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5044 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5045 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5047 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5048
5049 NotifyMotionArgs motionArgs;
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5051 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5052 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5053 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5054 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5056 ASSERT_EQ(0, motionArgs.flags);
5057 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5058 ASSERT_EQ(0, motionArgs.buttonState);
5059 ASSERT_EQ(0, motionArgs.edgeFlags);
5060 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5061 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5064 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5065 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5066 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5068
5069 // Keep moving out of bounds. Should generate a pointer move.
5070 y -= 50;
5071 processMove(mapper, x, y);
5072 processSync(mapper);
5073
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5075 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5076 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5077 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5078 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5080 ASSERT_EQ(0, motionArgs.flags);
5081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5082 ASSERT_EQ(0, motionArgs.buttonState);
5083 ASSERT_EQ(0, motionArgs.edgeFlags);
5084 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5085 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5088 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5089 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5090 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5091 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5092
5093 // Release out of bounds. Should generate a pointer up.
5094 processUp(mapper);
5095 processSync(mapper);
5096
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5098 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5099 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5100 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5101 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5102 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5103 ASSERT_EQ(0, motionArgs.flags);
5104 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5105 ASSERT_EQ(0, motionArgs.buttonState);
5106 ASSERT_EQ(0, motionArgs.edgeFlags);
5107 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5108 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5111 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5112 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5113 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5114 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5115
5116 // Should not have sent any more keys or motions.
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5119}
5120
5121TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122 addConfigurationProperty("touch.deviceType", "touchScreen");
5123 prepareDisplay(DISPLAY_ORIENTATION_0);
5124 prepareButtons();
5125 prepareAxes(POSITION);
5126 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005127 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128
arthurhungdcef2dc2020-08-11 14:47:50 +08005129 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130
5131 NotifyMotionArgs motionArgs;
5132
5133 // Initially go down out of bounds.
5134 int32_t x = -10;
5135 int32_t y = -10;
5136 processDown(mapper, x, y);
5137 processSync(mapper);
5138
5139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5140
5141 // Move into the display area. Should generate a pointer down.
5142 x = 50;
5143 y = 75;
5144 processMove(mapper, x, y);
5145 processSync(mapper);
5146
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5149 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5150 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5151 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5152 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5153 ASSERT_EQ(0, motionArgs.flags);
5154 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5155 ASSERT_EQ(0, motionArgs.buttonState);
5156 ASSERT_EQ(0, motionArgs.edgeFlags);
5157 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5158 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5161 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5162 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5163 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5164 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5165
5166 // Release. Should generate a pointer up.
5167 processUp(mapper);
5168 processSync(mapper);
5169
5170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5172 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5173 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5174 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5175 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5176 ASSERT_EQ(0, motionArgs.flags);
5177 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5178 ASSERT_EQ(0, motionArgs.buttonState);
5179 ASSERT_EQ(0, motionArgs.edgeFlags);
5180 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5181 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5184 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5185 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5186 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5188
5189 // Should not have sent any more keys or motions.
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5192}
5193
Santos Cordonfa5cf462017-04-05 10:37:00 -07005194TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005195 addConfigurationProperty("touch.deviceType", "touchScreen");
5196 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5197
5198 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5199 prepareButtons();
5200 prepareAxes(POSITION);
5201 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005202 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005203
arthurhungdcef2dc2020-08-11 14:47:50 +08005204 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005205
5206 NotifyMotionArgs motionArgs;
5207
5208 // Down.
5209 int32_t x = 100;
5210 int32_t y = 125;
5211 processDown(mapper, x, y);
5212 processSync(mapper);
5213
5214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5215 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5216 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5217 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5220 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5221 ASSERT_EQ(0, motionArgs.flags);
5222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5223 ASSERT_EQ(0, motionArgs.buttonState);
5224 ASSERT_EQ(0, motionArgs.edgeFlags);
5225 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5229 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5230 1, 0, 0, 0, 0, 0, 0, 0));
5231 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5232 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5234
5235 // Move.
5236 x += 50;
5237 y += 75;
5238 processMove(mapper, x, y);
5239 processSync(mapper);
5240
5241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5242 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5243 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5244 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5245 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5246 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5248 ASSERT_EQ(0, motionArgs.flags);
5249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5250 ASSERT_EQ(0, motionArgs.buttonState);
5251 ASSERT_EQ(0, motionArgs.edgeFlags);
5252 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5253 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5256 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5257 1, 0, 0, 0, 0, 0, 0, 0));
5258 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5259 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5260 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5261
5262 // Up.
5263 processUp(mapper);
5264 processSync(mapper);
5265
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5268 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5269 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5270 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5271 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5272 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5273 ASSERT_EQ(0, motionArgs.flags);
5274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5275 ASSERT_EQ(0, motionArgs.buttonState);
5276 ASSERT_EQ(0, motionArgs.edgeFlags);
5277 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5278 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5281 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5282 1, 0, 0, 0, 0, 0, 0, 0));
5283 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5284 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5286
5287 // Should not have sent any more keys or motions.
5288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5290}
5291
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293 addConfigurationProperty("touch.deviceType", "touchScreen");
5294 prepareDisplay(DISPLAY_ORIENTATION_0);
5295 prepareButtons();
5296 prepareAxes(POSITION);
5297 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005298 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299
arthurhungdcef2dc2020-08-11 14:47:50 +08005300 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301
5302 NotifyMotionArgs motionArgs;
5303
5304 // Down.
5305 int32_t x = 100;
5306 int32_t y = 125;
5307 processDown(mapper, x, y);
5308 processSync(mapper);
5309
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5312 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5313 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5314 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5315 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5316 ASSERT_EQ(0, motionArgs.flags);
5317 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5318 ASSERT_EQ(0, motionArgs.buttonState);
5319 ASSERT_EQ(0, motionArgs.edgeFlags);
5320 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5321 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5324 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5325 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5326 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5328
5329 // Move.
5330 x += 50;
5331 y += 75;
5332 processMove(mapper, x, y);
5333 processSync(mapper);
5334
5335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5337 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5338 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5339 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5341 ASSERT_EQ(0, motionArgs.flags);
5342 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5343 ASSERT_EQ(0, motionArgs.buttonState);
5344 ASSERT_EQ(0, motionArgs.edgeFlags);
5345 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5346 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5349 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5350 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5351 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5353
5354 // Up.
5355 processUp(mapper);
5356 processSync(mapper);
5357
5358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5360 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5361 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5362 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5363 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5364 ASSERT_EQ(0, motionArgs.flags);
5365 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5366 ASSERT_EQ(0, motionArgs.buttonState);
5367 ASSERT_EQ(0, motionArgs.edgeFlags);
5368 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5369 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5372 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5373 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5374 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5376
5377 // Should not have sent any more keys or motions.
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5380}
5381
5382TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383 addConfigurationProperty("touch.deviceType", "touchScreen");
5384 prepareButtons();
5385 prepareAxes(POSITION);
5386 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388
5389 NotifyMotionArgs args;
5390
5391 // Rotation 90.
5392 prepareDisplay(DISPLAY_ORIENTATION_90);
5393 processDown(mapper, toRawX(50), toRawY(75));
5394 processSync(mapper);
5395
5396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5397 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5398 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5399
5400 processUp(mapper);
5401 processSync(mapper);
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5403}
5404
5405TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 addConfigurationProperty("touch.deviceType", "touchScreen");
5407 prepareButtons();
5408 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410
5411 NotifyMotionArgs args;
5412
5413 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005414 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415 prepareDisplay(DISPLAY_ORIENTATION_0);
5416 processDown(mapper, toRawX(50), toRawY(75));
5417 processSync(mapper);
5418
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5420 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5421 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5422
5423 processUp(mapper);
5424 processSync(mapper);
5425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5426
5427 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005428 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 prepareDisplay(DISPLAY_ORIENTATION_90);
5430 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5431 processSync(mapper);
5432
5433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5434 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5435 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5436
5437 processUp(mapper);
5438 processSync(mapper);
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5440
5441 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005442 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443 prepareDisplay(DISPLAY_ORIENTATION_180);
5444 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5445 processSync(mapper);
5446
5447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5448 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5449 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5450
5451 processUp(mapper);
5452 processSync(mapper);
5453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5454
5455 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005456 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457 prepareDisplay(DISPLAY_ORIENTATION_270);
5458 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5459 processSync(mapper);
5460
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5462 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5463 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5464
5465 processUp(mapper);
5466 processSync(mapper);
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5468}
5469
5470TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 addConfigurationProperty("touch.deviceType", "touchScreen");
5472 prepareDisplay(DISPLAY_ORIENTATION_0);
5473 prepareButtons();
5474 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005475 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476
5477 // These calculations are based on the input device calibration documentation.
5478 int32_t rawX = 100;
5479 int32_t rawY = 200;
5480 int32_t rawPressure = 10;
5481 int32_t rawToolMajor = 12;
5482 int32_t rawDistance = 2;
5483 int32_t rawTiltX = 30;
5484 int32_t rawTiltY = 110;
5485
5486 float x = toDisplayX(rawX);
5487 float y = toDisplayY(rawY);
5488 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5489 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5490 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5491 float distance = float(rawDistance);
5492
5493 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5494 float tiltScale = M_PI / 180;
5495 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5496 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5497 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5498 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5499
5500 processDown(mapper, rawX, rawY);
5501 processPressure(mapper, rawPressure);
5502 processToolMajor(mapper, rawToolMajor);
5503 processDistance(mapper, rawDistance);
5504 processTilt(mapper, rawTiltX, rawTiltY);
5505 processSync(mapper);
5506
5507 NotifyMotionArgs args;
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5510 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5511 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5512}
5513
Jason Gerecke489fda82012-09-07 17:19:40 -07005514TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005515 addConfigurationProperty("touch.deviceType", "touchScreen");
5516 prepareDisplay(DISPLAY_ORIENTATION_0);
5517 prepareLocationCalibration();
5518 prepareButtons();
5519 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005520 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005521
5522 int32_t rawX = 100;
5523 int32_t rawY = 200;
5524
5525 float x = toDisplayX(toCookedX(rawX, rawY));
5526 float y = toDisplayY(toCookedY(rawX, rawY));
5527
5528 processDown(mapper, rawX, rawY);
5529 processSync(mapper);
5530
5531 NotifyMotionArgs args;
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5534 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5535}
5536
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538 addConfigurationProperty("touch.deviceType", "touchScreen");
5539 prepareDisplay(DISPLAY_ORIENTATION_0);
5540 prepareButtons();
5541 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005542 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543
5544 NotifyMotionArgs motionArgs;
5545 NotifyKeyArgs keyArgs;
5546
5547 processDown(mapper, 100, 200);
5548 processSync(mapper);
5549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5550 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5551 ASSERT_EQ(0, motionArgs.buttonState);
5552
5553 // press BTN_LEFT, release BTN_LEFT
5554 processKey(mapper, BTN_LEFT, 1);
5555 processSync(mapper);
5556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5558 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5559
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5561 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5562 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5563
Michael Wrightd02c5b62014-02-10 15:10:22 -08005564 processKey(mapper, BTN_LEFT, 0);
5565 processSync(mapper);
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005569
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005572 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573
5574 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5575 processKey(mapper, BTN_RIGHT, 1);
5576 processKey(mapper, BTN_MIDDLE, 1);
5577 processSync(mapper);
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5580 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5581 motionArgs.buttonState);
5582
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5585 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5589 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5590 motionArgs.buttonState);
5591
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 processKey(mapper, BTN_RIGHT, 0);
5593 processSync(mapper);
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005600 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601
5602 processKey(mapper, BTN_MIDDLE, 0);
5603 processSync(mapper);
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005605 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005607
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005610 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611
5612 // press BTN_BACK, release BTN_BACK
5613 processKey(mapper, BTN_BACK, 1);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5616 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5617 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005618
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005621 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5622
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5624 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5625 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626
5627 processKey(mapper, BTN_BACK, 0);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005631 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005632
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005635 ASSERT_EQ(0, motionArgs.buttonState);
5636
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5638 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5639 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5640
5641 // press BTN_SIDE, release BTN_SIDE
5642 processKey(mapper, BTN_SIDE, 1);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5645 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5646 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005647
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005650 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5651
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5654 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655
5656 processKey(mapper, BTN_SIDE, 0);
5657 processSync(mapper);
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005659 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005661
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005664 ASSERT_EQ(0, motionArgs.buttonState);
5665
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5667 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5668 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5669
5670 // press BTN_FORWARD, release BTN_FORWARD
5671 processKey(mapper, BTN_FORWARD, 1);
5672 processSync(mapper);
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5674 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5675 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005676
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005679 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5680
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5682 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5683 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684
5685 processKey(mapper, BTN_FORWARD, 0);
5686 processSync(mapper);
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005690
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005693 ASSERT_EQ(0, motionArgs.buttonState);
5694
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5696 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5697 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5698
5699 // press BTN_EXTRA, release BTN_EXTRA
5700 processKey(mapper, BTN_EXTRA, 1);
5701 processSync(mapper);
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5703 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5704 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005705
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5712 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713
5714 processKey(mapper, BTN_EXTRA, 0);
5715 processSync(mapper);
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005719
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005722 ASSERT_EQ(0, motionArgs.buttonState);
5723
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5725 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5726 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5727
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5729
Michael Wrightd02c5b62014-02-10 15:10:22 -08005730 // press BTN_STYLUS, release BTN_STYLUS
5731 processKey(mapper, BTN_STYLUS, 1);
5732 processSync(mapper);
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005735 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5739 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005740
5741 processKey(mapper, BTN_STYLUS, 0);
5742 processSync(mapper);
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005749 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750
5751 // press BTN_STYLUS2, release BTN_STYLUS2
5752 processKey(mapper, BTN_STYLUS2, 1);
5753 processSync(mapper);
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005756 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5757
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5760 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761
5762 processKey(mapper, BTN_STYLUS2, 0);
5763 processSync(mapper);
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005765 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005767
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005770 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771
5772 // release touch
5773 processUp(mapper);
5774 processSync(mapper);
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5776 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5777 ASSERT_EQ(0, motionArgs.buttonState);
5778}
5779
5780TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781 addConfigurationProperty("touch.deviceType", "touchScreen");
5782 prepareDisplay(DISPLAY_ORIENTATION_0);
5783 prepareButtons();
5784 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005785 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786
5787 NotifyMotionArgs motionArgs;
5788
5789 // default tool type is finger
5790 processDown(mapper, 100, 200);
5791 processSync(mapper);
5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5793 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5795
5796 // eraser
5797 processKey(mapper, BTN_TOOL_RUBBER, 1);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5802
5803 // stylus
5804 processKey(mapper, BTN_TOOL_RUBBER, 0);
5805 processKey(mapper, BTN_TOOL_PEN, 1);
5806 processSync(mapper);
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5810
5811 // brush
5812 processKey(mapper, BTN_TOOL_PEN, 0);
5813 processKey(mapper, BTN_TOOL_BRUSH, 1);
5814 processSync(mapper);
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5818
5819 // pencil
5820 processKey(mapper, BTN_TOOL_BRUSH, 0);
5821 processKey(mapper, BTN_TOOL_PENCIL, 1);
5822 processSync(mapper);
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5826
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005827 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 processKey(mapper, BTN_TOOL_PENCIL, 0);
5829 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5830 processSync(mapper);
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5834
5835 // mouse
5836 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5837 processKey(mapper, BTN_TOOL_MOUSE, 1);
5838 processSync(mapper);
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5842
5843 // lens
5844 processKey(mapper, BTN_TOOL_MOUSE, 0);
5845 processKey(mapper, BTN_TOOL_LENS, 1);
5846 processSync(mapper);
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5850
5851 // double-tap
5852 processKey(mapper, BTN_TOOL_LENS, 0);
5853 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5858
5859 // triple-tap
5860 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5861 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5862 processSync(mapper);
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5866
5867 // quad-tap
5868 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5869 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5870 processSync(mapper);
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5874
5875 // finger
5876 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5877 processKey(mapper, BTN_TOOL_FINGER, 1);
5878 processSync(mapper);
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5882
5883 // stylus trumps finger
5884 processKey(mapper, BTN_TOOL_PEN, 1);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5889
5890 // eraser trumps stylus
5891 processKey(mapper, BTN_TOOL_RUBBER, 1);
5892 processSync(mapper);
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5896
5897 // mouse trumps eraser
5898 processKey(mapper, BTN_TOOL_MOUSE, 1);
5899 processSync(mapper);
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5903
5904 // back to default tool type
5905 processKey(mapper, BTN_TOOL_MOUSE, 0);
5906 processKey(mapper, BTN_TOOL_RUBBER, 0);
5907 processKey(mapper, BTN_TOOL_PEN, 0);
5908 processKey(mapper, BTN_TOOL_FINGER, 0);
5909 processSync(mapper);
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5913}
5914
5915TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 addConfigurationProperty("touch.deviceType", "touchScreen");
5917 prepareDisplay(DISPLAY_ORIENTATION_0);
5918 prepareButtons();
5919 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005920 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005921 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922
5923 NotifyMotionArgs motionArgs;
5924
5925 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5926 processKey(mapper, BTN_TOOL_FINGER, 1);
5927 processMove(mapper, 100, 200);
5928 processSync(mapper);
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5932 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5933
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5937 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5938
5939 // move a little
5940 processMove(mapper, 150, 250);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5945 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5946
5947 // down when BTN_TOUCH is pressed, pressure defaults to 1
5948 processKey(mapper, BTN_TOUCH, 1);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5953 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5954
5955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5956 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5958 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5959
5960 // up when BTN_TOUCH is released, hover restored
5961 processKey(mapper, BTN_TOUCH, 0);
5962 processSync(mapper);
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5964 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5966 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5967
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5971 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5972
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5974 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5976 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5977
5978 // exit hover when pointer goes away
5979 processKey(mapper, BTN_TOOL_FINGER, 0);
5980 processSync(mapper);
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5984 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5985}
5986
5987TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988 addConfigurationProperty("touch.deviceType", "touchScreen");
5989 prepareDisplay(DISPLAY_ORIENTATION_0);
5990 prepareButtons();
5991 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005992 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993
5994 NotifyMotionArgs motionArgs;
5995
5996 // initially hovering because pressure is 0
5997 processDown(mapper, 100, 200);
5998 processPressure(mapper, 0);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6001 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6003 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6008 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6009
6010 // move a little
6011 processMove(mapper, 150, 250);
6012 processSync(mapper);
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6016 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6017
6018 // down when pressure is non-zero
6019 processPressure(mapper, RAW_PRESSURE_MAX);
6020 processSync(mapper);
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6022 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6024 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6029 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6030
6031 // up when pressure becomes 0, hover restored
6032 processPressure(mapper, 0);
6033 processSync(mapper);
6034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6035 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6037 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6038
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6040 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6042 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6043
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6047 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6048
6049 // exit hover when pointer goes away
6050 processUp(mapper);
6051 processSync(mapper);
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6053 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6055 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6056}
6057
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058// --- MultiTouchInputMapperTest ---
6059
6060class MultiTouchInputMapperTest : public TouchInputMapperTest {
6061protected:
6062 void prepareAxes(int axes);
6063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006064 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6065 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6066 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6067 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6068 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6069 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6070 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6071 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6072 void processId(MultiTouchInputMapper& mapper, int32_t id);
6073 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6074 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6075 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6076 void processMTSync(MultiTouchInputMapper& mapper);
6077 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078};
6079
6080void MultiTouchInputMapperTest::prepareAxes(int axes) {
6081 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006082 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084 }
6085 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6087 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006089 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6090 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 }
6092 }
6093 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6095 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6098 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099 }
6100 }
6101 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006102 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6103 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104 }
6105 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006106 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6107 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 }
6109 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006110 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6111 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 }
6113 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006114 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6115 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116 }
6117 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006118 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6119 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 }
6121 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 }
6124}
6125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006126void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6127 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130}
6131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006132void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6133 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135}
6136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6138 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140}
6141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006142void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144}
6145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006146void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148}
6149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006150void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6151 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153}
6154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006155void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157}
6158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006159void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161}
6162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006163void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165}
6166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006167void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169}
6170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006171void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173}
6174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006175void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6176 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178}
6179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006180void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182}
6183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006184void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186}
6187
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189 addConfigurationProperty("touch.deviceType", "touchScreen");
6190 prepareDisplay(DISPLAY_ORIENTATION_0);
6191 prepareAxes(POSITION);
6192 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006193 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194
arthurhungdcef2dc2020-08-11 14:47:50 +08006195 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196
6197 NotifyMotionArgs motionArgs;
6198
6199 // Two fingers down at once.
6200 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6201 processPosition(mapper, x1, y1);
6202 processMTSync(mapper);
6203 processPosition(mapper, x2, y2);
6204 processMTSync(mapper);
6205 processSync(mapper);
6206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6209 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6210 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6211 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6213 ASSERT_EQ(0, motionArgs.flags);
6214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6215 ASSERT_EQ(0, motionArgs.buttonState);
6216 ASSERT_EQ(0, motionArgs.edgeFlags);
6217 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6218 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6221 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6222 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6223 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6225
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6227 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6228 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6229 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6230 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6231 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6232 motionArgs.action);
6233 ASSERT_EQ(0, motionArgs.flags);
6234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6235 ASSERT_EQ(0, motionArgs.buttonState);
6236 ASSERT_EQ(0, motionArgs.edgeFlags);
6237 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6240 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6243 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6245 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6246 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6247 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6249
6250 // Move.
6251 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6252 processPosition(mapper, x1, y1);
6253 processMTSync(mapper);
6254 processPosition(mapper, x2, y2);
6255 processMTSync(mapper);
6256 processSync(mapper);
6257
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6260 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6261 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6262 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6264 ASSERT_EQ(0, motionArgs.flags);
6265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6266 ASSERT_EQ(0, motionArgs.buttonState);
6267 ASSERT_EQ(0, motionArgs.edgeFlags);
6268 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6269 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6271 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6274 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6276 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6277 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6278 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6280
6281 // First finger up.
6282 x2 += 15; y2 -= 20;
6283 processPosition(mapper, x2, y2);
6284 processMTSync(mapper);
6285 processSync(mapper);
6286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6289 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6290 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6291 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6293 motionArgs.action);
6294 ASSERT_EQ(0, motionArgs.flags);
6295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6296 ASSERT_EQ(0, motionArgs.buttonState);
6297 ASSERT_EQ(0, motionArgs.edgeFlags);
6298 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6299 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6301 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6304 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6306 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6310
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6313 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6314 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6315 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6317 ASSERT_EQ(0, motionArgs.flags);
6318 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6319 ASSERT_EQ(0, motionArgs.buttonState);
6320 ASSERT_EQ(0, motionArgs.edgeFlags);
6321 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6322 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6325 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6326 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6327 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6329
6330 // Move.
6331 x2 += 20; y2 -= 25;
6332 processPosition(mapper, x2, y2);
6333 processMTSync(mapper);
6334 processSync(mapper);
6335
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6339 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6340 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6342 ASSERT_EQ(0, motionArgs.flags);
6343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6344 ASSERT_EQ(0, motionArgs.buttonState);
6345 ASSERT_EQ(0, motionArgs.edgeFlags);
6346 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6347 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6351 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6352 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6354
6355 // New finger down.
6356 int32_t x3 = 700, y3 = 300;
6357 processPosition(mapper, x2, y2);
6358 processMTSync(mapper);
6359 processPosition(mapper, x3, y3);
6360 processMTSync(mapper);
6361 processSync(mapper);
6362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6365 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6366 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6367 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6369 motionArgs.action);
6370 ASSERT_EQ(0, motionArgs.flags);
6371 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6372 ASSERT_EQ(0, motionArgs.buttonState);
6373 ASSERT_EQ(0, motionArgs.edgeFlags);
6374 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6377 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6380 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6382 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6383 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6384 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6386
6387 // Second finger up.
6388 x3 += 30; y3 -= 20;
6389 processPosition(mapper, x3, y3);
6390 processMTSync(mapper);
6391 processSync(mapper);
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6395 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6396 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6397 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6399 motionArgs.action);
6400 ASSERT_EQ(0, motionArgs.flags);
6401 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6402 ASSERT_EQ(0, motionArgs.buttonState);
6403 ASSERT_EQ(0, motionArgs.edgeFlags);
6404 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6405 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6407 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6410 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6412 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6416
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6419 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6420 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6421 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6423 ASSERT_EQ(0, motionArgs.flags);
6424 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6425 ASSERT_EQ(0, motionArgs.buttonState);
6426 ASSERT_EQ(0, motionArgs.edgeFlags);
6427 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6428 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6431 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6432 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6433 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6435
6436 // Last finger up.
6437 processMTSync(mapper);
6438 processSync(mapper);
6439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6442 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6446 ASSERT_EQ(0, motionArgs.flags);
6447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6448 ASSERT_EQ(0, motionArgs.buttonState);
6449 ASSERT_EQ(0, motionArgs.edgeFlags);
6450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6454 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6455 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6456 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6458
6459 // Should not have sent any more keys or motions.
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6462}
6463
6464TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465 addConfigurationProperty("touch.deviceType", "touchScreen");
6466 prepareDisplay(DISPLAY_ORIENTATION_0);
6467 prepareAxes(POSITION | ID);
6468 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006469 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470
arthurhungdcef2dc2020-08-11 14:47:50 +08006471 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472
6473 NotifyMotionArgs motionArgs;
6474
6475 // Two fingers down at once.
6476 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6477 processPosition(mapper, x1, y1);
6478 processId(mapper, 1);
6479 processMTSync(mapper);
6480 processPosition(mapper, x2, y2);
6481 processId(mapper, 2);
6482 processMTSync(mapper);
6483 processSync(mapper);
6484
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6487 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6488 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6491 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6492
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6495 motionArgs.action);
6496 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6497 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6499 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6502 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6504 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6505
6506 // Move.
6507 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6508 processPosition(mapper, x1, y1);
6509 processId(mapper, 1);
6510 processMTSync(mapper);
6511 processPosition(mapper, x2, y2);
6512 processId(mapper, 2);
6513 processMTSync(mapper);
6514 processSync(mapper);
6515
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6518 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6519 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6521 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6524 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6526 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6527
6528 // First finger up.
6529 x2 += 15; y2 -= 20;
6530 processPosition(mapper, x2, y2);
6531 processId(mapper, 2);
6532 processMTSync(mapper);
6533 processSync(mapper);
6534
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6537 motionArgs.action);
6538 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6541 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6544 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6546 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6550 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6551 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6554 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6555
6556 // Move.
6557 x2 += 20; y2 -= 25;
6558 processPosition(mapper, x2, y2);
6559 processId(mapper, 2);
6560 processMTSync(mapper);
6561 processSync(mapper);
6562
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6565 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6566 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6569 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6570
6571 // New finger down.
6572 int32_t x3 = 700, y3 = 300;
6573 processPosition(mapper, x2, y2);
6574 processId(mapper, 2);
6575 processMTSync(mapper);
6576 processPosition(mapper, x3, y3);
6577 processId(mapper, 3);
6578 processMTSync(mapper);
6579 processSync(mapper);
6580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6583 motionArgs.action);
6584 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6585 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6587 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6590 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6592 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6593
6594 // Second finger up.
6595 x3 += 30; y3 -= 20;
6596 processPosition(mapper, x3, y3);
6597 processId(mapper, 3);
6598 processMTSync(mapper);
6599 processSync(mapper);
6600
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6603 motionArgs.action);
6604 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6607 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6610 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6612 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6613
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6616 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6617 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6620 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6621
6622 // Last finger up.
6623 processMTSync(mapper);
6624 processSync(mapper);
6625
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6627 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6628 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6632 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6633
6634 // Should not have sent any more keys or motions.
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6637}
6638
6639TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640 addConfigurationProperty("touch.deviceType", "touchScreen");
6641 prepareDisplay(DISPLAY_ORIENTATION_0);
6642 prepareAxes(POSITION | ID | SLOT);
6643 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006645
arthurhungdcef2dc2020-08-11 14:47:50 +08006646 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006647
6648 NotifyMotionArgs motionArgs;
6649
6650 // Two fingers down at once.
6651 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6652 processPosition(mapper, x1, y1);
6653 processId(mapper, 1);
6654 processSlot(mapper, 1);
6655 processPosition(mapper, x2, y2);
6656 processId(mapper, 2);
6657 processSync(mapper);
6658
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6661 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6669 motionArgs.action);
6670 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6673 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6676 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6678 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6679
6680 // Move.
6681 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6682 processSlot(mapper, 0);
6683 processPosition(mapper, x1, y1);
6684 processSlot(mapper, 1);
6685 processPosition(mapper, x2, y2);
6686 processSync(mapper);
6687
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6690 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6693 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6698 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6699
6700 // First finger up.
6701 x2 += 15; y2 -= 20;
6702 processSlot(mapper, 0);
6703 processId(mapper, -1);
6704 processSlot(mapper, 1);
6705 processPosition(mapper, x2, y2);
6706 processSync(mapper);
6707
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6710 motionArgs.action);
6711 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6714 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6717 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6719 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6720
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6724 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6727 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6728
6729 // Move.
6730 x2 += 20; y2 -= 25;
6731 processPosition(mapper, x2, y2);
6732 processSync(mapper);
6733
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6736 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6737 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6741
6742 // New finger down.
6743 int32_t x3 = 700, y3 = 300;
6744 processPosition(mapper, x2, y2);
6745 processSlot(mapper, 0);
6746 processId(mapper, 3);
6747 processPosition(mapper, x3, y3);
6748 processSync(mapper);
6749
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6752 motionArgs.action);
6753 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6756 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6761 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6762
6763 // Second finger up.
6764 x3 += 30; y3 -= 20;
6765 processSlot(mapper, 1);
6766 processId(mapper, -1);
6767 processSlot(mapper, 0);
6768 processPosition(mapper, x3, y3);
6769 processSync(mapper);
6770
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6773 motionArgs.action);
6774 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6777 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6780 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6782 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6783
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6786 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6790 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6791
6792 // Last finger up.
6793 processId(mapper, -1);
6794 processSync(mapper);
6795
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6797 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6798 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6802 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6803
6804 // Should not have sent any more keys or motions.
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6807}
6808
6809TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006810 addConfigurationProperty("touch.deviceType", "touchScreen");
6811 prepareDisplay(DISPLAY_ORIENTATION_0);
6812 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006813 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006814
6815 // These calculations are based on the input device calibration documentation.
6816 int32_t rawX = 100;
6817 int32_t rawY = 200;
6818 int32_t rawTouchMajor = 7;
6819 int32_t rawTouchMinor = 6;
6820 int32_t rawToolMajor = 9;
6821 int32_t rawToolMinor = 8;
6822 int32_t rawPressure = 11;
6823 int32_t rawDistance = 0;
6824 int32_t rawOrientation = 3;
6825 int32_t id = 5;
6826
6827 float x = toDisplayX(rawX);
6828 float y = toDisplayY(rawY);
6829 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6830 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6831 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6832 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6833 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6834 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6835 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6836 float distance = float(rawDistance);
6837
6838 processPosition(mapper, rawX, rawY);
6839 processTouchMajor(mapper, rawTouchMajor);
6840 processTouchMinor(mapper, rawTouchMinor);
6841 processToolMajor(mapper, rawToolMajor);
6842 processToolMinor(mapper, rawToolMinor);
6843 processPressure(mapper, rawPressure);
6844 processOrientation(mapper, rawOrientation);
6845 processDistance(mapper, rawDistance);
6846 processId(mapper, id);
6847 processMTSync(mapper);
6848 processSync(mapper);
6849
6850 NotifyMotionArgs args;
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6852 ASSERT_EQ(0, args.pointerProperties[0].id);
6853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6854 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6855 orientation, distance));
6856}
6857
6858TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006859 addConfigurationProperty("touch.deviceType", "touchScreen");
6860 prepareDisplay(DISPLAY_ORIENTATION_0);
6861 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6862 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006864
6865 // These calculations are based on the input device calibration documentation.
6866 int32_t rawX = 100;
6867 int32_t rawY = 200;
6868 int32_t rawTouchMajor = 140;
6869 int32_t rawTouchMinor = 120;
6870 int32_t rawToolMajor = 180;
6871 int32_t rawToolMinor = 160;
6872
6873 float x = toDisplayX(rawX);
6874 float y = toDisplayY(rawY);
6875 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6876 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6877 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6878 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6879 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6880
6881 processPosition(mapper, rawX, rawY);
6882 processTouchMajor(mapper, rawTouchMajor);
6883 processTouchMinor(mapper, rawTouchMinor);
6884 processToolMajor(mapper, rawToolMajor);
6885 processToolMinor(mapper, rawToolMinor);
6886 processMTSync(mapper);
6887 processSync(mapper);
6888
6889 NotifyMotionArgs args;
6890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6892 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6893}
6894
6895TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006896 addConfigurationProperty("touch.deviceType", "touchScreen");
6897 prepareDisplay(DISPLAY_ORIENTATION_0);
6898 prepareAxes(POSITION | TOUCH | TOOL);
6899 addConfigurationProperty("touch.size.calibration", "diameter");
6900 addConfigurationProperty("touch.size.scale", "10");
6901 addConfigurationProperty("touch.size.bias", "160");
6902 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006903 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006904
6905 // These calculations are based on the input device calibration documentation.
6906 // Note: We only provide a single common touch/tool value because the device is assumed
6907 // not to emit separate values for each pointer (isSummed = 1).
6908 int32_t rawX = 100;
6909 int32_t rawY = 200;
6910 int32_t rawX2 = 150;
6911 int32_t rawY2 = 250;
6912 int32_t rawTouchMajor = 5;
6913 int32_t rawToolMajor = 8;
6914
6915 float x = toDisplayX(rawX);
6916 float y = toDisplayY(rawY);
6917 float x2 = toDisplayX(rawX2);
6918 float y2 = toDisplayY(rawY2);
6919 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6920 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6921 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6922
6923 processPosition(mapper, rawX, rawY);
6924 processTouchMajor(mapper, rawTouchMajor);
6925 processToolMajor(mapper, rawToolMajor);
6926 processMTSync(mapper);
6927 processPosition(mapper, rawX2, rawY2);
6928 processTouchMajor(mapper, rawTouchMajor);
6929 processToolMajor(mapper, rawToolMajor);
6930 processMTSync(mapper);
6931 processSync(mapper);
6932
6933 NotifyMotionArgs args;
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6935 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6936
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6938 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6939 args.action);
6940 ASSERT_EQ(size_t(2), args.pointerCount);
6941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6942 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6944 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6945}
6946
6947TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006948 addConfigurationProperty("touch.deviceType", "touchScreen");
6949 prepareDisplay(DISPLAY_ORIENTATION_0);
6950 prepareAxes(POSITION | TOUCH | TOOL);
6951 addConfigurationProperty("touch.size.calibration", "area");
6952 addConfigurationProperty("touch.size.scale", "43");
6953 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006954 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955
6956 // These calculations are based on the input device calibration documentation.
6957 int32_t rawX = 100;
6958 int32_t rawY = 200;
6959 int32_t rawTouchMajor = 5;
6960 int32_t rawToolMajor = 8;
6961
6962 float x = toDisplayX(rawX);
6963 float y = toDisplayY(rawY);
6964 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6965 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6966 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6967
6968 processPosition(mapper, rawX, rawY);
6969 processTouchMajor(mapper, rawTouchMajor);
6970 processToolMajor(mapper, rawToolMajor);
6971 processMTSync(mapper);
6972 processSync(mapper);
6973
6974 NotifyMotionArgs args;
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6977 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6978}
6979
6980TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006981 addConfigurationProperty("touch.deviceType", "touchScreen");
6982 prepareDisplay(DISPLAY_ORIENTATION_0);
6983 prepareAxes(POSITION | PRESSURE);
6984 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6985 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006986 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006987
Michael Wrightaa449c92017-12-13 21:21:43 +00006988 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006989 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006990 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6991 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6992 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6993
Michael Wrightd02c5b62014-02-10 15:10:22 -08006994 // These calculations are based on the input device calibration documentation.
6995 int32_t rawX = 100;
6996 int32_t rawY = 200;
6997 int32_t rawPressure = 60;
6998
6999 float x = toDisplayX(rawX);
7000 float y = toDisplayY(rawY);
7001 float pressure = float(rawPressure) * 0.01f;
7002
7003 processPosition(mapper, rawX, rawY);
7004 processPressure(mapper, rawPressure);
7005 processMTSync(mapper);
7006 processSync(mapper);
7007
7008 NotifyMotionArgs args;
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7011 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7012}
7013
7014TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007015 addConfigurationProperty("touch.deviceType", "touchScreen");
7016 prepareDisplay(DISPLAY_ORIENTATION_0);
7017 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007018 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019
7020 NotifyMotionArgs motionArgs;
7021 NotifyKeyArgs keyArgs;
7022
7023 processId(mapper, 1);
7024 processPosition(mapper, 100, 200);
7025 processSync(mapper);
7026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7028 ASSERT_EQ(0, motionArgs.buttonState);
7029
7030 // press BTN_LEFT, release BTN_LEFT
7031 processKey(mapper, BTN_LEFT, 1);
7032 processSync(mapper);
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7035 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7036
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7039 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7040
Michael Wrightd02c5b62014-02-10 15:10:22 -08007041 processKey(mapper, BTN_LEFT, 0);
7042 processSync(mapper);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007045 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007046
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007049 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050
7051 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7052 processKey(mapper, BTN_RIGHT, 1);
7053 processKey(mapper, BTN_MIDDLE, 1);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7057 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7058 motionArgs.buttonState);
7059
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7062 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7063
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7065 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7066 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7067 motionArgs.buttonState);
7068
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069 processKey(mapper, BTN_RIGHT, 0);
7070 processSync(mapper);
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007072 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007074
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007077 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078
7079 processKey(mapper, BTN_MIDDLE, 0);
7080 processSync(mapper);
7081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007083 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007084
7085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007087 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088
7089 // press BTN_BACK, release BTN_BACK
7090 processKey(mapper, BTN_BACK, 1);
7091 processSync(mapper);
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7093 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7094 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007095
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007098 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7099
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7102 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103
7104 processKey(mapper, BTN_BACK, 0);
7105 processSync(mapper);
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007109
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007112 ASSERT_EQ(0, motionArgs.buttonState);
7113
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7115 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7116 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7117
7118 // press BTN_SIDE, release BTN_SIDE
7119 processKey(mapper, BTN_SIDE, 1);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7122 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7123 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007124
Michael Wrightd02c5b62014-02-10 15:10:22 -08007125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7128
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7131 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132
7133 processKey(mapper, BTN_SIDE, 0);
7134 processSync(mapper);
7135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007136 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007138
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007141 ASSERT_EQ(0, motionArgs.buttonState);
7142
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7144 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7145 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7146
7147 // press BTN_FORWARD, release BTN_FORWARD
7148 processKey(mapper, BTN_FORWARD, 1);
7149 processSync(mapper);
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7151 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7152 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007153
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007156 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7157
7158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7160 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161
7162 processKey(mapper, BTN_FORWARD, 0);
7163 processSync(mapper);
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007167
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007170 ASSERT_EQ(0, motionArgs.buttonState);
7171
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7173 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7174 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7175
7176 // press BTN_EXTRA, release BTN_EXTRA
7177 processKey(mapper, BTN_EXTRA, 1);
7178 processSync(mapper);
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7180 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7181 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007182
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007185 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7186
7187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7189 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190
7191 processKey(mapper, BTN_EXTRA, 0);
7192 processSync(mapper);
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007196
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007199 ASSERT_EQ(0, motionArgs.buttonState);
7200
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7202 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7203 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7204
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7206
Michael Wrightd02c5b62014-02-10 15:10:22 -08007207 // press BTN_STYLUS, release BTN_STYLUS
7208 processKey(mapper, BTN_STYLUS, 1);
7209 processSync(mapper);
7210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007212 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7213
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7216 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007217
7218 processKey(mapper, BTN_STYLUS, 0);
7219 processSync(mapper);
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007222 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007223
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007226 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007227
7228 // press BTN_STYLUS2, release BTN_STYLUS2
7229 processKey(mapper, BTN_STYLUS2, 1);
7230 processSync(mapper);
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007233 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7234
7235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7237 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007238
7239 processKey(mapper, BTN_STYLUS2, 0);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007243 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007244
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007247 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007248
7249 // release touch
7250 processId(mapper, -1);
7251 processSync(mapper);
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7254 ASSERT_EQ(0, motionArgs.buttonState);
7255}
7256
7257TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258 addConfigurationProperty("touch.deviceType", "touchScreen");
7259 prepareDisplay(DISPLAY_ORIENTATION_0);
7260 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007261 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007262
7263 NotifyMotionArgs motionArgs;
7264
7265 // default tool type is finger
7266 processId(mapper, 1);
7267 processPosition(mapper, 100, 200);
7268 processSync(mapper);
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7272
7273 // eraser
7274 processKey(mapper, BTN_TOOL_RUBBER, 1);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7279
7280 // stylus
7281 processKey(mapper, BTN_TOOL_RUBBER, 0);
7282 processKey(mapper, BTN_TOOL_PEN, 1);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7287
7288 // brush
7289 processKey(mapper, BTN_TOOL_PEN, 0);
7290 processKey(mapper, BTN_TOOL_BRUSH, 1);
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7295
7296 // pencil
7297 processKey(mapper, BTN_TOOL_BRUSH, 0);
7298 processKey(mapper, BTN_TOOL_PENCIL, 1);
7299 processSync(mapper);
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7303
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007304 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305 processKey(mapper, BTN_TOOL_PENCIL, 0);
7306 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7307 processSync(mapper);
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7311
7312 // mouse
7313 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7314 processKey(mapper, BTN_TOOL_MOUSE, 1);
7315 processSync(mapper);
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7319
7320 // lens
7321 processKey(mapper, BTN_TOOL_MOUSE, 0);
7322 processKey(mapper, BTN_TOOL_LENS, 1);
7323 processSync(mapper);
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7327
7328 // double-tap
7329 processKey(mapper, BTN_TOOL_LENS, 0);
7330 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7331 processSync(mapper);
7332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7335
7336 // triple-tap
7337 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7338 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7339 processSync(mapper);
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7343
7344 // quad-tap
7345 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7346 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7351
7352 // finger
7353 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7354 processKey(mapper, BTN_TOOL_FINGER, 1);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7359
7360 // stylus trumps finger
7361 processKey(mapper, BTN_TOOL_PEN, 1);
7362 processSync(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7366
7367 // eraser trumps stylus
7368 processKey(mapper, BTN_TOOL_RUBBER, 1);
7369 processSync(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7373
7374 // mouse trumps eraser
7375 processKey(mapper, BTN_TOOL_MOUSE, 1);
7376 processSync(mapper);
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7380
7381 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7382 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7383 processSync(mapper);
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387
7388 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7389 processToolType(mapper, MT_TOOL_PEN);
7390 processSync(mapper);
7391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7394
7395 // back to default tool type
7396 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7397 processKey(mapper, BTN_TOOL_MOUSE, 0);
7398 processKey(mapper, BTN_TOOL_RUBBER, 0);
7399 processKey(mapper, BTN_TOOL_PEN, 0);
7400 processKey(mapper, BTN_TOOL_FINGER, 0);
7401 processSync(mapper);
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7405}
7406
7407TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007408 addConfigurationProperty("touch.deviceType", "touchScreen");
7409 prepareDisplay(DISPLAY_ORIENTATION_0);
7410 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007411 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007412 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007413
7414 NotifyMotionArgs motionArgs;
7415
7416 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7417 processId(mapper, 1);
7418 processPosition(mapper, 100, 200);
7419 processSync(mapper);
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7421 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7423 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7424
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7426 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7428 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7429
7430 // move a little
7431 processPosition(mapper, 150, 250);
7432 processSync(mapper);
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7434 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7436 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7437
7438 // down when BTN_TOUCH is pressed, pressure defaults to 1
7439 processKey(mapper, BTN_TOUCH, 1);
7440 processSync(mapper);
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7444 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7445
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7449 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7450
7451 // up when BTN_TOUCH is released, hover restored
7452 processKey(mapper, BTN_TOUCH, 0);
7453 processSync(mapper);
7454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7455 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7457 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7458
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7460 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7462 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7463
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7467 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7468
7469 // exit hover when pointer goes away
7470 processId(mapper, -1);
7471 processSync(mapper);
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7475 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7476}
7477
7478TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007479 addConfigurationProperty("touch.deviceType", "touchScreen");
7480 prepareDisplay(DISPLAY_ORIENTATION_0);
7481 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007482 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007483
7484 NotifyMotionArgs motionArgs;
7485
7486 // initially hovering because pressure is 0
7487 processId(mapper, 1);
7488 processPosition(mapper, 100, 200);
7489 processPressure(mapper, 0);
7490 processSync(mapper);
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7494 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7495
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7499 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7500
7501 // move a little
7502 processPosition(mapper, 150, 250);
7503 processSync(mapper);
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7507 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7508
7509 // down when pressure becomes non-zero
7510 processPressure(mapper, RAW_PRESSURE_MAX);
7511 processSync(mapper);
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7515 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7516
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7520 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7521
7522 // up when pressure becomes 0, hover restored
7523 processPressure(mapper, 0);
7524 processSync(mapper);
7525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7526 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7528 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7529
7530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7533 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7534
7535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7538 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7539
7540 // exit hover when pointer goes away
7541 processId(mapper, -1);
7542 processSync(mapper);
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7546 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7547}
7548
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007549/**
7550 * Set the input device port <--> display port associations, and check that the
7551 * events are routed to the display that matches the display port.
7552 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7553 */
7554TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007555 const std::string usb2 = "USB2";
7556 const uint8_t hdmi1 = 0;
7557 const uint8_t hdmi2 = 1;
7558 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007559 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007560
7561 addConfigurationProperty("touch.deviceType", "touchScreen");
7562 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007563 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007564
7565 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7566 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7567
7568 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7569 // for this input device is specified, and the matching viewport is not present,
7570 // the input device should be disabled (at the mapper level).
7571
7572 // Add viewport for display 2 on hdmi2
7573 prepareSecondaryDisplay(type, hdmi2);
7574 // Send a touch event
7575 processPosition(mapper, 100, 100);
7576 processSync(mapper);
7577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7578
7579 // Add viewport for display 1 on hdmi1
7580 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7581 // Send a touch event again
7582 processPosition(mapper, 100, 100);
7583 processSync(mapper);
7584
7585 NotifyMotionArgs args;
7586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7587 ASSERT_EQ(DISPLAY_ID, args.displayId);
7588}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007590TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007591 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007592 std::shared_ptr<FakePointerController> fakePointerController =
7593 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007594 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007595 fakePointerController->setPosition(100, 200);
7596 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007597 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7598
Garfield Tan888a6a42020-01-09 11:39:16 -08007599 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007600 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007601
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007602 prepareDisplay(DISPLAY_ORIENTATION_0);
7603 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007604 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007605
7606 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007607 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007608
7609 NotifyMotionArgs motionArgs;
7610 processPosition(mapper, 100, 100);
7611 processSync(mapper);
7612
7613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7615 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7616}
7617
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007618/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007619 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7620 */
7621TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7622 addConfigurationProperty("touch.deviceType", "touchScreen");
7623 prepareAxes(POSITION);
7624 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7625
7626 prepareDisplay(DISPLAY_ORIENTATION_0);
7627 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7628 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7629 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7630 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7631
7632 NotifyMotionArgs args;
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7634 ASSERT_EQ(26, args.readTime);
7635
7636 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7637 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7638 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7639
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7641 ASSERT_EQ(33, args.readTime);
7642}
7643
7644/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007645 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7646 * events should not be delivered to the listener.
7647 */
7648TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7649 addConfigurationProperty("touch.deviceType", "touchScreen");
7650 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7651 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7652 ViewportType::INTERNAL);
7653 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7654 prepareAxes(POSITION);
7655 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7656
7657 NotifyMotionArgs motionArgs;
7658 processPosition(mapper, 100, 100);
7659 processSync(mapper);
7660
7661 mFakeListener->assertNotifyMotionWasNotCalled();
7662}
7663
Garfield Tanc734e4f2021-01-15 20:01:39 -08007664TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7665 addConfigurationProperty("touch.deviceType", "touchScreen");
7666 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7667 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7668 ViewportType::INTERNAL);
7669 std::optional<DisplayViewport> optionalDisplayViewport =
7670 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7671 ASSERT_TRUE(optionalDisplayViewport.has_value());
7672 DisplayViewport displayViewport = *optionalDisplayViewport;
7673
7674 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7675 prepareAxes(POSITION);
7676 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7677
7678 // Finger down
7679 int32_t x = 100, y = 100;
7680 processPosition(mapper, x, y);
7681 processSync(mapper);
7682
7683 NotifyMotionArgs motionArgs;
7684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7685 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7686
7687 // Deactivate display viewport
7688 displayViewport.isActive = false;
7689 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7690 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7691
7692 // Finger move
7693 x += 10, y += 10;
7694 processPosition(mapper, x, y);
7695 processSync(mapper);
7696
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7698 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7699
7700 // Reactivate display viewport
7701 displayViewport.isActive = true;
7702 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7703 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7704
7705 // Finger move again
7706 x += 10, y += 10;
7707 processPosition(mapper, x, y);
7708 processSync(mapper);
7709
7710 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7711 // no pointer on the touch device.
7712 mFakeListener->assertNotifyMotionWasNotCalled();
7713}
7714
Arthur Hung7c645402019-01-25 17:45:42 +08007715TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7716 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007717 prepareAxes(POSITION | ID | SLOT);
7718 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007719 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007720
7721 // Create the second touch screen device, and enable multi fingers.
7722 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007723 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007724 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007725 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007726 std::shared_ptr<InputDevice> device2 =
7727 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7728 Flags<InputDeviceClass>(0));
7729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007730 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7731 0 /*flat*/, 0 /*fuzz*/);
7732 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7733 0 /*flat*/, 0 /*fuzz*/);
7734 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7735 0 /*flat*/, 0 /*fuzz*/);
7736 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7737 0 /*flat*/, 0 /*fuzz*/);
7738 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7739 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7740 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007741
7742 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007743 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007744 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7745 device2->reset(ARBITRARY_TIME);
7746
7747 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007748 std::shared_ptr<FakePointerController> fakePointerController =
7749 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007750 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7751 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7752
7753 // Setup policy for associated displays and show touches.
7754 const uint8_t hdmi1 = 0;
7755 const uint8_t hdmi2 = 1;
7756 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7757 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7758 mFakePolicy->setShowTouches(true);
7759
7760 // Create displays.
7761 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007762 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007763
7764 // Default device will reconfigure above, need additional reconfiguration for another device.
7765 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007766 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007767
7768 // Two fingers down at default display.
7769 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7770 processPosition(mapper, x1, y1);
7771 processId(mapper, 1);
7772 processSlot(mapper, 1);
7773 processPosition(mapper, x2, y2);
7774 processId(mapper, 2);
7775 processSync(mapper);
7776
7777 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7778 fakePointerController->getSpots().find(DISPLAY_ID);
7779 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7780 ASSERT_EQ(size_t(2), iter->second.size());
7781
7782 // Two fingers down at second display.
7783 processPosition(mapper2, x1, y1);
7784 processId(mapper2, 1);
7785 processSlot(mapper2, 1);
7786 processPosition(mapper2, x2, y2);
7787 processId(mapper2, 2);
7788 processSync(mapper2);
7789
7790 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7791 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7792 ASSERT_EQ(size_t(2), iter->second.size());
7793}
7794
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007795TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007796 prepareAxes(POSITION);
7797 addConfigurationProperty("touch.deviceType", "touchScreen");
7798 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007800
7801 NotifyMotionArgs motionArgs;
7802 // Unrotated video frame
7803 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7804 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007805 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007806 processPosition(mapper, 100, 200);
7807 processSync(mapper);
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7809 ASSERT_EQ(frames, motionArgs.videoFrames);
7810
7811 // Subsequent touch events should not have any videoframes
7812 // This is implemented separately in FakeEventHub,
7813 // but that should match the behaviour of TouchVideoDevice.
7814 processPosition(mapper, 200, 200);
7815 processSync(mapper);
7816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7817 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7818}
7819
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007820TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007821 prepareAxes(POSITION);
7822 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007824 // Unrotated video frame
7825 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7826 NotifyMotionArgs motionArgs;
7827
7828 // Test all 4 orientations
7829 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7830 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7831 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7832 clearViewports();
7833 prepareDisplay(orientation);
7834 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007835 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007836 processPosition(mapper, 100, 200);
7837 processSync(mapper);
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7839 frames[0].rotate(orientation);
7840 ASSERT_EQ(frames, motionArgs.videoFrames);
7841 }
7842}
7843
7844TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007845 prepareAxes(POSITION);
7846 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007847 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007848 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7849 // so mix these.
7850 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7851 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7852 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7853 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7854 NotifyMotionArgs motionArgs;
7855
7856 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007857 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007858 processPosition(mapper, 100, 200);
7859 processSync(mapper);
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7861 std::for_each(frames.begin(), frames.end(),
7862 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7863 ASSERT_EQ(frames, motionArgs.videoFrames);
7864}
7865
Arthur Hung9da14732019-09-02 16:16:58 +08007866/**
7867 * If we had defined port associations, but the viewport is not ready, the touch device would be
7868 * expected to be disabled, and it should be enabled after the viewport has found.
7869 */
7870TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007871 constexpr uint8_t hdmi2 = 1;
7872 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007873 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007874
7875 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7876
7877 addConfigurationProperty("touch.deviceType", "touchScreen");
7878 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007879 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007880
7881 ASSERT_EQ(mDevice->isEnabled(), false);
7882
7883 // Add display on hdmi2, the device should be enabled and can receive touch event.
7884 prepareSecondaryDisplay(type, hdmi2);
7885 ASSERT_EQ(mDevice->isEnabled(), true);
7886
7887 // Send a touch event.
7888 processPosition(mapper, 100, 100);
7889 processSync(mapper);
7890
7891 NotifyMotionArgs args;
7892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7893 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7894}
7895
Arthur Hung421eb1c2020-01-16 00:09:42 +08007896TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007897 addConfigurationProperty("touch.deviceType", "touchScreen");
7898 prepareDisplay(DISPLAY_ORIENTATION_0);
7899 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007900 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007901
7902 NotifyMotionArgs motionArgs;
7903
7904 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7905 // finger down
7906 processId(mapper, 1);
7907 processPosition(mapper, x1, y1);
7908 processSync(mapper);
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7912
7913 // finger move
7914 processId(mapper, 1);
7915 processPosition(mapper, x2, y2);
7916 processSync(mapper);
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7920
7921 // finger up.
7922 processId(mapper, -1);
7923 processSync(mapper);
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7925 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7927
7928 // new finger down
7929 processId(mapper, 1);
7930 processPosition(mapper, x3, y3);
7931 processSync(mapper);
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7933 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7935}
7936
7937/**
arthurhungcc7f9802020-04-30 17:55:40 +08007938 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7939 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007940 */
arthurhungcc7f9802020-04-30 17:55:40 +08007941TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007942 addConfigurationProperty("touch.deviceType", "touchScreen");
7943 prepareDisplay(DISPLAY_ORIENTATION_0);
7944 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007946
7947 NotifyMotionArgs motionArgs;
7948
7949 // default tool type is finger
7950 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007951 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007952 processPosition(mapper, x1, y1);
7953 processSync(mapper);
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7955 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7957
7958 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7959 processToolType(mapper, MT_TOOL_PALM);
7960 processSync(mapper);
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7962 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7963
7964 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007965 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007966 processPosition(mapper, x2, y2);
7967 processSync(mapper);
7968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7969
7970 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007971 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007972 processSync(mapper);
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7974
7975 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007976 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007977 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007978 processPosition(mapper, x3, y3);
7979 processSync(mapper);
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7983}
7984
arthurhungbf89a482020-04-17 17:37:55 +08007985/**
arthurhungcc7f9802020-04-30 17:55:40 +08007986 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7987 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007988 */
arthurhungcc7f9802020-04-30 17:55:40 +08007989TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007990 addConfigurationProperty("touch.deviceType", "touchScreen");
7991 prepareDisplay(DISPLAY_ORIENTATION_0);
7992 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7993 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7994
7995 NotifyMotionArgs motionArgs;
7996
7997 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007998 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7999 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008000 processPosition(mapper, x1, y1);
8001 processSync(mapper);
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8005
8006 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008007 processSlot(mapper, SECOND_SLOT);
8008 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008009 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008010 processSync(mapper);
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8012 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8013 motionArgs.action);
8014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8015
8016 // If the tool type of the first finger changes to MT_TOOL_PALM,
8017 // we expect to receive ACTION_POINTER_UP with cancel flag.
8018 processSlot(mapper, FIRST_SLOT);
8019 processId(mapper, FIRST_TRACKING_ID);
8020 processToolType(mapper, MT_TOOL_PALM);
8021 processSync(mapper);
8022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8023 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8024 motionArgs.action);
8025 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8026
8027 // The following MOVE events of second finger should be processed.
8028 processSlot(mapper, SECOND_SLOT);
8029 processId(mapper, SECOND_TRACKING_ID);
8030 processPosition(mapper, x2 + 1, y2 + 1);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8034 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8035
8036 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8037 // it. Second finger receive move.
8038 processSlot(mapper, FIRST_SLOT);
8039 processId(mapper, INVALID_TRACKING_ID);
8040 processSync(mapper);
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8043 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8044
8045 // Second finger keeps moving.
8046 processSlot(mapper, SECOND_SLOT);
8047 processId(mapper, SECOND_TRACKING_ID);
8048 processPosition(mapper, x2 + 2, y2 + 2);
8049 processSync(mapper);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8052 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8053
8054 // Second finger up.
8055 processId(mapper, INVALID_TRACKING_ID);
8056 processSync(mapper);
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8058 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8059 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8060}
8061
8062/**
8063 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8064 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8065 */
8066TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8067 addConfigurationProperty("touch.deviceType", "touchScreen");
8068 prepareDisplay(DISPLAY_ORIENTATION_0);
8069 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8070 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8071
8072 NotifyMotionArgs motionArgs;
8073
8074 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8075 // First finger down.
8076 processId(mapper, FIRST_TRACKING_ID);
8077 processPosition(mapper, x1, y1);
8078 processSync(mapper);
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8082
8083 // Second finger down.
8084 processSlot(mapper, SECOND_SLOT);
8085 processId(mapper, SECOND_TRACKING_ID);
8086 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008087 processSync(mapper);
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8090 motionArgs.action);
8091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8092
arthurhungcc7f9802020-04-30 17:55:40 +08008093 // If the tool type of the first finger changes to MT_TOOL_PALM,
8094 // we expect to receive ACTION_POINTER_UP with cancel flag.
8095 processSlot(mapper, FIRST_SLOT);
8096 processId(mapper, FIRST_TRACKING_ID);
8097 processToolType(mapper, MT_TOOL_PALM);
8098 processSync(mapper);
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8101 motionArgs.action);
8102 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8103
8104 // Second finger keeps moving.
8105 processSlot(mapper, SECOND_SLOT);
8106 processId(mapper, SECOND_TRACKING_ID);
8107 processPosition(mapper, x2 + 1, y2 + 1);
8108 processSync(mapper);
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8111
8112 // second finger becomes palm, receive cancel due to only 1 finger is active.
8113 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008114 processToolType(mapper, MT_TOOL_PALM);
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8117 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8118
arthurhungcc7f9802020-04-30 17:55:40 +08008119 // third finger down.
8120 processSlot(mapper, THIRD_SLOT);
8121 processId(mapper, THIRD_TRACKING_ID);
8122 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008123 processPosition(mapper, x3, y3);
8124 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008128 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8129
8130 // third finger move
8131 processId(mapper, THIRD_TRACKING_ID);
8132 processPosition(mapper, x3 + 1, y3 + 1);
8133 processSync(mapper);
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8136
8137 // first finger up, third finger receive move.
8138 processSlot(mapper, FIRST_SLOT);
8139 processId(mapper, INVALID_TRACKING_ID);
8140 processSync(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8143 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8144
8145 // second finger up, third finger receive move.
8146 processSlot(mapper, SECOND_SLOT);
8147 processId(mapper, INVALID_TRACKING_ID);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8151 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8152
8153 // third finger up.
8154 processSlot(mapper, THIRD_SLOT);
8155 processId(mapper, INVALID_TRACKING_ID);
8156 processSync(mapper);
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8159 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8160}
8161
8162/**
8163 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8164 * and the active finger could still be allowed to receive the events
8165 */
8166TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8167 addConfigurationProperty("touch.deviceType", "touchScreen");
8168 prepareDisplay(DISPLAY_ORIENTATION_0);
8169 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8170 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8171
8172 NotifyMotionArgs motionArgs;
8173
8174 // default tool type is finger
8175 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8176 processId(mapper, FIRST_TRACKING_ID);
8177 processPosition(mapper, x1, y1);
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8182
8183 // Second finger down.
8184 processSlot(mapper, SECOND_SLOT);
8185 processId(mapper, SECOND_TRACKING_ID);
8186 processPosition(mapper, x2, y2);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8189 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8190 motionArgs.action);
8191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8192
8193 // If the tool type of the second finger changes to MT_TOOL_PALM,
8194 // we expect to receive ACTION_POINTER_UP with cancel flag.
8195 processId(mapper, SECOND_TRACKING_ID);
8196 processToolType(mapper, MT_TOOL_PALM);
8197 processSync(mapper);
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8199 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8200 motionArgs.action);
8201 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8202
8203 // The following MOVE event should be processed.
8204 processSlot(mapper, FIRST_SLOT);
8205 processId(mapper, FIRST_TRACKING_ID);
8206 processPosition(mapper, x1 + 1, y1 + 1);
8207 processSync(mapper);
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8210 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8211
8212 // second finger up.
8213 processSlot(mapper, SECOND_SLOT);
8214 processId(mapper, INVALID_TRACKING_ID);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8218
8219 // first finger keep moving
8220 processSlot(mapper, FIRST_SLOT);
8221 processId(mapper, FIRST_TRACKING_ID);
8222 processPosition(mapper, x1 + 2, y1 + 2);
8223 processSync(mapper);
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8226
8227 // first finger up.
8228 processId(mapper, INVALID_TRACKING_ID);
8229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8232 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008233}
8234
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008235// --- MultiTouchInputMapperTest_ExternalDevice ---
8236
8237class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8238protected:
Chris Yea52ade12020-08-27 16:49:20 -07008239 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008240};
8241
8242/**
8243 * Expect fallback to internal viewport if device is external and external viewport is not present.
8244 */
8245TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8246 prepareAxes(POSITION);
8247 addConfigurationProperty("touch.deviceType", "touchScreen");
8248 prepareDisplay(DISPLAY_ORIENTATION_0);
8249 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8250
8251 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8252
8253 NotifyMotionArgs motionArgs;
8254
8255 // Expect the event to be sent to the internal viewport,
8256 // because an external viewport is not present.
8257 processPosition(mapper, 100, 100);
8258 processSync(mapper);
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8260 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8261
8262 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008263 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008264 processPosition(mapper, 100, 100);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8268}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008269
8270/**
8271 * Test touch should not work if outside of surface.
8272 */
8273class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8274protected:
8275 void halfDisplayToCenterHorizontal(int32_t orientation) {
8276 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008277 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008278
8279 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8280 internalViewport->orientation = orientation;
8281 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8282 internalViewport->logicalLeft = 0;
8283 internalViewport->logicalTop = 0;
8284 internalViewport->logicalRight = DISPLAY_HEIGHT;
8285 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8286
8287 internalViewport->physicalLeft = 0;
8288 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8289 internalViewport->physicalRight = DISPLAY_HEIGHT;
8290 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8291
8292 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8293 internalViewport->deviceHeight = DISPLAY_WIDTH;
8294 } else {
8295 internalViewport->logicalLeft = 0;
8296 internalViewport->logicalTop = 0;
8297 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8298 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8299
8300 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8301 internalViewport->physicalTop = 0;
8302 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8303 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8304
8305 internalViewport->deviceWidth = DISPLAY_WIDTH;
8306 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8307 }
8308
8309 mFakePolicy->updateViewport(internalViewport.value());
8310 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8311 }
8312
arthurhung5d547942020-12-14 17:04:45 +08008313 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8314 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008315 int32_t yExpected) {
8316 // touch on outside area should not work.
8317 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8320
8321 // touch on inside area should receive the event.
8322 NotifyMotionArgs args;
8323 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8326 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8327 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8328
8329 // Reset.
8330 mapper.reset(ARBITRARY_TIME);
8331 }
8332};
8333
arthurhung5d547942020-12-14 17:04:45 +08008334TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008335 addConfigurationProperty("touch.deviceType", "touchScreen");
8336 prepareDisplay(DISPLAY_ORIENTATION_0);
8337 prepareAxes(POSITION);
8338 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8339
8340 // Touch on center of normal display should work.
8341 const int32_t x = DISPLAY_WIDTH / 4;
8342 const int32_t y = DISPLAY_HEIGHT / 2;
8343 processPosition(mapper, toRawX(x), toRawY(y));
8344 processSync(mapper);
8345 NotifyMotionArgs args;
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8348 0.0f, 0.0f, 0.0f, 0.0f));
8349 // Reset.
8350 mapper.reset(ARBITRARY_TIME);
8351
8352 // Let physical display be different to device, and make surface and physical could be 1:1.
8353 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8354
8355 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8356 const int32_t yExpected = y;
8357 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8358}
8359
arthurhung5d547942020-12-14 17:04:45 +08008360TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008361 addConfigurationProperty("touch.deviceType", "touchScreen");
8362 prepareDisplay(DISPLAY_ORIENTATION_0);
8363 prepareAxes(POSITION);
8364 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8365
8366 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8367 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8368
8369 const int32_t x = DISPLAY_WIDTH / 4;
8370 const int32_t y = DISPLAY_HEIGHT / 2;
8371
8372 // expect x/y = swap x/y then reverse y.
8373 const int32_t xExpected = y;
8374 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8375 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8376}
8377
arthurhung5d547942020-12-14 17:04:45 +08008378TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008379 addConfigurationProperty("touch.deviceType", "touchScreen");
8380 prepareDisplay(DISPLAY_ORIENTATION_0);
8381 prepareAxes(POSITION);
8382 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8383
8384 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8385 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8386
8387 const int32_t x = DISPLAY_WIDTH / 4;
8388 const int32_t y = DISPLAY_HEIGHT / 2;
8389
8390 // expect x/y = swap x/y then reverse x.
8391 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8392 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8393 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8394}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008395
arthurhunga36b28e2020-12-29 20:28:15 +08008396TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8397 addConfigurationProperty("touch.deviceType", "touchScreen");
8398 prepareDisplay(DISPLAY_ORIENTATION_0);
8399 prepareAxes(POSITION);
8400 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8401
8402 const int32_t x = 0;
8403 const int32_t y = 0;
8404
8405 const int32_t xExpected = x;
8406 const int32_t yExpected = y;
8407 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8408
8409 clearViewports();
8410 prepareDisplay(DISPLAY_ORIENTATION_90);
8411 // expect x/y = swap x/y then reverse y.
8412 const int32_t xExpected90 = y;
8413 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8414 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8415
8416 clearViewports();
8417 prepareDisplay(DISPLAY_ORIENTATION_270);
8418 // expect x/y = swap x/y then reverse x.
8419 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8420 const int32_t yExpected270 = x;
8421 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8422}
8423
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008424TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8425 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8426 std::shared_ptr<FakePointerController> fakePointerController =
8427 std::make_shared<FakePointerController>();
8428 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8429 fakePointerController->setPosition(0, 0);
8430 fakePointerController->setButtonState(0);
8431
8432 // prepare device and capture
8433 prepareDisplay(DISPLAY_ORIENTATION_0);
8434 prepareAxes(POSITION | ID | SLOT);
8435 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8436 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8437 mFakePolicy->setPointerCapture(true);
8438 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8439 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8440
8441 // captured touchpad should be a touchpad source
8442 NotifyDeviceResetArgs resetArgs;
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8444 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8445
Chris Yef74dc422020-09-02 22:41:50 -07008446 InputDeviceInfo deviceInfo;
8447 mDevice->getDeviceInfo(&deviceInfo);
8448
8449 const InputDeviceInfo::MotionRange* relRangeX =
8450 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8451 ASSERT_NE(relRangeX, nullptr);
8452 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8453 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8454 const InputDeviceInfo::MotionRange* relRangeY =
8455 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8456 ASSERT_NE(relRangeY, nullptr);
8457 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8458 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8459
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008460 // run captured pointer tests - note that this is unscaled, so input listener events should be
8461 // identical to what the hardware sends (accounting for any
8462 // calibration).
8463 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008464 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008465 processId(mapper, 1);
8466 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8467 processKey(mapper, BTN_TOUCH, 1);
8468 processSync(mapper);
8469
8470 // expect coord[0] to contain initial location of touch 0
8471 NotifyMotionArgs args;
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8474 ASSERT_EQ(1U, args.pointerCount);
8475 ASSERT_EQ(0, args.pointerProperties[0].id);
8476 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8477 ASSERT_NO_FATAL_FAILURE(
8478 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8479
8480 // FINGER 1 DOWN
8481 processSlot(mapper, 1);
8482 processId(mapper, 2);
8483 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8484 processSync(mapper);
8485
8486 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008488 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8489 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008490 ASSERT_EQ(2U, args.pointerCount);
8491 ASSERT_EQ(0, args.pointerProperties[0].id);
8492 ASSERT_EQ(1, args.pointerProperties[1].id);
8493 ASSERT_NO_FATAL_FAILURE(
8494 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8495 ASSERT_NO_FATAL_FAILURE(
8496 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8497
8498 // FINGER 1 MOVE
8499 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8500 processSync(mapper);
8501
8502 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8503 // from move
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8506 ASSERT_NO_FATAL_FAILURE(
8507 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8508 ASSERT_NO_FATAL_FAILURE(
8509 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8510
8511 // FINGER 0 MOVE
8512 processSlot(mapper, 0);
8513 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8514 processSync(mapper);
8515
8516 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8519 ASSERT_NO_FATAL_FAILURE(
8520 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8521 ASSERT_NO_FATAL_FAILURE(
8522 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8523
8524 // BUTTON DOWN
8525 processKey(mapper, BTN_LEFT, 1);
8526 processSync(mapper);
8527
8528 // touchinputmapper design sends a move before button press
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8533
8534 // BUTTON UP
8535 processKey(mapper, BTN_LEFT, 0);
8536 processSync(mapper);
8537
8538 // touchinputmapper design sends a move after button release
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8543
8544 // FINGER 0 UP
8545 processId(mapper, -1);
8546 processSync(mapper);
8547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8548 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8549
8550 // FINGER 1 MOVE
8551 processSlot(mapper, 1);
8552 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8553 processSync(mapper);
8554
8555 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8558 ASSERT_EQ(1U, args.pointerCount);
8559 ASSERT_EQ(1, args.pointerProperties[0].id);
8560 ASSERT_NO_FATAL_FAILURE(
8561 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8562
8563 // FINGER 1 UP
8564 processId(mapper, -1);
8565 processKey(mapper, BTN_TOUCH, 0);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8569
8570 // non captured touchpad should be a mouse source
8571 mFakePolicy->setPointerCapture(false);
8572 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8574 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8575}
8576
8577TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8578 std::shared_ptr<FakePointerController> fakePointerController =
8579 std::make_shared<FakePointerController>();
8580 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8581 fakePointerController->setPosition(0, 0);
8582 fakePointerController->setButtonState(0);
8583
8584 // prepare device and capture
8585 prepareDisplay(DISPLAY_ORIENTATION_0);
8586 prepareAxes(POSITION | ID | SLOT);
8587 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8588 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8589 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8591 // run uncaptured pointer tests - pushes out generic events
8592 // FINGER 0 DOWN
8593 processId(mapper, 3);
8594 processPosition(mapper, 100, 100);
8595 processKey(mapper, BTN_TOUCH, 1);
8596 processSync(mapper);
8597
8598 // start at (100,100), cursor should be at (0,0) * scale
8599 NotifyMotionArgs args;
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8602 ASSERT_NO_FATAL_FAILURE(
8603 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8604
8605 // FINGER 0 MOVE
8606 processPosition(mapper, 200, 200);
8607 processSync(mapper);
8608
8609 // compute scaling to help with touch position checking
8610 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8611 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8612 float scale =
8613 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8614
8615 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8617 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8619 0, 0, 0, 0, 0, 0, 0));
8620}
8621
8622TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8623 std::shared_ptr<FakePointerController> fakePointerController =
8624 std::make_shared<FakePointerController>();
8625
8626 prepareDisplay(DISPLAY_ORIENTATION_0);
8627 prepareAxes(POSITION | ID | SLOT);
8628 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8629 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8630 mFakePolicy->setPointerCapture(false);
8631 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8632
8633 // uncaptured touchpad should be a pointer device
8634 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8635
8636 // captured touchpad should be a touchpad device
8637 mFakePolicy->setPointerCapture(true);
8638 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8639 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8640}
8641
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008642// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008643
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008644class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008645protected:
8646 static const char* DEVICE_NAME;
8647 static const char* DEVICE_LOCATION;
8648 static const int32_t DEVICE_ID;
8649 static const int32_t DEVICE_GENERATION;
8650 static const int32_t DEVICE_CONTROLLER_NUMBER;
8651 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8652 static const int32_t EVENTHUB_ID;
8653
8654 std::shared_ptr<FakeEventHub> mFakeEventHub;
8655 sp<FakeInputReaderPolicy> mFakePolicy;
8656 sp<TestInputListener> mFakeListener;
8657 std::unique_ptr<InstrumentedInputReader> mReader;
8658 std::shared_ptr<InputDevice> mDevice;
8659
8660 virtual void SetUp(Flags<InputDeviceClass> classes) {
8661 mFakeEventHub = std::make_unique<FakeEventHub>();
8662 mFakePolicy = new FakeInputReaderPolicy();
8663 mFakeListener = new TestInputListener();
8664 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8665 mFakeListener);
8666 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8667 }
8668
8669 void SetUp() override { SetUp(DEVICE_CLASSES); }
8670
8671 void TearDown() override {
8672 mFakeListener.clear();
8673 mFakePolicy.clear();
8674 }
8675
8676 void configureDevice(uint32_t changes) {
8677 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8678 mReader->requestRefreshConfiguration(changes);
8679 mReader->loopOnce();
8680 }
8681 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8682 }
8683
8684 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8685 const std::string& location, int32_t eventHubId,
8686 Flags<InputDeviceClass> classes) {
8687 InputDeviceIdentifier identifier;
8688 identifier.name = name;
8689 identifier.location = location;
8690 std::shared_ptr<InputDevice> device =
8691 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8692 identifier);
8693 mReader->pushNextDevice(device);
8694 mFakeEventHub->addDevice(eventHubId, name, classes);
8695 mReader->loopOnce();
8696 return device;
8697 }
8698
8699 template <class T, typename... Args>
8700 T& addControllerAndConfigure(Args... args) {
8701 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8702
8703 return controller;
8704 }
8705};
8706
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008707const char* PeripheralControllerTest::DEVICE_NAME = "device";
8708const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8709const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8710const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8711const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8712const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008713 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008714const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008715
8716// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008717class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008718protected:
8719 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008720 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008721 }
8722};
8723
8724TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008725 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008726
8727 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8728 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8729}
8730
8731TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008732 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008733
8734 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8735 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8736}
8737
8738// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008739class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008740protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008741 void SetUp() override {
8742 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8743 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008744};
8745
Chris Ye85758332021-05-16 23:05:17 -07008746TEST_F(LightControllerTest, MonoLight) {
8747 RawLightInfo infoMono = {.id = 1,
8748 .name = "Mono",
8749 .maxBrightness = 255,
8750 .flags = InputLightClass::BRIGHTNESS,
8751 .path = ""};
8752 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008753
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008754 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008755 InputDeviceInfo info;
8756 controller.populateDeviceInfo(&info);
8757 const auto& ids = info.getLightIds();
8758 ASSERT_EQ(1UL, ids.size());
Chris Ye85758332021-05-16 23:05:17 -07008759 ASSERT_EQ(InputDeviceLightType::MONO, info.getLightInfo(ids[0])->type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008760
8761 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_BRIGHTNESS));
8762 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_BRIGHTNESS);
8763}
8764
8765TEST_F(LightControllerTest, RGBLight) {
8766 RawLightInfo infoRed = {.id = 1,
8767 .name = "red",
8768 .maxBrightness = 255,
8769 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8770 .path = ""};
8771 RawLightInfo infoGreen = {.id = 2,
8772 .name = "green",
8773 .maxBrightness = 255,
8774 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8775 .path = ""};
8776 RawLightInfo infoBlue = {.id = 3,
8777 .name = "blue",
8778 .maxBrightness = 255,
8779 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8780 .path = ""};
8781 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8782 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8783 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8784
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008785 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008786 InputDeviceInfo info;
8787 controller.populateDeviceInfo(&info);
8788 const auto& ids = info.getLightIds();
8789 ASSERT_EQ(1UL, ids.size());
8790 ASSERT_EQ(InputDeviceLightType::RGB, info.getLightInfo(ids[0])->type);
8791
8792 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_COLOR));
8793 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
8794}
8795
8796TEST_F(LightControllerTest, MultiColorRGBLight) {
8797 RawLightInfo infoColor = {.id = 1,
8798 .name = "red",
8799 .maxBrightness = 255,
8800 .flags = InputLightClass::BRIGHTNESS |
8801 InputLightClass::MULTI_INTENSITY |
8802 InputLightClass::MULTI_INDEX,
8803 .path = ""};
8804
8805 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8806
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008807 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008808 InputDeviceInfo info;
8809 controller.populateDeviceInfo(&info);
8810 const auto& ids = info.getLightIds();
8811 ASSERT_EQ(1UL, ids.size());
8812 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, info.getLightInfo(ids[0])->type);
8813
8814 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_COLOR));
8815 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
8816}
8817
8818TEST_F(LightControllerTest, PlayerIdLight) {
8819 RawLightInfo info1 = {.id = 1,
8820 .name = "player1",
8821 .maxBrightness = 255,
8822 .flags = InputLightClass::BRIGHTNESS,
8823 .path = ""};
8824 RawLightInfo info2 = {.id = 2,
8825 .name = "player2",
8826 .maxBrightness = 255,
8827 .flags = InputLightClass::BRIGHTNESS,
8828 .path = ""};
8829 RawLightInfo info3 = {.id = 3,
8830 .name = "player3",
8831 .maxBrightness = 255,
8832 .flags = InputLightClass::BRIGHTNESS,
8833 .path = ""};
8834 RawLightInfo info4 = {.id = 4,
8835 .name = "player4",
8836 .maxBrightness = 255,
8837 .flags = InputLightClass::BRIGHTNESS,
8838 .path = ""};
8839 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8840 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8841 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8842 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8843
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008844 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008845 InputDeviceInfo info;
8846 controller.populateDeviceInfo(&info);
8847 const auto& ids = info.getLightIds();
8848 ASSERT_EQ(1UL, ids.size());
8849 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, info.getLightInfo(ids[0])->type);
8850
8851 ASSERT_FALSE(controller.setLightColor(ids[0], LIGHT_COLOR));
8852 ASSERT_TRUE(controller.setLightPlayerId(ids[0], LIGHT_PLAYER_ID));
8853 ASSERT_EQ(controller.getLightPlayerId(ids[0]).value_or(-1), LIGHT_PLAYER_ID);
8854}
8855
Michael Wrightd02c5b62014-02-10 15:10:22 -08008856} // namespace android