blob: 38dfe4041fdda1da0cc630038715a6ef0795d7cb [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
Prabir Pradhanf192a102021-08-06 14:01:18 +0000302 PointerCaptureRequest setPointerCapture(bool enabled) {
303 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
304 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800305 }
306
Arthur Hung7c645402019-01-25 17:45:42 +0800307 void setShowTouches(bool enabled) {
308 mConfig.showTouches = enabled;
309 }
310
Garfield Tan888a6a42020-01-09 11:39:16 -0800311 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
312 mConfig.defaultPointerDisplayId = pointerDisplayId;
313 }
314
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800315 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
316
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317private:
Prabir Pradhanf192a102021-08-06 14:01:18 +0000318 uint32_t mNextPointerCaptureSequenceNumber = 0;
319
Santos Cordonfa5cf462017-04-05 10:37:00 -0700320 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000321 int32_t orientation, bool isActive,
322 const std::string& uniqueId,
323 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700324 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
325 || orientation == DISPLAY_ORIENTATION_270);
326 DisplayViewport v;
327 v.displayId = displayId;
328 v.orientation = orientation;
329 v.logicalLeft = 0;
330 v.logicalTop = 0;
331 v.logicalRight = isRotated ? height : width;
332 v.logicalBottom = isRotated ? width : height;
333 v.physicalLeft = 0;
334 v.physicalTop = 0;
335 v.physicalRight = isRotated ? height : width;
336 v.physicalBottom = isRotated ? width : height;
337 v.deviceWidth = isRotated ? height : width;
338 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000339 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700340 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700341 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100342 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700343 return v;
344 }
345
Chris Yea52ade12020-08-27 16:49:20 -0700346 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 *outConfig = mConfig;
348 }
349
Chris Yea52ade12020-08-27 16:49:20 -0700350 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100351 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352 }
353
Chris Yea52ade12020-08-27 16:49:20 -0700354 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700355 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700357 mInputDevicesChanged = true;
358 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359 }
360
Chris Yea52ade12020-08-27 16:49:20 -0700361 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
362 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700363 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 }
365
Chris Yea52ade12020-08-27 16:49:20 -0700366 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800367
368 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
369 std::unique_lock<std::mutex> lock(mLock);
370 base::ScopedLockAssertion assumeLocked(mLock);
371
372 const bool devicesChanged =
373 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
374 return mInputDevicesChanged;
375 });
376 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
377 mInputDevicesChanged = false;
378 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379};
380
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381// --- FakeEventHub ---
382
383class FakeEventHub : public EventHubInterface {
384 struct KeyInfo {
385 int32_t keyCode;
386 uint32_t flags;
387 };
388
Chris Yef59a2f42020-10-16 12:55:26 -0700389 struct SensorInfo {
390 InputDeviceSensorType sensorType;
391 int32_t sensorDataIndex;
392 };
393
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 struct Device {
395 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700396 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 PropertyMap configuration;
398 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
399 KeyedVector<int, bool> relativeAxes;
400 KeyedVector<int32_t, int32_t> keyCodeStates;
401 KeyedVector<int32_t, int32_t> scanCodeStates;
402 KeyedVector<int32_t, int32_t> switchStates;
403 KeyedVector<int32_t, int32_t> absoluteAxisValue;
404 KeyedVector<int32_t, KeyInfo> keysByScanCode;
405 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
406 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700407 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
408 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800409 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700410 bool enabled;
411
412 status_t enable() {
413 enabled = true;
414 return OK;
415 }
416
417 status_t disable() {
418 enabled = false;
419 return OK;
420 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421
Chris Ye1b0c7342020-07-28 21:57:03 -0700422 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 };
424
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700425 std::mutex mLock;
426 std::condition_variable mEventsCondition;
427
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100429 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000430 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600431 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000432 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800433 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
434 // Simulates a device light brightness, from light id to light brightness.
435 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
436 // Simulates a device light intensities, from light id to light intensities map.
437 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
438 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700440public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441 virtual ~FakeEventHub() {
442 for (size_t i = 0; i < mDevices.size(); i++) {
443 delete mDevices.valueAt(i);
444 }
445 }
446
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447 FakeEventHub() { }
448
Chris Ye1b0c7342020-07-28 21:57:03 -0700449 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450 Device* device = new Device(classes);
451 device->identifier.name = name;
452 mDevices.add(deviceId, device);
453
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000454 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 }
456
457 void removeDevice(int32_t deviceId) {
458 delete mDevices.valueFor(deviceId);
459 mDevices.removeItem(deviceId);
460
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000461 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462 }
463
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700464 bool isDeviceEnabled(int32_t deviceId) {
465 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700466 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700467 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
468 return false;
469 }
470 return device->enabled;
471 }
472
473 status_t enableDevice(int32_t deviceId) {
474 status_t result;
475 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700476 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700477 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
478 return BAD_VALUE;
479 }
480 if (device->enabled) {
481 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
482 return OK;
483 }
484 result = device->enable();
485 return result;
486 }
487
488 status_t disableDevice(int32_t deviceId) {
489 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700490 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700491 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
492 return BAD_VALUE;
493 }
494 if (!device->enabled) {
495 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
496 return OK;
497 }
498 return device->disable();
499 }
500
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000502 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 }
504
505 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
506 Device* device = getDevice(deviceId);
507 device->configuration.addProperty(key, value);
508 }
509
510 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
511 Device* device = getDevice(deviceId);
512 device->configuration.addAll(configuration);
513 }
514
515 void addAbsoluteAxis(int32_t deviceId, int axis,
516 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
517 Device* device = getDevice(deviceId);
518
519 RawAbsoluteAxisInfo info;
520 info.valid = true;
521 info.minValue = minValue;
522 info.maxValue = maxValue;
523 info.flat = flat;
524 info.fuzz = fuzz;
525 info.resolution = resolution;
526 device->absoluteAxes.add(axis, info);
527 }
528
529 void addRelativeAxis(int32_t deviceId, int32_t axis) {
530 Device* device = getDevice(deviceId);
531 device->relativeAxes.add(axis, true);
532 }
533
534 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
535 Device* device = getDevice(deviceId);
536 device->keyCodeStates.replaceValueFor(keyCode, state);
537 }
538
539 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
540 Device* device = getDevice(deviceId);
541 device->scanCodeStates.replaceValueFor(scanCode, state);
542 }
543
544 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
545 Device* device = getDevice(deviceId);
546 device->switchStates.replaceValueFor(switchCode, state);
547 }
548
549 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
550 Device* device = getDevice(deviceId);
551 device->absoluteAxisValue.replaceValueFor(axis, value);
552 }
553
554 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
555 int32_t keyCode, uint32_t flags) {
556 Device* device = getDevice(deviceId);
557 KeyInfo info;
558 info.keyCode = keyCode;
559 info.flags = flags;
560 if (scanCode) {
561 device->keysByScanCode.add(scanCode, info);
562 }
563 if (usageCode) {
564 device->keysByUsageCode.add(usageCode, info);
565 }
566 }
567
568 void addLed(int32_t deviceId, int32_t led, bool initialState) {
569 Device* device = getDevice(deviceId);
570 device->leds.add(led, initialState);
571 }
572
Chris Yef59a2f42020-10-16 12:55:26 -0700573 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
574 int32_t sensorDataIndex) {
575 Device* device = getDevice(deviceId);
576 SensorInfo info;
577 info.sensorType = sensorType;
578 info.sensorDataIndex = sensorDataIndex;
579 device->sensorsByAbsCode.emplace(absCode, info);
580 }
581
582 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
583 Device* device = getDevice(deviceId);
584 typename BitArray<MSC_MAX>::Buffer buffer;
585 buffer[mscEvent / 32] = 1 << mscEvent % 32;
586 device->mscBitmask.loadFromBuffer(buffer);
587 }
588
Chris Ye3fdbfef2021-01-06 18:45:18 -0800589 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
590 mRawLightInfos.emplace(rawId, std::move(info));
591 }
592
593 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
594 mLightBrightness.emplace(rawId, brightness);
595 }
596
597 void fakeLightIntensities(int32_t rawId,
598 const std::unordered_map<LightColor, int32_t> intensities) {
599 mLightIntensities.emplace(rawId, std::move(intensities));
600 }
601
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 bool getLedState(int32_t deviceId, int32_t led) {
603 Device* device = getDevice(deviceId);
604 return device->leds.valueFor(led);
605 }
606
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100607 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 return mExcludedDevices;
609 }
610
611 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
612 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800613 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 }
615
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000616 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
617 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700618 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 RawEvent event;
620 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000621 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 event.deviceId = deviceId;
623 event.type = type;
624 event.code = code;
625 event.value = value;
626 mEvents.push_back(event);
627
628 if (type == EV_ABS) {
629 setAbsoluteAxisValue(deviceId, code, value);
630 }
631 }
632
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600633 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
634 std::vector<TouchVideoFrame>> videoFrames) {
635 mVideoFrames = std::move(videoFrames);
636 }
637
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700639 std::unique_lock<std::mutex> lock(mLock);
640 base::ScopedLockAssertion assumeLocked(mLock);
641 const bool queueIsEmpty =
642 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
643 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
644 if (!queueIsEmpty) {
645 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
646 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647 }
648
649private:
650 Device* getDevice(int32_t deviceId) const {
651 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100652 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 }
654
Chris Yea52ade12020-08-27 16:49:20 -0700655 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700657 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 }
659
Chris Yea52ade12020-08-27 16:49:20 -0700660 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 Device* device = getDevice(deviceId);
662 return device ? device->identifier : InputDeviceIdentifier();
663 }
664
Chris Yea52ade12020-08-27 16:49:20 -0700665 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666
Chris Yea52ade12020-08-27 16:49:20 -0700667 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 Device* device = getDevice(deviceId);
669 if (device) {
670 *outConfiguration = device->configuration;
671 }
672 }
673
Chris Yea52ade12020-08-27 16:49:20 -0700674 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
675 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800677 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 ssize_t index = device->absoluteAxes.indexOfKey(axis);
679 if (index >= 0) {
680 *outAxisInfo = device->absoluteAxes.valueAt(index);
681 return OK;
682 }
683 }
684 outAxisInfo->clear();
685 return -1;
686 }
687
Chris Yea52ade12020-08-27 16:49:20 -0700688 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 Device* device = getDevice(deviceId);
690 if (device) {
691 return device->relativeAxes.indexOfKey(axis) >= 0;
692 }
693 return false;
694 }
695
Chris Yea52ade12020-08-27 16:49:20 -0700696 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697
Chris Yef59a2f42020-10-16 12:55:26 -0700698 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
699 Device* device = getDevice(deviceId);
700 if (device) {
701 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
702 }
703 return false;
704 }
705
Chris Yea52ade12020-08-27 16:49:20 -0700706 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
707 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 Device* device = getDevice(deviceId);
709 if (device) {
710 const KeyInfo* key = getKey(device, scanCode, usageCode);
711 if (key) {
712 if (outKeycode) {
713 *outKeycode = key->keyCode;
714 }
715 if (outFlags) {
716 *outFlags = key->flags;
717 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700718 if (outMetaState) {
719 *outMetaState = metaState;
720 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 return OK;
722 }
723 }
724 return NAME_NOT_FOUND;
725 }
726
727 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
728 if (usageCode) {
729 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
730 if (index >= 0) {
731 return &device->keysByUsageCode.valueAt(index);
732 }
733 }
734 if (scanCode) {
735 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
736 if (index >= 0) {
737 return &device->keysByScanCode.valueAt(index);
738 }
739 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700740 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741 }
742
Chris Yea52ade12020-08-27 16:49:20 -0700743 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744
Chris Yef59a2f42020-10-16 12:55:26 -0700745 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
746 int32_t absCode) {
747 Device* device = getDevice(deviceId);
748 if (!device) {
749 return Errorf("Sensor device not found.");
750 }
751 auto it = device->sensorsByAbsCode.find(absCode);
752 if (it == device->sensorsByAbsCode.end()) {
753 return Errorf("Sensor map not found.");
754 }
755 const SensorInfo& info = it->second;
756 return std::make_pair(info.sensorType, info.sensorDataIndex);
757 }
758
Chris Yea52ade12020-08-27 16:49:20 -0700759 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760 mExcludedDevices = devices;
761 }
762
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000763 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
764 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000766 const size_t filledSize = std::min(mEvents.size(), bufferSize);
767 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
768
769 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700770 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000771 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772 }
773
Chris Yea52ade12020-08-27 16:49:20 -0700774 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600775 auto it = mVideoFrames.find(deviceId);
776 if (it != mVideoFrames.end()) {
777 std::vector<TouchVideoFrame> frames = std::move(it->second);
778 mVideoFrames.erase(deviceId);
779 return frames;
780 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800781 return {};
782 }
783
Chris Yea52ade12020-08-27 16:49:20 -0700784 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800785 Device* device = getDevice(deviceId);
786 if (device) {
787 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
788 if (index >= 0) {
789 return device->scanCodeStates.valueAt(index);
790 }
791 }
792 return AKEY_STATE_UNKNOWN;
793 }
794
Chris Yea52ade12020-08-27 16:49:20 -0700795 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796 Device* device = getDevice(deviceId);
797 if (device) {
798 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
799 if (index >= 0) {
800 return device->keyCodeStates.valueAt(index);
801 }
802 }
803 return AKEY_STATE_UNKNOWN;
804 }
805
Chris Yea52ade12020-08-27 16:49:20 -0700806 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 Device* device = getDevice(deviceId);
808 if (device) {
809 ssize_t index = device->switchStates.indexOfKey(sw);
810 if (index >= 0) {
811 return device->switchStates.valueAt(index);
812 }
813 }
814 return AKEY_STATE_UNKNOWN;
815 }
816
Chris Yea52ade12020-08-27 16:49:20 -0700817 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
818 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800819 Device* device = getDevice(deviceId);
820 if (device) {
821 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
822 if (index >= 0) {
823 *outValue = device->absoluteAxisValue.valueAt(index);
824 return OK;
825 }
826 }
827 *outValue = 0;
828 return -1;
829 }
830
Chris Yea52ade12020-08-27 16:49:20 -0700831 // Return true if the device has non-empty key layout.
832 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
833 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 bool result = false;
835 Device* device = getDevice(deviceId);
836 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700837 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 for (size_t i = 0; i < numCodes; i++) {
839 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
840 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
841 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842 }
843 }
844 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
845 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
846 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 }
848 }
849 }
850 }
851 return result;
852 }
853
Chris Yea52ade12020-08-27 16:49:20 -0700854 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 Device* device = getDevice(deviceId);
856 if (device) {
857 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
858 return index >= 0;
859 }
860 return false;
861 }
862
Chris Yea52ade12020-08-27 16:49:20 -0700863 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864 Device* device = getDevice(deviceId);
865 return device && device->leds.indexOfKey(led) >= 0;
866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 Device* device = getDevice(deviceId);
870 if (device) {
871 ssize_t index = device->leds.indexOfKey(led);
872 if (index >= 0) {
873 device->leds.replaceValueAt(led, on);
874 } else {
875 ADD_FAILURE()
876 << "Attempted to set the state of an LED that the EventHub declared "
877 "was not present. led=" << led;
878 }
879 }
880 }
881
Chris Yea52ade12020-08-27 16:49:20 -0700882 void getVirtualKeyDefinitions(
883 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884 outVirtualKeys.clear();
885
886 Device* device = getDevice(deviceId);
887 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800888 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889 }
890 }
891
Chris Yea52ade12020-08-27 16:49:20 -0700892 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700893 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894 }
895
Chris Yea52ade12020-08-27 16:49:20 -0700896 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 return false;
898 }
899
Chris Yea52ade12020-08-27 16:49:20 -0700900 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Chris Yea52ade12020-08-27 16:49:20 -0700902 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903
Chris Ye87143712020-11-10 05:05:58 +0000904 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
905
Chris Yee2b1e5c2021-03-10 22:45:12 -0800906 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
907 return BATTERY_CAPACITY;
908 }
Kim Low03ea0352020-11-06 12:45:07 -0800909
Chris Yee2b1e5c2021-03-10 22:45:12 -0800910 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
911 return BATTERY_STATUS;
912 }
913
914 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
915
916 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
917 return std::nullopt;
918 }
Kim Low03ea0352020-11-06 12:45:07 -0800919
Chris Ye3fdbfef2021-01-06 18:45:18 -0800920 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
921 std::vector<int32_t> ids;
922 for (const auto& [rawId, info] : mRawLightInfos) {
923 ids.push_back(rawId);
924 }
925 return ids;
926 }
927
928 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
929 auto it = mRawLightInfos.find(lightId);
930 if (it == mRawLightInfos.end()) {
931 return std::nullopt;
932 }
933 return it->second;
934 }
935
936 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
937 mLightBrightness.emplace(lightId, brightness);
938 }
939
940 void setLightIntensities(int32_t deviceId, int32_t lightId,
941 std::unordered_map<LightColor, int32_t> intensities) override {
942 mLightIntensities.emplace(lightId, intensities);
943 };
944
945 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
946 auto lightIt = mLightBrightness.find(lightId);
947 if (lightIt == mLightBrightness.end()) {
948 return std::nullopt;
949 }
950 return lightIt->second;
951 }
952
953 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
954 int32_t deviceId, int32_t lightId) override {
955 auto lightIt = mLightIntensities.find(lightId);
956 if (lightIt == mLightIntensities.end()) {
957 return std::nullopt;
958 }
959 return lightIt->second;
960 };
961
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100962 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963 return false;
964 }
965
Chris Yea52ade12020-08-27 16:49:20 -0700966 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Chris Yea52ade12020-08-27 16:49:20 -0700968 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969
Chris Yea52ade12020-08-27 16:49:20 -0700970 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971
Chris Yea52ade12020-08-27 16:49:20 -0700972 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973};
974
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975// --- FakeInputMapper ---
976
977class FakeInputMapper : public InputMapper {
978 uint32_t mSources;
979 int32_t mKeyboardType;
980 int32_t mMetaState;
981 KeyedVector<int32_t, int32_t> mKeyCodeStates;
982 KeyedVector<int32_t, int32_t> mScanCodeStates;
983 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800984 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700986 std::mutex mLock;
987 std::condition_variable mStateChangedCondition;
988 bool mConfigureWasCalled GUARDED_BY(mLock);
989 bool mResetWasCalled GUARDED_BY(mLock);
990 bool mProcessWasCalled GUARDED_BY(mLock);
991 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992
Arthur Hungc23540e2018-11-29 20:42:11 +0800993 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800995 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
996 : InputMapper(deviceContext),
997 mSources(sources),
998 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001000 mConfigureWasCalled(false),
1001 mResetWasCalled(false),
1002 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
Chris Yea52ade12020-08-27 16:49:20 -07001004 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005
1006 void setKeyboardType(int32_t keyboardType) {
1007 mKeyboardType = keyboardType;
1008 }
1009
1010 void setMetaState(int32_t metaState) {
1011 mMetaState = metaState;
1012 }
1013
1014 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001015 std::unique_lock<std::mutex> lock(mLock);
1016 base::ScopedLockAssertion assumeLocked(mLock);
1017 const bool configureCalled =
1018 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1019 return mConfigureWasCalled;
1020 });
1021 if (!configureCalled) {
1022 FAIL() << "Expected configure() to have been called.";
1023 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024 mConfigureWasCalled = false;
1025 }
1026
1027 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001028 std::unique_lock<std::mutex> lock(mLock);
1029 base::ScopedLockAssertion assumeLocked(mLock);
1030 const bool resetCalled =
1031 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1032 return mResetWasCalled;
1033 });
1034 if (!resetCalled) {
1035 FAIL() << "Expected reset() to have been called.";
1036 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037 mResetWasCalled = false;
1038 }
1039
Yi Kong9b14ac62018-07-17 13:48:38 -07001040 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001041 std::unique_lock<std::mutex> lock(mLock);
1042 base::ScopedLockAssertion assumeLocked(mLock);
1043 const bool processCalled =
1044 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1045 return mProcessWasCalled;
1046 });
1047 if (!processCalled) {
1048 FAIL() << "Expected process() to have been called.";
1049 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050 if (outLastEvent) {
1051 *outLastEvent = mLastEvent;
1052 }
1053 mProcessWasCalled = false;
1054 }
1055
1056 void setKeyCodeState(int32_t keyCode, int32_t state) {
1057 mKeyCodeStates.replaceValueFor(keyCode, state);
1058 }
1059
1060 void setScanCodeState(int32_t scanCode, int32_t state) {
1061 mScanCodeStates.replaceValueFor(scanCode, state);
1062 }
1063
1064 void setSwitchState(int32_t switchCode, int32_t state) {
1065 mSwitchStates.replaceValueFor(switchCode, state);
1066 }
1067
1068 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001069 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070 }
1071
1072private:
Chris Yea52ade12020-08-27 16:49:20 -07001073 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Chris Yea52ade12020-08-27 16:49:20 -07001075 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076 InputMapper::populateDeviceInfo(deviceInfo);
1077
1078 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1079 deviceInfo->setKeyboardType(mKeyboardType);
1080 }
1081 }
1082
Chris Yea52ade12020-08-27 16:49:20 -07001083 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001084 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001086
1087 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001088 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001089 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1090 mViewport = config->getDisplayViewportByPort(*displayPort);
1091 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001092
1093 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094 }
1095
Chris Yea52ade12020-08-27 16:49:20 -07001096 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001097 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001099 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 }
1101
Chris Yea52ade12020-08-27 16:49:20 -07001102 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001103 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 mLastEvent = *rawEvent;
1105 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001106 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 }
1108
Chris Yea52ade12020-08-27 16:49:20 -07001109 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1111 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1112 }
1113
Chris Yea52ade12020-08-27 16:49:20 -07001114 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1116 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1117 }
1118
Chris Yea52ade12020-08-27 16:49:20 -07001119 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1121 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1122 }
1123
Chris Yea52ade12020-08-27 16:49:20 -07001124 // Return true if the device has non-empty key layout.
1125 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1126 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127 for (size_t i = 0; i < numCodes; i++) {
1128 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1129 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1130 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 }
1132 }
1133 }
Chris Yea52ade12020-08-27 16:49:20 -07001134 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 return result;
1136 }
1137
1138 virtual int32_t getMetaState() {
1139 return mMetaState;
1140 }
1141
1142 virtual void fadePointer() {
1143 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001144
1145 virtual std::optional<int32_t> getAssociatedDisplay() {
1146 if (mViewport) {
1147 return std::make_optional(mViewport->displayId);
1148 }
1149 return std::nullopt;
1150 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151};
1152
1153
1154// --- InstrumentedInputReader ---
1155
1156class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001157 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158
1159public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001160 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1161 const sp<InputReaderPolicyInterface>& policy,
1162 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001163 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001165 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001166
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001167 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001169 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001170 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 InputDeviceIdentifier identifier;
1172 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001173 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001175 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Prabir Pradhan28efc192019-11-05 01:10:04 +00001178 // Make the protected loopOnce method accessible to tests.
1179 using InputReader::loopOnce;
1180
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001182 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1183 const InputDeviceIdentifier& identifier)
1184 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001185 if (!mNextDevices.empty()) {
1186 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1187 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188 return device;
1189 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001190 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191 }
1192
arthurhungdcef2dc2020-08-11 14:47:50 +08001193 // --- FakeInputReaderContext ---
1194 class FakeInputReaderContext : public ContextImpl {
1195 int32_t mGlobalMetaState;
1196 bool mUpdateGlobalMetaStateWasCalled;
1197 int32_t mGeneration;
1198
1199 public:
1200 FakeInputReaderContext(InputReader* reader)
1201 : ContextImpl(reader),
1202 mGlobalMetaState(0),
1203 mUpdateGlobalMetaStateWasCalled(false),
1204 mGeneration(1) {}
1205
1206 virtual ~FakeInputReaderContext() {}
1207
1208 void assertUpdateGlobalMetaStateWasCalled() {
1209 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1210 << "Expected updateGlobalMetaState() to have been called.";
1211 mUpdateGlobalMetaStateWasCalled = false;
1212 }
1213
1214 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1215
1216 uint32_t getGeneration() { return mGeneration; }
1217
1218 void updateGlobalMetaState() override {
1219 mUpdateGlobalMetaStateWasCalled = true;
1220 ContextImpl::updateGlobalMetaState();
1221 }
1222
1223 int32_t getGlobalMetaState() override {
1224 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1225 }
1226
1227 int32_t bumpGeneration() override {
1228 mGeneration = ContextImpl::bumpGeneration();
1229 return mGeneration;
1230 }
1231 } mFakeContext;
1232
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001234
1235public:
1236 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001237};
1238
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239// --- InputReaderPolicyTest ---
1240class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001241protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001242 sp<FakeInputReaderPolicy> mFakePolicy;
1243
Chris Yea52ade12020-08-27 16:49:20 -07001244 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1245 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246};
1247
1248/**
1249 * Check that empty set of viewports is an acceptable configuration.
1250 * Also try to get internal viewport two different ways - by type and by uniqueId.
1251 *
1252 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1253 * Such configuration is not currently allowed.
1254 */
1255TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001256 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001257
1258 // We didn't add any viewports yet, so there shouldn't be any.
1259 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001260 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001261 ASSERT_FALSE(internalViewport);
1262
1263 // Add an internal viewport, then clear it
1264 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001265 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001266 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267
1268 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001269 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001271 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272
1273 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001274 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001276 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001277
1278 mFakePolicy->clearViewports();
1279 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001280 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001281 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001282 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001283 ASSERT_FALSE(internalViewport);
1284}
1285
1286TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1287 const std::string internalUniqueId = "local:0";
1288 const std::string externalUniqueId = "local:1";
1289 const std::string virtualUniqueId1 = "virtual:2";
1290 const std::string virtualUniqueId2 = "virtual:3";
1291 constexpr int32_t virtualDisplayId1 = 2;
1292 constexpr int32_t virtualDisplayId2 = 3;
1293
1294 // Add an internal viewport
1295 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001296 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1297 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298 // Add an external viewport
1299 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001300 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1301 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001302 // Add an virtual viewport
1303 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001304 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1305 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001306 // Add another virtual viewport
1307 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001308 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1309 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001310
1311 // Check matching by type for internal
1312 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001313 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001314 ASSERT_TRUE(internalViewport);
1315 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1316
1317 // Check matching by type for external
1318 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001319 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001320 ASSERT_TRUE(externalViewport);
1321 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1322
1323 // Check matching by uniqueId for virtual viewport #1
1324 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001325 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001327 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1329 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1330
1331 // Check matching by uniqueId for virtual viewport #2
1332 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001333 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001335 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001336 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1337 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1338}
1339
1340
1341/**
1342 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1343 * that lookup works by checking display id.
1344 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1345 */
1346TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1347 const std::string uniqueId1 = "uniqueId1";
1348 const std::string uniqueId2 = "uniqueId2";
1349 constexpr int32_t displayId1 = 2;
1350 constexpr int32_t displayId2 = 3;
1351
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001352 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1353 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354 for (const ViewportType& type : types) {
1355 mFakePolicy->clearViewports();
1356 // Add a viewport
1357 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001358 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1359 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001360 // Add another viewport
1361 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001362 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1363 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001366 std::optional<DisplayViewport> viewport1 =
1367 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_TRUE(viewport1);
1369 ASSERT_EQ(displayId1, viewport1->displayId);
1370 ASSERT_EQ(type, viewport1->type);
1371
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001372 std::optional<DisplayViewport> viewport2 =
1373 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374 ASSERT_TRUE(viewport2);
1375 ASSERT_EQ(displayId2, viewport2->displayId);
1376 ASSERT_EQ(type, viewport2->type);
1377
1378 // When there are multiple viewports of the same kind, and uniqueId is not specified
1379 // in the call to getDisplayViewport, then that situation is not supported.
1380 // The viewports can be stored in any order, so we cannot rely on the order, since that
1381 // is just implementation detail.
1382 // However, we can check that it still returns *a* viewport, we just cannot assert
1383 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001384 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385 ASSERT_TRUE(someViewport);
1386 }
1387}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001388
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001389/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001390 * When we have multiple internal displays make sure we always return the default display when
1391 * querying by type.
1392 */
1393TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1394 const std::string uniqueId1 = "uniqueId1";
1395 const std::string uniqueId2 = "uniqueId2";
1396 constexpr int32_t nonDefaultDisplayId = 2;
1397 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1398 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1399
1400 // Add the default display first and ensure it gets returned.
1401 mFakePolicy->clearViewports();
1402 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001403 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001404 ViewportType::INTERNAL);
1405 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001406 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001407 ViewportType::INTERNAL);
1408
1409 std::optional<DisplayViewport> viewport =
1410 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1411 ASSERT_TRUE(viewport);
1412 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1413 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1414
1415 // Add the default display second to make sure order doesn't matter.
1416 mFakePolicy->clearViewports();
1417 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001418 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001419 ViewportType::INTERNAL);
1420 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001421 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001422 ViewportType::INTERNAL);
1423
1424 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1425 ASSERT_TRUE(viewport);
1426 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1427 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1428}
1429
1430/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001431 * Check getDisplayViewportByPort
1432 */
1433TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001435 const std::string uniqueId1 = "uniqueId1";
1436 const std::string uniqueId2 = "uniqueId2";
1437 constexpr int32_t displayId1 = 1;
1438 constexpr int32_t displayId2 = 2;
1439 const uint8_t hdmi1 = 0;
1440 const uint8_t hdmi2 = 1;
1441 const uint8_t hdmi3 = 2;
1442
1443 mFakePolicy->clearViewports();
1444 // Add a viewport that's associated with some display port that's not of interest.
1445 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001446 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1447 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001448 // Add another viewport, connected to HDMI1 port
1449 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001450 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1451 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001452
1453 // Check that correct display viewport was returned by comparing the display ports.
1454 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1455 ASSERT_TRUE(hdmi1Viewport);
1456 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1457 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1458
1459 // Check that we can still get the same viewport using the uniqueId
1460 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1461 ASSERT_TRUE(hdmi1Viewport);
1462 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1463 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1464 ASSERT_EQ(type, hdmi1Viewport->type);
1465
1466 // Check that we cannot find a port with "HDMI2", because we never added one
1467 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1468 ASSERT_FALSE(hdmi2Viewport);
1469}
1470
Michael Wrightd02c5b62014-02-10 15:10:22 -08001471// --- InputReaderTest ---
1472
1473class InputReaderTest : public testing::Test {
1474protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001475 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001477 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001478 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479
Chris Yea52ade12020-08-27 16:49:20 -07001480 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001481 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001482 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001483 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001484
Prabir Pradhan28efc192019-11-05 01:10:04 +00001485 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1486 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001487 }
1488
Chris Yea52ade12020-08-27 16:49:20 -07001489 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001490 mFakeListener.clear();
1491 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001492 }
1493
Chris Ye1b0c7342020-07-28 21:57:03 -07001494 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001495 const PropertyMap* configuration) {
1496 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497
1498 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001499 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001500 }
1501 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001502 mReader->loopOnce();
1503 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001504 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1505 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001506 }
1507
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001508 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001509 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001510 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001511 }
1512
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001513 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001514 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001515 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001516 }
1517
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001518 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001519 const std::string& name,
1520 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001521 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001522 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1523 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001524 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001525 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526 return mapper;
1527 }
1528};
1529
Chris Ye98d3f532020-10-01 21:48:59 -07001530TEST_F(InputReaderTest, PolicyGetInputDevices) {
1531 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1532 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1533 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001534
1535 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001536 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001538 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001539 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001540 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1541 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001542 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001543}
1544
Chris Yee7310032020-09-22 15:36:28 -07001545TEST_F(InputReaderTest, GetMergedInputDevices) {
1546 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1547 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1548 // Add two subdevices to device
1549 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1550 // Must add at least one mapper or the device will be ignored!
1551 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1552 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1553
1554 // Push same device instance for next device to be added, so they'll have same identifier.
1555 mReader->pushNextDevice(device);
1556 mReader->pushNextDevice(device);
1557 ASSERT_NO_FATAL_FAILURE(
1558 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1559 ASSERT_NO_FATAL_FAILURE(
1560 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1561
1562 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001563 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001564}
1565
Chris Yee14523a2020-12-19 13:46:00 -08001566TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1567 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1568 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1569 // Add two subdevices to device
1570 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1571 // Must add at least one mapper or the device will be ignored!
1572 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1573 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1574
1575 // Push same device instance for next device to be added, so they'll have same identifier.
1576 mReader->pushNextDevice(device);
1577 mReader->pushNextDevice(device);
1578 // Sensor device is initially disabled
1579 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1580 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1581 nullptr));
1582 // Device is disabled because the only sub device is a sensor device and disabled initially.
1583 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1584 ASSERT_FALSE(device->isEnabled());
1585 ASSERT_NO_FATAL_FAILURE(
1586 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1587 // The merged device is enabled if any sub device is enabled
1588 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1589 ASSERT_TRUE(device->isEnabled());
1590}
1591
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001592TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001593 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001594 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001595 constexpr int32_t eventHubId = 1;
1596 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001597 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001598 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001599 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001600 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001601
Yi Kong9b14ac62018-07-17 13:48:38 -07001602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603
1604 NotifyDeviceResetArgs resetArgs;
1605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001606 ASSERT_EQ(deviceId, resetArgs.deviceId);
1607
1608 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001609 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001610 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001611
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001613 ASSERT_EQ(deviceId, resetArgs.deviceId);
1614 ASSERT_EQ(device->isEnabled(), false);
1615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001616 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001617 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001620 ASSERT_EQ(device->isEnabled(), false);
1621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001622 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001623 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625 ASSERT_EQ(deviceId, resetArgs.deviceId);
1626 ASSERT_EQ(device->isEnabled(), true);
1627}
1628
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001631 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001633 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001635 AINPUT_SOURCE_KEYBOARD, nullptr);
1636 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001637
1638 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1639 AINPUT_SOURCE_ANY, AKEYCODE_A))
1640 << "Should return unknown when the device id is >= 0 but unknown.";
1641
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1643 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1644 << "Should return unknown when the device id is valid but the sources are not "
1645 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001647 ASSERT_EQ(AKEY_STATE_DOWN,
1648 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1649 AKEYCODE_A))
1650 << "Should return value provided by mapper when device id is valid and the device "
1651 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652
1653 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1654 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1655 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1656
1657 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1658 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1659 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1660}
1661
1662TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001664 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001665 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001666 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001667 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001668 AINPUT_SOURCE_KEYBOARD, nullptr);
1669 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670
1671 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1672 AINPUT_SOURCE_ANY, KEY_A))
1673 << "Should return unknown when the device id is >= 0 but unknown.";
1674
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001675 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1676 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1677 << "Should return unknown when the device id is valid but the sources are not "
1678 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001680 ASSERT_EQ(AKEY_STATE_DOWN,
1681 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1682 KEY_A))
1683 << "Should return value provided by mapper when device id is valid and the device "
1684 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001685
1686 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1687 AINPUT_SOURCE_TRACKBALL, KEY_A))
1688 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1689
1690 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1691 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1692 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1693}
1694
1695TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001697 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001698 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001699 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001700 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001701 AINPUT_SOURCE_KEYBOARD, nullptr);
1702 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001703
1704 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1705 AINPUT_SOURCE_ANY, SW_LID))
1706 << "Should return unknown when the device id is >= 0 but unknown.";
1707
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1709 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1710 << "Should return unknown when the device id is valid but the sources are not "
1711 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 ASSERT_EQ(AKEY_STATE_DOWN,
1714 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1715 SW_LID))
1716 << "Should return value provided by mapper when device id is valid and the device "
1717 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718
1719 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1720 AINPUT_SOURCE_TRACKBALL, SW_LID))
1721 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1722
1723 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1724 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1725 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1726}
1727
1728TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001729 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001730 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001732 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001733 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001734 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001735
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001736 mapper.addSupportedKeyCode(AKEYCODE_A);
1737 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738
1739 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1740 uint8_t flags[4] = { 0, 0, 0, 1 };
1741
1742 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1743 << "Should return false when device id is >= 0 but unknown.";
1744 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1745
1746 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001747 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1748 << "Should return false when device id is valid but the sources are not supported by "
1749 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001750 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1751
1752 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001753 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1754 keyCodes, flags))
1755 << "Should return value provided by mapper when device id is valid and the device "
1756 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001757 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1758
1759 flags[3] = 1;
1760 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1761 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1762 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1763
1764 flags[3] = 1;
1765 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1766 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1767 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1768}
1769
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001770TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001771 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001772 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001773
1774 NotifyConfigurationChangedArgs args;
1775
1776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1777 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1778}
1779
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001780TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001782 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001783 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001785 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001786 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001787 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001788 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001789
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001790 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001791 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001792 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1793
1794 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001795 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001796 ASSERT_EQ(when, event.when);
1797 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799 ASSERT_EQ(EV_KEY, event.type);
1800 ASSERT_EQ(KEY_A, event.code);
1801 ASSERT_EQ(1, event.value);
1802}
1803
Garfield Tan1c7bc862020-01-28 13:24:04 -08001804TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001805 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001806 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001807 constexpr int32_t eventHubId = 1;
1808 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001809 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001811 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001813
1814 NotifyDeviceResetArgs resetArgs;
1815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001816 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001818 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001819 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001821 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001822 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001824 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001825 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001827 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001828 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001830 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001831 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001833 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001834 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001835}
1836
Garfield Tan1c7bc862020-01-28 13:24:04 -08001837TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1838 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001839 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001840 constexpr int32_t eventHubId = 1;
1841 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1842 // Must add at least one mapper or the device will be ignored!
1843 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001844 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001845 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1846
1847 NotifyDeviceResetArgs resetArgs;
1848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1849 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1850}
1851
Arthur Hungc23540e2018-11-29 20:42:11 +08001852TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001853 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001854 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001855 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001856 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001857 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1858 FakeInputMapper& mapper =
1859 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001860 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001861
1862 const uint8_t hdmi1 = 1;
1863
1864 // Associated touch screen with second display.
1865 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1866
1867 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001868 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001869 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001870 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001871 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001872 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001873 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001874 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001875 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001876 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001877
1878 // Add the device, and make sure all of the callbacks are triggered.
1879 // The device is added after the input port associations are processed since
1880 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001881 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001884 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001885
Arthur Hung2c9a3342019-07-23 14:18:59 +08001886 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001887 ASSERT_EQ(deviceId, device->getId());
1888 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1889 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001890
1891 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001892 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001893 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001894 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001895}
1896
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001897TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1898 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1899 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1900 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1901 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1902 // Must add at least one mapper or the device will be ignored!
1903 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1904 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1905 mReader->pushNextDevice(device);
1906 mReader->pushNextDevice(device);
1907 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1908 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1909
1910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1911
1912 NotifyDeviceResetArgs resetArgs;
1913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1914 ASSERT_EQ(deviceId, resetArgs.deviceId);
1915 ASSERT_TRUE(device->isEnabled());
1916 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1917 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1918
1919 disableDevice(deviceId);
1920 mReader->loopOnce();
1921
1922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1923 ASSERT_EQ(deviceId, resetArgs.deviceId);
1924 ASSERT_FALSE(device->isEnabled());
1925 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1926 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1927
1928 enableDevice(deviceId);
1929 mReader->loopOnce();
1930
1931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1932 ASSERT_EQ(deviceId, resetArgs.deviceId);
1933 ASSERT_TRUE(device->isEnabled());
1934 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1935 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1936}
1937
1938TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1939 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1940 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1941 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1942 // Add two subdevices to device
1943 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1944 FakeInputMapper& mapperDevice1 =
1945 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1946 FakeInputMapper& mapperDevice2 =
1947 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1948 mReader->pushNextDevice(device);
1949 mReader->pushNextDevice(device);
1950 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1951 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1952
1953 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1954 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1955
1956 ASSERT_EQ(AKEY_STATE_DOWN,
1957 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1958 ASSERT_EQ(AKEY_STATE_DOWN,
1959 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1960 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1961 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1962}
1963
Prabir Pradhan7e186182020-11-10 13:56:45 -08001964TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1965 NotifyPointerCaptureChangedArgs args;
1966
Prabir Pradhanf192a102021-08-06 14:01:18 +00001967 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001968 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1969 mReader->loopOnce();
1970 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhanf192a102021-08-06 14:01:18 +00001971 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1972 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001973
1974 mFakePolicy->setPointerCapture(false);
1975 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1976 mReader->loopOnce();
1977 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhanf192a102021-08-06 14:01:18 +00001978 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001979
Prabir Pradhanf192a102021-08-06 14:01:18 +00001980 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001981 // does not change.
1982 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1983 mReader->loopOnce();
Prabir Pradhanf192a102021-08-06 14:01:18 +00001984 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001985}
1986
Chris Ye87143712020-11-10 05:05:58 +00001987class FakeVibratorInputMapper : public FakeInputMapper {
1988public:
1989 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1990 : FakeInputMapper(deviceContext, sources) {}
1991
1992 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1993};
1994
1995TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1996 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1997 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1998 constexpr int32_t eventHubId = 1;
1999 const char* DEVICE_LOCATION = "BLUETOOTH";
2000 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2001 FakeVibratorInputMapper& mapper =
2002 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2003 mReader->pushNextDevice(device);
2004
2005 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2006 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2007
2008 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2009 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2010}
2011
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002012// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002013
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002014class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002015public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002016 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002017
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002018 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002019
2020 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2021
2022 void dump(std::string& dump) override {}
2023
2024 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2025 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002026 }
2027
Chris Yee2b1e5c2021-03-10 22:45:12 -08002028 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2029 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002030 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002031
2032 bool setLightColor(int32_t lightId, int32_t color) override {
2033 getDeviceContext().setLightBrightness(lightId, color >> 24);
2034 return true;
2035 }
2036
2037 std::optional<int32_t> getLightColor(int32_t lightId) override {
2038 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2039 if (!result.has_value()) {
2040 return std::nullopt;
2041 }
2042 return result.value() << 24;
2043 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002044
2045 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2046
2047 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2048
2049private:
2050 InputDeviceContext& mDeviceContext;
2051 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2052 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002053};
2054
Chris Yee2b1e5c2021-03-10 22:45:12 -08002055TEST_F(InputReaderTest, BatteryGetCapacity) {
2056 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2057 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2058 constexpr int32_t eventHubId = 1;
2059 const char* DEVICE_LOCATION = "BLUETOOTH";
2060 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002061 FakePeripheralController& controller =
2062 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002063 mReader->pushNextDevice(device);
2064
2065 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2066
2067 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2068 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2069}
2070
2071TEST_F(InputReaderTest, BatteryGetStatus) {
2072 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2073 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2074 constexpr int32_t eventHubId = 1;
2075 const char* DEVICE_LOCATION = "BLUETOOTH";
2076 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002077 FakePeripheralController& controller =
2078 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002079 mReader->pushNextDevice(device);
2080
2081 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2082
2083 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2084 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2085}
2086
Chris Ye3fdbfef2021-01-06 18:45:18 -08002087TEST_F(InputReaderTest, LightGetColor) {
2088 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2089 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2090 constexpr int32_t eventHubId = 1;
2091 const char* DEVICE_LOCATION = "BLUETOOTH";
2092 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002093 FakePeripheralController& controller =
2094 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002095 mReader->pushNextDevice(device);
2096 RawLightInfo info = {.id = 1,
2097 .name = "Mono",
2098 .maxBrightness = 255,
2099 .flags = InputLightClass::BRIGHTNESS,
2100 .path = ""};
2101 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2102 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2103
2104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002105
Chris Yee2b1e5c2021-03-10 22:45:12 -08002106 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2107 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002108 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2109 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2110}
2111
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002112// --- InputReaderIntegrationTest ---
2113
2114// These tests create and interact with the InputReader only through its interface.
2115// The InputReader is started during SetUp(), which starts its processing in its own
2116// thread. The tests use linux uinput to emulate input devices.
2117// NOTE: Interacting with the physical device while these tests are running may cause
2118// the tests to fail.
2119class InputReaderIntegrationTest : public testing::Test {
2120protected:
2121 sp<TestInputListener> mTestListener;
2122 sp<FakeInputReaderPolicy> mFakePolicy;
2123 sp<InputReaderInterface> mReader;
2124
Chris Yea52ade12020-08-27 16:49:20 -07002125 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002126 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002127 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2128 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002129
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002130 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002131 ASSERT_EQ(mReader->start(), OK);
2132
2133 // Since this test is run on a real device, all the input devices connected
2134 // to the test device will show up in mReader. We wait for those input devices to
2135 // show up before beginning the tests.
2136 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2137 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2138 }
2139
Chris Yea52ade12020-08-27 16:49:20 -07002140 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002141 ASSERT_EQ(mReader->stop(), OK);
2142 mTestListener.clear();
2143 mFakePolicy.clear();
2144 }
2145};
2146
2147TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2148 // An invalid input device that is only used for this test.
2149 class InvalidUinputDevice : public UinputDevice {
2150 public:
2151 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2152
2153 private:
2154 void configureDevice(int fd, uinput_user_dev* device) override {}
2155 };
2156
2157 const size_t numDevices = mFakePolicy->getInputDevices().size();
2158
2159 // UinputDevice does not set any event or key bits, so InputReader should not
2160 // consider it as a valid device.
2161 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2162 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2164 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2165
2166 invalidDevice.reset();
2167 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2168 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2169 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2170}
2171
2172TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2173 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2174
2175 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2176 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2177 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2178 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2179
2180 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002181 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002182 const auto& it =
2183 std::find_if(inputDevices.begin(), inputDevices.end(),
2184 [&keyboard](const InputDeviceInfo& info) {
2185 return info.getIdentifier().name == keyboard->getName();
2186 });
2187
2188 ASSERT_NE(it, inputDevices.end());
2189 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2190 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2191 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002192
2193 keyboard.reset();
2194 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2195 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2196 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2197}
2198
2199TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2200 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2201 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2202
2203 NotifyConfigurationChangedArgs configChangedArgs;
2204 ASSERT_NO_FATAL_FAILURE(
2205 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002206 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002207 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2208
2209 NotifyKeyArgs keyArgs;
2210 keyboard->pressAndReleaseHomeKey();
2211 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2212 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002213 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002214 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002215 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002216 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002217 prevTimestamp = keyArgs.eventTime;
2218
2219 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2220 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002221 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002222 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002223 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002224}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002225
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002226/**
2227 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2228 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2229 * are passed to the listener.
2230 */
2231static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2232TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2233 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2234 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2235 NotifyKeyArgs keyArgs;
2236
2237 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2238 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2239 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2240 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2241
2242 controller->pressAndReleaseKey(BTN_GEAR_UP);
2243 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2244 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2245 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2246}
2247
Arthur Hungaab25622020-01-16 11:22:11 +08002248// --- TouchProcessTest ---
2249class TouchIntegrationTest : public InputReaderIntegrationTest {
2250protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002251 const std::string UNIQUE_ID = "local:0";
2252
Chris Yea52ade12020-08-27 16:49:20 -07002253 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002254 InputReaderIntegrationTest::SetUp();
2255 // At least add an internal display.
2256 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2257 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002258 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002259
2260 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2261 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2262 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2263 }
2264
2265 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2266 int32_t orientation, const std::string& uniqueId,
2267 std::optional<uint8_t> physicalPort,
2268 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002269 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2270 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002271 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2272 }
2273
2274 std::unique_ptr<UinputTouchScreen> mDevice;
2275};
2276
2277TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2278 NotifyMotionArgs args;
2279 const Point centerPoint = mDevice->getCenterPoint();
2280
2281 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002282 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002283 mDevice->sendDown(centerPoint);
2284 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2286
2287 // ACTION_MOVE
2288 mDevice->sendMove(centerPoint + Point(1, 1));
2289 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2291
2292 // ACTION_UP
2293 mDevice->sendUp();
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2296}
2297
2298TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2299 NotifyMotionArgs args;
2300 const Point centerPoint = mDevice->getCenterPoint();
2301
2302 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002303 mDevice->sendSlot(FIRST_SLOT);
2304 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002305 mDevice->sendDown(centerPoint);
2306 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2308
2309 // ACTION_POINTER_DOWN (Second slot)
2310 const Point secondPoint = centerPoint + Point(100, 100);
2311 mDevice->sendSlot(SECOND_SLOT);
2312 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2313 mDevice->sendDown(secondPoint + Point(1, 1));
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2315 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2316 args.action);
2317
2318 // ACTION_MOVE (Second slot)
2319 mDevice->sendMove(secondPoint);
2320 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2322
2323 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002324 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002325 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002326 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002327 args.action);
2328
2329 // ACTION_UP
2330 mDevice->sendSlot(FIRST_SLOT);
2331 mDevice->sendUp();
2332 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2333 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2334}
2335
2336TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2337 NotifyMotionArgs args;
2338 const Point centerPoint = mDevice->getCenterPoint();
2339
2340 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002341 mDevice->sendSlot(FIRST_SLOT);
2342 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002343 mDevice->sendDown(centerPoint);
2344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2345 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2346
arthurhungcc7f9802020-04-30 17:55:40 +08002347 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002348 const Point secondPoint = centerPoint + Point(100, 100);
2349 mDevice->sendSlot(SECOND_SLOT);
2350 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2351 mDevice->sendDown(secondPoint);
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2353 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2354 args.action);
2355
arthurhungcc7f9802020-04-30 17:55:40 +08002356 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002357 mDevice->sendMove(secondPoint + Point(1, 1));
2358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2360
arthurhungcc7f9802020-04-30 17:55:40 +08002361 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2362 // a palm event.
2363 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002364 mDevice->sendToolType(MT_TOOL_PALM);
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002366 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2367 args.action);
2368 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002369
arthurhungcc7f9802020-04-30 17:55:40 +08002370 // Send up to second slot, expect first slot send moving.
2371 mDevice->sendPointerUp();
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002374
arthurhungcc7f9802020-04-30 17:55:40 +08002375 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002376 mDevice->sendSlot(FIRST_SLOT);
2377 mDevice->sendUp();
2378
arthurhungcc7f9802020-04-30 17:55:40 +08002379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2380 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002381}
2382
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384class InputDeviceTest : public testing::Test {
2385protected:
2386 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002387 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002388 static const int32_t DEVICE_ID;
2389 static const int32_t DEVICE_GENERATION;
2390 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002391 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002392 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002394 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002396 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002397 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002398 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399
Chris Yea52ade12020-08-27 16:49:20 -07002400 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002401 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002403 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002404 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2405 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406 InputDeviceIdentifier identifier;
2407 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002408 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002409 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002410 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002411 mReader->pushNextDevice(mDevice);
2412 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2413 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414 }
2415
Chris Yea52ade12020-08-27 16:49:20 -07002416 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417 mFakeListener.clear();
2418 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 }
2420};
2421
2422const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002423const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002424const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2426const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002427const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2428 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002429const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430
2431TEST_F(InputDeviceTest, ImmutableProperties) {
2432 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002433 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002434 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435}
2436
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002437TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2438 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002439}
2440
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2442 // Configuration.
2443 InputReaderConfiguration config;
2444 mDevice->configure(ARBITRARY_TIME, &config, 0);
2445
2446 // Reset.
2447 mDevice->reset(ARBITRARY_TIME);
2448
2449 NotifyDeviceResetArgs resetArgs;
2450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2451 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2452 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2453
2454 // Metadata.
2455 ASSERT_TRUE(mDevice->isIgnored());
2456 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2457
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002458 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002460 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2462 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2463
2464 // State queries.
2465 ASSERT_EQ(0, mDevice->getMetaState());
2466
2467 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2468 << "Ignored device should return unknown key code state.";
2469 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2470 << "Ignored device should return unknown scan code state.";
2471 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2472 << "Ignored device should return unknown switch state.";
2473
2474 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2475 uint8_t flags[2] = { 0, 1 };
2476 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2477 << "Ignored device should never mark any key codes.";
2478 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2479 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2480}
2481
2482TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2483 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002484 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002486 FakeInputMapper& mapper1 =
2487 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002488 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2489 mapper1.setMetaState(AMETA_ALT_ON);
2490 mapper1.addSupportedKeyCode(AKEYCODE_A);
2491 mapper1.addSupportedKeyCode(AKEYCODE_B);
2492 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2493 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2494 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2495 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2496 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002498 FakeInputMapper& mapper2 =
2499 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002500 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002501
2502 InputReaderConfiguration config;
2503 mDevice->configure(ARBITRARY_TIME, &config, 0);
2504
2505 String8 propertyValue;
2506 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2507 << "Device should have read configuration during configuration phase.";
2508 ASSERT_STREQ("value", propertyValue.string());
2509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002510 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2511 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002512
2513 // Reset
2514 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002515 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2516 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517
2518 NotifyDeviceResetArgs resetArgs;
2519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2520 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2521 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2522
2523 // Metadata.
2524 ASSERT_FALSE(mDevice->isIgnored());
2525 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2526
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002527 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002529 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2531 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2532
2533 // State queries.
2534 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2535 << "Should query mappers and combine meta states.";
2536
2537 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2538 << "Should return unknown key code state when source not supported.";
2539 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2540 << "Should return unknown scan code state when source not supported.";
2541 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2542 << "Should return unknown switch state when source not supported.";
2543
2544 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2545 << "Should query mapper when source is supported.";
2546 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2547 << "Should query mapper when source is supported.";
2548 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2549 << "Should query mapper when source is supported.";
2550
2551 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2552 uint8_t flags[4] = { 0, 0, 0, 1 };
2553 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2554 << "Should do nothing when source is unsupported.";
2555 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2556 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2557 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2558 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2559
2560 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2561 << "Should query mapper when source is supported.";
2562 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2563 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2564 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2565 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2566
2567 // Event handling.
2568 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002569 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570 mDevice->process(&event, 1);
2571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002572 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2573 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574}
2575
Arthur Hung2c9a3342019-07-23 14:18:59 +08002576// A single input device is associated with a specific display. Check that:
2577// 1. Device is disabled if the viewport corresponding to the associated display is not found
2578// 2. Device is disabled when setEnabled API is called
2579TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002580 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002581
2582 // First Configuration.
2583 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2584
2585 // Device should be enabled by default.
2586 ASSERT_TRUE(mDevice->isEnabled());
2587
2588 // Prepare associated info.
2589 constexpr uint8_t hdmi = 1;
2590 const std::string UNIQUE_ID = "local:1";
2591
2592 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2593 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2594 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2595 // Device should be disabled because it is associated with a specific display via
2596 // input port <-> display port association, but the corresponding display is not found
2597 ASSERT_FALSE(mDevice->isEnabled());
2598
2599 // Prepare displays.
2600 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002601 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2602 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002603 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2604 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2605 ASSERT_TRUE(mDevice->isEnabled());
2606
2607 // Device should be disabled after set disable.
2608 mFakePolicy->addDisabledDevice(mDevice->getId());
2609 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2610 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2611 ASSERT_FALSE(mDevice->isEnabled());
2612
2613 // Device should still be disabled even found the associated display.
2614 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2615 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2616 ASSERT_FALSE(mDevice->isEnabled());
2617}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618
Christine Franks1ba71cc2021-04-07 14:37:42 -07002619TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2620 // Device should be enabled by default.
2621 mFakePolicy->clearViewports();
2622 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2623 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2624 ASSERT_TRUE(mDevice->isEnabled());
2625
2626 // Device should be disabled because it is associated with a specific display, but the
2627 // corresponding display is not found.
2628 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2629 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2630 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2631 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2632 ASSERT_FALSE(mDevice->isEnabled());
2633
2634 // Device should be enabled when a display is found.
2635 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2636 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2637 NO_PORT, ViewportType::INTERNAL);
2638 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2639 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2640 ASSERT_TRUE(mDevice->isEnabled());
2641
2642 // Device should be disabled after set disable.
2643 mFakePolicy->addDisabledDevice(mDevice->getId());
2644 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2645 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2646 ASSERT_FALSE(mDevice->isEnabled());
2647
2648 // Device should still be disabled even found the associated display.
2649 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2650 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2651 ASSERT_FALSE(mDevice->isEnabled());
2652}
2653
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654// --- InputMapperTest ---
2655
2656class InputMapperTest : public testing::Test {
2657protected:
2658 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002659 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 static const int32_t DEVICE_ID;
2661 static const int32_t DEVICE_GENERATION;
2662 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002663 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002664 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002666 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002668 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002669 std::unique_ptr<InstrumentedInputReader> mReader;
2670 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671
Chris Ye1b0c7342020-07-28 21:57:03 -07002672 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002673 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002675 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002676 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2677 mFakeListener);
2678 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679 }
2680
Chris Yea52ade12020-08-27 16:49:20 -07002681 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002682
Chris Yea52ade12020-08-27 16:49:20 -07002683 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 mFakeListener.clear();
2685 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686 }
2687
2688 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002689 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 }
2691
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002692 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002693 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002694 mReader->requestRefreshConfiguration(changes);
2695 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002696 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002697 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2698 }
2699
arthurhungdcef2dc2020-08-11 14:47:50 +08002700 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2701 const std::string& location, int32_t eventHubId,
2702 Flags<InputDeviceClass> classes) {
2703 InputDeviceIdentifier identifier;
2704 identifier.name = name;
2705 identifier.location = location;
2706 std::shared_ptr<InputDevice> device =
2707 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2708 identifier);
2709 mReader->pushNextDevice(device);
2710 mFakeEventHub->addDevice(eventHubId, name, classes);
2711 mReader->loopOnce();
2712 return device;
2713 }
2714
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002715 template <class T, typename... Args>
2716 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002717 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002718 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002720 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002721 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002722 }
2723
2724 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002725 int32_t orientation, const std::string& uniqueId,
2726 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002727 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2728 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002729 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2730 }
2731
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002732 void clearViewports() {
2733 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734 }
2735
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002736 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2737 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738 RawEvent event;
2739 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002740 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002741 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742 event.type = type;
2743 event.code = code;
2744 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002745 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002746 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747 }
2748
2749 static void assertMotionRange(const InputDeviceInfo& info,
2750 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2751 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002752 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2754 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2755 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2756 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2757 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2758 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2759 }
2760
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002761 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2762 float size, float touchMajor, float touchMinor, float toolMajor,
2763 float toolMinor, float orientation, float distance,
2764 float scaledAxisEpsilon = 1.f) {
2765 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2766 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2768 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002769 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2770 scaledAxisEpsilon);
2771 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2772 scaledAxisEpsilon);
2773 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2774 scaledAxisEpsilon);
2775 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2776 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2778 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2779 }
2780
Michael Wright17db18e2020-06-26 20:51:44 +01002781 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002783 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_NEAR(x, actualX, 1);
2785 ASSERT_NEAR(y, actualY, 1);
2786 }
2787};
2788
2789const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002790const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002791const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2793const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002794const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2795 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002796const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797
2798// --- SwitchInputMapperTest ---
2799
2800class SwitchInputMapperTest : public InputMapperTest {
2801protected:
2802};
2803
2804TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002807 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808}
2809
2810TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002811 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002813 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002814 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002817 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818}
2819
2820TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002821 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827
2828 NotifySwitchArgs args;
2829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2830 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002831 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2832 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833 args.switchMask);
2834 ASSERT_EQ(uint32_t(0), args.policyFlags);
2835}
2836
Chris Ye87143712020-11-10 05:05:58 +00002837// --- VibratorInputMapperTest ---
2838class VibratorInputMapperTest : public InputMapperTest {
2839protected:
2840 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2841};
2842
2843TEST_F(VibratorInputMapperTest, GetSources) {
2844 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2845
2846 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2847}
2848
2849TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2850 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2851
2852 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2853}
2854
2855TEST_F(VibratorInputMapperTest, Vibrate) {
2856 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002857 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002858 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2859
2860 VibrationElement pattern(2);
2861 VibrationSequence sequence(2);
2862 pattern.duration = std::chrono::milliseconds(200);
2863 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2864 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2865 sequence.addElement(pattern);
2866 pattern.duration = std::chrono::milliseconds(500);
2867 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2868 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2869 sequence.addElement(pattern);
2870
2871 std::vector<int64_t> timings = {0, 1};
2872 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2873
2874 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002875 // Start vibrating
2876 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002877 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002878 // Verify vibrator state listener was notified.
2879 mReader->loopOnce();
2880 NotifyVibratorStateArgs args;
2881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2882 ASSERT_EQ(DEVICE_ID, args.deviceId);
2883 ASSERT_TRUE(args.isOn);
2884 // Stop vibrating
2885 mapper.cancelVibrate(VIBRATION_TOKEN);
2886 ASSERT_FALSE(mapper.isVibrating());
2887 // Verify vibrator state listener was notified.
2888 mReader->loopOnce();
2889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2890 ASSERT_EQ(DEVICE_ID, args.deviceId);
2891 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002892}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893
Chris Yef59a2f42020-10-16 12:55:26 -07002894// --- SensorInputMapperTest ---
2895
2896class SensorInputMapperTest : public InputMapperTest {
2897protected:
2898 static const int32_t ACCEL_RAW_MIN;
2899 static const int32_t ACCEL_RAW_MAX;
2900 static const int32_t ACCEL_RAW_FUZZ;
2901 static const int32_t ACCEL_RAW_FLAT;
2902 static const int32_t ACCEL_RAW_RESOLUTION;
2903
2904 static const int32_t GYRO_RAW_MIN;
2905 static const int32_t GYRO_RAW_MAX;
2906 static const int32_t GYRO_RAW_FUZZ;
2907 static const int32_t GYRO_RAW_FLAT;
2908 static const int32_t GYRO_RAW_RESOLUTION;
2909
2910 static const float GRAVITY_MS2_UNIT;
2911 static const float DEGREE_RADIAN_UNIT;
2912
2913 void prepareAccelAxes();
2914 void prepareGyroAxes();
2915 void setAccelProperties();
2916 void setGyroProperties();
2917 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2918};
2919
2920const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2921const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2922const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2923const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2924const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2925
2926const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2927const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2928const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2929const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2930const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2931
2932const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2933const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2934
2935void SensorInputMapperTest::prepareAccelAxes() {
2936 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2937 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2939 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2940 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2941 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2942}
2943
2944void SensorInputMapperTest::prepareGyroAxes() {
2945 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2946 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2947 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2948 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2949 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2950 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2951}
2952
2953void SensorInputMapperTest::setAccelProperties() {
2954 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2955 /* sensorDataIndex */ 0);
2956 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2957 /* sensorDataIndex */ 1);
2958 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2959 /* sensorDataIndex */ 2);
2960 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2961 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2962 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2963 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2964 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2965}
2966
2967void SensorInputMapperTest::setGyroProperties() {
2968 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2969 /* sensorDataIndex */ 0);
2970 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2971 /* sensorDataIndex */ 1);
2972 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2973 /* sensorDataIndex */ 2);
2974 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2975 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2976 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2977 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2978 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2979}
2980
2981TEST_F(SensorInputMapperTest, GetSources) {
2982 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2983
2984 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2985}
2986
2987TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2988 setAccelProperties();
2989 prepareAccelAxes();
2990 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2991
2992 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2993 std::chrono::microseconds(10000),
2994 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002995 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003001
3002 NotifySensorArgs args;
3003 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3004 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3005 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3006
3007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3008 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3009 ASSERT_EQ(args.deviceId, DEVICE_ID);
3010 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3011 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3012 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3013 ASSERT_EQ(args.values, values);
3014 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3015}
3016
3017TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3018 setGyroProperties();
3019 prepareGyroAxes();
3020 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3021
3022 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3023 std::chrono::microseconds(10000),
3024 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003025 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003031
3032 NotifySensorArgs args;
3033 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3034 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3035 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3036
3037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3038 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3039 ASSERT_EQ(args.deviceId, DEVICE_ID);
3040 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3041 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3042 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3043 ASSERT_EQ(args.values, values);
3044 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3045}
3046
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047// --- KeyboardInputMapperTest ---
3048
3049class KeyboardInputMapperTest : public InputMapperTest {
3050protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003051 const std::string UNIQUE_ID = "local:0";
3052
3053 void prepareDisplay(int32_t orientation);
3054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003055 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003056 int32_t originalKeyCode, int32_t rotatedKeyCode,
3057 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058};
3059
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003060/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3061 * orientation.
3062 */
3063void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003064 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3065 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003066}
3067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003068void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003069 int32_t originalScanCode, int32_t originalKeyCode,
3070 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071 NotifyKeyArgs args;
3072
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3075 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3076 ASSERT_EQ(originalScanCode, args.scanCode);
3077 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003078 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3082 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3083 ASSERT_EQ(originalScanCode, args.scanCode);
3084 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003085 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086}
3087
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 KeyboardInputMapper& mapper =
3090 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003093 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094}
3095
3096TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3097 const int32_t USAGE_A = 0x070004;
3098 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003099 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3100 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003101 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3102 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3103 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003105 KeyboardInputMapper& mapper =
3106 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3107 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003108 // Initial metastate to AMETA_NONE.
3109 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3110 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111
3112 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 NotifyKeyArgs args;
3115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3116 ASSERT_EQ(DEVICE_ID, args.deviceId);
3117 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3118 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3120 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3121 ASSERT_EQ(KEY_HOME, args.scanCode);
3122 ASSERT_EQ(AMETA_NONE, args.metaState);
3123 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3124 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3125 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3126
3127 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003128 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3130 ASSERT_EQ(DEVICE_ID, args.deviceId);
3131 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3132 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3133 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3134 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3135 ASSERT_EQ(KEY_HOME, args.scanCode);
3136 ASSERT_EQ(AMETA_NONE, args.metaState);
3137 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3138 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3139 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3140
3141 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3145 ASSERT_EQ(DEVICE_ID, args.deviceId);
3146 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3147 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3148 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3149 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3150 ASSERT_EQ(0, args.scanCode);
3151 ASSERT_EQ(AMETA_NONE, args.metaState);
3152 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3153 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3154 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3155
3156 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3158 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3160 ASSERT_EQ(DEVICE_ID, args.deviceId);
3161 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3162 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3163 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3164 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3165 ASSERT_EQ(0, args.scanCode);
3166 ASSERT_EQ(AMETA_NONE, args.metaState);
3167 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3168 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3169 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3170
3171 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3175 ASSERT_EQ(DEVICE_ID, args.deviceId);
3176 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3177 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3178 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3179 ASSERT_EQ(0, args.keyCode);
3180 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3181 ASSERT_EQ(AMETA_NONE, args.metaState);
3182 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3183 ASSERT_EQ(0U, args.policyFlags);
3184 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3185
3186 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3188 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3190 ASSERT_EQ(DEVICE_ID, args.deviceId);
3191 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3192 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3193 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3194 ASSERT_EQ(0, args.keyCode);
3195 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3196 ASSERT_EQ(AMETA_NONE, args.metaState);
3197 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3198 ASSERT_EQ(0U, args.policyFlags);
3199 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3200}
3201
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003202/**
3203 * Ensure that the readTime is set to the time when the EV_KEY is received.
3204 */
3205TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3206 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3207
3208 KeyboardInputMapper& mapper =
3209 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3210 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3211 NotifyKeyArgs args;
3212
3213 // Key down
3214 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3216 ASSERT_EQ(12, args.readTime);
3217
3218 // Key up
3219 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3221 ASSERT_EQ(15, args.readTime);
3222}
3223
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003225 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3226 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003227 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3228 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3229 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003231 KeyboardInputMapper& mapper =
3232 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3233 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234
arthurhungc903df12020-08-11 15:08:42 +08003235 // Initial metastate to AMETA_NONE.
3236 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3237 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238
3239 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241 NotifyKeyArgs args;
3242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003244 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003245 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246
3247 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003248 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
3253 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003254 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
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 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003260 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3262 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003263 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003264 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265}
3266
3267TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3269 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3270 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3271 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 KeyboardInputMapper& mapper =
3274 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3275 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003277 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3279 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3280 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3281 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3282 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3283 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3284 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3285 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3286}
3287
3288TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3291 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3292 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003295 KeyboardInputMapper& mapper =
3296 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3297 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003299 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003300 ASSERT_NO_FATAL_FAILURE(
3301 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3302 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3303 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3305 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3306 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3307 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003309 clearViewports();
3310 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003311 ASSERT_NO_FATAL_FAILURE(
3312 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3313 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3314 AKEYCODE_DPAD_UP, DISPLAY_ID));
3315 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3316 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3317 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3318 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003320 clearViewports();
3321 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003322 ASSERT_NO_FATAL_FAILURE(
3323 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3324 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3325 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3326 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3327 AKEYCODE_DPAD_UP, DISPLAY_ID));
3328 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3329 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003331 clearViewports();
3332 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003333 ASSERT_NO_FATAL_FAILURE(
3334 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3335 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3336 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3337 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3338 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3340 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341
3342 // Special case: if orientation changes while key is down, we still emit the same keycode
3343 // in the key up as we did in the key down.
3344 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003345 clearViewports();
3346 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3350 ASSERT_EQ(KEY_UP, args.scanCode);
3351 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3352
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003353 clearViewports();
3354 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3357 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3358 ASSERT_EQ(KEY_UP, args.scanCode);
3359 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3360}
3361
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003362TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3363 // If the keyboard is not orientation aware,
3364 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003365 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 KeyboardInputMapper& mapper =
3368 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3369 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003370 NotifyKeyArgs args;
3371
3372 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3377 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3378
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003379 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3384 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3385}
3386
3387TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3388 // If the keyboard is orientation aware,
3389 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003390 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003391
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003392 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003393 KeyboardInputMapper& mapper =
3394 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3395 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003396 NotifyKeyArgs args;
3397
3398 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3399 // ^--- already checked by the previous test
3400
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003401 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003402 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3407 ASSERT_EQ(DISPLAY_ID, args.displayId);
3408
3409 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003410 clearViewports();
3411 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003412 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3417 ASSERT_EQ(newDisplayId, args.displayId);
3418}
3419
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003421 KeyboardInputMapper& mapper =
3422 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3423 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003425 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003428 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003429 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430}
3431
3432TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003433 KeyboardInputMapper& mapper =
3434 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3435 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003437 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003438 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003440 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003441 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442}
3443
3444TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003445 KeyboardInputMapper& mapper =
3446 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3447 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003449 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450
3451 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3452 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003453 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454 ASSERT_TRUE(flags[0]);
3455 ASSERT_FALSE(flags[1]);
3456}
3457
3458TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003459 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3460 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3461 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3462 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3463 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3464 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003466 KeyboardInputMapper& mapper =
3467 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3468 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003469 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003470 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3471 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
3473 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003474 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3475 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3476 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477
3478 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003481 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3482 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3483 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003484 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485
3486 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003489 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3490 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3491 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003492 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493
3494 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3498 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3499 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501
3502 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3506 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3507 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003508 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
3510 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003513 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3514 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3515 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3522 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003524 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525}
3526
Chris Yea52ade12020-08-27 16:49:20 -07003527TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3528 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3529 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3530 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3531 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3532
3533 KeyboardInputMapper& mapper =
3534 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3535 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3536
3537 // Initial metastate should be AMETA_NONE as no meta keys added.
3538 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3539 // Meta state should be AMETA_NONE after reset
3540 mapper.reset(ARBITRARY_TIME);
3541 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3542 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3543 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3544 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3545
3546 NotifyKeyArgs args;
3547 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3550 ASSERT_EQ(AMETA_NONE, args.metaState);
3551 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3552 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3553 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3554
3555 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003556 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3558 ASSERT_EQ(AMETA_NONE, args.metaState);
3559 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3560 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3561 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3562}
3563
Arthur Hung2c9a3342019-07-23 14:18:59 +08003564TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3565 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003566 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3567 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3568 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3569 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003570
3571 // keyboard 2.
3572 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003573 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003574 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003575 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003576 std::shared_ptr<InputDevice> device2 =
3577 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3578 Flags<InputDeviceClass>(0));
3579
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003580 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3581 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3582 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3583 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 KeyboardInputMapper& mapper =
3586 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3587 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003588
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003589 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003590 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003591 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003592 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3593 device2->reset(ARBITRARY_TIME);
3594
3595 // Prepared displays and associated info.
3596 constexpr uint8_t hdmi1 = 0;
3597 constexpr uint8_t hdmi2 = 1;
3598 const std::string SECONDARY_UNIQUE_ID = "local:1";
3599
3600 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3601 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3602
3603 // No associated display viewport found, should disable the device.
3604 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3605 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3606 ASSERT_FALSE(device2->isEnabled());
3607
3608 // Prepare second display.
3609 constexpr int32_t newDisplayId = 2;
3610 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003611 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003612 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003613 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003614 // Default device will reconfigure above, need additional reconfiguration for another device.
3615 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3616 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3617
3618 // Device should be enabled after the associated display is found.
3619 ASSERT_TRUE(mDevice->isEnabled());
3620 ASSERT_TRUE(device2->isEnabled());
3621
3622 // Test pad key events
3623 ASSERT_NO_FATAL_FAILURE(
3624 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3626 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3627 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3628 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3629 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3630 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3631
3632 ASSERT_NO_FATAL_FAILURE(
3633 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3635 AKEYCODE_DPAD_RIGHT, newDisplayId));
3636 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3637 AKEYCODE_DPAD_DOWN, newDisplayId));
3638 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3639 AKEYCODE_DPAD_LEFT, newDisplayId));
3640}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003641
arthurhungc903df12020-08-11 15:08:42 +08003642TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3643 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3644 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3645 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3646 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3647 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3648 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3649
3650 KeyboardInputMapper& mapper =
3651 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3652 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3653 // Initial metastate to AMETA_NONE.
3654 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3655 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3656
3657 // Initialization should have turned all of the lights off.
3658 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3659 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3660 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3661
3662 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003665 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3666 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3667
3668 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003671 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3672 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3673
3674 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003677 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3678 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3679
3680 mFakeEventHub->removeDevice(EVENTHUB_ID);
3681 mReader->loopOnce();
3682
3683 // keyboard 2 should default toggle keys.
3684 const std::string USB2 = "USB2";
3685 const std::string DEVICE_NAME2 = "KEYBOARD2";
3686 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3687 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3688 std::shared_ptr<InputDevice> device2 =
3689 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3690 Flags<InputDeviceClass>(0));
3691 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3692 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3693 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3694 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3695 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3696 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3697
arthurhung6fe95782020-10-05 22:41:16 +08003698 KeyboardInputMapper& mapper2 =
3699 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3700 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003701 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3702 device2->reset(ARBITRARY_TIME);
3703
3704 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3705 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3706 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003707 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3708 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003709}
3710
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003711// --- KeyboardInputMapperTest_ExternalDevice ---
3712
3713class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3714protected:
Chris Yea52ade12020-08-27 16:49:20 -07003715 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003716};
3717
3718TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003719 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3720 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003721
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003722 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3723 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3724 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3725 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 KeyboardInputMapper& mapper =
3728 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3729 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003730
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003732 NotifyKeyArgs args;
3733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3734 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3735
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003736 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3738 ASSERT_EQ(uint32_t(0), args.policyFlags);
3739
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3742 ASSERT_EQ(uint32_t(0), args.policyFlags);
3743
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003744 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3746 ASSERT_EQ(uint32_t(0), args.policyFlags);
3747
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3750 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3751
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003752 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3754 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3755}
3756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003758 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003759
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3762 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003763
Powei Fengd041c5d2019-05-03 17:11:33 -07003764 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003765 KeyboardInputMapper& mapper =
3766 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3767 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003768
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003770 NotifyKeyArgs args;
3771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3772 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3773
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3776 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3777
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3780 ASSERT_EQ(uint32_t(0), args.policyFlags);
3781
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003782 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3784 ASSERT_EQ(uint32_t(0), args.policyFlags);
3785
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3788 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3789
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3792 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3793}
3794
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795// --- CursorInputMapperTest ---
3796
3797class CursorInputMapperTest : public InputMapperTest {
3798protected:
3799 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3800
Michael Wright17db18e2020-06-26 20:51:44 +01003801 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802
Chris Yea52ade12020-08-27 16:49:20 -07003803 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804 InputMapperTest::SetUp();
3805
Michael Wright17db18e2020-06-26 20:51:44 +01003806 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003807 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808 }
3809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3811 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003812
3813 void prepareDisplay(int32_t orientation) {
3814 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003815 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003816 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3817 orientation, uniqueId, NO_PORT, viewportType);
3818 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003819
3820 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3821 float pressure) {
3822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3823 0.0f, 0.0f, 0.0f, EPSILON));
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);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003839 ASSERT_NO_FATAL_FAILURE(
3840 assertCursorPointerCoords(args.pointerCoords[0],
3841 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3842 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843}
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);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003932 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3934 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3935 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3936
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3938 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3939 ASSERT_EQ(DEVICE_ID, args.deviceId);
3940 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3941 ASSERT_EQ(uint32_t(0), args.policyFlags);
3942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3943 ASSERT_EQ(0, args.flags);
3944 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3945 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3946 ASSERT_EQ(0, args.edgeFlags);
3947 ASSERT_EQ(uint32_t(1), args.pointerCount);
3948 ASSERT_EQ(0, args.pointerProperties[0].id);
3949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003950 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003951 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3952 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3953 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3954
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003956 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3957 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3959 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3960 ASSERT_EQ(DEVICE_ID, args.deviceId);
3961 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3962 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3964 ASSERT_EQ(0, args.flags);
3965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3966 ASSERT_EQ(0, args.buttonState);
3967 ASSERT_EQ(0, args.edgeFlags);
3968 ASSERT_EQ(uint32_t(1), args.pointerCount);
3969 ASSERT_EQ(0, args.pointerProperties[0].id);
3970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003971 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003972 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3973 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3974 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3975
3976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3977 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3978 ASSERT_EQ(DEVICE_ID, args.deviceId);
3979 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3980 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3982 ASSERT_EQ(0, args.flags);
3983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3984 ASSERT_EQ(0, args.buttonState);
3985 ASSERT_EQ(0, args.edgeFlags);
3986 ASSERT_EQ(uint32_t(1), args.pointerCount);
3987 ASSERT_EQ(0, args.pointerProperties[0].id);
3988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003989 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3991 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3992 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3993}
3994
3995TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998
3999 NotifyMotionArgs args;
4000
4001 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004006 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4007 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4008 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009
4010 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004015 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4016 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017}
4018
4019TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004021 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022
4023 NotifyMotionArgs args;
4024
4025 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4029 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004030 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004034 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004035
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004041 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004042
4043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004045 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046}
4047
4048TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051
4052 NotifyMotionArgs args;
4053
4054 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4060 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004061 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4062 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4063 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004067 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4068 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4069 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004070
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004077 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4078 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4079 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
4081 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004086 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004087
4088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004090 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091}
4092
4093TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004095 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004097 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4106}
4107
4108TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 addConfigurationProperty("cursor.mode", "navigation");
4110 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004111 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004113 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4118 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4119 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4120 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4121 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4122
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004123 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4132
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004133 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4142
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004143 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4152}
4153
4154TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004156 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157
4158 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4159 mFakePointerController->setPosition(100, 200);
4160 mFakePointerController->setButtonState(0);
4161
4162 NotifyMotionArgs motionArgs;
4163 NotifyKeyArgs keyArgs;
4164
4165 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4170 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4171 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004172 ASSERT_NO_FATAL_FAILURE(
4173 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4177 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4178 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(
4180 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004181
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186 ASSERT_EQ(0, motionArgs.buttonState);
4187 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004188 ASSERT_NO_FATAL_FAILURE(
4189 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190
4191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004192 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 ASSERT_EQ(0, motionArgs.buttonState);
4194 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004195 ASSERT_NO_FATAL_FAILURE(
4196 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197
4198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004200 ASSERT_EQ(0, motionArgs.buttonState);
4201 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004202 ASSERT_NO_FATAL_FAILURE(
4203 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204
4205 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4211 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4212 motionArgs.buttonState);
4213 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4214 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004215 ASSERT_NO_FATAL_FAILURE(
4216 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4220 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4221 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4222 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(
4224 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225
4226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4228 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4229 motionArgs.buttonState);
4230 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4231 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(
4233 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4240 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(
4242 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243
4244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4247 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004248 ASSERT_NO_FATAL_FAILURE(
4249 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4255 ASSERT_EQ(0, motionArgs.buttonState);
4256 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_EQ(0, motionArgs.buttonState);
4264 ASSERT_EQ(0, mFakePointerController->getButtonState());
4265 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004266 ASSERT_NO_FATAL_FAILURE(
4267 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4270 ASSERT_EQ(0, motionArgs.buttonState);
4271 ASSERT_EQ(0, mFakePointerController->getButtonState());
4272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004273 ASSERT_NO_FATAL_FAILURE(
4274 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
4276 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4280 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4281 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4286 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004287 ASSERT_NO_FATAL_FAILURE(
4288 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004289
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4292 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4293 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004294 ASSERT_NO_FATAL_FAILURE(
4295 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_EQ(0, motionArgs.buttonState);
4302 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004303 ASSERT_NO_FATAL_FAILURE(
4304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004308 ASSERT_EQ(0, motionArgs.buttonState);
4309 ASSERT_EQ(0, mFakePointerController->getButtonState());
4310
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004311 ASSERT_NO_FATAL_FAILURE(
4312 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4314 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4315 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4316
4317 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4321 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4322 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4327 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004328 ASSERT_NO_FATAL_FAILURE(
4329 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330
4331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4333 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4334 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004335 ASSERT_NO_FATAL_FAILURE(
4336 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 ASSERT_EQ(0, motionArgs.buttonState);
4343 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004344 ASSERT_NO_FATAL_FAILURE(
4345 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004346
4347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4348 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4349 ASSERT_EQ(0, motionArgs.buttonState);
4350 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(
4352 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4355 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4356 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4357
4358 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4362 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4363 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4368 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004369 ASSERT_NO_FATAL_FAILURE(
4370 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371
4372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4374 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4375 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(
4377 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_EQ(0, motionArgs.buttonState);
4384 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004385 ASSERT_NO_FATAL_FAILURE(
4386 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004387
4388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4390 ASSERT_EQ(0, motionArgs.buttonState);
4391 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004392 ASSERT_NO_FATAL_FAILURE(
4393 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004394
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4396 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4397 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4398
4399 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4403 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4404 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4409 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004410 ASSERT_NO_FATAL_FAILURE(
4411 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4414 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4415 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4416 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(
4418 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_EQ(0, motionArgs.buttonState);
4425 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004426 ASSERT_NO_FATAL_FAILURE(
4427 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4431 ASSERT_EQ(0, motionArgs.buttonState);
4432 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004433 ASSERT_NO_FATAL_FAILURE(
4434 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4437 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4438 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4439}
4440
4441TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004443 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444
4445 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4446 mFakePointerController->setPosition(100, 200);
4447 mFakePointerController->setButtonState(0);
4448
4449 NotifyMotionArgs args;
4450
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004455 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4458 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 +01004459 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004460}
4461
4462TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004463 addConfigurationProperty("cursor.mode", "pointer");
4464 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004465 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004466
4467 NotifyDeviceResetArgs resetArgs;
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4469 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4470 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4471
4472 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4473 mFakePointerController->setPosition(100, 200);
4474 mFakePointerController->setButtonState(0);
4475
4476 NotifyMotionArgs args;
4477
4478 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4483 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4486 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 +01004487 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004488
4489 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4493 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4494 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4496 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4498 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4501 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4502
4503 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004504 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4505 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4507 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4508 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4510 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4512 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4513 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4515 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4516
4517 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4522 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4525 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 +01004526 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004527
4528 // Disable pointer capture and check that the device generation got bumped
4529 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004530 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531 mFakePolicy->setPointerCapture(false);
4532 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004533 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004534
4535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4536 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4537 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4538
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4543 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4546 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 +01004547 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548}
4549
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004550TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004551 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004552
Garfield Tan888a6a42020-01-09 11:39:16 -08004553 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004554 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004555 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4556 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004557 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4558 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004559 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4560 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4561
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004562 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4563 mFakePointerController->setPosition(100, 200);
4564 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004565
4566 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4571 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4574 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 +01004575 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004576 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4577}
4578
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579// --- TouchInputMapperTest ---
4580
4581class TouchInputMapperTest : public InputMapperTest {
4582protected:
4583 static const int32_t RAW_X_MIN;
4584 static const int32_t RAW_X_MAX;
4585 static const int32_t RAW_Y_MIN;
4586 static const int32_t RAW_Y_MAX;
4587 static const int32_t RAW_TOUCH_MIN;
4588 static const int32_t RAW_TOUCH_MAX;
4589 static const int32_t RAW_TOOL_MIN;
4590 static const int32_t RAW_TOOL_MAX;
4591 static const int32_t RAW_PRESSURE_MIN;
4592 static const int32_t RAW_PRESSURE_MAX;
4593 static const int32_t RAW_ORIENTATION_MIN;
4594 static const int32_t RAW_ORIENTATION_MAX;
4595 static const int32_t RAW_DISTANCE_MIN;
4596 static const int32_t RAW_DISTANCE_MAX;
4597 static const int32_t RAW_TILT_MIN;
4598 static const int32_t RAW_TILT_MAX;
4599 static const int32_t RAW_ID_MIN;
4600 static const int32_t RAW_ID_MAX;
4601 static const int32_t RAW_SLOT_MIN;
4602 static const int32_t RAW_SLOT_MAX;
4603 static const float X_PRECISION;
4604 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004605 static const float X_PRECISION_VIRTUAL;
4606 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607
4608 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004609 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610
4611 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4612
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004613 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004614 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004615
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 enum Axes {
4617 POSITION = 1 << 0,
4618 TOUCH = 1 << 1,
4619 TOOL = 1 << 2,
4620 PRESSURE = 1 << 3,
4621 ORIENTATION = 1 << 4,
4622 MINOR = 1 << 5,
4623 ID = 1 << 6,
4624 DISTANCE = 1 << 7,
4625 TILT = 1 << 8,
4626 SLOT = 1 << 9,
4627 TOOL_TYPE = 1 << 10,
4628 };
4629
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004630 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4631 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004632 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004634 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 int32_t toRawX(float displayX);
4636 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004637 float toCookedX(float rawX, float rawY);
4638 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004640 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004642 float toDisplayY(int32_t rawY, int32_t displayHeight);
4643
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644};
4645
4646const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4647const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4648const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4649const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4650const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4651const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4652const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4653const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004654const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4655const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4657const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4658const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4659const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4660const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4661const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4662const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4663const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4664const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4665const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4666const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4667const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004668const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4669 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4670const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4671 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004672const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4673 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674
4675const float TouchInputMapperTest::GEOMETRIC_SCALE =
4676 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4677 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4678
4679const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4680 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4681 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4682};
4683
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004684void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004685 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4686 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004687}
4688
4689void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4690 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4691 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692}
4693
Santos Cordonfa5cf462017-04-05 10:37:00 -07004694void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004695 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4696 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4697 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004698}
4699
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004701 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4702 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705}
4706
Jason Gerecke489fda82012-09-07 17:19:40 -07004707void TouchInputMapperTest::prepareLocationCalibration() {
4708 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4709}
4710
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711int32_t TouchInputMapperTest::toRawX(float displayX) {
4712 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4713}
4714
4715int32_t TouchInputMapperTest::toRawY(float displayY) {
4716 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4717}
4718
Jason Gerecke489fda82012-09-07 17:19:40 -07004719float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4720 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4721 return rawX;
4722}
4723
4724float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4725 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4726 return rawY;
4727}
4728
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004730 return toDisplayX(rawX, DISPLAY_WIDTH);
4731}
4732
4733float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4734 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735}
4736
4737float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004738 return toDisplayY(rawY, DISPLAY_HEIGHT);
4739}
4740
4741float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4742 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743}
4744
4745
4746// --- SingleTouchInputMapperTest ---
4747
4748class SingleTouchInputMapperTest : public TouchInputMapperTest {
4749protected:
4750 void prepareButtons();
4751 void prepareAxes(int axes);
4752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004753 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4754 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4755 void processUp(SingleTouchInputMapper& mappery);
4756 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4757 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4758 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4759 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4760 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4761 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762};
4763
4764void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004765 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766}
4767
4768void SingleTouchInputMapperTest::prepareAxes(int axes) {
4769 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004770 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 }
4773 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004774 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4775 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 }
4777 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004778 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4779 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 }
4781 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004782 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4783 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004784 }
4785 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004786 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4787 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 }
4789}
4790
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004791void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795}
4796
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004797void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800}
4801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004802void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804}
4805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004806void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808}
4809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004810void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4811 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813}
4814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004815void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817}
4818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004819void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4820 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823}
4824
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004825void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4826 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828}
4829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004830void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832}
4833
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835 prepareButtons();
4836 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004837 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004839 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840}
4841
4842TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004843 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4844 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 prepareButtons();
4846 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004847 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004849 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850}
4851
4852TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 prepareButtons();
4854 prepareAxes(POSITION);
4855 addConfigurationProperty("touch.deviceType", "touchPad");
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_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 prepareButtons();
4863 prepareAxes(POSITION);
4864 addConfigurationProperty("touch.deviceType", "touchScreen");
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_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868}
4869
4870TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 addConfigurationProperty("touch.deviceType", "touchScreen");
4872 prepareDisplay(DISPLAY_ORIENTATION_0);
4873 prepareButtons();
4874 prepareAxes(POSITION);
4875 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004876 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877
4878 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004879 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880
4881 // Virtual key is down.
4882 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4883 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4884 processDown(mapper, x, y);
4885 processSync(mapper);
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4887
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004888 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889
4890 // Virtual key is up.
4891 processUp(mapper);
4892 processSync(mapper);
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004895 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896}
4897
4898TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899 addConfigurationProperty("touch.deviceType", "touchScreen");
4900 prepareDisplay(DISPLAY_ORIENTATION_0);
4901 prepareButtons();
4902 prepareAxes(POSITION);
4903 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004904 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905
4906 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004907 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908
4909 // Virtual key is down.
4910 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4911 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4912 processDown(mapper, x, y);
4913 processSync(mapper);
4914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004916 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917
4918 // Virtual key is up.
4919 processUp(mapper);
4920 processSync(mapper);
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924}
4925
4926TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927 addConfigurationProperty("touch.deviceType", "touchScreen");
4928 prepareDisplay(DISPLAY_ORIENTATION_0);
4929 prepareButtons();
4930 prepareAxes(POSITION);
4931 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004932 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933
4934 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4935 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004936 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 ASSERT_TRUE(flags[0]);
4938 ASSERT_FALSE(flags[1]);
4939}
4940
4941TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942 addConfigurationProperty("touch.deviceType", "touchScreen");
4943 prepareDisplay(DISPLAY_ORIENTATION_0);
4944 prepareButtons();
4945 prepareAxes(POSITION);
4946 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004947 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948
arthurhungdcef2dc2020-08-11 14:47:50 +08004949 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950
4951 NotifyKeyArgs args;
4952
4953 // Press virtual key.
4954 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4955 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4956 processDown(mapper, x, y);
4957 processSync(mapper);
4958
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4960 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4961 ASSERT_EQ(DEVICE_ID, args.deviceId);
4962 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4963 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4964 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4965 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4966 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4967 ASSERT_EQ(KEY_HOME, args.scanCode);
4968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4969 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4970
4971 // Release virtual key.
4972 processUp(mapper);
4973 processSync(mapper);
4974
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4976 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4977 ASSERT_EQ(DEVICE_ID, args.deviceId);
4978 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4979 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4980 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4981 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4982 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4983 ASSERT_EQ(KEY_HOME, args.scanCode);
4984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4985 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4986
4987 // Should not have sent any motions.
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4989}
4990
4991TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 addConfigurationProperty("touch.deviceType", "touchScreen");
4993 prepareDisplay(DISPLAY_ORIENTATION_0);
4994 prepareButtons();
4995 prepareAxes(POSITION);
4996 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998
arthurhungdcef2dc2020-08-11 14:47:50 +08004999 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000
5001 NotifyKeyArgs keyArgs;
5002
5003 // Press virtual key.
5004 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5005 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5006 processDown(mapper, x, y);
5007 processSync(mapper);
5008
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5010 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5011 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5012 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5013 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5014 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5015 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5016 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5017 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5018 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5019 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5020
5021 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5022 // into the display area.
5023 y -= 100;
5024 processMove(mapper, x, y);
5025 processSync(mapper);
5026
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5028 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5029 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5030 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5031 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5032 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5033 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5034 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5035 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5036 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5037 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5038 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5039
5040 NotifyMotionArgs motionArgs;
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5042 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5043 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5044 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5045 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5046 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5047 ASSERT_EQ(0, motionArgs.flags);
5048 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5049 ASSERT_EQ(0, motionArgs.buttonState);
5050 ASSERT_EQ(0, motionArgs.edgeFlags);
5051 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5052 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5055 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5056 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5057 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5059
5060 // Keep moving out of bounds. Should generate a pointer move.
5061 y -= 50;
5062 processMove(mapper, x, y);
5063 processSync(mapper);
5064
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5067 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5068 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5069 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5071 ASSERT_EQ(0, motionArgs.flags);
5072 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5073 ASSERT_EQ(0, motionArgs.buttonState);
5074 ASSERT_EQ(0, motionArgs.edgeFlags);
5075 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5076 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5079 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5080 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5081 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5083
5084 // Release out of bounds. Should generate a pointer up.
5085 processUp(mapper);
5086 processSync(mapper);
5087
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5090 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5091 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5092 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5093 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5094 ASSERT_EQ(0, motionArgs.flags);
5095 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5096 ASSERT_EQ(0, motionArgs.buttonState);
5097 ASSERT_EQ(0, motionArgs.edgeFlags);
5098 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5099 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5102 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5103 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5104 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5105 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5106
5107 // Should not have sent any more keys or motions.
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5110}
5111
5112TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 addConfigurationProperty("touch.deviceType", "touchScreen");
5114 prepareDisplay(DISPLAY_ORIENTATION_0);
5115 prepareButtons();
5116 prepareAxes(POSITION);
5117 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005118 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119
arthurhungdcef2dc2020-08-11 14:47:50 +08005120 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121
5122 NotifyMotionArgs motionArgs;
5123
5124 // Initially go down out of bounds.
5125 int32_t x = -10;
5126 int32_t y = -10;
5127 processDown(mapper, x, y);
5128 processSync(mapper);
5129
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5131
5132 // Move into the display area. Should generate a pointer down.
5133 x = 50;
5134 y = 75;
5135 processMove(mapper, x, y);
5136 processSync(mapper);
5137
5138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5139 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5140 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5141 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5142 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5144 ASSERT_EQ(0, motionArgs.flags);
5145 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5146 ASSERT_EQ(0, motionArgs.buttonState);
5147 ASSERT_EQ(0, motionArgs.edgeFlags);
5148 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5149 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5152 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5153 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5154 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5155 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5156
5157 // Release. Should generate a pointer up.
5158 processUp(mapper);
5159 processSync(mapper);
5160
5161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5162 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5163 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5164 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5165 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5166 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5167 ASSERT_EQ(0, motionArgs.flags);
5168 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5169 ASSERT_EQ(0, motionArgs.buttonState);
5170 ASSERT_EQ(0, motionArgs.edgeFlags);
5171 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5172 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5175 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5176 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5177 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5178 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5179
5180 // Should not have sent any more keys or motions.
5181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5183}
5184
Santos Cordonfa5cf462017-04-05 10:37:00 -07005185TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005186 addConfigurationProperty("touch.deviceType", "touchScreen");
5187 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5188
5189 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5190 prepareButtons();
5191 prepareAxes(POSITION);
5192 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005193 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005194
arthurhungdcef2dc2020-08-11 14:47:50 +08005195 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005196
5197 NotifyMotionArgs motionArgs;
5198
5199 // Down.
5200 int32_t x = 100;
5201 int32_t y = 125;
5202 processDown(mapper, x, y);
5203 processSync(mapper);
5204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5207 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5208 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5209 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5210 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5211 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5212 ASSERT_EQ(0, motionArgs.flags);
5213 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5214 ASSERT_EQ(0, motionArgs.buttonState);
5215 ASSERT_EQ(0, motionArgs.edgeFlags);
5216 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5217 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5220 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5221 1, 0, 0, 0, 0, 0, 0, 0));
5222 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5223 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5225
5226 // Move.
5227 x += 50;
5228 y += 75;
5229 processMove(mapper, x, y);
5230 processSync(mapper);
5231
5232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5234 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5235 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5237 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5239 ASSERT_EQ(0, motionArgs.flags);
5240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5241 ASSERT_EQ(0, motionArgs.buttonState);
5242 ASSERT_EQ(0, motionArgs.edgeFlags);
5243 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5244 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5247 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5248 1, 0, 0, 0, 0, 0, 0, 0));
5249 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5250 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5252
5253 // Up.
5254 processUp(mapper);
5255 processSync(mapper);
5256
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5260 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5261 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5262 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5263 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5264 ASSERT_EQ(0, motionArgs.flags);
5265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5266 ASSERT_EQ(0, motionArgs.buttonState);
5267 ASSERT_EQ(0, motionArgs.edgeFlags);
5268 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5269 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5272 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5273 1, 0, 0, 0, 0, 0, 0, 0));
5274 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5275 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5277
5278 // Should not have sent any more keys or motions.
5279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5281}
5282
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284 addConfigurationProperty("touch.deviceType", "touchScreen");
5285 prepareDisplay(DISPLAY_ORIENTATION_0);
5286 prepareButtons();
5287 prepareAxes(POSITION);
5288 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005289 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
arthurhungdcef2dc2020-08-11 14:47:50 +08005291 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292
5293 NotifyMotionArgs motionArgs;
5294
5295 // Down.
5296 int32_t x = 100;
5297 int32_t y = 125;
5298 processDown(mapper, x, y);
5299 processSync(mapper);
5300
5301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5303 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5305 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5306 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5307 ASSERT_EQ(0, motionArgs.flags);
5308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5309 ASSERT_EQ(0, motionArgs.buttonState);
5310 ASSERT_EQ(0, motionArgs.edgeFlags);
5311 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5312 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5315 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5316 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5317 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5318 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5319
5320 // Move.
5321 x += 50;
5322 y += 75;
5323 processMove(mapper, x, y);
5324 processSync(mapper);
5325
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5328 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5329 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5330 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5332 ASSERT_EQ(0, motionArgs.flags);
5333 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5334 ASSERT_EQ(0, motionArgs.buttonState);
5335 ASSERT_EQ(0, motionArgs.edgeFlags);
5336 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5340 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5341 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5342 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5344
5345 // Up.
5346 processUp(mapper);
5347 processSync(mapper);
5348
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5355 ASSERT_EQ(0, motionArgs.flags);
5356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5357 ASSERT_EQ(0, motionArgs.buttonState);
5358 ASSERT_EQ(0, motionArgs.edgeFlags);
5359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5363 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5367
5368 // Should not have sent any more keys or motions.
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5371}
5372
5373TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 addConfigurationProperty("touch.deviceType", "touchScreen");
5375 prepareButtons();
5376 prepareAxes(POSITION);
5377 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005378 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379
5380 NotifyMotionArgs args;
5381
5382 // Rotation 90.
5383 prepareDisplay(DISPLAY_ORIENTATION_90);
5384 processDown(mapper, toRawX(50), toRawY(75));
5385 processSync(mapper);
5386
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5388 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5389 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5390
5391 processUp(mapper);
5392 processSync(mapper);
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5394}
5395
5396TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397 addConfigurationProperty("touch.deviceType", "touchScreen");
5398 prepareButtons();
5399 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005400 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401
5402 NotifyMotionArgs args;
5403
5404 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005405 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 prepareDisplay(DISPLAY_ORIENTATION_0);
5407 processDown(mapper, toRawX(50), toRawY(75));
5408 processSync(mapper);
5409
5410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5411 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5412 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5413
5414 processUp(mapper);
5415 processSync(mapper);
5416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5417
5418 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005419 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 prepareDisplay(DISPLAY_ORIENTATION_90);
5421 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5422 processSync(mapper);
5423
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5425 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5426 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5427
5428 processUp(mapper);
5429 processSync(mapper);
5430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5431
5432 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005433 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434 prepareDisplay(DISPLAY_ORIENTATION_180);
5435 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5436 processSync(mapper);
5437
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5439 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5440 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5441
5442 processUp(mapper);
5443 processSync(mapper);
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5445
5446 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005447 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 prepareDisplay(DISPLAY_ORIENTATION_270);
5449 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5450 processSync(mapper);
5451
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5453 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5454 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5455
5456 processUp(mapper);
5457 processSync(mapper);
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5459}
5460
5461TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462 addConfigurationProperty("touch.deviceType", "touchScreen");
5463 prepareDisplay(DISPLAY_ORIENTATION_0);
5464 prepareButtons();
5465 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005466 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467
5468 // These calculations are based on the input device calibration documentation.
5469 int32_t rawX = 100;
5470 int32_t rawY = 200;
5471 int32_t rawPressure = 10;
5472 int32_t rawToolMajor = 12;
5473 int32_t rawDistance = 2;
5474 int32_t rawTiltX = 30;
5475 int32_t rawTiltY = 110;
5476
5477 float x = toDisplayX(rawX);
5478 float y = toDisplayY(rawY);
5479 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5480 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5481 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5482 float distance = float(rawDistance);
5483
5484 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5485 float tiltScale = M_PI / 180;
5486 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5487 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5488 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5489 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5490
5491 processDown(mapper, rawX, rawY);
5492 processPressure(mapper, rawPressure);
5493 processToolMajor(mapper, rawToolMajor);
5494 processDistance(mapper, rawDistance);
5495 processTilt(mapper, rawTiltX, rawTiltY);
5496 processSync(mapper);
5497
5498 NotifyMotionArgs args;
5499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5501 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5502 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5503}
5504
Jason Gerecke489fda82012-09-07 17:19:40 -07005505TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005506 addConfigurationProperty("touch.deviceType", "touchScreen");
5507 prepareDisplay(DISPLAY_ORIENTATION_0);
5508 prepareLocationCalibration();
5509 prepareButtons();
5510 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005511 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005512
5513 int32_t rawX = 100;
5514 int32_t rawY = 200;
5515
5516 float x = toDisplayX(toCookedX(rawX, rawY));
5517 float y = toDisplayY(toCookedY(rawX, rawY));
5518
5519 processDown(mapper, rawX, rawY);
5520 processSync(mapper);
5521
5522 NotifyMotionArgs args;
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5525 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5526}
5527
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529 addConfigurationProperty("touch.deviceType", "touchScreen");
5530 prepareDisplay(DISPLAY_ORIENTATION_0);
5531 prepareButtons();
5532 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005533 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534
5535 NotifyMotionArgs motionArgs;
5536 NotifyKeyArgs keyArgs;
5537
5538 processDown(mapper, 100, 200);
5539 processSync(mapper);
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5541 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5542 ASSERT_EQ(0, motionArgs.buttonState);
5543
5544 // press BTN_LEFT, release BTN_LEFT
5545 processKey(mapper, BTN_LEFT, 1);
5546 processSync(mapper);
5547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5549 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5550
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5553 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5554
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 processKey(mapper, BTN_LEFT, 0);
5556 processSync(mapper);
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560
5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005563 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005564
5565 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5566 processKey(mapper, BTN_RIGHT, 1);
5567 processKey(mapper, BTN_MIDDLE, 1);
5568 processSync(mapper);
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5571 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5572 motionArgs.buttonState);
5573
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5576 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5577
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5579 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5580 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5581 motionArgs.buttonState);
5582
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583 processKey(mapper, BTN_RIGHT, 0);
5584 processSync(mapper);
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005588
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005591 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592
5593 processKey(mapper, BTN_MIDDLE, 0);
5594 processSync(mapper);
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005598
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005601 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602
5603 // press BTN_BACK, release BTN_BACK
5604 processKey(mapper, BTN_BACK, 1);
5605 processSync(mapper);
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5607 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5608 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005609
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005612 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5613
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5616 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617
5618 processKey(mapper, BTN_BACK, 0);
5619 processSync(mapper);
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005622 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005623
5624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005626 ASSERT_EQ(0, motionArgs.buttonState);
5627
Michael Wrightd02c5b62014-02-10 15:10:22 -08005628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5629 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5630 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5631
5632 // press BTN_SIDE, release BTN_SIDE
5633 processKey(mapper, BTN_SIDE, 1);
5634 processSync(mapper);
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5636 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5637 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005638
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005641 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5645 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646
5647 processKey(mapper, BTN_SIDE, 0);
5648 processSync(mapper);
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005655 ASSERT_EQ(0, motionArgs.buttonState);
5656
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5658 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5659 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5660
5661 // press BTN_FORWARD, release BTN_FORWARD
5662 processKey(mapper, BTN_FORWARD, 1);
5663 processSync(mapper);
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5665 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5666 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005667
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005670 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5671
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5674 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675
5676 processKey(mapper, BTN_FORWARD, 0);
5677 processSync(mapper);
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005681
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005684 ASSERT_EQ(0, motionArgs.buttonState);
5685
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5687 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5688 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5689
5690 // press BTN_EXTRA, release BTN_EXTRA
5691 processKey(mapper, BTN_EXTRA, 1);
5692 processSync(mapper);
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5694 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5695 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005696
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005699 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5700
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5703 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704
5705 processKey(mapper, BTN_EXTRA, 0);
5706 processSync(mapper);
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005710
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005713 ASSERT_EQ(0, motionArgs.buttonState);
5714
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5716 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5717 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5718
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5720
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 // press BTN_STYLUS, release BTN_STYLUS
5722 processKey(mapper, BTN_STYLUS, 1);
5723 processSync(mapper);
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005726 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5727
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5730 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731
5732 processKey(mapper, BTN_STYLUS, 0);
5733 processSync(mapper);
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005737
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005740 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741
5742 // press BTN_STYLUS2, release BTN_STYLUS2
5743 processKey(mapper, BTN_STYLUS2, 1);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005747 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5751 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752
5753 processKey(mapper, BTN_STYLUS2, 0);
5754 processSync(mapper);
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005758
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005761 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762
5763 // release touch
5764 processUp(mapper);
5765 processSync(mapper);
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5768 ASSERT_EQ(0, motionArgs.buttonState);
5769}
5770
5771TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772 addConfigurationProperty("touch.deviceType", "touchScreen");
5773 prepareDisplay(DISPLAY_ORIENTATION_0);
5774 prepareButtons();
5775 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005776 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777
5778 NotifyMotionArgs motionArgs;
5779
5780 // default tool type is finger
5781 processDown(mapper, 100, 200);
5782 processSync(mapper);
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5786
5787 // eraser
5788 processKey(mapper, BTN_TOOL_RUBBER, 1);
5789 processSync(mapper);
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5793
5794 // stylus
5795 processKey(mapper, BTN_TOOL_RUBBER, 0);
5796 processKey(mapper, BTN_TOOL_PEN, 1);
5797 processSync(mapper);
5798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5801
5802 // brush
5803 processKey(mapper, BTN_TOOL_PEN, 0);
5804 processKey(mapper, BTN_TOOL_BRUSH, 1);
5805 processSync(mapper);
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5809
5810 // pencil
5811 processKey(mapper, BTN_TOOL_BRUSH, 0);
5812 processKey(mapper, BTN_TOOL_PENCIL, 1);
5813 processSync(mapper);
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5817
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005818 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 processKey(mapper, BTN_TOOL_PENCIL, 0);
5820 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5825
5826 // mouse
5827 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5828 processKey(mapper, BTN_TOOL_MOUSE, 1);
5829 processSync(mapper);
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5833
5834 // lens
5835 processKey(mapper, BTN_TOOL_MOUSE, 0);
5836 processKey(mapper, BTN_TOOL_LENS, 1);
5837 processSync(mapper);
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5841
5842 // double-tap
5843 processKey(mapper, BTN_TOOL_LENS, 0);
5844 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5845 processSync(mapper);
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5849
5850 // triple-tap
5851 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5852 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5853 processSync(mapper);
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5857
5858 // quad-tap
5859 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5860 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5861 processSync(mapper);
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5865
5866 // finger
5867 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5868 processKey(mapper, BTN_TOOL_FINGER, 1);
5869 processSync(mapper);
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5873
5874 // stylus trumps finger
5875 processKey(mapper, BTN_TOOL_PEN, 1);
5876 processSync(mapper);
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5880
5881 // eraser trumps stylus
5882 processKey(mapper, BTN_TOOL_RUBBER, 1);
5883 processSync(mapper);
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5887
5888 // mouse trumps eraser
5889 processKey(mapper, BTN_TOOL_MOUSE, 1);
5890 processSync(mapper);
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5894
5895 // back to default tool type
5896 processKey(mapper, BTN_TOOL_MOUSE, 0);
5897 processKey(mapper, BTN_TOOL_RUBBER, 0);
5898 processKey(mapper, BTN_TOOL_PEN, 0);
5899 processKey(mapper, BTN_TOOL_FINGER, 0);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5904}
5905
5906TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907 addConfigurationProperty("touch.deviceType", "touchScreen");
5908 prepareDisplay(DISPLAY_ORIENTATION_0);
5909 prepareButtons();
5910 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005911 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005912 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913
5914 NotifyMotionArgs motionArgs;
5915
5916 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5917 processKey(mapper, BTN_TOOL_FINGER, 1);
5918 processMove(mapper, 100, 200);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5923 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5928 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5929
5930 // move a little
5931 processMove(mapper, 150, 250);
5932 processSync(mapper);
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5936 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5937
5938 // down when BTN_TOUCH is pressed, pressure defaults to 1
5939 processKey(mapper, BTN_TOUCH, 1);
5940 processSync(mapper);
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5942 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5944 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5945
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5949 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5950
5951 // up when BTN_TOUCH is released, hover restored
5952 processKey(mapper, BTN_TOUCH, 0);
5953 processSync(mapper);
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5957 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5958
5959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5962 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5963
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5965 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5967 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5968
5969 // exit hover when pointer goes away
5970 processKey(mapper, BTN_TOOL_FINGER, 0);
5971 processSync(mapper);
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5973 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5975 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5976}
5977
5978TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 addConfigurationProperty("touch.deviceType", "touchScreen");
5980 prepareDisplay(DISPLAY_ORIENTATION_0);
5981 prepareButtons();
5982 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005983 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984
5985 NotifyMotionArgs motionArgs;
5986
5987 // initially hovering because pressure is 0
5988 processDown(mapper, 100, 200);
5989 processPressure(mapper, 0);
5990 processSync(mapper);
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5994 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5995
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5999 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6000
6001 // move a little
6002 processMove(mapper, 150, 250);
6003 processSync(mapper);
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6005 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6007 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6008
6009 // down when pressure is non-zero
6010 processPressure(mapper, RAW_PRESSURE_MAX);
6011 processSync(mapper);
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6015 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6016
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6020 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6021
6022 // up when pressure becomes 0, hover restored
6023 processPressure(mapper, 0);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6028 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6033 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6034
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6038 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6039
6040 // exit hover when pointer goes away
6041 processUp(mapper);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6046 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6047}
6048
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049// --- MultiTouchInputMapperTest ---
6050
6051class MultiTouchInputMapperTest : public TouchInputMapperTest {
6052protected:
6053 void prepareAxes(int axes);
6054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006055 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6056 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6057 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6058 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6059 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6060 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6061 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6062 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6063 void processId(MultiTouchInputMapper& mapper, int32_t id);
6064 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6065 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6066 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6067 void processMTSync(MultiTouchInputMapper& mapper);
6068 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069};
6070
6071void MultiTouchInputMapperTest::prepareAxes(int axes) {
6072 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006073 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6074 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 }
6076 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006077 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6078 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6081 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082 }
6083 }
6084 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6086 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6089 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 }
6091 }
6092 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006093 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6094 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095 }
6096 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6098 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099 }
6100 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6102 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 }
6104 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006105 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6106 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107 }
6108 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6110 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 }
6112 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006113 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 }
6115}
6116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006117void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6118 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121}
6122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006123void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6124 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126}
6127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006128void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6129 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131}
6132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006133void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135}
6136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139}
6140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006141void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6142 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144}
6145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006146void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148}
6149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006150void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152}
6153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006154void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156}
6157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006158void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160}
6161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006162void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164}
6165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006166void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6167 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169}
6170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006171void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173}
6174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006175void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177}
6178
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 addConfigurationProperty("touch.deviceType", "touchScreen");
6181 prepareDisplay(DISPLAY_ORIENTATION_0);
6182 prepareAxes(POSITION);
6183 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006184 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185
arthurhungdcef2dc2020-08-11 14:47:50 +08006186 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187
6188 NotifyMotionArgs motionArgs;
6189
6190 // Two fingers down at once.
6191 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6192 processPosition(mapper, x1, y1);
6193 processMTSync(mapper);
6194 processPosition(mapper, x2, y2);
6195 processMTSync(mapper);
6196 processSync(mapper);
6197
6198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6199 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6200 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6201 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6202 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6203 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6204 ASSERT_EQ(0, motionArgs.flags);
6205 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6206 ASSERT_EQ(0, motionArgs.buttonState);
6207 ASSERT_EQ(0, motionArgs.edgeFlags);
6208 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6209 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6212 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6213 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6214 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6215 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6216
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6219 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6220 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6221 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6223 motionArgs.action);
6224 ASSERT_EQ(0, motionArgs.flags);
6225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6226 ASSERT_EQ(0, motionArgs.buttonState);
6227 ASSERT_EQ(0, motionArgs.edgeFlags);
6228 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6229 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6231 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6234 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6236 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6240
6241 // Move.
6242 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6243 processPosition(mapper, x1, y1);
6244 processMTSync(mapper);
6245 processPosition(mapper, x2, y2);
6246 processMTSync(mapper);
6247 processSync(mapper);
6248
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6251 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6252 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6253 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6255 ASSERT_EQ(0, motionArgs.flags);
6256 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6257 ASSERT_EQ(0, motionArgs.buttonState);
6258 ASSERT_EQ(0, motionArgs.edgeFlags);
6259 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6260 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6262 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6265 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6267 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6268 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6269 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6271
6272 // First finger up.
6273 x2 += 15; y2 -= 20;
6274 processPosition(mapper, x2, y2);
6275 processMTSync(mapper);
6276 processSync(mapper);
6277
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6280 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6281 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6282 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6284 motionArgs.action);
6285 ASSERT_EQ(0, motionArgs.flags);
6286 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6287 ASSERT_EQ(0, motionArgs.buttonState);
6288 ASSERT_EQ(0, motionArgs.edgeFlags);
6289 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6290 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6292 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6295 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6297 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6298 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6299 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6301
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6304 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6306 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6308 ASSERT_EQ(0, motionArgs.flags);
6309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6310 ASSERT_EQ(0, motionArgs.buttonState);
6311 ASSERT_EQ(0, motionArgs.edgeFlags);
6312 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6313 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6316 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6317 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6318 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6320
6321 // Move.
6322 x2 += 20; y2 -= 25;
6323 processPosition(mapper, x2, y2);
6324 processMTSync(mapper);
6325 processSync(mapper);
6326
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6329 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6330 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6331 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6333 ASSERT_EQ(0, motionArgs.flags);
6334 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6335 ASSERT_EQ(0, motionArgs.buttonState);
6336 ASSERT_EQ(0, motionArgs.edgeFlags);
6337 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6338 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6341 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6342 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6343 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6345
6346 // New finger down.
6347 int32_t x3 = 700, y3 = 300;
6348 processPosition(mapper, x2, y2);
6349 processMTSync(mapper);
6350 processPosition(mapper, x3, y3);
6351 processMTSync(mapper);
6352 processSync(mapper);
6353
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6356 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6357 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6358 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6360 motionArgs.action);
6361 ASSERT_EQ(0, motionArgs.flags);
6362 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6363 ASSERT_EQ(0, motionArgs.buttonState);
6364 ASSERT_EQ(0, motionArgs.edgeFlags);
6365 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6366 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6368 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6373 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6374 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6375 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6377
6378 // Second finger up.
6379 x3 += 30; y3 -= 20;
6380 processPosition(mapper, x3, y3);
6381 processMTSync(mapper);
6382 processSync(mapper);
6383
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6386 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6387 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6388 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6390 motionArgs.action);
6391 ASSERT_EQ(0, motionArgs.flags);
6392 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6393 ASSERT_EQ(0, motionArgs.buttonState);
6394 ASSERT_EQ(0, motionArgs.edgeFlags);
6395 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6396 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6398 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6401 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6403 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6404 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6405 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6407
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6410 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6411 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6412 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6414 ASSERT_EQ(0, motionArgs.flags);
6415 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6416 ASSERT_EQ(0, motionArgs.buttonState);
6417 ASSERT_EQ(0, motionArgs.edgeFlags);
6418 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6419 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6422 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6423 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6424 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6426
6427 // Last finger up.
6428 processMTSync(mapper);
6429 processSync(mapper);
6430
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6433 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6434 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6435 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6437 ASSERT_EQ(0, motionArgs.flags);
6438 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6439 ASSERT_EQ(0, motionArgs.buttonState);
6440 ASSERT_EQ(0, motionArgs.edgeFlags);
6441 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6442 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6445 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6446 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6447 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6448 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6449
6450 // Should not have sent any more keys or motions.
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6453}
6454
6455TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456 addConfigurationProperty("touch.deviceType", "touchScreen");
6457 prepareDisplay(DISPLAY_ORIENTATION_0);
6458 prepareAxes(POSITION | ID);
6459 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006460 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461
arthurhungdcef2dc2020-08-11 14:47:50 +08006462 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463
6464 NotifyMotionArgs motionArgs;
6465
6466 // Two fingers down at once.
6467 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6468 processPosition(mapper, x1, y1);
6469 processId(mapper, 1);
6470 processMTSync(mapper);
6471 processPosition(mapper, x2, y2);
6472 processId(mapper, 2);
6473 processMTSync(mapper);
6474 processSync(mapper);
6475
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6478 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6479 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6482 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6483
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6486 motionArgs.action);
6487 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6488 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6490 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6493 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6495 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6496
6497 // Move.
6498 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6499 processPosition(mapper, x1, y1);
6500 processId(mapper, 1);
6501 processMTSync(mapper);
6502 processPosition(mapper, x2, y2);
6503 processId(mapper, 2);
6504 processMTSync(mapper);
6505 processSync(mapper);
6506
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6509 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6510 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6512 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6515 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6517 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6518
6519 // First finger up.
6520 x2 += 15; y2 -= 20;
6521 processPosition(mapper, x2, y2);
6522 processId(mapper, 2);
6523 processMTSync(mapper);
6524 processSync(mapper);
6525
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6528 motionArgs.action);
6529 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6530 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6532 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6535 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6537 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6538
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6541 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6542 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6545 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6546
6547 // Move.
6548 x2 += 20; y2 -= 25;
6549 processPosition(mapper, x2, y2);
6550 processId(mapper, 2);
6551 processMTSync(mapper);
6552 processSync(mapper);
6553
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6556 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6557 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6560 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6561
6562 // New finger down.
6563 int32_t x3 = 700, y3 = 300;
6564 processPosition(mapper, x2, y2);
6565 processId(mapper, 2);
6566 processMTSync(mapper);
6567 processPosition(mapper, x3, y3);
6568 processId(mapper, 3);
6569 processMTSync(mapper);
6570 processSync(mapper);
6571
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6574 motionArgs.action);
6575 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6578 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6581 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6583 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6584
6585 // Second finger up.
6586 x3 += 30; y3 -= 20;
6587 processPosition(mapper, x3, y3);
6588 processId(mapper, 3);
6589 processMTSync(mapper);
6590 processSync(mapper);
6591
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6594 motionArgs.action);
6595 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6596 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6598 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6604
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6612
6613 // Last finger up.
6614 processMTSync(mapper);
6615 processSync(mapper);
6616
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6619 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6620 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6623 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6624
6625 // Should not have sent any more keys or motions.
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6628}
6629
6630TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631 addConfigurationProperty("touch.deviceType", "touchScreen");
6632 prepareDisplay(DISPLAY_ORIENTATION_0);
6633 prepareAxes(POSITION | ID | SLOT);
6634 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006635 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006636
arthurhungdcef2dc2020-08-11 14:47:50 +08006637 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006638
6639 NotifyMotionArgs motionArgs;
6640
6641 // Two fingers down at once.
6642 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6643 processPosition(mapper, x1, y1);
6644 processId(mapper, 1);
6645 processSlot(mapper, 1);
6646 processPosition(mapper, x2, y2);
6647 processId(mapper, 2);
6648 processSync(mapper);
6649
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6652 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6653 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6656 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6657
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6660 motionArgs.action);
6661 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6664 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6667 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6669 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6670
6671 // Move.
6672 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6673 processSlot(mapper, 0);
6674 processPosition(mapper, x1, y1);
6675 processSlot(mapper, 1);
6676 processPosition(mapper, x2, y2);
6677 processSync(mapper);
6678
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6681 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6682 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6683 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6684 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6687 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6689 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6690
6691 // First finger up.
6692 x2 += 15; y2 -= 20;
6693 processSlot(mapper, 0);
6694 processId(mapper, -1);
6695 processSlot(mapper, 1);
6696 processPosition(mapper, x2, y2);
6697 processSync(mapper);
6698
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6701 motionArgs.action);
6702 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6703 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6705 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6708 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6710 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6711
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6714 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6715 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6718 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6719
6720 // Move.
6721 x2 += 20; y2 -= 25;
6722 processPosition(mapper, x2, y2);
6723 processSync(mapper);
6724
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6727 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6728 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6731 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6732
6733 // New finger down.
6734 int32_t x3 = 700, y3 = 300;
6735 processPosition(mapper, x2, y2);
6736 processSlot(mapper, 0);
6737 processId(mapper, 3);
6738 processPosition(mapper, x3, y3);
6739 processSync(mapper);
6740
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6742 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6743 motionArgs.action);
6744 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6745 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6747 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6752 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6753
6754 // Second finger up.
6755 x3 += 30; y3 -= 20;
6756 processSlot(mapper, 1);
6757 processId(mapper, -1);
6758 processSlot(mapper, 0);
6759 processPosition(mapper, x3, y3);
6760 processSync(mapper);
6761
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6763 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6764 motionArgs.action);
6765 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6766 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6768 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6771 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6773 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6774
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6777 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6778 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6781 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6782
6783 // Last finger up.
6784 processId(mapper, -1);
6785 processSync(mapper);
6786
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6788 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6789 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6790 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6793 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6794
6795 // Should not have sent any more keys or motions.
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6798}
6799
6800TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006801 addConfigurationProperty("touch.deviceType", "touchScreen");
6802 prepareDisplay(DISPLAY_ORIENTATION_0);
6803 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006804 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006805
6806 // These calculations are based on the input device calibration documentation.
6807 int32_t rawX = 100;
6808 int32_t rawY = 200;
6809 int32_t rawTouchMajor = 7;
6810 int32_t rawTouchMinor = 6;
6811 int32_t rawToolMajor = 9;
6812 int32_t rawToolMinor = 8;
6813 int32_t rawPressure = 11;
6814 int32_t rawDistance = 0;
6815 int32_t rawOrientation = 3;
6816 int32_t id = 5;
6817
6818 float x = toDisplayX(rawX);
6819 float y = toDisplayY(rawY);
6820 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6821 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6822 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6823 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6824 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6825 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6826 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6827 float distance = float(rawDistance);
6828
6829 processPosition(mapper, rawX, rawY);
6830 processTouchMajor(mapper, rawTouchMajor);
6831 processTouchMinor(mapper, rawTouchMinor);
6832 processToolMajor(mapper, rawToolMajor);
6833 processToolMinor(mapper, rawToolMinor);
6834 processPressure(mapper, rawPressure);
6835 processOrientation(mapper, rawOrientation);
6836 processDistance(mapper, rawDistance);
6837 processId(mapper, id);
6838 processMTSync(mapper);
6839 processSync(mapper);
6840
6841 NotifyMotionArgs args;
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6843 ASSERT_EQ(0, args.pointerProperties[0].id);
6844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6845 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6846 orientation, distance));
6847}
6848
6849TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006850 addConfigurationProperty("touch.deviceType", "touchScreen");
6851 prepareDisplay(DISPLAY_ORIENTATION_0);
6852 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6853 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006854 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006855
6856 // These calculations are based on the input device calibration documentation.
6857 int32_t rawX = 100;
6858 int32_t rawY = 200;
6859 int32_t rawTouchMajor = 140;
6860 int32_t rawTouchMinor = 120;
6861 int32_t rawToolMajor = 180;
6862 int32_t rawToolMinor = 160;
6863
6864 float x = toDisplayX(rawX);
6865 float y = toDisplayY(rawY);
6866 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6867 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6868 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6869 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6870 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6871
6872 processPosition(mapper, rawX, rawY);
6873 processTouchMajor(mapper, rawTouchMajor);
6874 processTouchMinor(mapper, rawTouchMinor);
6875 processToolMajor(mapper, rawToolMajor);
6876 processToolMinor(mapper, rawToolMinor);
6877 processMTSync(mapper);
6878 processSync(mapper);
6879
6880 NotifyMotionArgs args;
6881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6883 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6884}
6885
6886TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006887 addConfigurationProperty("touch.deviceType", "touchScreen");
6888 prepareDisplay(DISPLAY_ORIENTATION_0);
6889 prepareAxes(POSITION | TOUCH | TOOL);
6890 addConfigurationProperty("touch.size.calibration", "diameter");
6891 addConfigurationProperty("touch.size.scale", "10");
6892 addConfigurationProperty("touch.size.bias", "160");
6893 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006894 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006895
6896 // These calculations are based on the input device calibration documentation.
6897 // Note: We only provide a single common touch/tool value because the device is assumed
6898 // not to emit separate values for each pointer (isSummed = 1).
6899 int32_t rawX = 100;
6900 int32_t rawY = 200;
6901 int32_t rawX2 = 150;
6902 int32_t rawY2 = 250;
6903 int32_t rawTouchMajor = 5;
6904 int32_t rawToolMajor = 8;
6905
6906 float x = toDisplayX(rawX);
6907 float y = toDisplayY(rawY);
6908 float x2 = toDisplayX(rawX2);
6909 float y2 = toDisplayY(rawY2);
6910 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6911 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6912 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6913
6914 processPosition(mapper, rawX, rawY);
6915 processTouchMajor(mapper, rawTouchMajor);
6916 processToolMajor(mapper, rawToolMajor);
6917 processMTSync(mapper);
6918 processPosition(mapper, rawX2, rawY2);
6919 processTouchMajor(mapper, rawTouchMajor);
6920 processToolMajor(mapper, rawToolMajor);
6921 processMTSync(mapper);
6922 processSync(mapper);
6923
6924 NotifyMotionArgs args;
6925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6926 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6927
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6929 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6930 args.action);
6931 ASSERT_EQ(size_t(2), args.pointerCount);
6932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6933 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6935 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6936}
6937
6938TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006939 addConfigurationProperty("touch.deviceType", "touchScreen");
6940 prepareDisplay(DISPLAY_ORIENTATION_0);
6941 prepareAxes(POSITION | TOUCH | TOOL);
6942 addConfigurationProperty("touch.size.calibration", "area");
6943 addConfigurationProperty("touch.size.scale", "43");
6944 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006946
6947 // These calculations are based on the input device calibration documentation.
6948 int32_t rawX = 100;
6949 int32_t rawY = 200;
6950 int32_t rawTouchMajor = 5;
6951 int32_t rawToolMajor = 8;
6952
6953 float x = toDisplayX(rawX);
6954 float y = toDisplayY(rawY);
6955 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6956 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6957 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6958
6959 processPosition(mapper, rawX, rawY);
6960 processTouchMajor(mapper, rawTouchMajor);
6961 processToolMajor(mapper, rawToolMajor);
6962 processMTSync(mapper);
6963 processSync(mapper);
6964
6965 NotifyMotionArgs args;
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6968 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6969}
6970
6971TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972 addConfigurationProperty("touch.deviceType", "touchScreen");
6973 prepareDisplay(DISPLAY_ORIENTATION_0);
6974 prepareAxes(POSITION | PRESSURE);
6975 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6976 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006977 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978
Michael Wrightaa449c92017-12-13 21:21:43 +00006979 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006980 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006981 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6982 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6983 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6984
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985 // These calculations are based on the input device calibration documentation.
6986 int32_t rawX = 100;
6987 int32_t rawY = 200;
6988 int32_t rawPressure = 60;
6989
6990 float x = toDisplayX(rawX);
6991 float y = toDisplayY(rawY);
6992 float pressure = float(rawPressure) * 0.01f;
6993
6994 processPosition(mapper, rawX, rawY);
6995 processPressure(mapper, rawPressure);
6996 processMTSync(mapper);
6997 processSync(mapper);
6998
6999 NotifyMotionArgs args;
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7002 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7003}
7004
7005TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006 addConfigurationProperty("touch.deviceType", "touchScreen");
7007 prepareDisplay(DISPLAY_ORIENTATION_0);
7008 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007010
7011 NotifyMotionArgs motionArgs;
7012 NotifyKeyArgs keyArgs;
7013
7014 processId(mapper, 1);
7015 processPosition(mapper, 100, 200);
7016 processSync(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7019 ASSERT_EQ(0, motionArgs.buttonState);
7020
7021 // press BTN_LEFT, release BTN_LEFT
7022 processKey(mapper, BTN_LEFT, 1);
7023 processSync(mapper);
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7026 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7027
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7030 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7031
Michael Wrightd02c5b62014-02-10 15:10:22 -08007032 processKey(mapper, BTN_LEFT, 0);
7033 processSync(mapper);
7034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007037
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007040 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007041
7042 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7043 processKey(mapper, BTN_RIGHT, 1);
7044 processKey(mapper, BTN_MIDDLE, 1);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7048 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7049 motionArgs.buttonState);
7050
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7052 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7053 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7054
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7057 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7058 motionArgs.buttonState);
7059
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060 processKey(mapper, BTN_RIGHT, 0);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007065
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007068 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069
7070 processKey(mapper, BTN_MIDDLE, 0);
7071 processSync(mapper);
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007075
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007078 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079
7080 // press BTN_BACK, release BTN_BACK
7081 processKey(mapper, BTN_BACK, 1);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7084 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7085 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007086
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007089 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7090
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7093 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094
7095 processKey(mapper, BTN_BACK, 0);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007100
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007103 ASSERT_EQ(0, motionArgs.buttonState);
7104
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7106 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7107 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7108
7109 // press BTN_SIDE, release BTN_SIDE
7110 processKey(mapper, BTN_SIDE, 1);
7111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7113 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7114 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007115
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007118 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7119
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7122 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123
7124 processKey(mapper, BTN_SIDE, 0);
7125 processSync(mapper);
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007129
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007132 ASSERT_EQ(0, motionArgs.buttonState);
7133
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7135 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7136 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7137
7138 // press BTN_FORWARD, release BTN_FORWARD
7139 processKey(mapper, BTN_FORWARD, 1);
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7142 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7143 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007144
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007147 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7148
7149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7151 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152
7153 processKey(mapper, BTN_FORWARD, 0);
7154 processSync(mapper);
7155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007158
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007161 ASSERT_EQ(0, motionArgs.buttonState);
7162
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7164 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7165 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7166
7167 // press BTN_EXTRA, release BTN_EXTRA
7168 processKey(mapper, BTN_EXTRA, 1);
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7171 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7172 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007173
Michael Wrightd02c5b62014-02-10 15:10:22 -08007174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007176 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7177
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7180 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181
7182 processKey(mapper, BTN_EXTRA, 0);
7183 processSync(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007187
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007190 ASSERT_EQ(0, motionArgs.buttonState);
7191
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7193 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7194 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7195
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7197
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198 // press BTN_STYLUS, release BTN_STYLUS
7199 processKey(mapper, BTN_STYLUS, 1);
7200 processSync(mapper);
7201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007203 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7204
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7207 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208
7209 processKey(mapper, BTN_STYLUS, 0);
7210 processSync(mapper);
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007213 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007214
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007217 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218
7219 // press BTN_STYLUS2, release BTN_STYLUS2
7220 processKey(mapper, BTN_STYLUS2, 1);
7221 processSync(mapper);
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007224 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7225
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7228 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229
7230 processKey(mapper, BTN_STYLUS2, 0);
7231 processSync(mapper);
7232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007235
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007238 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007239
7240 // release touch
7241 processId(mapper, -1);
7242 processSync(mapper);
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7244 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7245 ASSERT_EQ(0, motionArgs.buttonState);
7246}
7247
7248TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007249 addConfigurationProperty("touch.deviceType", "touchScreen");
7250 prepareDisplay(DISPLAY_ORIENTATION_0);
7251 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007253
7254 NotifyMotionArgs motionArgs;
7255
7256 // default tool type is finger
7257 processId(mapper, 1);
7258 processPosition(mapper, 100, 200);
7259 processSync(mapper);
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7263
7264 // eraser
7265 processKey(mapper, BTN_TOOL_RUBBER, 1);
7266 processSync(mapper);
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7270
7271 // stylus
7272 processKey(mapper, BTN_TOOL_RUBBER, 0);
7273 processKey(mapper, BTN_TOOL_PEN, 1);
7274 processSync(mapper);
7275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7278
7279 // brush
7280 processKey(mapper, BTN_TOOL_PEN, 0);
7281 processKey(mapper, BTN_TOOL_BRUSH, 1);
7282 processSync(mapper);
7283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7286
7287 // pencil
7288 processKey(mapper, BTN_TOOL_BRUSH, 0);
7289 processKey(mapper, BTN_TOOL_PENCIL, 1);
7290 processSync(mapper);
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7294
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007295 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007296 processKey(mapper, BTN_TOOL_PENCIL, 0);
7297 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7298 processSync(mapper);
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7302
7303 // mouse
7304 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7305 processKey(mapper, BTN_TOOL_MOUSE, 1);
7306 processSync(mapper);
7307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7310
7311 // lens
7312 processKey(mapper, BTN_TOOL_MOUSE, 0);
7313 processKey(mapper, BTN_TOOL_LENS, 1);
7314 processSync(mapper);
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7318
7319 // double-tap
7320 processKey(mapper, BTN_TOOL_LENS, 0);
7321 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7322 processSync(mapper);
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7326
7327 // triple-tap
7328 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7329 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7334
7335 // quad-tap
7336 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7337 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7338 processSync(mapper);
7339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7342
7343 // finger
7344 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7345 processKey(mapper, BTN_TOOL_FINGER, 1);
7346 processSync(mapper);
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7350
7351 // stylus trumps finger
7352 processKey(mapper, BTN_TOOL_PEN, 1);
7353 processSync(mapper);
7354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7357
7358 // eraser trumps stylus
7359 processKey(mapper, BTN_TOOL_RUBBER, 1);
7360 processSync(mapper);
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7364
7365 // mouse trumps eraser
7366 processKey(mapper, BTN_TOOL_MOUSE, 1);
7367 processSync(mapper);
7368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7371
7372 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7373 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7374 processSync(mapper);
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7378
7379 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7380 processToolType(mapper, MT_TOOL_PEN);
7381 processSync(mapper);
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7385
7386 // back to default tool type
7387 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7388 processKey(mapper, BTN_TOOL_MOUSE, 0);
7389 processKey(mapper, BTN_TOOL_RUBBER, 0);
7390 processKey(mapper, BTN_TOOL_PEN, 0);
7391 processKey(mapper, BTN_TOOL_FINGER, 0);
7392 processSync(mapper);
7393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7396}
7397
7398TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007399 addConfigurationProperty("touch.deviceType", "touchScreen");
7400 prepareDisplay(DISPLAY_ORIENTATION_0);
7401 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007402 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007403 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007404
7405 NotifyMotionArgs motionArgs;
7406
7407 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7408 processId(mapper, 1);
7409 processPosition(mapper, 100, 200);
7410 processSync(mapper);
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7414 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7415
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7419 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7420
7421 // move a little
7422 processPosition(mapper, 150, 250);
7423 processSync(mapper);
7424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7427 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7428
7429 // down when BTN_TOUCH is pressed, pressure defaults to 1
7430 processKey(mapper, BTN_TOUCH, 1);
7431 processSync(mapper);
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7433 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7435 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7436
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7438 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7440 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7441
7442 // up when BTN_TOUCH is released, hover restored
7443 processKey(mapper, BTN_TOUCH, 0);
7444 processSync(mapper);
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7448 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7449
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7453 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7454
7455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7458 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7459
7460 // exit hover when pointer goes away
7461 processId(mapper, -1);
7462 processSync(mapper);
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7466 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7467}
7468
7469TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470 addConfigurationProperty("touch.deviceType", "touchScreen");
7471 prepareDisplay(DISPLAY_ORIENTATION_0);
7472 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007473 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007474
7475 NotifyMotionArgs motionArgs;
7476
7477 // initially hovering because pressure is 0
7478 processId(mapper, 1);
7479 processPosition(mapper, 100, 200);
7480 processPressure(mapper, 0);
7481 processSync(mapper);
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7486
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7490 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7491
7492 // move a little
7493 processPosition(mapper, 150, 250);
7494 processSync(mapper);
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7496 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7498 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7499
7500 // down when pressure becomes non-zero
7501 processPressure(mapper, RAW_PRESSURE_MAX);
7502 processSync(mapper);
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7504 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7506 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7507
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7511 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7512
7513 // up when pressure becomes 0, hover restored
7514 processPressure(mapper, 0);
7515 processSync(mapper);
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7524 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7525
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7529 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7530
7531 // exit hover when pointer goes away
7532 processId(mapper, -1);
7533 processSync(mapper);
7534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7535 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7537 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7538}
7539
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007540/**
7541 * Set the input device port <--> display port associations, and check that the
7542 * events are routed to the display that matches the display port.
7543 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7544 */
7545TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007546 const std::string usb2 = "USB2";
7547 const uint8_t hdmi1 = 0;
7548 const uint8_t hdmi2 = 1;
7549 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007550 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007551
7552 addConfigurationProperty("touch.deviceType", "touchScreen");
7553 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007555
7556 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7557 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7558
7559 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7560 // for this input device is specified, and the matching viewport is not present,
7561 // the input device should be disabled (at the mapper level).
7562
7563 // Add viewport for display 2 on hdmi2
7564 prepareSecondaryDisplay(type, hdmi2);
7565 // Send a touch event
7566 processPosition(mapper, 100, 100);
7567 processSync(mapper);
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7569
7570 // Add viewport for display 1 on hdmi1
7571 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7572 // Send a touch event again
7573 processPosition(mapper, 100, 100);
7574 processSync(mapper);
7575
7576 NotifyMotionArgs args;
7577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7578 ASSERT_EQ(DISPLAY_ID, args.displayId);
7579}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007580
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007581TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007582 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007583 std::shared_ptr<FakePointerController> fakePointerController =
7584 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007585 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007586 fakePointerController->setPosition(100, 200);
7587 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007588 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7589
Garfield Tan888a6a42020-01-09 11:39:16 -08007590 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007591 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007592
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007593 prepareDisplay(DISPLAY_ORIENTATION_0);
7594 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007595 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007596
7597 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007598 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007599
7600 NotifyMotionArgs motionArgs;
7601 processPosition(mapper, 100, 100);
7602 processSync(mapper);
7603
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7606 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7607}
7608
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007609/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007610 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7611 */
7612TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7613 addConfigurationProperty("touch.deviceType", "touchScreen");
7614 prepareAxes(POSITION);
7615 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7616
7617 prepareDisplay(DISPLAY_ORIENTATION_0);
7618 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7619 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7620 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7621 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7622
7623 NotifyMotionArgs args;
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7625 ASSERT_EQ(26, args.readTime);
7626
7627 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7628 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7629 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7630
7631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7632 ASSERT_EQ(33, args.readTime);
7633}
7634
7635/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007636 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7637 * events should not be delivered to the listener.
7638 */
7639TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7640 addConfigurationProperty("touch.deviceType", "touchScreen");
7641 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7642 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7643 ViewportType::INTERNAL);
7644 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7645 prepareAxes(POSITION);
7646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7647
7648 NotifyMotionArgs motionArgs;
7649 processPosition(mapper, 100, 100);
7650 processSync(mapper);
7651
7652 mFakeListener->assertNotifyMotionWasNotCalled();
7653}
7654
Garfield Tanc734e4f2021-01-15 20:01:39 -08007655TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7656 addConfigurationProperty("touch.deviceType", "touchScreen");
7657 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7658 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7659 ViewportType::INTERNAL);
7660 std::optional<DisplayViewport> optionalDisplayViewport =
7661 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7662 ASSERT_TRUE(optionalDisplayViewport.has_value());
7663 DisplayViewport displayViewport = *optionalDisplayViewport;
7664
7665 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7666 prepareAxes(POSITION);
7667 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7668
7669 // Finger down
7670 int32_t x = 100, y = 100;
7671 processPosition(mapper, x, y);
7672 processSync(mapper);
7673
7674 NotifyMotionArgs motionArgs;
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7676 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7677
7678 // Deactivate display viewport
7679 displayViewport.isActive = false;
7680 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7681 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7682
7683 // Finger move
7684 x += 10, y += 10;
7685 processPosition(mapper, x, y);
7686 processSync(mapper);
7687
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7689 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7690
7691 // Reactivate display viewport
7692 displayViewport.isActive = true;
7693 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7694 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7695
7696 // Finger move again
7697 x += 10, y += 10;
7698 processPosition(mapper, x, y);
7699 processSync(mapper);
7700
7701 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7702 // no pointer on the touch device.
7703 mFakeListener->assertNotifyMotionWasNotCalled();
7704}
7705
Arthur Hung7c645402019-01-25 17:45:42 +08007706TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7707 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007708 prepareAxes(POSITION | ID | SLOT);
7709 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007711
7712 // Create the second touch screen device, and enable multi fingers.
7713 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007714 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007715 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007716 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007717 std::shared_ptr<InputDevice> device2 =
7718 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7719 Flags<InputDeviceClass>(0));
7720
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007721 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7722 0 /*flat*/, 0 /*fuzz*/);
7723 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7724 0 /*flat*/, 0 /*fuzz*/);
7725 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7726 0 /*flat*/, 0 /*fuzz*/);
7727 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7728 0 /*flat*/, 0 /*fuzz*/);
7729 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7730 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7731 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007732
7733 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007734 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007735 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7736 device2->reset(ARBITRARY_TIME);
7737
7738 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007739 std::shared_ptr<FakePointerController> fakePointerController =
7740 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007741 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7742 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7743
7744 // Setup policy for associated displays and show touches.
7745 const uint8_t hdmi1 = 0;
7746 const uint8_t hdmi2 = 1;
7747 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7748 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7749 mFakePolicy->setShowTouches(true);
7750
7751 // Create displays.
7752 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007753 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007754
7755 // Default device will reconfigure above, need additional reconfiguration for another device.
7756 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007757 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007758
7759 // Two fingers down at default display.
7760 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7761 processPosition(mapper, x1, y1);
7762 processId(mapper, 1);
7763 processSlot(mapper, 1);
7764 processPosition(mapper, x2, y2);
7765 processId(mapper, 2);
7766 processSync(mapper);
7767
7768 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7769 fakePointerController->getSpots().find(DISPLAY_ID);
7770 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7771 ASSERT_EQ(size_t(2), iter->second.size());
7772
7773 // Two fingers down at second display.
7774 processPosition(mapper2, x1, y1);
7775 processId(mapper2, 1);
7776 processSlot(mapper2, 1);
7777 processPosition(mapper2, x2, y2);
7778 processId(mapper2, 2);
7779 processSync(mapper2);
7780
7781 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7782 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7783 ASSERT_EQ(size_t(2), iter->second.size());
7784}
7785
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007786TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007787 prepareAxes(POSITION);
7788 addConfigurationProperty("touch.deviceType", "touchScreen");
7789 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007790 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007791
7792 NotifyMotionArgs motionArgs;
7793 // Unrotated video frame
7794 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7795 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007796 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007797 processPosition(mapper, 100, 200);
7798 processSync(mapper);
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7800 ASSERT_EQ(frames, motionArgs.videoFrames);
7801
7802 // Subsequent touch events should not have any videoframes
7803 // This is implemented separately in FakeEventHub,
7804 // but that should match the behaviour of TouchVideoDevice.
7805 processPosition(mapper, 200, 200);
7806 processSync(mapper);
7807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7808 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7809}
7810
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007811TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007812 prepareAxes(POSITION);
7813 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007814 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007815 // Unrotated video frame
7816 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7817 NotifyMotionArgs motionArgs;
7818
7819 // Test all 4 orientations
7820 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7821 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7822 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7823 clearViewports();
7824 prepareDisplay(orientation);
7825 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007826 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007827 processPosition(mapper, 100, 200);
7828 processSync(mapper);
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7830 frames[0].rotate(orientation);
7831 ASSERT_EQ(frames, motionArgs.videoFrames);
7832 }
7833}
7834
7835TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007836 prepareAxes(POSITION);
7837 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007839 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7840 // so mix these.
7841 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7842 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7843 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7844 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7845 NotifyMotionArgs motionArgs;
7846
7847 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007848 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007849 processPosition(mapper, 100, 200);
7850 processSync(mapper);
7851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7852 std::for_each(frames.begin(), frames.end(),
7853 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7854 ASSERT_EQ(frames, motionArgs.videoFrames);
7855}
7856
Arthur Hung9da14732019-09-02 16:16:58 +08007857/**
7858 * If we had defined port associations, but the viewport is not ready, the touch device would be
7859 * expected to be disabled, and it should be enabled after the viewport has found.
7860 */
7861TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007862 constexpr uint8_t hdmi2 = 1;
7863 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007864 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007865
7866 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7867
7868 addConfigurationProperty("touch.deviceType", "touchScreen");
7869 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007871
7872 ASSERT_EQ(mDevice->isEnabled(), false);
7873
7874 // Add display on hdmi2, the device should be enabled and can receive touch event.
7875 prepareSecondaryDisplay(type, hdmi2);
7876 ASSERT_EQ(mDevice->isEnabled(), true);
7877
7878 // Send a touch event.
7879 processPosition(mapper, 100, 100);
7880 processSync(mapper);
7881
7882 NotifyMotionArgs args;
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7884 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7885}
7886
Arthur Hung421eb1c2020-01-16 00:09:42 +08007887TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007888 addConfigurationProperty("touch.deviceType", "touchScreen");
7889 prepareDisplay(DISPLAY_ORIENTATION_0);
7890 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007891 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007892
7893 NotifyMotionArgs motionArgs;
7894
7895 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7896 // finger down
7897 processId(mapper, 1);
7898 processPosition(mapper, x1, y1);
7899 processSync(mapper);
7900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7901 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7903
7904 // finger move
7905 processId(mapper, 1);
7906 processPosition(mapper, x2, y2);
7907 processSync(mapper);
7908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7911
7912 // finger up.
7913 processId(mapper, -1);
7914 processSync(mapper);
7915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7916 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7918
7919 // new finger down
7920 processId(mapper, 1);
7921 processPosition(mapper, x3, y3);
7922 processSync(mapper);
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7926}
7927
7928/**
arthurhungcc7f9802020-04-30 17:55:40 +08007929 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7930 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007931 */
arthurhungcc7f9802020-04-30 17:55:40 +08007932TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007933 addConfigurationProperty("touch.deviceType", "touchScreen");
7934 prepareDisplay(DISPLAY_ORIENTATION_0);
7935 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007936 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007937
7938 NotifyMotionArgs motionArgs;
7939
7940 // default tool type is finger
7941 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007942 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007943 processPosition(mapper, x1, y1);
7944 processSync(mapper);
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7948
7949 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7950 processToolType(mapper, MT_TOOL_PALM);
7951 processSync(mapper);
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7953 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7954
7955 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007956 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007957 processPosition(mapper, x2, y2);
7958 processSync(mapper);
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7960
7961 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007962 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007963 processSync(mapper);
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7965
7966 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007967 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007968 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007969 processPosition(mapper, x3, y3);
7970 processSync(mapper);
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7972 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7974}
7975
arthurhungbf89a482020-04-17 17:37:55 +08007976/**
arthurhungcc7f9802020-04-30 17:55:40 +08007977 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7978 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007979 */
arthurhungcc7f9802020-04-30 17:55:40 +08007980TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007981 addConfigurationProperty("touch.deviceType", "touchScreen");
7982 prepareDisplay(DISPLAY_ORIENTATION_0);
7983 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7984 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7985
7986 NotifyMotionArgs motionArgs;
7987
7988 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007989 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7990 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007991 processPosition(mapper, x1, y1);
7992 processSync(mapper);
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7994 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7996
7997 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007998 processSlot(mapper, SECOND_SLOT);
7999 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008000 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008001 processSync(mapper);
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8004 motionArgs.action);
8005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8006
8007 // If the tool type of the first finger changes to MT_TOOL_PALM,
8008 // we expect to receive ACTION_POINTER_UP with cancel flag.
8009 processSlot(mapper, FIRST_SLOT);
8010 processId(mapper, FIRST_TRACKING_ID);
8011 processToolType(mapper, MT_TOOL_PALM);
8012 processSync(mapper);
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8015 motionArgs.action);
8016 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8017
8018 // The following MOVE events of second finger should be processed.
8019 processSlot(mapper, SECOND_SLOT);
8020 processId(mapper, SECOND_TRACKING_ID);
8021 processPosition(mapper, x2 + 1, y2 + 1);
8022 processSync(mapper);
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8025 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8026
8027 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8028 // it. Second finger receive move.
8029 processSlot(mapper, FIRST_SLOT);
8030 processId(mapper, INVALID_TRACKING_ID);
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 // Second finger keeps moving.
8037 processSlot(mapper, SECOND_SLOT);
8038 processId(mapper, SECOND_TRACKING_ID);
8039 processPosition(mapper, x2 + 2, y2 + 2);
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 up.
8046 processId(mapper, INVALID_TRACKING_ID);
8047 processSync(mapper);
8048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8049 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8050 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8051}
8052
8053/**
8054 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8055 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8056 */
8057TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8058 addConfigurationProperty("touch.deviceType", "touchScreen");
8059 prepareDisplay(DISPLAY_ORIENTATION_0);
8060 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8061 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8062
8063 NotifyMotionArgs motionArgs;
8064
8065 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8066 // First finger down.
8067 processId(mapper, FIRST_TRACKING_ID);
8068 processPosition(mapper, x1, y1);
8069 processSync(mapper);
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8071 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8073
8074 // Second finger down.
8075 processSlot(mapper, SECOND_SLOT);
8076 processId(mapper, SECOND_TRACKING_ID);
8077 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008078 processSync(mapper);
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8081 motionArgs.action);
8082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8083
arthurhungcc7f9802020-04-30 17:55:40 +08008084 // If the tool type of the first finger changes to MT_TOOL_PALM,
8085 // we expect to receive ACTION_POINTER_UP with cancel flag.
8086 processSlot(mapper, FIRST_SLOT);
8087 processId(mapper, FIRST_TRACKING_ID);
8088 processToolType(mapper, MT_TOOL_PALM);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8092 motionArgs.action);
8093 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8094
8095 // Second finger keeps moving.
8096 processSlot(mapper, SECOND_SLOT);
8097 processId(mapper, SECOND_TRACKING_ID);
8098 processPosition(mapper, x2 + 1, y2 + 1);
8099 processSync(mapper);
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8102
8103 // second finger becomes palm, receive cancel due to only 1 finger is active.
8104 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008105 processToolType(mapper, MT_TOOL_PALM);
8106 processSync(mapper);
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8108 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8109
arthurhungcc7f9802020-04-30 17:55:40 +08008110 // third finger down.
8111 processSlot(mapper, THIRD_SLOT);
8112 processId(mapper, THIRD_TRACKING_ID);
8113 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008114 processPosition(mapper, x3, y3);
8115 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8117 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008119 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8120
8121 // third finger move
8122 processId(mapper, THIRD_TRACKING_ID);
8123 processPosition(mapper, x3 + 1, y3 + 1);
8124 processSync(mapper);
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8127
8128 // first finger up, third finger receive move.
8129 processSlot(mapper, FIRST_SLOT);
8130 processId(mapper, INVALID_TRACKING_ID);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8134 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8135
8136 // second finger up, third finger receive move.
8137 processSlot(mapper, SECOND_SLOT);
8138 processId(mapper, INVALID_TRACKING_ID);
8139 processSync(mapper);
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8142 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8143
8144 // third finger up.
8145 processSlot(mapper, THIRD_SLOT);
8146 processId(mapper, INVALID_TRACKING_ID);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8149 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8150 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8151}
8152
8153/**
8154 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8155 * and the active finger could still be allowed to receive the events
8156 */
8157TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8158 addConfigurationProperty("touch.deviceType", "touchScreen");
8159 prepareDisplay(DISPLAY_ORIENTATION_0);
8160 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8161 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8162
8163 NotifyMotionArgs motionArgs;
8164
8165 // default tool type is finger
8166 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8167 processId(mapper, FIRST_TRACKING_ID);
8168 processPosition(mapper, x1, y1);
8169 processSync(mapper);
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8171 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8173
8174 // Second finger down.
8175 processSlot(mapper, SECOND_SLOT);
8176 processId(mapper, SECOND_TRACKING_ID);
8177 processPosition(mapper, x2, y2);
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8181 motionArgs.action);
8182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8183
8184 // If the tool type of the second finger changes to MT_TOOL_PALM,
8185 // we expect to receive ACTION_POINTER_UP with cancel flag.
8186 processId(mapper, SECOND_TRACKING_ID);
8187 processToolType(mapper, MT_TOOL_PALM);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8191 motionArgs.action);
8192 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8193
8194 // The following MOVE event should be processed.
8195 processSlot(mapper, FIRST_SLOT);
8196 processId(mapper, FIRST_TRACKING_ID);
8197 processPosition(mapper, x1 + 1, y1 + 1);
8198 processSync(mapper);
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8201 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8202
8203 // second finger up.
8204 processSlot(mapper, SECOND_SLOT);
8205 processId(mapper, INVALID_TRACKING_ID);
8206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8209
8210 // first finger keep moving
8211 processSlot(mapper, FIRST_SLOT);
8212 processId(mapper, FIRST_TRACKING_ID);
8213 processPosition(mapper, x1 + 2, y1 + 2);
8214 processSync(mapper);
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8217
8218 // first finger up.
8219 processId(mapper, INVALID_TRACKING_ID);
8220 processSync(mapper);
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8222 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8223 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008224}
8225
Arthur Hung9ad18942021-06-19 02:04:46 +00008226/**
8227 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8228 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8229 * cause slot be valid again.
8230 */
8231TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8232 addConfigurationProperty("touch.deviceType", "touchScreen");
8233 prepareDisplay(DISPLAY_ORIENTATION_0);
8234 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8235 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8236
8237 NotifyMotionArgs motionArgs;
8238
8239 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8240 // First finger down.
8241 processId(mapper, FIRST_TRACKING_ID);
8242 processPosition(mapper, x1, y1);
8243 processPressure(mapper, RAW_PRESSURE_MAX);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8247 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8248
8249 // First finger move.
8250 processId(mapper, FIRST_TRACKING_ID);
8251 processPosition(mapper, x1 + 1, y1 + 1);
8252 processPressure(mapper, RAW_PRESSURE_MAX);
8253 processSync(mapper);
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8256 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8257
8258 // Second finger down.
8259 processSlot(mapper, SECOND_SLOT);
8260 processId(mapper, SECOND_TRACKING_ID);
8261 processPosition(mapper, x2, y2);
8262 processPressure(mapper, RAW_PRESSURE_MAX);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8266 motionArgs.action);
8267 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8268
8269 // second finger up with some unexpected data.
8270 processSlot(mapper, SECOND_SLOT);
8271 processId(mapper, INVALID_TRACKING_ID);
8272 processPosition(mapper, x2, y2);
8273 processSync(mapper);
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8275 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8276 motionArgs.action);
8277 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8278
8279 // first finger up with some unexpected data.
8280 processSlot(mapper, FIRST_SLOT);
8281 processId(mapper, INVALID_TRACKING_ID);
8282 processPosition(mapper, x2, y2);
8283 processPressure(mapper, RAW_PRESSURE_MAX);
8284 processSync(mapper);
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8286 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8287 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8288}
8289
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008290// --- MultiTouchInputMapperTest_ExternalDevice ---
8291
8292class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8293protected:
Chris Yea52ade12020-08-27 16:49:20 -07008294 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008295};
8296
8297/**
8298 * Expect fallback to internal viewport if device is external and external viewport is not present.
8299 */
8300TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8301 prepareAxes(POSITION);
8302 addConfigurationProperty("touch.deviceType", "touchScreen");
8303 prepareDisplay(DISPLAY_ORIENTATION_0);
8304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8305
8306 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8307
8308 NotifyMotionArgs motionArgs;
8309
8310 // Expect the event to be sent to the internal viewport,
8311 // because an external viewport is not present.
8312 processPosition(mapper, 100, 100);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8315 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8316
8317 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008318 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008319 processPosition(mapper, 100, 100);
8320 processSync(mapper);
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8322 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8323}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008324
8325/**
8326 * Test touch should not work if outside of surface.
8327 */
8328class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8329protected:
8330 void halfDisplayToCenterHorizontal(int32_t orientation) {
8331 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008332 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008333
8334 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8335 internalViewport->orientation = orientation;
8336 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8337 internalViewport->logicalLeft = 0;
8338 internalViewport->logicalTop = 0;
8339 internalViewport->logicalRight = DISPLAY_HEIGHT;
8340 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8341
8342 internalViewport->physicalLeft = 0;
8343 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8344 internalViewport->physicalRight = DISPLAY_HEIGHT;
8345 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8346
8347 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8348 internalViewport->deviceHeight = DISPLAY_WIDTH;
8349 } else {
8350 internalViewport->logicalLeft = 0;
8351 internalViewport->logicalTop = 0;
8352 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8353 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8354
8355 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8356 internalViewport->physicalTop = 0;
8357 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8358 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8359
8360 internalViewport->deviceWidth = DISPLAY_WIDTH;
8361 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8362 }
8363
8364 mFakePolicy->updateViewport(internalViewport.value());
8365 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8366 }
8367
arthurhung5d547942020-12-14 17:04:45 +08008368 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8369 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008370 int32_t yExpected) {
8371 // touch on outside area should not work.
8372 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8373 processSync(mapper);
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8375
8376 // touch on inside area should receive the event.
8377 NotifyMotionArgs args;
8378 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8379 processSync(mapper);
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8381 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8382 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8383
8384 // Reset.
8385 mapper.reset(ARBITRARY_TIME);
8386 }
8387};
8388
arthurhung5d547942020-12-14 17:04:45 +08008389TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008390 addConfigurationProperty("touch.deviceType", "touchScreen");
8391 prepareDisplay(DISPLAY_ORIENTATION_0);
8392 prepareAxes(POSITION);
8393 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8394
8395 // Touch on center of normal display should work.
8396 const int32_t x = DISPLAY_WIDTH / 4;
8397 const int32_t y = DISPLAY_HEIGHT / 2;
8398 processPosition(mapper, toRawX(x), toRawY(y));
8399 processSync(mapper);
8400 NotifyMotionArgs args;
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8403 0.0f, 0.0f, 0.0f, 0.0f));
8404 // Reset.
8405 mapper.reset(ARBITRARY_TIME);
8406
8407 // Let physical display be different to device, and make surface and physical could be 1:1.
8408 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8409
8410 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8411 const int32_t yExpected = y;
8412 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8413}
8414
arthurhung5d547942020-12-14 17:04:45 +08008415TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008416 addConfigurationProperty("touch.deviceType", "touchScreen");
8417 prepareDisplay(DISPLAY_ORIENTATION_0);
8418 prepareAxes(POSITION);
8419 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8420
8421 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8422 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8423
8424 const int32_t x = DISPLAY_WIDTH / 4;
8425 const int32_t y = DISPLAY_HEIGHT / 2;
8426
8427 // expect x/y = swap x/y then reverse y.
8428 const int32_t xExpected = y;
8429 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8430 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8431}
8432
arthurhung5d547942020-12-14 17:04:45 +08008433TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008434 addConfigurationProperty("touch.deviceType", "touchScreen");
8435 prepareDisplay(DISPLAY_ORIENTATION_0);
8436 prepareAxes(POSITION);
8437 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8438
8439 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8440 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8441
8442 const int32_t x = DISPLAY_WIDTH / 4;
8443 const int32_t y = DISPLAY_HEIGHT / 2;
8444
8445 // expect x/y = swap x/y then reverse x.
8446 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8447 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8448 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8449}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008450
arthurhunga36b28e2020-12-29 20:28:15 +08008451TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8452 addConfigurationProperty("touch.deviceType", "touchScreen");
8453 prepareDisplay(DISPLAY_ORIENTATION_0);
8454 prepareAxes(POSITION);
8455 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8456
8457 const int32_t x = 0;
8458 const int32_t y = 0;
8459
8460 const int32_t xExpected = x;
8461 const int32_t yExpected = y;
8462 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8463
8464 clearViewports();
8465 prepareDisplay(DISPLAY_ORIENTATION_90);
8466 // expect x/y = swap x/y then reverse y.
8467 const int32_t xExpected90 = y;
8468 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8469 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8470
8471 clearViewports();
8472 prepareDisplay(DISPLAY_ORIENTATION_270);
8473 // expect x/y = swap x/y then reverse x.
8474 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8475 const int32_t yExpected270 = x;
8476 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8477}
8478
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008479TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8480 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8481 std::shared_ptr<FakePointerController> fakePointerController =
8482 std::make_shared<FakePointerController>();
8483 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8484 fakePointerController->setPosition(0, 0);
8485 fakePointerController->setButtonState(0);
8486
8487 // prepare device and capture
8488 prepareDisplay(DISPLAY_ORIENTATION_0);
8489 prepareAxes(POSITION | ID | SLOT);
8490 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8491 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8492 mFakePolicy->setPointerCapture(true);
8493 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8495
8496 // captured touchpad should be a touchpad source
8497 NotifyDeviceResetArgs resetArgs;
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8499 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8500
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008501 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008502
8503 const InputDeviceInfo::MotionRange* relRangeX =
8504 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8505 ASSERT_NE(relRangeX, nullptr);
8506 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8507 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8508 const InputDeviceInfo::MotionRange* relRangeY =
8509 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8510 ASSERT_NE(relRangeY, nullptr);
8511 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8512 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8513
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008514 // run captured pointer tests - note that this is unscaled, so input listener events should be
8515 // identical to what the hardware sends (accounting for any
8516 // calibration).
8517 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008518 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008519 processId(mapper, 1);
8520 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8521 processKey(mapper, BTN_TOUCH, 1);
8522 processSync(mapper);
8523
8524 // expect coord[0] to contain initial location of touch 0
8525 NotifyMotionArgs args;
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8528 ASSERT_EQ(1U, args.pointerCount);
8529 ASSERT_EQ(0, args.pointerProperties[0].id);
8530 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8531 ASSERT_NO_FATAL_FAILURE(
8532 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8533
8534 // FINGER 1 DOWN
8535 processSlot(mapper, 1);
8536 processId(mapper, 2);
8537 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8538 processSync(mapper);
8539
8540 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008542 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8543 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008544 ASSERT_EQ(2U, args.pointerCount);
8545 ASSERT_EQ(0, args.pointerProperties[0].id);
8546 ASSERT_EQ(1, args.pointerProperties[1].id);
8547 ASSERT_NO_FATAL_FAILURE(
8548 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8549 ASSERT_NO_FATAL_FAILURE(
8550 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8551
8552 // FINGER 1 MOVE
8553 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8554 processSync(mapper);
8555
8556 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8557 // from move
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8560 ASSERT_NO_FATAL_FAILURE(
8561 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8562 ASSERT_NO_FATAL_FAILURE(
8563 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8564
8565 // FINGER 0 MOVE
8566 processSlot(mapper, 0);
8567 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8568 processSync(mapper);
8569
8570 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8573 ASSERT_NO_FATAL_FAILURE(
8574 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8575 ASSERT_NO_FATAL_FAILURE(
8576 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8577
8578 // BUTTON DOWN
8579 processKey(mapper, BTN_LEFT, 1);
8580 processSync(mapper);
8581
8582 // touchinputmapper design sends a move before button press
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8587
8588 // BUTTON UP
8589 processKey(mapper, BTN_LEFT, 0);
8590 processSync(mapper);
8591
8592 // touchinputmapper design sends a move after button release
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8597
8598 // FINGER 0 UP
8599 processId(mapper, -1);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8603
8604 // FINGER 1 MOVE
8605 processSlot(mapper, 1);
8606 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8607 processSync(mapper);
8608
8609 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8612 ASSERT_EQ(1U, args.pointerCount);
8613 ASSERT_EQ(1, args.pointerProperties[0].id);
8614 ASSERT_NO_FATAL_FAILURE(
8615 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8616
8617 // FINGER 1 UP
8618 processId(mapper, -1);
8619 processKey(mapper, BTN_TOUCH, 0);
8620 processSync(mapper);
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8622 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8623
8624 // non captured touchpad should be a mouse source
8625 mFakePolicy->setPointerCapture(false);
8626 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8628 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8629}
8630
8631TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8632 std::shared_ptr<FakePointerController> fakePointerController =
8633 std::make_shared<FakePointerController>();
8634 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8635 fakePointerController->setPosition(0, 0);
8636 fakePointerController->setButtonState(0);
8637
8638 // prepare device and capture
8639 prepareDisplay(DISPLAY_ORIENTATION_0);
8640 prepareAxes(POSITION | ID | SLOT);
8641 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8642 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8643 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8645 // run uncaptured pointer tests - pushes out generic events
8646 // FINGER 0 DOWN
8647 processId(mapper, 3);
8648 processPosition(mapper, 100, 100);
8649 processKey(mapper, BTN_TOUCH, 1);
8650 processSync(mapper);
8651
8652 // start at (100,100), cursor should be at (0,0) * scale
8653 NotifyMotionArgs args;
8654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8656 ASSERT_NO_FATAL_FAILURE(
8657 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8658
8659 // FINGER 0 MOVE
8660 processPosition(mapper, 200, 200);
8661 processSync(mapper);
8662
8663 // compute scaling to help with touch position checking
8664 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8665 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8666 float scale =
8667 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8668
8669 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8673 0, 0, 0, 0, 0, 0, 0));
8674}
8675
8676TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8677 std::shared_ptr<FakePointerController> fakePointerController =
8678 std::make_shared<FakePointerController>();
8679
8680 prepareDisplay(DISPLAY_ORIENTATION_0);
8681 prepareAxes(POSITION | ID | SLOT);
8682 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8683 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8684 mFakePolicy->setPointerCapture(false);
8685 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8686
8687 // uncaptured touchpad should be a pointer device
8688 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8689
8690 // captured touchpad should be a touchpad device
8691 mFakePolicy->setPointerCapture(true);
8692 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8693 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8694}
8695
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008696// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008697
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008698class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008699protected:
8700 static const char* DEVICE_NAME;
8701 static const char* DEVICE_LOCATION;
8702 static const int32_t DEVICE_ID;
8703 static const int32_t DEVICE_GENERATION;
8704 static const int32_t DEVICE_CONTROLLER_NUMBER;
8705 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8706 static const int32_t EVENTHUB_ID;
8707
8708 std::shared_ptr<FakeEventHub> mFakeEventHub;
8709 sp<FakeInputReaderPolicy> mFakePolicy;
8710 sp<TestInputListener> mFakeListener;
8711 std::unique_ptr<InstrumentedInputReader> mReader;
8712 std::shared_ptr<InputDevice> mDevice;
8713
8714 virtual void SetUp(Flags<InputDeviceClass> classes) {
8715 mFakeEventHub = std::make_unique<FakeEventHub>();
8716 mFakePolicy = new FakeInputReaderPolicy();
8717 mFakeListener = new TestInputListener();
8718 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8719 mFakeListener);
8720 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8721 }
8722
8723 void SetUp() override { SetUp(DEVICE_CLASSES); }
8724
8725 void TearDown() override {
8726 mFakeListener.clear();
8727 mFakePolicy.clear();
8728 }
8729
8730 void configureDevice(uint32_t changes) {
8731 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8732 mReader->requestRefreshConfiguration(changes);
8733 mReader->loopOnce();
8734 }
8735 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8736 }
8737
8738 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8739 const std::string& location, int32_t eventHubId,
8740 Flags<InputDeviceClass> classes) {
8741 InputDeviceIdentifier identifier;
8742 identifier.name = name;
8743 identifier.location = location;
8744 std::shared_ptr<InputDevice> device =
8745 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8746 identifier);
8747 mReader->pushNextDevice(device);
8748 mFakeEventHub->addDevice(eventHubId, name, classes);
8749 mReader->loopOnce();
8750 return device;
8751 }
8752
8753 template <class T, typename... Args>
8754 T& addControllerAndConfigure(Args... args) {
8755 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8756
8757 return controller;
8758 }
8759};
8760
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008761const char* PeripheralControllerTest::DEVICE_NAME = "device";
8762const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8763const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8764const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8765const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8766const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008767 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008768const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008769
8770// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008771class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008772protected:
8773 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008774 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008775 }
8776};
8777
8778TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008779 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008780
8781 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8782 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8783}
8784
8785TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008786 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008787
8788 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8789 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8790}
8791
8792// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008793class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008794protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008795 void SetUp() override {
8796 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8797 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008798};
8799
Chris Ye85758332021-05-16 23:05:17 -07008800TEST_F(LightControllerTest, MonoLight) {
8801 RawLightInfo infoMono = {.id = 1,
8802 .name = "Mono",
8803 .maxBrightness = 255,
8804 .flags = InputLightClass::BRIGHTNESS,
8805 .path = ""};
8806 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008807
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008808 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008809 InputDeviceInfo info;
8810 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008811 std::vector<InputDeviceLightInfo> lights = info.getLights();
8812 ASSERT_EQ(1U, lights.size());
8813 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008814
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008815 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8816 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008817}
8818
8819TEST_F(LightControllerTest, RGBLight) {
8820 RawLightInfo infoRed = {.id = 1,
8821 .name = "red",
8822 .maxBrightness = 255,
8823 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8824 .path = ""};
8825 RawLightInfo infoGreen = {.id = 2,
8826 .name = "green",
8827 .maxBrightness = 255,
8828 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8829 .path = ""};
8830 RawLightInfo infoBlue = {.id = 3,
8831 .name = "blue",
8832 .maxBrightness = 255,
8833 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8834 .path = ""};
8835 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8836 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8837 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8838
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008839 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008840 InputDeviceInfo info;
8841 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008842 std::vector<InputDeviceLightInfo> lights = info.getLights();
8843 ASSERT_EQ(1U, lights.size());
8844 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008845
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008846 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8847 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008848}
8849
8850TEST_F(LightControllerTest, MultiColorRGBLight) {
8851 RawLightInfo infoColor = {.id = 1,
8852 .name = "red",
8853 .maxBrightness = 255,
8854 .flags = InputLightClass::BRIGHTNESS |
8855 InputLightClass::MULTI_INTENSITY |
8856 InputLightClass::MULTI_INDEX,
8857 .path = ""};
8858
8859 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8860
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008861 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008862 InputDeviceInfo info;
8863 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008864 std::vector<InputDeviceLightInfo> lights = info.getLights();
8865 ASSERT_EQ(1U, lights.size());
8866 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008867
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008868 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8869 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008870}
8871
8872TEST_F(LightControllerTest, PlayerIdLight) {
8873 RawLightInfo info1 = {.id = 1,
8874 .name = "player1",
8875 .maxBrightness = 255,
8876 .flags = InputLightClass::BRIGHTNESS,
8877 .path = ""};
8878 RawLightInfo info2 = {.id = 2,
8879 .name = "player2",
8880 .maxBrightness = 255,
8881 .flags = InputLightClass::BRIGHTNESS,
8882 .path = ""};
8883 RawLightInfo info3 = {.id = 3,
8884 .name = "player3",
8885 .maxBrightness = 255,
8886 .flags = InputLightClass::BRIGHTNESS,
8887 .path = ""};
8888 RawLightInfo info4 = {.id = 4,
8889 .name = "player4",
8890 .maxBrightness = 255,
8891 .flags = InputLightClass::BRIGHTNESS,
8892 .path = ""};
8893 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8894 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8895 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8896 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8897
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008898 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008899 InputDeviceInfo info;
8900 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008901 std::vector<InputDeviceLightInfo> lights = info.getLights();
8902 ASSERT_EQ(1U, lights.size());
8903 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008904
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008905 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8906 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
8907 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008908}
8909
Michael Wrightd02c5b62014-02-10 15:10:22 -08008910} // namespace android