blob: 0e721e909de5eee24d485e36f1dcf98a58f883f1 [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
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000272 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700273
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000274 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700275
Michael Wright17db18e2020-06-26 20:51:44 +0100276 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
277 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278 }
279
280 const InputReaderConfiguration* getReaderConfiguration() const {
281 return &mConfig;
282 }
283
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800284 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 return mInputDevices;
286 }
287
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100288 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700289 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700290 return transform;
291 }
292
293 void setTouchAffineTransformation(const TouchAffineTransformation t) {
294 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800295 }
296
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800297 void setPointerCapture(bool enabled) {
298 mConfig.pointerCapture = enabled;
299 }
300
Arthur Hung7c645402019-01-25 17:45:42 +0800301 void setShowTouches(bool enabled) {
302 mConfig.showTouches = enabled;
303 }
304
Garfield Tan888a6a42020-01-09 11:39:16 -0800305 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
306 mConfig.defaultPointerDisplayId = pointerDisplayId;
307 }
308
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800309 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
310
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700312 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000313 int32_t orientation, bool isActive,
314 const std::string& uniqueId,
315 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700316 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
317 || orientation == DISPLAY_ORIENTATION_270);
318 DisplayViewport v;
319 v.displayId = displayId;
320 v.orientation = orientation;
321 v.logicalLeft = 0;
322 v.logicalTop = 0;
323 v.logicalRight = isRotated ? height : width;
324 v.logicalBottom = isRotated ? width : height;
325 v.physicalLeft = 0;
326 v.physicalTop = 0;
327 v.physicalRight = isRotated ? height : width;
328 v.physicalBottom = isRotated ? width : height;
329 v.deviceWidth = isRotated ? height : width;
330 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000331 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700332 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700333 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100334 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700335 return v;
336 }
337
Chris Yea52ade12020-08-27 16:49:20 -0700338 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 *outConfig = mConfig;
340 }
341
Chris Yea52ade12020-08-27 16:49:20 -0700342 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100343 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
Chris Yea52ade12020-08-27 16:49:20 -0700346 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700347 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700349 mInputDevicesChanged = true;
350 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 }
352
Chris Yea52ade12020-08-27 16:49:20 -0700353 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
354 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700355 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 }
357
Chris Yea52ade12020-08-27 16:49:20 -0700358 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800359
360 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
361 std::unique_lock<std::mutex> lock(mLock);
362 base::ScopedLockAssertion assumeLocked(mLock);
363
364 const bool devicesChanged =
365 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
366 return mInputDevicesChanged;
367 });
368 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
369 mInputDevicesChanged = false;
370 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371};
372
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373// --- FakeEventHub ---
374
375class FakeEventHub : public EventHubInterface {
376 struct KeyInfo {
377 int32_t keyCode;
378 uint32_t flags;
379 };
380
Chris Yef59a2f42020-10-16 12:55:26 -0700381 struct SensorInfo {
382 InputDeviceSensorType sensorType;
383 int32_t sensorDataIndex;
384 };
385
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386 struct Device {
387 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700388 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 PropertyMap configuration;
390 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
391 KeyedVector<int, bool> relativeAxes;
392 KeyedVector<int32_t, int32_t> keyCodeStates;
393 KeyedVector<int32_t, int32_t> scanCodeStates;
394 KeyedVector<int32_t, int32_t> switchStates;
395 KeyedVector<int32_t, int32_t> absoluteAxisValue;
396 KeyedVector<int32_t, KeyInfo> keysByScanCode;
397 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
398 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700399 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
400 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800401 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700402 bool enabled;
403
404 status_t enable() {
405 enabled = true;
406 return OK;
407 }
408
409 status_t disable() {
410 enabled = false;
411 return OK;
412 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413
Chris Ye1b0c7342020-07-28 21:57:03 -0700414 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 };
416
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700417 std::mutex mLock;
418 std::condition_variable mEventsCondition;
419
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100421 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000422 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600423 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000424 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800425 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
426 // Simulates a device light brightness, from light id to light brightness.
427 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
428 // Simulates a device light intensities, from light id to light intensities map.
429 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
430 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700432public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 virtual ~FakeEventHub() {
434 for (size_t i = 0; i < mDevices.size(); i++) {
435 delete mDevices.valueAt(i);
436 }
437 }
438
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 FakeEventHub() { }
440
Chris Ye1b0c7342020-07-28 21:57:03 -0700441 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442 Device* device = new Device(classes);
443 device->identifier.name = name;
444 mDevices.add(deviceId, device);
445
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000446 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447 }
448
449 void removeDevice(int32_t deviceId) {
450 delete mDevices.valueFor(deviceId);
451 mDevices.removeItem(deviceId);
452
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000453 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454 }
455
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700456 bool isDeviceEnabled(int32_t deviceId) {
457 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700458 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700459 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
460 return false;
461 }
462 return device->enabled;
463 }
464
465 status_t enableDevice(int32_t deviceId) {
466 status_t result;
467 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700468 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700469 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
470 return BAD_VALUE;
471 }
472 if (device->enabled) {
473 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
474 return OK;
475 }
476 result = device->enable();
477 return result;
478 }
479
480 status_t disableDevice(int32_t deviceId) {
481 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700482 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700483 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
484 return BAD_VALUE;
485 }
486 if (!device->enabled) {
487 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
488 return OK;
489 }
490 return device->disable();
491 }
492
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000494 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495 }
496
497 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
498 Device* device = getDevice(deviceId);
499 device->configuration.addProperty(key, value);
500 }
501
502 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
503 Device* device = getDevice(deviceId);
504 device->configuration.addAll(configuration);
505 }
506
507 void addAbsoluteAxis(int32_t deviceId, int axis,
508 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
509 Device* device = getDevice(deviceId);
510
511 RawAbsoluteAxisInfo info;
512 info.valid = true;
513 info.minValue = minValue;
514 info.maxValue = maxValue;
515 info.flat = flat;
516 info.fuzz = fuzz;
517 info.resolution = resolution;
518 device->absoluteAxes.add(axis, info);
519 }
520
521 void addRelativeAxis(int32_t deviceId, int32_t axis) {
522 Device* device = getDevice(deviceId);
523 device->relativeAxes.add(axis, true);
524 }
525
526 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
527 Device* device = getDevice(deviceId);
528 device->keyCodeStates.replaceValueFor(keyCode, state);
529 }
530
531 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
532 Device* device = getDevice(deviceId);
533 device->scanCodeStates.replaceValueFor(scanCode, state);
534 }
535
536 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
537 Device* device = getDevice(deviceId);
538 device->switchStates.replaceValueFor(switchCode, state);
539 }
540
541 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
542 Device* device = getDevice(deviceId);
543 device->absoluteAxisValue.replaceValueFor(axis, value);
544 }
545
546 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
547 int32_t keyCode, uint32_t flags) {
548 Device* device = getDevice(deviceId);
549 KeyInfo info;
550 info.keyCode = keyCode;
551 info.flags = flags;
552 if (scanCode) {
553 device->keysByScanCode.add(scanCode, info);
554 }
555 if (usageCode) {
556 device->keysByUsageCode.add(usageCode, info);
557 }
558 }
559
560 void addLed(int32_t deviceId, int32_t led, bool initialState) {
561 Device* device = getDevice(deviceId);
562 device->leds.add(led, initialState);
563 }
564
Chris Yef59a2f42020-10-16 12:55:26 -0700565 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
566 int32_t sensorDataIndex) {
567 Device* device = getDevice(deviceId);
568 SensorInfo info;
569 info.sensorType = sensorType;
570 info.sensorDataIndex = sensorDataIndex;
571 device->sensorsByAbsCode.emplace(absCode, info);
572 }
573
574 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
575 Device* device = getDevice(deviceId);
576 typename BitArray<MSC_MAX>::Buffer buffer;
577 buffer[mscEvent / 32] = 1 << mscEvent % 32;
578 device->mscBitmask.loadFromBuffer(buffer);
579 }
580
Chris Ye3fdbfef2021-01-06 18:45:18 -0800581 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
582 mRawLightInfos.emplace(rawId, std::move(info));
583 }
584
585 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
586 mLightBrightness.emplace(rawId, brightness);
587 }
588
589 void fakeLightIntensities(int32_t rawId,
590 const std::unordered_map<LightColor, int32_t> intensities) {
591 mLightIntensities.emplace(rawId, std::move(intensities));
592 }
593
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 bool getLedState(int32_t deviceId, int32_t led) {
595 Device* device = getDevice(deviceId);
596 return device->leds.valueFor(led);
597 }
598
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100599 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 return mExcludedDevices;
601 }
602
603 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
604 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800605 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 }
607
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000608 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
609 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700610 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 RawEvent event;
612 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000613 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 event.deviceId = deviceId;
615 event.type = type;
616 event.code = code;
617 event.value = value;
618 mEvents.push_back(event);
619
620 if (type == EV_ABS) {
621 setAbsoluteAxisValue(deviceId, code, value);
622 }
623 }
624
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600625 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
626 std::vector<TouchVideoFrame>> videoFrames) {
627 mVideoFrames = std::move(videoFrames);
628 }
629
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 std::unique_lock<std::mutex> lock(mLock);
632 base::ScopedLockAssertion assumeLocked(mLock);
633 const bool queueIsEmpty =
634 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
635 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
636 if (!queueIsEmpty) {
637 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
638 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639 }
640
641private:
642 Device* getDevice(int32_t deviceId) const {
643 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100644 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 }
646
Chris Yea52ade12020-08-27 16:49:20 -0700647 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700649 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650 }
651
Chris Yea52ade12020-08-27 16:49:20 -0700652 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 Device* device = getDevice(deviceId);
654 return device ? device->identifier : InputDeviceIdentifier();
655 }
656
Chris Yea52ade12020-08-27 16:49:20 -0700657 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658
Chris Yea52ade12020-08-27 16:49:20 -0700659 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660 Device* device = getDevice(deviceId);
661 if (device) {
662 *outConfiguration = device->configuration;
663 }
664 }
665
Chris Yea52ade12020-08-27 16:49:20 -0700666 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
667 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800669 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 ssize_t index = device->absoluteAxes.indexOfKey(axis);
671 if (index >= 0) {
672 *outAxisInfo = device->absoluteAxes.valueAt(index);
673 return OK;
674 }
675 }
676 outAxisInfo->clear();
677 return -1;
678 }
679
Chris Yea52ade12020-08-27 16:49:20 -0700680 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 Device* device = getDevice(deviceId);
682 if (device) {
683 return device->relativeAxes.indexOfKey(axis) >= 0;
684 }
685 return false;
686 }
687
Chris Yea52ade12020-08-27 16:49:20 -0700688 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689
Chris Yef59a2f42020-10-16 12:55:26 -0700690 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
691 Device* device = getDevice(deviceId);
692 if (device) {
693 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
694 }
695 return false;
696 }
697
Chris Yea52ade12020-08-27 16:49:20 -0700698 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
699 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 Device* device = getDevice(deviceId);
701 if (device) {
702 const KeyInfo* key = getKey(device, scanCode, usageCode);
703 if (key) {
704 if (outKeycode) {
705 *outKeycode = key->keyCode;
706 }
707 if (outFlags) {
708 *outFlags = key->flags;
709 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700710 if (outMetaState) {
711 *outMetaState = metaState;
712 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 return OK;
714 }
715 }
716 return NAME_NOT_FOUND;
717 }
718
719 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
720 if (usageCode) {
721 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
722 if (index >= 0) {
723 return &device->keysByUsageCode.valueAt(index);
724 }
725 }
726 if (scanCode) {
727 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
728 if (index >= 0) {
729 return &device->keysByScanCode.valueAt(index);
730 }
731 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700732 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 }
734
Chris Yea52ade12020-08-27 16:49:20 -0700735 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736
Chris Yef59a2f42020-10-16 12:55:26 -0700737 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
738 int32_t absCode) {
739 Device* device = getDevice(deviceId);
740 if (!device) {
741 return Errorf("Sensor device not found.");
742 }
743 auto it = device->sensorsByAbsCode.find(absCode);
744 if (it == device->sensorsByAbsCode.end()) {
745 return Errorf("Sensor map not found.");
746 }
747 const SensorInfo& info = it->second;
748 return std::make_pair(info.sensorType, info.sensorDataIndex);
749 }
750
Chris Yea52ade12020-08-27 16:49:20 -0700751 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 mExcludedDevices = devices;
753 }
754
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000755 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
756 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000758 const size_t filledSize = std::min(mEvents.size(), bufferSize);
759 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
760
761 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700762 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000763 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 }
765
Chris Yea52ade12020-08-27 16:49:20 -0700766 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600767 auto it = mVideoFrames.find(deviceId);
768 if (it != mVideoFrames.end()) {
769 std::vector<TouchVideoFrame> frames = std::move(it->second);
770 mVideoFrames.erase(deviceId);
771 return frames;
772 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800773 return {};
774 }
775
Chris Yea52ade12020-08-27 16:49:20 -0700776 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 Device* device = getDevice(deviceId);
778 if (device) {
779 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
780 if (index >= 0) {
781 return device->scanCodeStates.valueAt(index);
782 }
783 }
784 return AKEY_STATE_UNKNOWN;
785 }
786
Chris Yea52ade12020-08-27 16:49:20 -0700787 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788 Device* device = getDevice(deviceId);
789 if (device) {
790 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
791 if (index >= 0) {
792 return device->keyCodeStates.valueAt(index);
793 }
794 }
795 return AKEY_STATE_UNKNOWN;
796 }
797
Chris Yea52ade12020-08-27 16:49:20 -0700798 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799 Device* device = getDevice(deviceId);
800 if (device) {
801 ssize_t index = device->switchStates.indexOfKey(sw);
802 if (index >= 0) {
803 return device->switchStates.valueAt(index);
804 }
805 }
806 return AKEY_STATE_UNKNOWN;
807 }
808
Chris Yea52ade12020-08-27 16:49:20 -0700809 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
810 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811 Device* device = getDevice(deviceId);
812 if (device) {
813 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
814 if (index >= 0) {
815 *outValue = device->absoluteAxisValue.valueAt(index);
816 return OK;
817 }
818 }
819 *outValue = 0;
820 return -1;
821 }
822
Chris Yea52ade12020-08-27 16:49:20 -0700823 // Return true if the device has non-empty key layout.
824 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
825 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 bool result = false;
827 Device* device = getDevice(deviceId);
828 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700829 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800830 for (size_t i = 0; i < numCodes; i++) {
831 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
832 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
833 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 }
835 }
836 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
837 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
838 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839 }
840 }
841 }
842 }
843 return result;
844 }
845
Chris Yea52ade12020-08-27 16:49:20 -0700846 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 Device* device = getDevice(deviceId);
848 if (device) {
849 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
850 return index >= 0;
851 }
852 return false;
853 }
854
Chris Yea52ade12020-08-27 16:49:20 -0700855 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 Device* device = getDevice(deviceId);
857 return device && device->leds.indexOfKey(led) >= 0;
858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 Device* device = getDevice(deviceId);
862 if (device) {
863 ssize_t index = device->leds.indexOfKey(led);
864 if (index >= 0) {
865 device->leds.replaceValueAt(led, on);
866 } else {
867 ADD_FAILURE()
868 << "Attempted to set the state of an LED that the EventHub declared "
869 "was not present. led=" << led;
870 }
871 }
872 }
873
Chris Yea52ade12020-08-27 16:49:20 -0700874 void getVirtualKeyDefinitions(
875 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 outVirtualKeys.clear();
877
878 Device* device = getDevice(deviceId);
879 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800880 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 }
882 }
883
Chris Yea52ade12020-08-27 16:49:20 -0700884 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700885 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800886 }
887
Chris Yea52ade12020-08-27 16:49:20 -0700888 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889 return false;
890 }
891
Chris Yea52ade12020-08-27 16:49:20 -0700892 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893
Chris Yea52ade12020-08-27 16:49:20 -0700894 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895
Chris Ye87143712020-11-10 05:05:58 +0000896 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
897
Chris Yee2b1e5c2021-03-10 22:45:12 -0800898 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
899 return BATTERY_CAPACITY;
900 }
Kim Low03ea0352020-11-06 12:45:07 -0800901
Chris Yee2b1e5c2021-03-10 22:45:12 -0800902 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
903 return BATTERY_STATUS;
904 }
905
906 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
907
908 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
909 return std::nullopt;
910 }
Kim Low03ea0352020-11-06 12:45:07 -0800911
Chris Ye3fdbfef2021-01-06 18:45:18 -0800912 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
913 std::vector<int32_t> ids;
914 for (const auto& [rawId, info] : mRawLightInfos) {
915 ids.push_back(rawId);
916 }
917 return ids;
918 }
919
920 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
921 auto it = mRawLightInfos.find(lightId);
922 if (it == mRawLightInfos.end()) {
923 return std::nullopt;
924 }
925 return it->second;
926 }
927
928 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
929 mLightBrightness.emplace(lightId, brightness);
930 }
931
932 void setLightIntensities(int32_t deviceId, int32_t lightId,
933 std::unordered_map<LightColor, int32_t> intensities) override {
934 mLightIntensities.emplace(lightId, intensities);
935 };
936
937 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
938 auto lightIt = mLightBrightness.find(lightId);
939 if (lightIt == mLightBrightness.end()) {
940 return std::nullopt;
941 }
942 return lightIt->second;
943 }
944
945 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
946 int32_t deviceId, int32_t lightId) override {
947 auto lightIt = mLightIntensities.find(lightId);
948 if (lightIt == mLightIntensities.end()) {
949 return std::nullopt;
950 }
951 return lightIt->second;
952 };
953
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100954 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 return false;
956 }
957
Chris Yea52ade12020-08-27 16:49:20 -0700958 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959
Chris Yea52ade12020-08-27 16:49:20 -0700960 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961
Chris Yea52ade12020-08-27 16:49:20 -0700962 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963
Chris Yea52ade12020-08-27 16:49:20 -0700964 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965};
966
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967// --- FakeInputMapper ---
968
969class FakeInputMapper : public InputMapper {
970 uint32_t mSources;
971 int32_t mKeyboardType;
972 int32_t mMetaState;
973 KeyedVector<int32_t, int32_t> mKeyCodeStates;
974 KeyedVector<int32_t, int32_t> mScanCodeStates;
975 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800976 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700978 std::mutex mLock;
979 std::condition_variable mStateChangedCondition;
980 bool mConfigureWasCalled GUARDED_BY(mLock);
981 bool mResetWasCalled GUARDED_BY(mLock);
982 bool mProcessWasCalled GUARDED_BY(mLock);
983 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984
Arthur Hungc23540e2018-11-29 20:42:11 +0800985 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800987 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
988 : InputMapper(deviceContext),
989 mSources(sources),
990 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800992 mConfigureWasCalled(false),
993 mResetWasCalled(false),
994 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995
Chris Yea52ade12020-08-27 16:49:20 -0700996 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997
998 void setKeyboardType(int32_t keyboardType) {
999 mKeyboardType = keyboardType;
1000 }
1001
1002 void setMetaState(int32_t metaState) {
1003 mMetaState = metaState;
1004 }
1005
1006 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001007 std::unique_lock<std::mutex> lock(mLock);
1008 base::ScopedLockAssertion assumeLocked(mLock);
1009 const bool configureCalled =
1010 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1011 return mConfigureWasCalled;
1012 });
1013 if (!configureCalled) {
1014 FAIL() << "Expected configure() to have been called.";
1015 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016 mConfigureWasCalled = false;
1017 }
1018
1019 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001020 std::unique_lock<std::mutex> lock(mLock);
1021 base::ScopedLockAssertion assumeLocked(mLock);
1022 const bool resetCalled =
1023 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1024 return mResetWasCalled;
1025 });
1026 if (!resetCalled) {
1027 FAIL() << "Expected reset() to have been called.";
1028 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029 mResetWasCalled = false;
1030 }
1031
Yi Kong9b14ac62018-07-17 13:48:38 -07001032 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001033 std::unique_lock<std::mutex> lock(mLock);
1034 base::ScopedLockAssertion assumeLocked(mLock);
1035 const bool processCalled =
1036 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1037 return mProcessWasCalled;
1038 });
1039 if (!processCalled) {
1040 FAIL() << "Expected process() to have been called.";
1041 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 if (outLastEvent) {
1043 *outLastEvent = mLastEvent;
1044 }
1045 mProcessWasCalled = false;
1046 }
1047
1048 void setKeyCodeState(int32_t keyCode, int32_t state) {
1049 mKeyCodeStates.replaceValueFor(keyCode, state);
1050 }
1051
1052 void setScanCodeState(int32_t scanCode, int32_t state) {
1053 mScanCodeStates.replaceValueFor(scanCode, state);
1054 }
1055
1056 void setSwitchState(int32_t switchCode, int32_t state) {
1057 mSwitchStates.replaceValueFor(switchCode, state);
1058 }
1059
1060 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001061 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062 }
1063
1064private:
Chris Yea52ade12020-08-27 16:49:20 -07001065 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001066
Chris Yea52ade12020-08-27 16:49:20 -07001067 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068 InputMapper::populateDeviceInfo(deviceInfo);
1069
1070 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1071 deviceInfo->setKeyboardType(mKeyboardType);
1072 }
1073 }
1074
Chris Yea52ade12020-08-27 16:49:20 -07001075 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001078
1079 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001080 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001081 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1082 mViewport = config->getDisplayViewportByPort(*displayPort);
1083 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001084
1085 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 }
1087
Chris Yea52ade12020-08-27 16:49:20 -07001088 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001089 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001091 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 }
1093
Chris Yea52ade12020-08-27 16:49:20 -07001094 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001095 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096 mLastEvent = *rawEvent;
1097 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001098 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 }
1100
Chris Yea52ade12020-08-27 16:49:20 -07001101 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1103 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1104 }
1105
Chris Yea52ade12020-08-27 16:49:20 -07001106 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1108 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1109 }
1110
Chris Yea52ade12020-08-27 16:49:20 -07001111 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1113 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1114 }
1115
Chris Yea52ade12020-08-27 16:49:20 -07001116 // Return true if the device has non-empty key layout.
1117 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1118 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119 for (size_t i = 0; i < numCodes; i++) {
1120 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1121 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1122 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 }
1124 }
1125 }
Chris Yea52ade12020-08-27 16:49:20 -07001126 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127 return result;
1128 }
1129
1130 virtual int32_t getMetaState() {
1131 return mMetaState;
1132 }
1133
1134 virtual void fadePointer() {
1135 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001136
1137 virtual std::optional<int32_t> getAssociatedDisplay() {
1138 if (mViewport) {
1139 return std::make_optional(mViewport->displayId);
1140 }
1141 return std::nullopt;
1142 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143};
1144
1145
1146// --- InstrumentedInputReader ---
1147
1148class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001149 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150
1151public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001152 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1153 const sp<InputReaderPolicyInterface>& policy,
1154 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001155 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001157 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001159 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001161 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001162 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163 InputDeviceIdentifier identifier;
1164 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001165 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001166 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001167 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 }
1169
Prabir Pradhan28efc192019-11-05 01:10:04 +00001170 // Make the protected loopOnce method accessible to tests.
1171 using InputReader::loopOnce;
1172
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001174 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1175 const InputDeviceIdentifier& identifier)
1176 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001177 if (!mNextDevices.empty()) {
1178 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1179 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 return device;
1181 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001182 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 }
1184
arthurhungdcef2dc2020-08-11 14:47:50 +08001185 // --- FakeInputReaderContext ---
1186 class FakeInputReaderContext : public ContextImpl {
1187 int32_t mGlobalMetaState;
1188 bool mUpdateGlobalMetaStateWasCalled;
1189 int32_t mGeneration;
1190
1191 public:
1192 FakeInputReaderContext(InputReader* reader)
1193 : ContextImpl(reader),
1194 mGlobalMetaState(0),
1195 mUpdateGlobalMetaStateWasCalled(false),
1196 mGeneration(1) {}
1197
1198 virtual ~FakeInputReaderContext() {}
1199
1200 void assertUpdateGlobalMetaStateWasCalled() {
1201 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1202 << "Expected updateGlobalMetaState() to have been called.";
1203 mUpdateGlobalMetaStateWasCalled = false;
1204 }
1205
1206 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1207
1208 uint32_t getGeneration() { return mGeneration; }
1209
1210 void updateGlobalMetaState() override {
1211 mUpdateGlobalMetaStateWasCalled = true;
1212 ContextImpl::updateGlobalMetaState();
1213 }
1214
1215 int32_t getGlobalMetaState() override {
1216 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1217 }
1218
1219 int32_t bumpGeneration() override {
1220 mGeneration = ContextImpl::bumpGeneration();
1221 return mGeneration;
1222 }
1223 } mFakeContext;
1224
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001226
1227public:
1228 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229};
1230
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001231// --- InputReaderPolicyTest ---
1232class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001233protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001234 sp<FakeInputReaderPolicy> mFakePolicy;
1235
Chris Yea52ade12020-08-27 16:49:20 -07001236 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1237 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238};
1239
1240/**
1241 * Check that empty set of viewports is an acceptable configuration.
1242 * Also try to get internal viewport two different ways - by type and by uniqueId.
1243 *
1244 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1245 * Such configuration is not currently allowed.
1246 */
1247TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001248 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001249
1250 // We didn't add any viewports yet, so there shouldn't be any.
1251 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001252 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001253 ASSERT_FALSE(internalViewport);
1254
1255 // Add an internal viewport, then clear it
1256 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001257 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001258 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001259
1260 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001262 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001263 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264
1265 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001266 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001268 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001269
1270 mFakePolicy->clearViewports();
1271 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001272 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001273 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001274 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275 ASSERT_FALSE(internalViewport);
1276}
1277
1278TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1279 const std::string internalUniqueId = "local:0";
1280 const std::string externalUniqueId = "local:1";
1281 const std::string virtualUniqueId1 = "virtual:2";
1282 const std::string virtualUniqueId2 = "virtual:3";
1283 constexpr int32_t virtualDisplayId1 = 2;
1284 constexpr int32_t virtualDisplayId2 = 3;
1285
1286 // Add an internal viewport
1287 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001288 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1289 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001290 // Add an external viewport
1291 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001292 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1293 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294 // Add an virtual viewport
1295 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001296 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1297 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298 // Add another virtual viewport
1299 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001300 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1301 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001302
1303 // Check matching by type for internal
1304 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001305 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001306 ASSERT_TRUE(internalViewport);
1307 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1308
1309 // Check matching by type for external
1310 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001311 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001312 ASSERT_TRUE(externalViewport);
1313 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1314
1315 // Check matching by uniqueId for virtual viewport #1
1316 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001317 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001318 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001319 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001320 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1321 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1322
1323 // Check matching by uniqueId for virtual viewport #2
1324 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001325 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001327 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1329 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1330}
1331
1332
1333/**
1334 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1335 * that lookup works by checking display id.
1336 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1337 */
1338TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1339 const std::string uniqueId1 = "uniqueId1";
1340 const std::string uniqueId2 = "uniqueId2";
1341 constexpr int32_t displayId1 = 2;
1342 constexpr int32_t displayId2 = 3;
1343
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001344 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1345 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346 for (const ViewportType& type : types) {
1347 mFakePolicy->clearViewports();
1348 // Add a viewport
1349 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001350 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1351 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 // Add another viewport
1353 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001354 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1355 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001356
1357 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001358 std::optional<DisplayViewport> viewport1 =
1359 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001360 ASSERT_TRUE(viewport1);
1361 ASSERT_EQ(displayId1, viewport1->displayId);
1362 ASSERT_EQ(type, viewport1->type);
1363
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001364 std::optional<DisplayViewport> viewport2 =
1365 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366 ASSERT_TRUE(viewport2);
1367 ASSERT_EQ(displayId2, viewport2->displayId);
1368 ASSERT_EQ(type, viewport2->type);
1369
1370 // When there are multiple viewports of the same kind, and uniqueId is not specified
1371 // in the call to getDisplayViewport, then that situation is not supported.
1372 // The viewports can be stored in any order, so we cannot rely on the order, since that
1373 // is just implementation detail.
1374 // However, we can check that it still returns *a* viewport, we just cannot assert
1375 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001376 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377 ASSERT_TRUE(someViewport);
1378 }
1379}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001381/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001382 * When we have multiple internal displays make sure we always return the default display when
1383 * querying by type.
1384 */
1385TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1386 const std::string uniqueId1 = "uniqueId1";
1387 const std::string uniqueId2 = "uniqueId2";
1388 constexpr int32_t nonDefaultDisplayId = 2;
1389 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1390 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1391
1392 // Add the default display first and ensure it gets returned.
1393 mFakePolicy->clearViewports();
1394 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001396 ViewportType::INTERNAL);
1397 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001398 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001399 ViewportType::INTERNAL);
1400
1401 std::optional<DisplayViewport> viewport =
1402 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1403 ASSERT_TRUE(viewport);
1404 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1405 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1406
1407 // Add the default display second to make sure order doesn't matter.
1408 mFakePolicy->clearViewports();
1409 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001410 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001411 ViewportType::INTERNAL);
1412 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001413 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001414 ViewportType::INTERNAL);
1415
1416 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1417 ASSERT_TRUE(viewport);
1418 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1419 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1420}
1421
1422/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001423 * Check getDisplayViewportByPort
1424 */
1425TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001427 const std::string uniqueId1 = "uniqueId1";
1428 const std::string uniqueId2 = "uniqueId2";
1429 constexpr int32_t displayId1 = 1;
1430 constexpr int32_t displayId2 = 2;
1431 const uint8_t hdmi1 = 0;
1432 const uint8_t hdmi2 = 1;
1433 const uint8_t hdmi3 = 2;
1434
1435 mFakePolicy->clearViewports();
1436 // Add a viewport that's associated with some display port that's not of interest.
1437 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001438 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1439 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001440 // Add another viewport, connected to HDMI1 port
1441 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001442 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1443 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001444
1445 // Check that correct display viewport was returned by comparing the display ports.
1446 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1447 ASSERT_TRUE(hdmi1Viewport);
1448 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1449 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1450
1451 // Check that we can still get the same viewport using the uniqueId
1452 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1453 ASSERT_TRUE(hdmi1Viewport);
1454 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1455 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1456 ASSERT_EQ(type, hdmi1Viewport->type);
1457
1458 // Check that we cannot find a port with "HDMI2", because we never added one
1459 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1460 ASSERT_FALSE(hdmi2Viewport);
1461}
1462
Michael Wrightd02c5b62014-02-10 15:10:22 -08001463// --- InputReaderTest ---
1464
1465class InputReaderTest : public testing::Test {
1466protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001467 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001468 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001469 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001470 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001471
Chris Yea52ade12020-08-27 16:49:20 -07001472 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001473 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001474 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001475 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476
Prabir Pradhan28efc192019-11-05 01:10:04 +00001477 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1478 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479 }
1480
Chris Yea52ade12020-08-27 16:49:20 -07001481 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001482 mFakeListener.clear();
1483 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001484 }
1485
Chris Ye1b0c7342020-07-28 21:57:03 -07001486 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001487 const PropertyMap* configuration) {
1488 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001489
1490 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001491 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001492 }
1493 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001494 mReader->loopOnce();
1495 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001496 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1497 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001498 }
1499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001500 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001501 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001502 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001503 }
1504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001505 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001506 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001507 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001508 }
1509
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001510 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001511 const std::string& name,
1512 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001513 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001514 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1515 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001516 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001517 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518 return mapper;
1519 }
1520};
1521
Chris Ye98d3f532020-10-01 21:48:59 -07001522TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001523 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1524 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1525 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526
Chris Ye98d3f532020-10-01 21:48:59 -07001527 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001529 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001530 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001531 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1532 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1533 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001534}
1535
1536TEST_F(InputReaderTest, PolicyGetInputDevices) {
1537 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1538 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1539 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001540
1541 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001542 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001543 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001544 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001545 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001546 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1547 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1548 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1549}
1550
Chris Yee7310032020-09-22 15:36:28 -07001551TEST_F(InputReaderTest, GetMergedInputDevices) {
1552 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1553 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1554 // Add two subdevices to device
1555 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1556 // Must add at least one mapper or the device will be ignored!
1557 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1558 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1559
1560 // Push same device instance for next device to be added, so they'll have same identifier.
1561 mReader->pushNextDevice(device);
1562 mReader->pushNextDevice(device);
1563 ASSERT_NO_FATAL_FAILURE(
1564 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1565 ASSERT_NO_FATAL_FAILURE(
1566 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1567
1568 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001569 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001570}
1571
Chris Yee14523a2020-12-19 13:46:00 -08001572TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1573 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1574 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1575 // Add two subdevices to device
1576 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1577 // Must add at least one mapper or the device will be ignored!
1578 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1579 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1580
1581 // Push same device instance for next device to be added, so they'll have same identifier.
1582 mReader->pushNextDevice(device);
1583 mReader->pushNextDevice(device);
1584 // Sensor device is initially disabled
1585 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1586 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1587 nullptr));
1588 // Device is disabled because the only sub device is a sensor device and disabled initially.
1589 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1590 ASSERT_FALSE(device->isEnabled());
1591 ASSERT_NO_FATAL_FAILURE(
1592 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1593 // The merged device is enabled if any sub device is enabled
1594 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1595 ASSERT_TRUE(device->isEnabled());
1596}
1597
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001599 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001600 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001601 constexpr int32_t eventHubId = 1;
1602 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001604 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001605 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607
Yi Kong9b14ac62018-07-17 13:48:38 -07001608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001609
1610 NotifyDeviceResetArgs resetArgs;
1611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001612 ASSERT_EQ(deviceId, resetArgs.deviceId);
1613
1614 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001615 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001616 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001617
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001619 ASSERT_EQ(deviceId, resetArgs.deviceId);
1620 ASSERT_EQ(device->isEnabled(), false);
1621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001622 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001623 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001626 ASSERT_EQ(device->isEnabled(), false);
1627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001628 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001629 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001631 ASSERT_EQ(deviceId, resetArgs.deviceId);
1632 ASSERT_EQ(device->isEnabled(), true);
1633}
1634
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001636 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001637 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001639 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001640 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001641 AINPUT_SOURCE_KEYBOARD, nullptr);
1642 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643
1644 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1645 AINPUT_SOURCE_ANY, AKEYCODE_A))
1646 << "Should return unknown when the device id is >= 0 but unknown.";
1647
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1649 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1650 << "Should return unknown when the device id is valid but the sources are not "
1651 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_EQ(AKEY_STATE_DOWN,
1654 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1655 AKEYCODE_A))
1656 << "Should return value provided by mapper when device id is valid and the device "
1657 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658
1659 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1660 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1661 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1662
1663 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1664 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1665 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1666}
1667
1668TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001670 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001671 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001672 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001673 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001674 AINPUT_SOURCE_KEYBOARD, nullptr);
1675 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001676
1677 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1678 AINPUT_SOURCE_ANY, KEY_A))
1679 << "Should return unknown when the device id is >= 0 but unknown.";
1680
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1682 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1683 << "Should return unknown when the device id is valid but the sources are not "
1684 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001685
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 ASSERT_EQ(AKEY_STATE_DOWN,
1687 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1688 KEY_A))
1689 << "Should return value provided by mapper when device id is valid and the device "
1690 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001691
1692 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1693 AINPUT_SOURCE_TRACKBALL, KEY_A))
1694 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1695
1696 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1697 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1698 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1699}
1700
1701TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001702 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001703 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001704 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001705 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001707 AINPUT_SOURCE_KEYBOARD, nullptr);
1708 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001709
1710 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1711 AINPUT_SOURCE_ANY, SW_LID))
1712 << "Should return unknown when the device id is >= 0 but unknown.";
1713
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001714 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1715 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1716 << "Should return unknown when the device id is valid but the sources are not "
1717 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001719 ASSERT_EQ(AKEY_STATE_DOWN,
1720 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1721 SW_LID))
1722 << "Should return value provided by mapper when device id is valid and the device "
1723 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001724
1725 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1726 AINPUT_SOURCE_TRACKBALL, SW_LID))
1727 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1728
1729 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1730 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1731 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1732}
1733
1734TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001735 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001736 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001737 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001738 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001740 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001741
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001742 mapper.addSupportedKeyCode(AKEYCODE_A);
1743 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744
1745 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1746 uint8_t flags[4] = { 0, 0, 0, 1 };
1747
1748 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1749 << "Should return false when device id is >= 0 but unknown.";
1750 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_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1754 << "Should return false when device id is valid but the sources are not supported by "
1755 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001756 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1757
1758 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001759 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1760 keyCodes, flags))
1761 << "Should return value provided by mapper when device id is valid and the device "
1762 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001763 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1764
1765 flags[3] = 1;
1766 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1767 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1768 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1769
1770 flags[3] = 1;
1771 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1772 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1773 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1774}
1775
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001776TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001778 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001779
1780 NotifyConfigurationChangedArgs args;
1781
1782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1783 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1784}
1785
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001786TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001787 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001788 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001789 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001790 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001791 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001792 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001793 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001794 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001795
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001796 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001797 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001798 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1799
1800 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001801 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001802 ASSERT_EQ(when, event.when);
1803 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805 ASSERT_EQ(EV_KEY, event.type);
1806 ASSERT_EQ(KEY_A, event.code);
1807 ASSERT_EQ(1, event.value);
1808}
1809
Garfield Tan1c7bc862020-01-28 13:24:04 -08001810TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001811 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001812 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001813 constexpr int32_t eventHubId = 1;
1814 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001815 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001817 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001819
1820 NotifyDeviceResetArgs resetArgs;
1821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001822 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001824 disableDevice(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 enableDevice(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
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001836 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001837 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001839 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001840 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001841}
1842
Garfield Tan1c7bc862020-01-28 13:24:04 -08001843TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1844 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001845 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001846 constexpr int32_t eventHubId = 1;
1847 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1848 // Must add at least one mapper or the device will be ignored!
1849 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001850 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001851 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1852
1853 NotifyDeviceResetArgs resetArgs;
1854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1855 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1856}
1857
Arthur Hungc23540e2018-11-29 20:42:11 +08001858TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001859 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001860 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001862 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001863 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1864 FakeInputMapper& mapper =
1865 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001866 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001867
1868 const uint8_t hdmi1 = 1;
1869
1870 // Associated touch screen with second display.
1871 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1872
1873 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001874 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001875 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001876 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001877 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001878 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001879 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001880 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001881 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001882 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001883
1884 // Add the device, and make sure all of the callbacks are triggered.
1885 // The device is added after the input port associations are processed since
1886 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001887 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001890 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001891
Arthur Hung2c9a3342019-07-23 14:18:59 +08001892 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001893 ASSERT_EQ(deviceId, device->getId());
1894 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1895 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001896
1897 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001898 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001899 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001900 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001901}
1902
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001903TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1904 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1905 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1906 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1907 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1908 // Must add at least one mapper or the device will be ignored!
1909 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1910 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1911 mReader->pushNextDevice(device);
1912 mReader->pushNextDevice(device);
1913 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1914 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1915
1916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1917
1918 NotifyDeviceResetArgs resetArgs;
1919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1920 ASSERT_EQ(deviceId, resetArgs.deviceId);
1921 ASSERT_TRUE(device->isEnabled());
1922 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1923 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1924
1925 disableDevice(deviceId);
1926 mReader->loopOnce();
1927
1928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1929 ASSERT_EQ(deviceId, resetArgs.deviceId);
1930 ASSERT_FALSE(device->isEnabled());
1931 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1932 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1933
1934 enableDevice(deviceId);
1935 mReader->loopOnce();
1936
1937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1938 ASSERT_EQ(deviceId, resetArgs.deviceId);
1939 ASSERT_TRUE(device->isEnabled());
1940 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1941 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1942}
1943
1944TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1945 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1946 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1947 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1948 // Add two subdevices to device
1949 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1950 FakeInputMapper& mapperDevice1 =
1951 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1952 FakeInputMapper& mapperDevice2 =
1953 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1954 mReader->pushNextDevice(device);
1955 mReader->pushNextDevice(device);
1956 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1957 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1958
1959 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1960 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1961
1962 ASSERT_EQ(AKEY_STATE_DOWN,
1963 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1964 ASSERT_EQ(AKEY_STATE_DOWN,
1965 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1966 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1967 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1968}
1969
Prabir Pradhan7e186182020-11-10 13:56:45 -08001970TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1971 NotifyPointerCaptureChangedArgs args;
1972
1973 mFakePolicy->setPointerCapture(true);
1974 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1975 mReader->loopOnce();
1976 mFakeListener->assertNotifyCaptureWasCalled(&args);
1977 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1978
1979 mFakePolicy->setPointerCapture(false);
1980 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1981 mReader->loopOnce();
1982 mFakeListener->assertNotifyCaptureWasCalled(&args);
1983 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1984
1985 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1986 // does not change.
1987 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1988 mReader->loopOnce();
1989 mFakeListener->assertNotifyCaptureWasCalled(&args);
1990 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1991}
1992
Chris Ye87143712020-11-10 05:05:58 +00001993class FakeVibratorInputMapper : public FakeInputMapper {
1994public:
1995 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1996 : FakeInputMapper(deviceContext, sources) {}
1997
1998 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1999};
2000
2001TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2002 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2003 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2004 constexpr int32_t eventHubId = 1;
2005 const char* DEVICE_LOCATION = "BLUETOOTH";
2006 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2007 FakeVibratorInputMapper& mapper =
2008 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2009 mReader->pushNextDevice(device);
2010
2011 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2012 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2013
2014 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2015 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2016}
2017
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002018// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002019
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002020class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002021public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002022 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002023
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002024 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002025
2026 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2027
2028 void dump(std::string& dump) override {}
2029
2030 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2031 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002032 }
2033
Chris Yee2b1e5c2021-03-10 22:45:12 -08002034 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2035 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002036 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002037
2038 bool setLightColor(int32_t lightId, int32_t color) override {
2039 getDeviceContext().setLightBrightness(lightId, color >> 24);
2040 return true;
2041 }
2042
2043 std::optional<int32_t> getLightColor(int32_t lightId) override {
2044 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2045 if (!result.has_value()) {
2046 return std::nullopt;
2047 }
2048 return result.value() << 24;
2049 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002050
2051 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2052
2053 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2054
2055private:
2056 InputDeviceContext& mDeviceContext;
2057 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2058 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002059};
2060
Chris Yee2b1e5c2021-03-10 22:45:12 -08002061TEST_F(InputReaderTest, BatteryGetCapacity) {
2062 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2063 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2064 constexpr int32_t eventHubId = 1;
2065 const char* DEVICE_LOCATION = "BLUETOOTH";
2066 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002067 FakePeripheralController& controller =
2068 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002069 mReader->pushNextDevice(device);
2070
2071 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2072
2073 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2074 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2075}
2076
2077TEST_F(InputReaderTest, BatteryGetStatus) {
2078 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2079 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2080 constexpr int32_t eventHubId = 1;
2081 const char* DEVICE_LOCATION = "BLUETOOTH";
2082 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002083 FakePeripheralController& controller =
2084 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002085 mReader->pushNextDevice(device);
2086
2087 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2088
2089 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2090 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2091}
2092
Chris Ye3fdbfef2021-01-06 18:45:18 -08002093TEST_F(InputReaderTest, LightGetColor) {
2094 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2095 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2096 constexpr int32_t eventHubId = 1;
2097 const char* DEVICE_LOCATION = "BLUETOOTH";
2098 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002099 FakePeripheralController& controller =
2100 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002101 mReader->pushNextDevice(device);
2102 RawLightInfo info = {.id = 1,
2103 .name = "Mono",
2104 .maxBrightness = 255,
2105 .flags = InputLightClass::BRIGHTNESS,
2106 .path = ""};
2107 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2108 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2109
2110 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002111
Chris Yee2b1e5c2021-03-10 22:45:12 -08002112 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2113 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002114 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2115 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2116}
2117
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002118// --- InputReaderIntegrationTest ---
2119
2120// These tests create and interact with the InputReader only through its interface.
2121// The InputReader is started during SetUp(), which starts its processing in its own
2122// thread. The tests use linux uinput to emulate input devices.
2123// NOTE: Interacting with the physical device while these tests are running may cause
2124// the tests to fail.
2125class InputReaderIntegrationTest : public testing::Test {
2126protected:
2127 sp<TestInputListener> mTestListener;
2128 sp<FakeInputReaderPolicy> mFakePolicy;
2129 sp<InputReaderInterface> mReader;
2130
Chris Yea52ade12020-08-27 16:49:20 -07002131 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002132 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002133 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2134 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002135
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002136 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002137 ASSERT_EQ(mReader->start(), OK);
2138
2139 // Since this test is run on a real device, all the input devices connected
2140 // to the test device will show up in mReader. We wait for those input devices to
2141 // show up before beginning the tests.
2142 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2143 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2144 }
2145
Chris Yea52ade12020-08-27 16:49:20 -07002146 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002147 ASSERT_EQ(mReader->stop(), OK);
2148 mTestListener.clear();
2149 mFakePolicy.clear();
2150 }
2151};
2152
2153TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2154 // An invalid input device that is only used for this test.
2155 class InvalidUinputDevice : public UinputDevice {
2156 public:
2157 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2158
2159 private:
2160 void configureDevice(int fd, uinput_user_dev* device) override {}
2161 };
2162
2163 const size_t numDevices = mFakePolicy->getInputDevices().size();
2164
2165 // UinputDevice does not set any event or key bits, so InputReader should not
2166 // consider it as a valid device.
2167 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2168 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2170 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2171
2172 invalidDevice.reset();
2173 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2174 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2175 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2176}
2177
2178TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2179 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2180
2181 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2182 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2184 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2185
2186 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002187 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2188 const auto& it =
2189 std::find_if(inputDevices.begin(), inputDevices.end(),
2190 [&keyboard](const InputDeviceInfo& info) {
2191 return info.getIdentifier().name == keyboard->getName();
2192 });
2193
2194 ASSERT_NE(it, inputDevices.end());
2195 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2196 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2197 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002198
2199 keyboard.reset();
2200 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2201 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2202 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2203}
2204
2205TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2206 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2207 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2208
2209 NotifyConfigurationChangedArgs configChangedArgs;
2210 ASSERT_NO_FATAL_FAILURE(
2211 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002212 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002213 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2214
2215 NotifyKeyArgs keyArgs;
2216 keyboard->pressAndReleaseHomeKey();
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2218 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002219 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002220 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002221 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002222 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002223 prevTimestamp = keyArgs.eventTime;
2224
2225 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002227 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002228 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002229 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002230}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002232/**
2233 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2234 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2235 * are passed to the listener.
2236 */
2237static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2238TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2239 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2240 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2241 NotifyKeyArgs keyArgs;
2242
2243 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2244 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2245 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2246 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2247
2248 controller->pressAndReleaseKey(BTN_GEAR_UP);
2249 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2250 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2251 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2252}
2253
Arthur Hungaab25622020-01-16 11:22:11 +08002254// --- TouchProcessTest ---
2255class TouchIntegrationTest : public InputReaderIntegrationTest {
2256protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002257 const std::string UNIQUE_ID = "local:0";
2258
Chris Yea52ade12020-08-27 16:49:20 -07002259 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002260 InputReaderIntegrationTest::SetUp();
2261 // At least add an internal display.
2262 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2263 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002264 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002265
2266 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2267 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2268 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2269 }
2270
2271 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2272 int32_t orientation, const std::string& uniqueId,
2273 std::optional<uint8_t> physicalPort,
2274 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002275 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2276 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002277 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2278 }
2279
2280 std::unique_ptr<UinputTouchScreen> mDevice;
2281};
2282
2283TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2284 NotifyMotionArgs args;
2285 const Point centerPoint = mDevice->getCenterPoint();
2286
2287 // ACTION_DOWN
2288 mDevice->sendDown(centerPoint);
2289 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2291
2292 // ACTION_MOVE
2293 mDevice->sendMove(centerPoint + Point(1, 1));
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2296
2297 // ACTION_UP
2298 mDevice->sendUp();
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2300 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2301}
2302
2303TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2304 NotifyMotionArgs args;
2305 const Point centerPoint = mDevice->getCenterPoint();
2306
2307 // ACTION_DOWN
2308 mDevice->sendDown(centerPoint);
2309 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2310 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2311
2312 // ACTION_POINTER_DOWN (Second slot)
2313 const Point secondPoint = centerPoint + Point(100, 100);
2314 mDevice->sendSlot(SECOND_SLOT);
2315 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2316 mDevice->sendDown(secondPoint + Point(1, 1));
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2318 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2319 args.action);
2320
2321 // ACTION_MOVE (Second slot)
2322 mDevice->sendMove(secondPoint);
2323 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2325
2326 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002327 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002329 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002330 args.action);
2331
2332 // ACTION_UP
2333 mDevice->sendSlot(FIRST_SLOT);
2334 mDevice->sendUp();
2335 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2336 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2337}
2338
2339TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2340 NotifyMotionArgs args;
2341 const Point centerPoint = mDevice->getCenterPoint();
2342
2343 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002344 mDevice->sendSlot(FIRST_SLOT);
2345 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002346 mDevice->sendDown(centerPoint);
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2348 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2349
arthurhungcc7f9802020-04-30 17:55:40 +08002350 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002351 const Point secondPoint = centerPoint + Point(100, 100);
2352 mDevice->sendSlot(SECOND_SLOT);
2353 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2354 mDevice->sendDown(secondPoint);
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2356 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2357 args.action);
2358
arthurhungcc7f9802020-04-30 17:55:40 +08002359 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002360 mDevice->sendMove(secondPoint + Point(1, 1));
2361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2363
arthurhungcc7f9802020-04-30 17:55:40 +08002364 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2365 // a palm event.
2366 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002367 mDevice->sendToolType(MT_TOOL_PALM);
2368 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002369 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2370 args.action);
2371 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002372
arthurhungcc7f9802020-04-30 17:55:40 +08002373 // Send up to second slot, expect first slot send moving.
2374 mDevice->sendPointerUp();
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002377
arthurhungcc7f9802020-04-30 17:55:40 +08002378 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002379 mDevice->sendSlot(FIRST_SLOT);
2380 mDevice->sendUp();
2381
arthurhungcc7f9802020-04-30 17:55:40 +08002382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2383 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002384}
2385
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387class InputDeviceTest : public testing::Test {
2388protected:
2389 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002390 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391 static const int32_t DEVICE_ID;
2392 static const int32_t DEVICE_GENERATION;
2393 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002394 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002395 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002397 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002399 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002400 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002401 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402
Chris Yea52ade12020-08-27 16:49:20 -07002403 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002404 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002406 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002407 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2408 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409 InputDeviceIdentifier identifier;
2410 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002411 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002412 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002413 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002414 mReader->pushNextDevice(mDevice);
2415 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2416 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417 }
2418
Chris Yea52ade12020-08-27 16:49:20 -07002419 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 mFakeListener.clear();
2421 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422 }
2423};
2424
2425const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002426const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002427const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2429const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002430const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2431 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002432const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433
2434TEST_F(InputDeviceTest, ImmutableProperties) {
2435 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002436 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002437 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438}
2439
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002440TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2441 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002442}
2443
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2445 // Configuration.
2446 InputReaderConfiguration config;
2447 mDevice->configure(ARBITRARY_TIME, &config, 0);
2448
2449 // Reset.
2450 mDevice->reset(ARBITRARY_TIME);
2451
2452 NotifyDeviceResetArgs resetArgs;
2453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2454 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2455 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2456
2457 // Metadata.
2458 ASSERT_TRUE(mDevice->isIgnored());
2459 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2460
2461 InputDeviceInfo info;
2462 mDevice->getDeviceInfo(&info);
2463 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002464 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2466 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2467
2468 // State queries.
2469 ASSERT_EQ(0, mDevice->getMetaState());
2470
2471 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2472 << "Ignored device should return unknown key code state.";
2473 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2474 << "Ignored device should return unknown scan code state.";
2475 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2476 << "Ignored device should return unknown switch state.";
2477
2478 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2479 uint8_t flags[2] = { 0, 1 };
2480 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2481 << "Ignored device should never mark any key codes.";
2482 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2483 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2484}
2485
2486TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2487 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002488 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002490 FakeInputMapper& mapper1 =
2491 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002492 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2493 mapper1.setMetaState(AMETA_ALT_ON);
2494 mapper1.addSupportedKeyCode(AKEYCODE_A);
2495 mapper1.addSupportedKeyCode(AKEYCODE_B);
2496 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2497 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2498 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2499 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2500 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002501
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002502 FakeInputMapper& mapper2 =
2503 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002504 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002505
2506 InputReaderConfiguration config;
2507 mDevice->configure(ARBITRARY_TIME, &config, 0);
2508
2509 String8 propertyValue;
2510 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2511 << "Device should have read configuration during configuration phase.";
2512 ASSERT_STREQ("value", propertyValue.string());
2513
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002514 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2515 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516
2517 // Reset
2518 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002519 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2520 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521
2522 NotifyDeviceResetArgs resetArgs;
2523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2524 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2525 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2526
2527 // Metadata.
2528 ASSERT_FALSE(mDevice->isIgnored());
2529 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2530
2531 InputDeviceInfo info;
2532 mDevice->getDeviceInfo(&info);
2533 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002534 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002535 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2536 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2537
2538 // State queries.
2539 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2540 << "Should query mappers and combine meta states.";
2541
2542 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2543 << "Should return unknown key code state when source not supported.";
2544 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2545 << "Should return unknown scan code state when source not supported.";
2546 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2547 << "Should return unknown switch state when source not supported.";
2548
2549 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2550 << "Should query mapper when source is supported.";
2551 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2552 << "Should query mapper when source is supported.";
2553 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2554 << "Should query mapper when source is supported.";
2555
2556 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2557 uint8_t flags[4] = { 0, 0, 0, 1 };
2558 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2559 << "Should do nothing when source is unsupported.";
2560 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2561 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2562 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2563 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2564
2565 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2566 << "Should query mapper when source is supported.";
2567 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2568 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2569 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2570 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2571
2572 // Event handling.
2573 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002574 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 mDevice->process(&event, 1);
2576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002577 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2578 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579}
2580
Arthur Hung2c9a3342019-07-23 14:18:59 +08002581// A single input device is associated with a specific display. Check that:
2582// 1. Device is disabled if the viewport corresponding to the associated display is not found
2583// 2. Device is disabled when setEnabled API is called
2584TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002585 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002586
2587 // First Configuration.
2588 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2589
2590 // Device should be enabled by default.
2591 ASSERT_TRUE(mDevice->isEnabled());
2592
2593 // Prepare associated info.
2594 constexpr uint8_t hdmi = 1;
2595 const std::string UNIQUE_ID = "local:1";
2596
2597 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2598 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2599 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2600 // Device should be disabled because it is associated with a specific display via
2601 // input port <-> display port association, but the corresponding display is not found
2602 ASSERT_FALSE(mDevice->isEnabled());
2603
2604 // Prepare displays.
2605 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002606 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2607 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002608 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2609 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2610 ASSERT_TRUE(mDevice->isEnabled());
2611
2612 // Device should be disabled after set disable.
2613 mFakePolicy->addDisabledDevice(mDevice->getId());
2614 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2615 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2616 ASSERT_FALSE(mDevice->isEnabled());
2617
2618 // Device should still be disabled even found the associated display.
2619 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2620 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2621 ASSERT_FALSE(mDevice->isEnabled());
2622}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002623
2624// --- InputMapperTest ---
2625
2626class InputMapperTest : public testing::Test {
2627protected:
2628 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002629 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630 static const int32_t DEVICE_ID;
2631 static const int32_t DEVICE_GENERATION;
2632 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002633 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002634 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002636 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002638 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002639 std::unique_ptr<InstrumentedInputReader> mReader;
2640 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
Chris Ye1b0c7342020-07-28 21:57:03 -07002642 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002643 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002645 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002646 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2647 mFakeListener);
2648 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 }
2650
Chris Yea52ade12020-08-27 16:49:20 -07002651 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002652
Chris Yea52ade12020-08-27 16:49:20 -07002653 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 mFakeListener.clear();
2655 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 }
2657
2658 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002659 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 }
2661
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002662 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002663 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002664 mReader->requestRefreshConfiguration(changes);
2665 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002666 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002667 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2668 }
2669
arthurhungdcef2dc2020-08-11 14:47:50 +08002670 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2671 const std::string& location, int32_t eventHubId,
2672 Flags<InputDeviceClass> classes) {
2673 InputDeviceIdentifier identifier;
2674 identifier.name = name;
2675 identifier.location = location;
2676 std::shared_ptr<InputDevice> device =
2677 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2678 identifier);
2679 mReader->pushNextDevice(device);
2680 mFakeEventHub->addDevice(eventHubId, name, classes);
2681 mReader->loopOnce();
2682 return device;
2683 }
2684
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002685 template <class T, typename... Args>
2686 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002687 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002688 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002690 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002691 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 }
2693
2694 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002695 int32_t orientation, const std::string& uniqueId,
2696 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002697 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2698 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002699 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2700 }
2701
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002702 void clearViewports() {
2703 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704 }
2705
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002706 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2707 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 RawEvent event;
2709 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002710 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002711 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712 event.type = type;
2713 event.code = code;
2714 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002715 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002716 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717 }
2718
2719 static void assertMotionRange(const InputDeviceInfo& info,
2720 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2721 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002722 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002723 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2724 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2725 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2726 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2727 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2728 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2729 }
2730
2731 static void assertPointerCoords(const PointerCoords& coords,
2732 float x, float y, float pressure, float size,
2733 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2734 float orientation, float distance) {
2735 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2736 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2737 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2738 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2739 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2740 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2741 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2742 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2743 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2744 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2745 }
2746
Michael Wright17db18e2020-06-26 20:51:44 +01002747 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002749 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002750 ASSERT_NEAR(x, actualX, 1);
2751 ASSERT_NEAR(y, actualY, 1);
2752 }
2753};
2754
2755const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002756const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002757const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2759const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002760const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2761 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002762const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
2764// --- SwitchInputMapperTest ---
2765
2766class SwitchInputMapperTest : public InputMapperTest {
2767protected:
2768};
2769
2770TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002771 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002773 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774}
2775
2776TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002777 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002779 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002780 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002782 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002783 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784}
2785
2786TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
2794 NotifySwitchArgs args;
2795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2796 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002797 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2798 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 args.switchMask);
2800 ASSERT_EQ(uint32_t(0), args.policyFlags);
2801}
2802
Chris Ye87143712020-11-10 05:05:58 +00002803// --- VibratorInputMapperTest ---
2804class VibratorInputMapperTest : public InputMapperTest {
2805protected:
2806 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2807};
2808
2809TEST_F(VibratorInputMapperTest, GetSources) {
2810 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2811
2812 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2813}
2814
2815TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2816 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2817
2818 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2819}
2820
2821TEST_F(VibratorInputMapperTest, Vibrate) {
2822 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002823 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002824 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2825
2826 VibrationElement pattern(2);
2827 VibrationSequence sequence(2);
2828 pattern.duration = std::chrono::milliseconds(200);
2829 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2830 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2831 sequence.addElement(pattern);
2832 pattern.duration = std::chrono::milliseconds(500);
2833 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2834 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2835 sequence.addElement(pattern);
2836
2837 std::vector<int64_t> timings = {0, 1};
2838 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2839
2840 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002841 // Start vibrating
2842 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002843 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002844 // Verify vibrator state listener was notified.
2845 mReader->loopOnce();
2846 NotifyVibratorStateArgs args;
2847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2848 ASSERT_EQ(DEVICE_ID, args.deviceId);
2849 ASSERT_TRUE(args.isOn);
2850 // Stop vibrating
2851 mapper.cancelVibrate(VIBRATION_TOKEN);
2852 ASSERT_FALSE(mapper.isVibrating());
2853 // Verify vibrator state listener was notified.
2854 mReader->loopOnce();
2855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2856 ASSERT_EQ(DEVICE_ID, args.deviceId);
2857 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002858}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859
Chris Yef59a2f42020-10-16 12:55:26 -07002860// --- SensorInputMapperTest ---
2861
2862class SensorInputMapperTest : public InputMapperTest {
2863protected:
2864 static const int32_t ACCEL_RAW_MIN;
2865 static const int32_t ACCEL_RAW_MAX;
2866 static const int32_t ACCEL_RAW_FUZZ;
2867 static const int32_t ACCEL_RAW_FLAT;
2868 static const int32_t ACCEL_RAW_RESOLUTION;
2869
2870 static const int32_t GYRO_RAW_MIN;
2871 static const int32_t GYRO_RAW_MAX;
2872 static const int32_t GYRO_RAW_FUZZ;
2873 static const int32_t GYRO_RAW_FLAT;
2874 static const int32_t GYRO_RAW_RESOLUTION;
2875
2876 static const float GRAVITY_MS2_UNIT;
2877 static const float DEGREE_RADIAN_UNIT;
2878
2879 void prepareAccelAxes();
2880 void prepareGyroAxes();
2881 void setAccelProperties();
2882 void setGyroProperties();
2883 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2884};
2885
2886const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2887const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2888const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2889const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2890const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2891
2892const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2893const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2894const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2895const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2896const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2897
2898const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2899const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2900
2901void SensorInputMapperTest::prepareAccelAxes() {
2902 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2903 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2904 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2905 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2907 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2908}
2909
2910void SensorInputMapperTest::prepareGyroAxes() {
2911 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2912 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2913 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2914 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2915 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2916 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2917}
2918
2919void SensorInputMapperTest::setAccelProperties() {
2920 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2921 /* sensorDataIndex */ 0);
2922 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2923 /* sensorDataIndex */ 1);
2924 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2925 /* sensorDataIndex */ 2);
2926 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2927 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2928 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2929 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2930 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2931}
2932
2933void SensorInputMapperTest::setGyroProperties() {
2934 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2935 /* sensorDataIndex */ 0);
2936 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2937 /* sensorDataIndex */ 1);
2938 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2939 /* sensorDataIndex */ 2);
2940 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2941 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2942 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2943 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2944 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2945}
2946
2947TEST_F(SensorInputMapperTest, GetSources) {
2948 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2949
2950 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2951}
2952
2953TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2954 setAccelProperties();
2955 prepareAccelAxes();
2956 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2957
2958 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2959 std::chrono::microseconds(10000),
2960 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002961 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002967
2968 NotifySensorArgs args;
2969 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2970 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2971 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2972
2973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2974 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2975 ASSERT_EQ(args.deviceId, DEVICE_ID);
2976 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2977 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2978 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2979 ASSERT_EQ(args.values, values);
2980 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2981}
2982
2983TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2984 setGyroProperties();
2985 prepareGyroAxes();
2986 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2987
2988 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2989 std::chrono::microseconds(10000),
2990 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002991 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002997
2998 NotifySensorArgs args;
2999 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3000 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3001 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3002
3003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3004 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3005 ASSERT_EQ(args.deviceId, DEVICE_ID);
3006 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3007 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3008 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3009 ASSERT_EQ(args.values, values);
3010 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3011}
3012
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013// --- KeyboardInputMapperTest ---
3014
3015class KeyboardInputMapperTest : public InputMapperTest {
3016protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003017 const std::string UNIQUE_ID = "local:0";
3018
3019 void prepareDisplay(int32_t orientation);
3020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003021 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003022 int32_t originalKeyCode, int32_t rotatedKeyCode,
3023 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024};
3025
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003026/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3027 * orientation.
3028 */
3029void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003030 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3031 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003032}
3033
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003034void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003035 int32_t originalScanCode, int32_t originalKeyCode,
3036 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 NotifyKeyArgs args;
3038
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3041 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3042 ASSERT_EQ(originalScanCode, args.scanCode);
3043 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003044 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3048 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3049 ASSERT_EQ(originalScanCode, args.scanCode);
3050 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003051 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052}
3053
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003055 KeyboardInputMapper& mapper =
3056 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3057 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060}
3061
3062TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3063 const int32_t USAGE_A = 0x070004;
3064 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003065 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3066 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003067 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3068 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3069 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003071 KeyboardInputMapper& mapper =
3072 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3073 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003074 // Initial metastate to AMETA_NONE.
3075 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3076 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
3078 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 NotifyKeyArgs args;
3081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3082 ASSERT_EQ(DEVICE_ID, args.deviceId);
3083 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3084 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3085 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3086 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3087 ASSERT_EQ(KEY_HOME, args.scanCode);
3088 ASSERT_EQ(AMETA_NONE, args.metaState);
3089 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3090 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3091 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3092
3093 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003094 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3096 ASSERT_EQ(DEVICE_ID, args.deviceId);
3097 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3098 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3099 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3100 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3101 ASSERT_EQ(KEY_HOME, args.scanCode);
3102 ASSERT_EQ(AMETA_NONE, args.metaState);
3103 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3104 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3105 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3106
3107 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3111 ASSERT_EQ(DEVICE_ID, args.deviceId);
3112 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3113 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3115 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3116 ASSERT_EQ(0, args.scanCode);
3117 ASSERT_EQ(AMETA_NONE, args.metaState);
3118 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3119 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3120 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3121
3122 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3124 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3126 ASSERT_EQ(DEVICE_ID, args.deviceId);
3127 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3128 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3129 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3130 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3131 ASSERT_EQ(0, args.scanCode);
3132 ASSERT_EQ(AMETA_NONE, args.metaState);
3133 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3134 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3135 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3136
3137 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3141 ASSERT_EQ(DEVICE_ID, args.deviceId);
3142 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3143 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3144 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3145 ASSERT_EQ(0, args.keyCode);
3146 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3147 ASSERT_EQ(AMETA_NONE, args.metaState);
3148 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3149 ASSERT_EQ(0U, args.policyFlags);
3150 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3151
3152 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3154 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3156 ASSERT_EQ(DEVICE_ID, args.deviceId);
3157 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3158 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3159 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3160 ASSERT_EQ(0, args.keyCode);
3161 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3162 ASSERT_EQ(AMETA_NONE, args.metaState);
3163 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3164 ASSERT_EQ(0U, args.policyFlags);
3165 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3166}
3167
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003168/**
3169 * Ensure that the readTime is set to the time when the EV_KEY is received.
3170 */
3171TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3172 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3173
3174 KeyboardInputMapper& mapper =
3175 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3176 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3177 NotifyKeyArgs args;
3178
3179 // Key down
3180 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3182 ASSERT_EQ(12, args.readTime);
3183
3184 // Key up
3185 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3187 ASSERT_EQ(15, args.readTime);
3188}
3189
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003191 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003193 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3194 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3195 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003197 KeyboardInputMapper& mapper =
3198 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3199 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200
arthurhungc903df12020-08-11 15:08:42 +08003201 // Initial metastate to AMETA_NONE.
3202 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3203 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204
3205 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 NotifyKeyArgs args;
3208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003210 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003211 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212
3213 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003214 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3216 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218
3219 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003220 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224
3225 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003226 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3228 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003229 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003230 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231}
3232
3233TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003234 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3235 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3236 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3237 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003239 KeyboardInputMapper& mapper =
3240 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3241 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003243 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3245 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3246 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3247 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3248 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3249 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3250 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3251 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3252}
3253
3254TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003255 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3256 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3257 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3258 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003261 KeyboardInputMapper& mapper =
3262 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3263 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003265 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003266 ASSERT_NO_FATAL_FAILURE(
3267 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3268 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3269 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3270 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3271 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3272 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3273 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003275 clearViewports();
3276 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003277 ASSERT_NO_FATAL_FAILURE(
3278 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3279 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3280 AKEYCODE_DPAD_UP, DISPLAY_ID));
3281 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3282 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3283 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3284 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003286 clearViewports();
3287 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003288 ASSERT_NO_FATAL_FAILURE(
3289 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3290 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3291 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3292 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3293 AKEYCODE_DPAD_UP, DISPLAY_ID));
3294 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3295 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003297 clearViewports();
3298 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003299 ASSERT_NO_FATAL_FAILURE(
3300 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3302 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3303 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3304 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3305 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3306 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307
3308 // Special case: if orientation changes while key is down, we still emit the same keycode
3309 // in the key up as we did in the key down.
3310 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003311 clearViewports();
3312 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3316 ASSERT_EQ(KEY_UP, args.scanCode);
3317 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3318
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003319 clearViewports();
3320 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3323 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3324 ASSERT_EQ(KEY_UP, args.scanCode);
3325 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3326}
3327
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003328TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3329 // If the keyboard is not orientation aware,
3330 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003331 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003332
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 KeyboardInputMapper& mapper =
3334 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3335 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003336 NotifyKeyArgs args;
3337
3338 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3343 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3344
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003345 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3350 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3351}
3352
3353TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3354 // If the keyboard is orientation aware,
3355 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003356 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003357
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003358 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 KeyboardInputMapper& mapper =
3360 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3361 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003362 NotifyKeyArgs args;
3363
3364 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3365 // ^--- already checked by the previous test
3366
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003367 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003368 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3373 ASSERT_EQ(DISPLAY_ID, args.displayId);
3374
3375 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003376 clearViewports();
3377 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003378 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(newDisplayId, args.displayId);
3384}
3385
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003387 KeyboardInputMapper& mapper =
3388 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3389 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003391 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003392 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003394 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003395 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396}
3397
3398TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003399 KeyboardInputMapper& mapper =
3400 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3401 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003403 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003404 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003406 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003407 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408}
3409
3410TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 KeyboardInputMapper& mapper =
3412 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3413 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003415 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416
3417 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3418 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 ASSERT_TRUE(flags[0]);
3421 ASSERT_FALSE(flags[1]);
3422}
3423
3424TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003425 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3426 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3427 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3430 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003432 KeyboardInputMapper& mapper =
3433 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3434 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003435 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003436 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3437 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438
3439 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003440 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3441 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3442 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443
3444 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003447 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3448 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3449 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
3452 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003455 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3456 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3457 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003458 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459
3460 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003463 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3464 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3465 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003466 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467
3468 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003471 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3472 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3473 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003474 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475
3476 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003479 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3480 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3481 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
3484 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003487 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3488 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3489 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491}
3492
Chris Yea52ade12020-08-27 16:49:20 -07003493TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3494 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3496 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3497 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3498
3499 KeyboardInputMapper& mapper =
3500 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3501 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3502
3503 // Initial metastate should be AMETA_NONE as no meta keys added.
3504 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3505 // Meta state should be AMETA_NONE after reset
3506 mapper.reset(ARBITRARY_TIME);
3507 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3508 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3509 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3510 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3511
3512 NotifyKeyArgs args;
3513 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3516 ASSERT_EQ(AMETA_NONE, args.metaState);
3517 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3518 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3519 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3520
3521 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003522 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3524 ASSERT_EQ(AMETA_NONE, args.metaState);
3525 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3526 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3527 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3528}
3529
Arthur Hung2c9a3342019-07-23 14:18:59 +08003530TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3531 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003532 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3533 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003536
3537 // keyboard 2.
3538 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003539 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003540 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003541 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003542 std::shared_ptr<InputDevice> device2 =
3543 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3544 Flags<InputDeviceClass>(0));
3545
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003546 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3547 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3548 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3549 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003550
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003551 KeyboardInputMapper& mapper =
3552 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3553 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003556 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003557 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003558 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3559 device2->reset(ARBITRARY_TIME);
3560
3561 // Prepared displays and associated info.
3562 constexpr uint8_t hdmi1 = 0;
3563 constexpr uint8_t hdmi2 = 1;
3564 const std::string SECONDARY_UNIQUE_ID = "local:1";
3565
3566 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3567 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3568
3569 // No associated display viewport found, should disable the device.
3570 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3571 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3572 ASSERT_FALSE(device2->isEnabled());
3573
3574 // Prepare second display.
3575 constexpr int32_t newDisplayId = 2;
3576 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003577 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003578 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003579 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003580 // Default device will reconfigure above, need additional reconfiguration for another device.
3581 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3582 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3583
3584 // Device should be enabled after the associated display is found.
3585 ASSERT_TRUE(mDevice->isEnabled());
3586 ASSERT_TRUE(device2->isEnabled());
3587
3588 // Test pad key events
3589 ASSERT_NO_FATAL_FAILURE(
3590 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3591 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3592 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3594 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3595 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3596 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3597
3598 ASSERT_NO_FATAL_FAILURE(
3599 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3601 AKEYCODE_DPAD_RIGHT, newDisplayId));
3602 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3603 AKEYCODE_DPAD_DOWN, newDisplayId));
3604 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3605 AKEYCODE_DPAD_LEFT, newDisplayId));
3606}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607
arthurhungc903df12020-08-11 15:08:42 +08003608TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3609 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3610 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3611 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3615
3616 KeyboardInputMapper& mapper =
3617 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3618 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3619 // Initial metastate to AMETA_NONE.
3620 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3621 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3622
3623 // Initialization should have turned all of the lights off.
3624 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3625 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3626 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3627
3628 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003631 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3632 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3633
3634 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003637 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3638 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3639
3640 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003643 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3644 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3645
3646 mFakeEventHub->removeDevice(EVENTHUB_ID);
3647 mReader->loopOnce();
3648
3649 // keyboard 2 should default toggle keys.
3650 const std::string USB2 = "USB2";
3651 const std::string DEVICE_NAME2 = "KEYBOARD2";
3652 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3653 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3654 std::shared_ptr<InputDevice> device2 =
3655 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3656 Flags<InputDeviceClass>(0));
3657 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3658 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3659 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3660 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3661 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3662 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3663
arthurhung6fe95782020-10-05 22:41:16 +08003664 KeyboardInputMapper& mapper2 =
3665 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3666 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003667 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3668 device2->reset(ARBITRARY_TIME);
3669
3670 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3671 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3672 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003673 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3674 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003675}
3676
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003677// --- KeyboardInputMapperTest_ExternalDevice ---
3678
3679class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3680protected:
Chris Yea52ade12020-08-27 16:49:20 -07003681 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003682};
3683
3684TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003685 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3686 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003687
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003688 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3689 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3691 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003692
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003693 KeyboardInputMapper& mapper =
3694 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3695 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003696
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003698 NotifyKeyArgs args;
3699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3700 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3701
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003702 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3704 ASSERT_EQ(uint32_t(0), args.policyFlags);
3705
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3708 ASSERT_EQ(uint32_t(0), args.policyFlags);
3709
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003710 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3712 ASSERT_EQ(uint32_t(0), args.policyFlags);
3713
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3716 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3717
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003718 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3720 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3721}
3722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003723TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003724 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003725
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003726 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3727 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3728 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003729
Powei Fengd041c5d2019-05-03 17:11:33 -07003730 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003731 KeyboardInputMapper& mapper =
3732 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3733 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003734
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003736 NotifyKeyArgs args;
3737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3738 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3739
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003740 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3742 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3743
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
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 + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3750 ASSERT_EQ(uint32_t(0), args.policyFlags);
3751
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3754 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3755
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003756 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3758 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3759}
3760
Michael Wrightd02c5b62014-02-10 15:10:22 -08003761// --- CursorInputMapperTest ---
3762
3763class CursorInputMapperTest : public InputMapperTest {
3764protected:
3765 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3766
Michael Wright17db18e2020-06-26 20:51:44 +01003767 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768
Chris Yea52ade12020-08-27 16:49:20 -07003769 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770 InputMapperTest::SetUp();
3771
Michael Wright17db18e2020-06-26 20:51:44 +01003772 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003773 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774 }
3775
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3777 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003778
3779 void prepareDisplay(int32_t orientation) {
3780 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003781 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003782 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3783 orientation, uniqueId, NO_PORT, viewportType);
3784 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785};
3786
3787const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3790 int32_t originalY, int32_t rotatedX,
3791 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792 NotifyMotionArgs args;
3793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3800 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3801 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3802 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3803}
3804
3805TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003809 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810}
3811
3812TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003814 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003816 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817}
3818
3819TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003820 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003821 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822
3823 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003824 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003825
3826 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003827 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3828 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3830 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3831
3832 // When the bounds are set, then there should be a valid motion range.
3833 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3834
3835 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837
3838 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3839 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3840 1, 800 - 1, 0.0f, 0.0f));
3841 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3842 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3843 2, 480 - 1, 0.0f, 0.0f));
3844 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3845 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3846 0.0f, 1.0f, 0.0f, 0.0f));
3847}
3848
3849TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003851 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852
3853 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003854 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855
3856 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3857 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3858 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3859 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3860 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3861 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3862 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3863 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3864 0.0f, 1.0f, 0.0f, 0.0f));
3865}
3866
3867TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870
arthurhungdcef2dc2020-08-11 14:47:50 +08003871 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872
3873 NotifyMotionArgs args;
3874
3875 // Button press.
3876 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3880 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3881 ASSERT_EQ(DEVICE_ID, args.deviceId);
3882 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3883 ASSERT_EQ(uint32_t(0), args.policyFlags);
3884 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3885 ASSERT_EQ(0, args.flags);
3886 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3887 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3888 ASSERT_EQ(0, args.edgeFlags);
3889 ASSERT_EQ(uint32_t(1), args.pointerCount);
3890 ASSERT_EQ(0, args.pointerProperties[0].id);
3891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3893 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3894 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3895 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3896 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3897
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3899 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3900 ASSERT_EQ(DEVICE_ID, args.deviceId);
3901 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3902 ASSERT_EQ(uint32_t(0), args.policyFlags);
3903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3904 ASSERT_EQ(0, args.flags);
3905 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3906 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3907 ASSERT_EQ(0, args.edgeFlags);
3908 ASSERT_EQ(uint32_t(1), args.pointerCount);
3909 ASSERT_EQ(0, args.pointerProperties[0].id);
3910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3912 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3913 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3914 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3915 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3916
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003918 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3919 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3921 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3922 ASSERT_EQ(DEVICE_ID, args.deviceId);
3923 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3924 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003925 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3926 ASSERT_EQ(0, args.flags);
3927 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3928 ASSERT_EQ(0, args.buttonState);
3929 ASSERT_EQ(0, args.edgeFlags);
3930 ASSERT_EQ(uint32_t(1), args.pointerCount);
3931 ASSERT_EQ(0, args.pointerProperties[0].id);
3932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3934 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3935 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3936 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3937 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3938
3939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3940 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3941 ASSERT_EQ(DEVICE_ID, args.deviceId);
3942 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3943 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3945 ASSERT_EQ(0, args.flags);
3946 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3947 ASSERT_EQ(0, args.buttonState);
3948 ASSERT_EQ(0, args.edgeFlags);
3949 ASSERT_EQ(uint32_t(1), args.pointerCount);
3950 ASSERT_EQ(0, args.pointerProperties[0].id);
3951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3953 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3954 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3955 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3956 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3957}
3958
3959TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003961 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962
3963 NotifyMotionArgs args;
3964
3965 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3971 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3972
3973 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3979 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3980}
3981
3982TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003984 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985
3986 NotifyMotionArgs args;
3987
3988 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3994 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3995
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3999 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4000
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4007 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4008
4009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4012 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4013}
4014
4015TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004017 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018
4019 NotifyMotionArgs args;
4020
4021 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4029 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4030 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4031
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);
4034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4035 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4036 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4037
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4045 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4046 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4047
4048 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004052 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4054 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4055
4056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4059 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4060}
4061
4062TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004064 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004066 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4071 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4072 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4073 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4074 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4075}
4076
4077TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 addConfigurationProperty("cursor.mode", "navigation");
4079 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004080 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004082 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4084 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4085 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4086 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4087 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4088 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4089 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4090 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4091
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004092 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4101
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004102 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4111
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004112 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4118 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4119 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4120 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4121}
4122
4123TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004125 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126
4127 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4128 mFakePointerController->setPosition(100, 200);
4129 mFakePointerController->setButtonState(0);
4130
4131 NotifyMotionArgs motionArgs;
4132 NotifyKeyArgs keyArgs;
4133
4134 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4138 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4139 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4140 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4142 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4143
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4145 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4146 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4147 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4149 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4150
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_EQ(0, motionArgs.buttonState);
4156 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4158 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4159
4160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004161 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_EQ(0, motionArgs.buttonState);
4163 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4165 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4166
4167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004169 ASSERT_EQ(0, motionArgs.buttonState);
4170 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4172 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4173
4174 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4179 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4180 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4181 motionArgs.buttonState);
4182 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4183 mFakePointerController->getButtonState());
4184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4185 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4186
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4189 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4190 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4191 mFakePointerController->getButtonState());
4192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4193 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4194
4195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4196 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4197 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4198 motionArgs.buttonState);
4199 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4200 mFakePointerController->getButtonState());
4201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4202 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4203
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4209 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4211 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4212
4213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004215 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4216 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4218 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4219
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4224 ASSERT_EQ(0, motionArgs.buttonState);
4225 ASSERT_EQ(0, mFakePointerController->getButtonState());
4226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4227 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004230
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_EQ(0, motionArgs.buttonState);
4233 ASSERT_EQ(0, mFakePointerController->getButtonState());
4234 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4236 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004237
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4239 ASSERT_EQ(0, motionArgs.buttonState);
4240 ASSERT_EQ(0, mFakePointerController->getButtonState());
4241 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4243 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4244
4245 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4249 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4250 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004251
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004253 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4255 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4257 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4258
4259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4261 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4262 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4264 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4265
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004269 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 ASSERT_EQ(0, motionArgs.buttonState);
4271 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4273 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4274
4275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004277 ASSERT_EQ(0, motionArgs.buttonState);
4278 ASSERT_EQ(0, mFakePointerController->getButtonState());
4279
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4281 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4283 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4284 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4285
4286 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4290 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4291 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004292
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004294 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4296 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4298 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4299
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4302 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4305 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4306
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 ASSERT_EQ(0, motionArgs.buttonState);
4312 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4314 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004315
4316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4318 ASSERT_EQ(0, motionArgs.buttonState);
4319 ASSERT_EQ(0, mFakePointerController->getButtonState());
4320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4321 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4322
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4324 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4325 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4326
4327 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4331 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4332 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4337 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4339 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4340
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4343 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4344 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4346 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4347
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_EQ(0, motionArgs.buttonState);
4353 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4355 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356
4357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4359 ASSERT_EQ(0, motionArgs.buttonState);
4360 ASSERT_EQ(0, mFakePointerController->getButtonState());
4361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4362 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4363
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4365 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4366 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4367
4368 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4372 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4373 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4378 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4380 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4381
4382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4384 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4385 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4387 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4388
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_EQ(0, motionArgs.buttonState);
4394 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4396 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4400 ASSERT_EQ(0, motionArgs.buttonState);
4401 ASSERT_EQ(0, mFakePointerController->getButtonState());
4402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4403 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4404
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4406 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4407 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4408}
4409
4410TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004412 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413
4414 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4415 mFakePointerController->setPosition(100, 200);
4416 mFakePointerController->setButtonState(0);
4417
4418 NotifyMotionArgs args;
4419
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004424 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4427 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 +01004428 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004429}
4430
4431TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004432 addConfigurationProperty("cursor.mode", "pointer");
4433 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004434 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004435
4436 NotifyDeviceResetArgs resetArgs;
4437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4438 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4439 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4440
4441 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4442 mFakePointerController->setPosition(100, 200);
4443 mFakePointerController->setButtonState(0);
4444
4445 NotifyMotionArgs args;
4446
4447 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4452 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4455 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 +01004456 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004457
4458 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4462 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4463 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4465 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4467 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4470 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4471
4472 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004473 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4474 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4476 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4479 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4481 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4482 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4484 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4485
4486 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4491 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4494 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 +01004495 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004496
4497 // Disable pointer capture and check that the device generation got bumped
4498 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004499 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004500 mFakePolicy->setPointerCapture(false);
4501 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004502 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004503
4504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4505 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4506 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4507
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4512 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4515 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 +01004516 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517}
4518
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004519TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004520 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004521
Garfield Tan888a6a42020-01-09 11:39:16 -08004522 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004523 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004524 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4525 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004526 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4527 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004528 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4529 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4530
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004531 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4532 mFakePointerController->setPosition(100, 200);
4533 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004534
4535 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4540 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4543 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004544 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004545 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4546}
4547
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548// --- TouchInputMapperTest ---
4549
4550class TouchInputMapperTest : public InputMapperTest {
4551protected:
4552 static const int32_t RAW_X_MIN;
4553 static const int32_t RAW_X_MAX;
4554 static const int32_t RAW_Y_MIN;
4555 static const int32_t RAW_Y_MAX;
4556 static const int32_t RAW_TOUCH_MIN;
4557 static const int32_t RAW_TOUCH_MAX;
4558 static const int32_t RAW_TOOL_MIN;
4559 static const int32_t RAW_TOOL_MAX;
4560 static const int32_t RAW_PRESSURE_MIN;
4561 static const int32_t RAW_PRESSURE_MAX;
4562 static const int32_t RAW_ORIENTATION_MIN;
4563 static const int32_t RAW_ORIENTATION_MAX;
4564 static const int32_t RAW_DISTANCE_MIN;
4565 static const int32_t RAW_DISTANCE_MAX;
4566 static const int32_t RAW_TILT_MIN;
4567 static const int32_t RAW_TILT_MAX;
4568 static const int32_t RAW_ID_MIN;
4569 static const int32_t RAW_ID_MAX;
4570 static const int32_t RAW_SLOT_MIN;
4571 static const int32_t RAW_SLOT_MAX;
4572 static const float X_PRECISION;
4573 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004574 static const float X_PRECISION_VIRTUAL;
4575 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576
4577 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004578 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579
4580 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4581
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004582 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004583 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004584
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 enum Axes {
4586 POSITION = 1 << 0,
4587 TOUCH = 1 << 1,
4588 TOOL = 1 << 2,
4589 PRESSURE = 1 << 3,
4590 ORIENTATION = 1 << 4,
4591 MINOR = 1 << 5,
4592 ID = 1 << 6,
4593 DISTANCE = 1 << 7,
4594 TILT = 1 << 8,
4595 SLOT = 1 << 9,
4596 TOOL_TYPE = 1 << 10,
4597 };
4598
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004599 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4600 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004601 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004603 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 int32_t toRawX(float displayX);
4605 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004606 float toCookedX(float rawX, float rawY);
4607 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004609 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004611 float toDisplayY(int32_t rawY, int32_t displayHeight);
4612
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613};
4614
4615const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4616const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4617const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4618const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4619const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4620const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4621const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4622const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004623const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4624const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4626const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4627const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4628const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4629const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4630const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4631const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4632const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4633const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4634const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4635const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4636const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004637const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4638 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4639const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4640 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004641const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4642 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643
4644const float TouchInputMapperTest::GEOMETRIC_SCALE =
4645 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4646 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4647
4648const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4649 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4650 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4651};
4652
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004653void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004654 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4655 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004656}
4657
4658void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4659 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4660 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661}
4662
Santos Cordonfa5cf462017-04-05 10:37:00 -07004663void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004664 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4665 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4666 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004667}
4668
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004670 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4671 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4672 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4673 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674}
4675
Jason Gerecke489fda82012-09-07 17:19:40 -07004676void TouchInputMapperTest::prepareLocationCalibration() {
4677 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4678}
4679
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680int32_t TouchInputMapperTest::toRawX(float displayX) {
4681 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4682}
4683
4684int32_t TouchInputMapperTest::toRawY(float displayY) {
4685 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4686}
4687
Jason Gerecke489fda82012-09-07 17:19:40 -07004688float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4689 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4690 return rawX;
4691}
4692
4693float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4694 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4695 return rawY;
4696}
4697
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004699 return toDisplayX(rawX, DISPLAY_WIDTH);
4700}
4701
4702float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4703 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704}
4705
4706float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004707 return toDisplayY(rawY, DISPLAY_HEIGHT);
4708}
4709
4710float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4711 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712}
4713
4714
4715// --- SingleTouchInputMapperTest ---
4716
4717class SingleTouchInputMapperTest : public TouchInputMapperTest {
4718protected:
4719 void prepareButtons();
4720 void prepareAxes(int axes);
4721
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004722 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4723 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4724 void processUp(SingleTouchInputMapper& mappery);
4725 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4726 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4727 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4728 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4729 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4730 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731};
4732
4733void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004734 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735}
4736
4737void SingleTouchInputMapperTest::prepareAxes(int axes) {
4738 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004739 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4740 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 }
4742 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004743 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4744 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 }
4746 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004747 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4748 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 }
4750 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004751 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4752 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753 }
4754 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4756 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 }
4758}
4759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004760void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764}
4765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004766void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769}
4770
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004771void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773}
4774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004775void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777}
4778
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004779void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4780 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782}
4783
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004784void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786}
4787
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004788void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4789 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792}
4793
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004794void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4795 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797}
4798
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004799void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801}
4802
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 prepareButtons();
4805 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004806 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004808 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809}
4810
4811TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004812 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4813 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 prepareButtons();
4815 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004816 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004818 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819}
4820
4821TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 prepareButtons();
4823 prepareAxes(POSITION);
4824 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004825 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004827 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828}
4829
4830TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 prepareButtons();
4832 prepareAxes(POSITION);
4833 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004834 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004836 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837}
4838
4839TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 addConfigurationProperty("touch.deviceType", "touchScreen");
4841 prepareDisplay(DISPLAY_ORIENTATION_0);
4842 prepareButtons();
4843 prepareAxes(POSITION);
4844 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004845 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846
4847 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004848 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849
4850 // Virtual key is down.
4851 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4852 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4853 processDown(mapper, x, y);
4854 processSync(mapper);
4855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004857 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858
4859 // Virtual key is up.
4860 processUp(mapper);
4861 processSync(mapper);
4862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004864 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865}
4866
4867TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 addConfigurationProperty("touch.deviceType", "touchScreen");
4869 prepareDisplay(DISPLAY_ORIENTATION_0);
4870 prepareButtons();
4871 prepareAxes(POSITION);
4872 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004873 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874
4875 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877
4878 // Virtual key is down.
4879 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4880 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4881 processDown(mapper, x, y);
4882 processSync(mapper);
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886
4887 // Virtual key is up.
4888 processUp(mapper);
4889 processSync(mapper);
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893}
4894
4895TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 addConfigurationProperty("touch.deviceType", "touchScreen");
4897 prepareDisplay(DISPLAY_ORIENTATION_0);
4898 prepareButtons();
4899 prepareAxes(POSITION);
4900 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004901 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902
4903 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4904 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004905 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 ASSERT_TRUE(flags[0]);
4907 ASSERT_FALSE(flags[1]);
4908}
4909
4910TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 addConfigurationProperty("touch.deviceType", "touchScreen");
4912 prepareDisplay(DISPLAY_ORIENTATION_0);
4913 prepareButtons();
4914 prepareAxes(POSITION);
4915 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004916 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917
arthurhungdcef2dc2020-08-11 14:47:50 +08004918 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
4920 NotifyKeyArgs args;
4921
4922 // Press virtual key.
4923 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4924 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4925 processDown(mapper, x, y);
4926 processSync(mapper);
4927
4928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4929 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4930 ASSERT_EQ(DEVICE_ID, args.deviceId);
4931 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4932 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4933 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4934 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4935 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4936 ASSERT_EQ(KEY_HOME, args.scanCode);
4937 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4938 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4939
4940 // Release virtual key.
4941 processUp(mapper);
4942 processSync(mapper);
4943
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4945 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4946 ASSERT_EQ(DEVICE_ID, args.deviceId);
4947 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4948 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4949 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4950 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4951 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4952 ASSERT_EQ(KEY_HOME, args.scanCode);
4953 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4954 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4955
4956 // Should not have sent any motions.
4957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4958}
4959
4960TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 addConfigurationProperty("touch.deviceType", "touchScreen");
4962 prepareDisplay(DISPLAY_ORIENTATION_0);
4963 prepareButtons();
4964 prepareAxes(POSITION);
4965 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004966 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967
arthurhungdcef2dc2020-08-11 14:47:50 +08004968 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969
4970 NotifyKeyArgs keyArgs;
4971
4972 // Press virtual key.
4973 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4974 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4975 processDown(mapper, x, y);
4976 processSync(mapper);
4977
4978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4979 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4980 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4981 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4982 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4983 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4984 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4985 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4986 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4987 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4988 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4989
4990 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4991 // into the display area.
4992 y -= 100;
4993 processMove(mapper, x, y);
4994 processSync(mapper);
4995
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4997 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4998 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4999 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5000 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5001 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5002 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5003 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5004 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5005 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5006 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5007 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5008
5009 NotifyMotionArgs motionArgs;
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5011 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5012 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5013 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5014 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5015 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5016 ASSERT_EQ(0, motionArgs.flags);
5017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5018 ASSERT_EQ(0, motionArgs.buttonState);
5019 ASSERT_EQ(0, motionArgs.edgeFlags);
5020 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5024 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5025 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5026 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5028
5029 // Keep moving out of bounds. Should generate a pointer move.
5030 y -= 50;
5031 processMove(mapper, x, y);
5032 processSync(mapper);
5033
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5035 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5036 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5037 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5038 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5040 ASSERT_EQ(0, motionArgs.flags);
5041 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5042 ASSERT_EQ(0, motionArgs.buttonState);
5043 ASSERT_EQ(0, motionArgs.edgeFlags);
5044 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5045 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5048 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5049 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5050 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5051 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5052
5053 // Release out of bounds. Should generate a pointer up.
5054 processUp(mapper);
5055 processSync(mapper);
5056
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5059 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5060 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5061 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5063 ASSERT_EQ(0, motionArgs.flags);
5064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5065 ASSERT_EQ(0, motionArgs.buttonState);
5066 ASSERT_EQ(0, motionArgs.edgeFlags);
5067 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5068 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5071 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5072 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5073 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5074 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5075
5076 // Should not have sent any more keys or motions.
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5079}
5080
5081TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 addConfigurationProperty("touch.deviceType", "touchScreen");
5083 prepareDisplay(DISPLAY_ORIENTATION_0);
5084 prepareButtons();
5085 prepareAxes(POSITION);
5086 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005087 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088
arthurhungdcef2dc2020-08-11 14:47:50 +08005089 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090
5091 NotifyMotionArgs motionArgs;
5092
5093 // Initially go down out of bounds.
5094 int32_t x = -10;
5095 int32_t y = -10;
5096 processDown(mapper, x, y);
5097 processSync(mapper);
5098
5099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5100
5101 // Move into the display area. Should generate a pointer down.
5102 x = 50;
5103 y = 75;
5104 processMove(mapper, x, y);
5105 processSync(mapper);
5106
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5108 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5109 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5110 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5111 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5113 ASSERT_EQ(0, motionArgs.flags);
5114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5115 ASSERT_EQ(0, motionArgs.buttonState);
5116 ASSERT_EQ(0, motionArgs.edgeFlags);
5117 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5118 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5121 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5122 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5123 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5125
5126 // Release. Should generate a pointer up.
5127 processUp(mapper);
5128 processSync(mapper);
5129
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5131 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5132 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5133 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5134 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5135 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5136 ASSERT_EQ(0, motionArgs.flags);
5137 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5138 ASSERT_EQ(0, motionArgs.buttonState);
5139 ASSERT_EQ(0, motionArgs.edgeFlags);
5140 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5141 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5144 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5145 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5146 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5147 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5148
5149 // Should not have sent any more keys or motions.
5150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5152}
5153
Santos Cordonfa5cf462017-04-05 10:37:00 -07005154TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005155 addConfigurationProperty("touch.deviceType", "touchScreen");
5156 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5157
5158 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5159 prepareButtons();
5160 prepareAxes(POSITION);
5161 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005162 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005163
arthurhungdcef2dc2020-08-11 14:47:50 +08005164 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005165
5166 NotifyMotionArgs motionArgs;
5167
5168 // Down.
5169 int32_t x = 100;
5170 int32_t y = 125;
5171 processDown(mapper, x, y);
5172 processSync(mapper);
5173
5174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5175 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5176 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5177 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5178 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5179 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5181 ASSERT_EQ(0, motionArgs.flags);
5182 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5183 ASSERT_EQ(0, motionArgs.buttonState);
5184 ASSERT_EQ(0, motionArgs.edgeFlags);
5185 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5186 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5189 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5190 1, 0, 0, 0, 0, 0, 0, 0));
5191 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5192 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5193 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5194
5195 // Move.
5196 x += 50;
5197 y += 75;
5198 processMove(mapper, x, y);
5199 processSync(mapper);
5200
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5202 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5203 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5204 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5205 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5206 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5208 ASSERT_EQ(0, motionArgs.flags);
5209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5210 ASSERT_EQ(0, motionArgs.buttonState);
5211 ASSERT_EQ(0, motionArgs.edgeFlags);
5212 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5213 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5216 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5217 1, 0, 0, 0, 0, 0, 0, 0));
5218 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5219 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5220 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5221
5222 // Up.
5223 processUp(mapper);
5224 processSync(mapper);
5225
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5227 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5228 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5229 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5231 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5232 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5233 ASSERT_EQ(0, motionArgs.flags);
5234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5235 ASSERT_EQ(0, motionArgs.buttonState);
5236 ASSERT_EQ(0, motionArgs.edgeFlags);
5237 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5241 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5242 1, 0, 0, 0, 0, 0, 0, 0));
5243 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5244 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5246
5247 // Should not have sent any more keys or motions.
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5250}
5251
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253 addConfigurationProperty("touch.deviceType", "touchScreen");
5254 prepareDisplay(DISPLAY_ORIENTATION_0);
5255 prepareButtons();
5256 prepareAxes(POSITION);
5257 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005258 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259
arthurhungdcef2dc2020-08-11 14:47:50 +08005260 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261
5262 NotifyMotionArgs motionArgs;
5263
5264 // Down.
5265 int32_t x = 100;
5266 int32_t y = 125;
5267 processDown(mapper, x, y);
5268 processSync(mapper);
5269
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5272 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5273 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5274 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5276 ASSERT_EQ(0, motionArgs.flags);
5277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5278 ASSERT_EQ(0, motionArgs.buttonState);
5279 ASSERT_EQ(0, motionArgs.edgeFlags);
5280 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5281 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5284 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5285 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5286 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5288
5289 // Move.
5290 x += 50;
5291 y += 75;
5292 processMove(mapper, x, y);
5293 processSync(mapper);
5294
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5301 ASSERT_EQ(0, motionArgs.flags);
5302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5303 ASSERT_EQ(0, motionArgs.buttonState);
5304 ASSERT_EQ(0, motionArgs.edgeFlags);
5305 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5306 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5309 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5310 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5311 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5313
5314 // Up.
5315 processUp(mapper);
5316 processSync(mapper);
5317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5320 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5321 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5322 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5323 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5324 ASSERT_EQ(0, motionArgs.flags);
5325 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5326 ASSERT_EQ(0, motionArgs.buttonState);
5327 ASSERT_EQ(0, motionArgs.edgeFlags);
5328 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5329 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5332 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5333 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5334 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5336
5337 // Should not have sent any more keys or motions.
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5340}
5341
5342TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343 addConfigurationProperty("touch.deviceType", "touchScreen");
5344 prepareButtons();
5345 prepareAxes(POSITION);
5346 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005347 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348
5349 NotifyMotionArgs args;
5350
5351 // Rotation 90.
5352 prepareDisplay(DISPLAY_ORIENTATION_90);
5353 processDown(mapper, toRawX(50), toRawY(75));
5354 processSync(mapper);
5355
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5357 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5358 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5359
5360 processUp(mapper);
5361 processSync(mapper);
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5363}
5364
5365TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366 addConfigurationProperty("touch.deviceType", "touchScreen");
5367 prepareButtons();
5368 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005369 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370
5371 NotifyMotionArgs args;
5372
5373 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005374 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375 prepareDisplay(DISPLAY_ORIENTATION_0);
5376 processDown(mapper, toRawX(50), toRawY(75));
5377 processSync(mapper);
5378
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5380 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5381 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5382
5383 processUp(mapper);
5384 processSync(mapper);
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5386
5387 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005388 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389 prepareDisplay(DISPLAY_ORIENTATION_90);
5390 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5391 processSync(mapper);
5392
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5394 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5395 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5396
5397 processUp(mapper);
5398 processSync(mapper);
5399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5400
5401 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005402 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403 prepareDisplay(DISPLAY_ORIENTATION_180);
5404 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5405 processSync(mapper);
5406
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5408 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5409 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5410
5411 processUp(mapper);
5412 processSync(mapper);
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5414
5415 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005416 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 prepareDisplay(DISPLAY_ORIENTATION_270);
5418 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5419 processSync(mapper);
5420
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5422 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5423 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5424
5425 processUp(mapper);
5426 processSync(mapper);
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5428}
5429
5430TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 addConfigurationProperty("touch.deviceType", "touchScreen");
5432 prepareDisplay(DISPLAY_ORIENTATION_0);
5433 prepareButtons();
5434 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005435 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436
5437 // These calculations are based on the input device calibration documentation.
5438 int32_t rawX = 100;
5439 int32_t rawY = 200;
5440 int32_t rawPressure = 10;
5441 int32_t rawToolMajor = 12;
5442 int32_t rawDistance = 2;
5443 int32_t rawTiltX = 30;
5444 int32_t rawTiltY = 110;
5445
5446 float x = toDisplayX(rawX);
5447 float y = toDisplayY(rawY);
5448 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5449 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5450 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5451 float distance = float(rawDistance);
5452
5453 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5454 float tiltScale = M_PI / 180;
5455 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5456 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5457 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5458 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5459
5460 processDown(mapper, rawX, rawY);
5461 processPressure(mapper, rawPressure);
5462 processToolMajor(mapper, rawToolMajor);
5463 processDistance(mapper, rawDistance);
5464 processTilt(mapper, rawTiltX, rawTiltY);
5465 processSync(mapper);
5466
5467 NotifyMotionArgs args;
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5470 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5471 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5472}
5473
Jason Gerecke489fda82012-09-07 17:19:40 -07005474TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005475 addConfigurationProperty("touch.deviceType", "touchScreen");
5476 prepareDisplay(DISPLAY_ORIENTATION_0);
5477 prepareLocationCalibration();
5478 prepareButtons();
5479 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005480 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005481
5482 int32_t rawX = 100;
5483 int32_t rawY = 200;
5484
5485 float x = toDisplayX(toCookedX(rawX, rawY));
5486 float y = toDisplayY(toCookedY(rawX, rawY));
5487
5488 processDown(mapper, rawX, rawY);
5489 processSync(mapper);
5490
5491 NotifyMotionArgs args;
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5494 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5495}
5496
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 addConfigurationProperty("touch.deviceType", "touchScreen");
5499 prepareDisplay(DISPLAY_ORIENTATION_0);
5500 prepareButtons();
5501 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005502 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503
5504 NotifyMotionArgs motionArgs;
5505 NotifyKeyArgs keyArgs;
5506
5507 processDown(mapper, 100, 200);
5508 processSync(mapper);
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5510 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5511 ASSERT_EQ(0, motionArgs.buttonState);
5512
5513 // press BTN_LEFT, release BTN_LEFT
5514 processKey(mapper, BTN_LEFT, 1);
5515 processSync(mapper);
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5518 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5519
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5521 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5522 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5523
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524 processKey(mapper, BTN_LEFT, 0);
5525 processSync(mapper);
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005527 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005529
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005532 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533
5534 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5535 processKey(mapper, BTN_RIGHT, 1);
5536 processKey(mapper, BTN_MIDDLE, 1);
5537 processSync(mapper);
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5540 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5541 motionArgs.buttonState);
5542
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5545 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5546
5547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5548 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5549 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5550 motionArgs.buttonState);
5551
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552 processKey(mapper, BTN_RIGHT, 0);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561
5562 processKey(mapper, BTN_MIDDLE, 0);
5563 processSync(mapper);
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005570 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571
5572 // press BTN_BACK, release BTN_BACK
5573 processKey(mapper, BTN_BACK, 1);
5574 processSync(mapper);
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5576 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5577 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005578
Michael Wrightd02c5b62014-02-10 15:10:22 -08005579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005581 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5585 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586
5587 processKey(mapper, BTN_BACK, 0);
5588 processSync(mapper);
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005590 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005592
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005595 ASSERT_EQ(0, motionArgs.buttonState);
5596
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5598 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5599 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5600
5601 // press BTN_SIDE, release BTN_SIDE
5602 processKey(mapper, BTN_SIDE, 1);
5603 processSync(mapper);
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5605 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5606 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005607
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005610 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5614 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005615
5616 processKey(mapper, BTN_SIDE, 0);
5617 processSync(mapper);
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005621
5622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005624 ASSERT_EQ(0, motionArgs.buttonState);
5625
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5627 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5628 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5629
5630 // press BTN_FORWARD, release BTN_FORWARD
5631 processKey(mapper, BTN_FORWARD, 1);
5632 processSync(mapper);
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5634 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5635 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005636
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005639 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5642 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5643 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005644
5645 processKey(mapper, BTN_FORWARD, 0);
5646 processSync(mapper);
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005650
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005653 ASSERT_EQ(0, motionArgs.buttonState);
5654
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5656 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5657 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5658
5659 // press BTN_EXTRA, release BTN_EXTRA
5660 processKey(mapper, BTN_EXTRA, 1);
5661 processSync(mapper);
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5663 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5664 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005665
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005668 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5669
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5672 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673
5674 processKey(mapper, BTN_EXTRA, 0);
5675 processSync(mapper);
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005677 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005679
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005682 ASSERT_EQ(0, motionArgs.buttonState);
5683
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5685 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5686 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5687
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5689
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690 // press BTN_STYLUS, release BTN_STYLUS
5691 processKey(mapper, BTN_STYLUS, 1);
5692 processSync(mapper);
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005695 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5696
5697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5699 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700
5701 processKey(mapper, BTN_STYLUS, 0);
5702 processSync(mapper);
5703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005704 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005709 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710
5711 // press BTN_STYLUS2, release BTN_STYLUS2
5712 processKey(mapper, BTN_STYLUS2, 1);
5713 processSync(mapper);
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005716 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5720 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721
5722 processKey(mapper, BTN_STYLUS2, 0);
5723 processSync(mapper);
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005727
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005730 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731
5732 // release touch
5733 processUp(mapper);
5734 processSync(mapper);
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5736 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5737 ASSERT_EQ(0, motionArgs.buttonState);
5738}
5739
5740TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741 addConfigurationProperty("touch.deviceType", "touchScreen");
5742 prepareDisplay(DISPLAY_ORIENTATION_0);
5743 prepareButtons();
5744 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005745 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746
5747 NotifyMotionArgs motionArgs;
5748
5749 // default tool type is finger
5750 processDown(mapper, 100, 200);
5751 processSync(mapper);
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5753 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5755
5756 // eraser
5757 processKey(mapper, BTN_TOOL_RUBBER, 1);
5758 processSync(mapper);
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5762
5763 // stylus
5764 processKey(mapper, BTN_TOOL_RUBBER, 0);
5765 processKey(mapper, BTN_TOOL_PEN, 1);
5766 processSync(mapper);
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5770
5771 // brush
5772 processKey(mapper, BTN_TOOL_PEN, 0);
5773 processKey(mapper, BTN_TOOL_BRUSH, 1);
5774 processSync(mapper);
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5778
5779 // pencil
5780 processKey(mapper, BTN_TOOL_BRUSH, 0);
5781 processKey(mapper, BTN_TOOL_PENCIL, 1);
5782 processSync(mapper);
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5786
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005787 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788 processKey(mapper, BTN_TOOL_PENCIL, 0);
5789 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5790 processSync(mapper);
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5794
5795 // mouse
5796 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5797 processKey(mapper, BTN_TOOL_MOUSE, 1);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5802
5803 // lens
5804 processKey(mapper, BTN_TOOL_MOUSE, 0);
5805 processKey(mapper, BTN_TOOL_LENS, 1);
5806 processSync(mapper);
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5810
5811 // double-tap
5812 processKey(mapper, BTN_TOOL_LENS, 0);
5813 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5814 processSync(mapper);
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5818
5819 // triple-tap
5820 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5821 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5822 processSync(mapper);
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5826
5827 // quad-tap
5828 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5829 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5830 processSync(mapper);
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5834
5835 // finger
5836 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5837 processKey(mapper, BTN_TOOL_FINGER, 1);
5838 processSync(mapper);
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842
5843 // stylus trumps finger
5844 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
5849
5850 // eraser trumps stylus
5851 processKey(mapper, BTN_TOOL_RUBBER, 1);
5852 processSync(mapper);
5853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5856
5857 // mouse trumps eraser
5858 processKey(mapper, BTN_TOOL_MOUSE, 1);
5859 processSync(mapper);
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5863
5864 // back to default tool type
5865 processKey(mapper, BTN_TOOL_MOUSE, 0);
5866 processKey(mapper, BTN_TOOL_RUBBER, 0);
5867 processKey(mapper, BTN_TOOL_PEN, 0);
5868 processKey(mapper, BTN_TOOL_FINGER, 0);
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
5875TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876 addConfigurationProperty("touch.deviceType", "touchScreen");
5877 prepareDisplay(DISPLAY_ORIENTATION_0);
5878 prepareButtons();
5879 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005880 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005881 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005882
5883 NotifyMotionArgs motionArgs;
5884
5885 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5886 processKey(mapper, BTN_TOOL_FINGER, 1);
5887 processMove(mapper, 100, 200);
5888 processSync(mapper);
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5890 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5892 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5893
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5895 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5897 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5898
5899 // move a little
5900 processMove(mapper, 150, 250);
5901 processSync(mapper);
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5905 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5906
5907 // down when BTN_TOUCH is pressed, pressure defaults to 1
5908 processKey(mapper, BTN_TOUCH, 1);
5909 processSync(mapper);
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5913 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5916 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5918 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5919
5920 // up when BTN_TOUCH is released, hover restored
5921 processKey(mapper, BTN_TOUCH, 0);
5922 processSync(mapper);
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5926 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5927
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5931 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5932
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 // exit hover when pointer goes away
5939 processKey(mapper, BTN_TOOL_FINGER, 0);
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
5947TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005948 addConfigurationProperty("touch.deviceType", "touchScreen");
5949 prepareDisplay(DISPLAY_ORIENTATION_0);
5950 prepareButtons();
5951 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005952 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953
5954 NotifyMotionArgs motionArgs;
5955
5956 // initially hovering because pressure is 0
5957 processDown(mapper, 100, 200);
5958 processPressure(mapper, 0);
5959 processSync(mapper);
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5961 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5963 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5964
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5968 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5969
5970 // move a little
5971 processMove(mapper, 150, 250);
5972 processSync(mapper);
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5974 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5976 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5977
5978 // down when pressure is non-zero
5979 processPressure(mapper, RAW_PRESSURE_MAX);
5980 processSync(mapper);
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5984 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5989 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5990
5991 // up when pressure becomes 0, hover restored
5992 processPressure(mapper, 0);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5998
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6002 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6003
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 // exit hover when pointer goes away
6010 processUp(mapper);
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
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018// --- MultiTouchInputMapperTest ---
6019
6020class MultiTouchInputMapperTest : public TouchInputMapperTest {
6021protected:
6022 void prepareAxes(int axes);
6023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006024 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6025 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6026 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6027 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6028 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6029 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6030 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6031 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6032 void processId(MultiTouchInputMapper& mapper, int32_t id);
6033 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6034 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6035 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6036 void processMTSync(MultiTouchInputMapper& mapper);
6037 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038};
6039
6040void MultiTouchInputMapperTest::prepareAxes(int axes) {
6041 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006042 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6043 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 }
6045 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006046 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6047 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006049 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6050 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051 }
6052 }
6053 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6055 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006057 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6058 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 }
6060 }
6061 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006062 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6063 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 }
6065 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006066 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6067 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068 }
6069 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6071 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072 }
6073 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006074 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6075 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 }
6077 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006078 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6079 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 }
6081 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006082 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083 }
6084}
6085
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006086void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6087 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090}
6091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006092void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6093 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095}
6096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006097void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6098 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100}
6101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006102void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104}
6105
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006106void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108}
6109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006110void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6111 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113}
6114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006115void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117}
6118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006119void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121}
6122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006123void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006125}
6126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006127void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129}
6130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006131void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133}
6134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006135void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6136 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138}
6139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006140void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142}
6143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006144void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146}
6147
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 addConfigurationProperty("touch.deviceType", "touchScreen");
6150 prepareDisplay(DISPLAY_ORIENTATION_0);
6151 prepareAxes(POSITION);
6152 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006153 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154
arthurhungdcef2dc2020-08-11 14:47:50 +08006155 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156
6157 NotifyMotionArgs motionArgs;
6158
6159 // Two fingers down at once.
6160 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6161 processPosition(mapper, x1, y1);
6162 processMTSync(mapper);
6163 processPosition(mapper, x2, y2);
6164 processMTSync(mapper);
6165 processSync(mapper);
6166
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6169 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6170 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6171 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6173 ASSERT_EQ(0, motionArgs.flags);
6174 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6175 ASSERT_EQ(0, motionArgs.buttonState);
6176 ASSERT_EQ(0, motionArgs.edgeFlags);
6177 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6178 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6181 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6182 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6183 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6188 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6189 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6190 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6191 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6192 motionArgs.action);
6193 ASSERT_EQ(0, motionArgs.flags);
6194 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6195 ASSERT_EQ(0, motionArgs.buttonState);
6196 ASSERT_EQ(0, motionArgs.edgeFlags);
6197 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6198 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6200 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6203 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6205 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6206 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6207 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6209
6210 // Move.
6211 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6212 processPosition(mapper, x1, y1);
6213 processMTSync(mapper);
6214 processPosition(mapper, x2, y2);
6215 processMTSync(mapper);
6216 processSync(mapper);
6217
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6220 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6221 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6222 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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 // First finger up.
6242 x2 += 15; y2 -= 20;
6243 processPosition(mapper, x2, y2);
6244 processMTSync(mapper);
6245 processSync(mapper);
6246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6249 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6250 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6251 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6253 motionArgs.action);
6254 ASSERT_EQ(0, motionArgs.flags);
6255 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6256 ASSERT_EQ(0, motionArgs.buttonState);
6257 ASSERT_EQ(0, motionArgs.edgeFlags);
6258 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6261 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6264 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6266 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6267 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6268 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6270
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6273 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6275 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6277 ASSERT_EQ(0, motionArgs.flags);
6278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6279 ASSERT_EQ(0, motionArgs.buttonState);
6280 ASSERT_EQ(0, motionArgs.edgeFlags);
6281 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6282 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6285 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6286 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6287 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6289
6290 // Move.
6291 x2 += 20; y2 -= 25;
6292 processPosition(mapper, x2, y2);
6293 processMTSync(mapper);
6294 processSync(mapper);
6295
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6298 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6300 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6302 ASSERT_EQ(0, motionArgs.flags);
6303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6304 ASSERT_EQ(0, motionArgs.buttonState);
6305 ASSERT_EQ(0, motionArgs.edgeFlags);
6306 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6307 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6314
6315 // New finger down.
6316 int32_t x3 = 700, y3 = 300;
6317 processPosition(mapper, x2, y2);
6318 processMTSync(mapper);
6319 processPosition(mapper, x3, y3);
6320 processMTSync(mapper);
6321 processSync(mapper);
6322
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6324 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6325 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6326 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6327 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6328 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6329 motionArgs.action);
6330 ASSERT_EQ(0, motionArgs.flags);
6331 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6332 ASSERT_EQ(0, motionArgs.buttonState);
6333 ASSERT_EQ(0, motionArgs.edgeFlags);
6334 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6335 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6337 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6340 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6342 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6343 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6344 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6345 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6346
6347 // Second finger up.
6348 x3 += 30; y3 -= 20;
6349 processPosition(mapper, x3, y3);
6350 processMTSync(mapper);
6351 processSync(mapper);
6352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6355 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6356 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6357 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6359 motionArgs.action);
6360 ASSERT_EQ(0, motionArgs.flags);
6361 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6362 ASSERT_EQ(0, motionArgs.buttonState);
6363 ASSERT_EQ(0, motionArgs.edgeFlags);
6364 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6365 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6367 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6370 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6372 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6373 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6374 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6376
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6379 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6380 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6381 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6383 ASSERT_EQ(0, motionArgs.flags);
6384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6385 ASSERT_EQ(0, motionArgs.buttonState);
6386 ASSERT_EQ(0, motionArgs.edgeFlags);
6387 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6388 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6391 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6392 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6393 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6395
6396 // Last finger up.
6397 processMTSync(mapper);
6398 processSync(mapper);
6399
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6402 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6403 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6404 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6406 ASSERT_EQ(0, motionArgs.flags);
6407 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6408 ASSERT_EQ(0, motionArgs.buttonState);
6409 ASSERT_EQ(0, motionArgs.edgeFlags);
6410 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6411 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6414 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6415 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6416 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6418
6419 // Should not have sent any more keys or motions.
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6422}
6423
6424TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 addConfigurationProperty("touch.deviceType", "touchScreen");
6426 prepareDisplay(DISPLAY_ORIENTATION_0);
6427 prepareAxes(POSITION | ID);
6428 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006429 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430
arthurhungdcef2dc2020-08-11 14:47:50 +08006431 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432
6433 NotifyMotionArgs motionArgs;
6434
6435 // Two fingers down at once.
6436 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6437 processPosition(mapper, x1, y1);
6438 processId(mapper, 1);
6439 processMTSync(mapper);
6440 processPosition(mapper, x2, y2);
6441 processId(mapper, 2);
6442 processMTSync(mapper);
6443 processSync(mapper);
6444
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6447 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6451 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6452
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6455 motionArgs.action);
6456 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6457 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6459 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6462 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6464 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6465
6466 // Move.
6467 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
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_MOVE, motionArgs.action);
6478 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6479 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6481 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6484 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6486 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6487
6488 // First finger up.
6489 x2 += 15; y2 -= 20;
6490 processPosition(mapper, x2, y2);
6491 processId(mapper, 2);
6492 processMTSync(mapper);
6493 processSync(mapper);
6494
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6497 motionArgs.action);
6498 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6499 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6501 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6504 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6506 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6507
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6510 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6511 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6514 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6515
6516 // Move.
6517 x2 += 20; y2 -= 25;
6518 processPosition(mapper, x2, y2);
6519 processId(mapper, 2);
6520 processMTSync(mapper);
6521 processSync(mapper);
6522
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6525 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6526 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6529 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6530
6531 // New finger down.
6532 int32_t x3 = 700, y3 = 300;
6533 processPosition(mapper, x2, y2);
6534 processId(mapper, 2);
6535 processMTSync(mapper);
6536 processPosition(mapper, x3, y3);
6537 processId(mapper, 3);
6538 processMTSync(mapper);
6539 processSync(mapper);
6540
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6543 motionArgs.action);
6544 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6545 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6547 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6550 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6552 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6553
6554 // Second finger up.
6555 x3 += 30; y3 -= 20;
6556 processPosition(mapper, x3, y3);
6557 processId(mapper, 3);
6558 processMTSync(mapper);
6559 processSync(mapper);
6560
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6563 motionArgs.action);
6564 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6565 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6567 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6570 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6572 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6573
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6576 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6577 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6580 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6581
6582 // Last finger up.
6583 processMTSync(mapper);
6584 processSync(mapper);
6585
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6588 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6589 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6592 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6593
6594 // Should not have sent any more keys or motions.
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6597}
6598
6599TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006600 addConfigurationProperty("touch.deviceType", "touchScreen");
6601 prepareDisplay(DISPLAY_ORIENTATION_0);
6602 prepareAxes(POSITION | ID | SLOT);
6603 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006604 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006605
arthurhungdcef2dc2020-08-11 14:47:50 +08006606 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607
6608 NotifyMotionArgs motionArgs;
6609
6610 // Two fingers down at once.
6611 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6612 processPosition(mapper, x1, y1);
6613 processId(mapper, 1);
6614 processSlot(mapper, 1);
6615 processPosition(mapper, x2, y2);
6616 processId(mapper, 2);
6617 processSync(mapper);
6618
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6621 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6622 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6625 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6626
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6628 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6629 motionArgs.action);
6630 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6633 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6636 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6638 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6639
6640 // Move.
6641 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6642 processSlot(mapper, 0);
6643 processPosition(mapper, x1, y1);
6644 processSlot(mapper, 1);
6645 processPosition(mapper, x2, y2);
6646 processSync(mapper);
6647
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6650 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6653 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6656 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6658 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6659
6660 // First finger up.
6661 x2 += 15; y2 -= 20;
6662 processSlot(mapper, 0);
6663 processId(mapper, -1);
6664 processSlot(mapper, 1);
6665 processPosition(mapper, x2, y2);
6666 processSync(mapper);
6667
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6670 motionArgs.action);
6671 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6672 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6674 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6679 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6680
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6683 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6684 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6687 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6688
6689 // Move.
6690 x2 += 20; y2 -= 25;
6691 processPosition(mapper, x2, y2);
6692 processSync(mapper);
6693
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6696 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6697 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6700 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6701
6702 // New finger down.
6703 int32_t x3 = 700, y3 = 300;
6704 processPosition(mapper, x2, y2);
6705 processSlot(mapper, 0);
6706 processId(mapper, 3);
6707 processPosition(mapper, x3, y3);
6708 processSync(mapper);
6709
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6711 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6712 motionArgs.action);
6713 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6716 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6719 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6721 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6722
6723 // Second finger up.
6724 x3 += 30; y3 -= 20;
6725 processSlot(mapper, 1);
6726 processId(mapper, -1);
6727 processSlot(mapper, 0);
6728 processPosition(mapper, x3, y3);
6729 processSync(mapper);
6730
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6732 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6733 motionArgs.action);
6734 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6735 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6737 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6742 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6743
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6746 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6747 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6751
6752 // Last finger up.
6753 processId(mapper, -1);
6754 processSync(mapper);
6755
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6758 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6759 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6762 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6763
6764 // Should not have sent any more keys or motions.
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6767}
6768
6769TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006770 addConfigurationProperty("touch.deviceType", "touchScreen");
6771 prepareDisplay(DISPLAY_ORIENTATION_0);
6772 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006773 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006774
6775 // These calculations are based on the input device calibration documentation.
6776 int32_t rawX = 100;
6777 int32_t rawY = 200;
6778 int32_t rawTouchMajor = 7;
6779 int32_t rawTouchMinor = 6;
6780 int32_t rawToolMajor = 9;
6781 int32_t rawToolMinor = 8;
6782 int32_t rawPressure = 11;
6783 int32_t rawDistance = 0;
6784 int32_t rawOrientation = 3;
6785 int32_t id = 5;
6786
6787 float x = toDisplayX(rawX);
6788 float y = toDisplayY(rawY);
6789 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6790 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6791 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6792 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6793 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6794 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6795 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6796 float distance = float(rawDistance);
6797
6798 processPosition(mapper, rawX, rawY);
6799 processTouchMajor(mapper, rawTouchMajor);
6800 processTouchMinor(mapper, rawTouchMinor);
6801 processToolMajor(mapper, rawToolMajor);
6802 processToolMinor(mapper, rawToolMinor);
6803 processPressure(mapper, rawPressure);
6804 processOrientation(mapper, rawOrientation);
6805 processDistance(mapper, rawDistance);
6806 processId(mapper, id);
6807 processMTSync(mapper);
6808 processSync(mapper);
6809
6810 NotifyMotionArgs args;
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6812 ASSERT_EQ(0, args.pointerProperties[0].id);
6813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6814 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6815 orientation, distance));
6816}
6817
6818TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006819 addConfigurationProperty("touch.deviceType", "touchScreen");
6820 prepareDisplay(DISPLAY_ORIENTATION_0);
6821 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6822 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006824
6825 // These calculations are based on the input device calibration documentation.
6826 int32_t rawX = 100;
6827 int32_t rawY = 200;
6828 int32_t rawTouchMajor = 140;
6829 int32_t rawTouchMinor = 120;
6830 int32_t rawToolMajor = 180;
6831 int32_t rawToolMinor = 160;
6832
6833 float x = toDisplayX(rawX);
6834 float y = toDisplayY(rawY);
6835 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6836 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6837 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6838 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6839 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6840
6841 processPosition(mapper, rawX, rawY);
6842 processTouchMajor(mapper, rawTouchMajor);
6843 processTouchMinor(mapper, rawTouchMinor);
6844 processToolMajor(mapper, rawToolMajor);
6845 processToolMinor(mapper, rawToolMinor);
6846 processMTSync(mapper);
6847 processSync(mapper);
6848
6849 NotifyMotionArgs args;
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6852 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6853}
6854
6855TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006856 addConfigurationProperty("touch.deviceType", "touchScreen");
6857 prepareDisplay(DISPLAY_ORIENTATION_0);
6858 prepareAxes(POSITION | TOUCH | TOOL);
6859 addConfigurationProperty("touch.size.calibration", "diameter");
6860 addConfigurationProperty("touch.size.scale", "10");
6861 addConfigurationProperty("touch.size.bias", "160");
6862 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006864
6865 // These calculations are based on the input device calibration documentation.
6866 // Note: We only provide a single common touch/tool value because the device is assumed
6867 // not to emit separate values for each pointer (isSummed = 1).
6868 int32_t rawX = 100;
6869 int32_t rawY = 200;
6870 int32_t rawX2 = 150;
6871 int32_t rawY2 = 250;
6872 int32_t rawTouchMajor = 5;
6873 int32_t rawToolMajor = 8;
6874
6875 float x = toDisplayX(rawX);
6876 float y = toDisplayY(rawY);
6877 float x2 = toDisplayX(rawX2);
6878 float y2 = toDisplayY(rawY2);
6879 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6880 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6881 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6882
6883 processPosition(mapper, rawX, rawY);
6884 processTouchMajor(mapper, rawTouchMajor);
6885 processToolMajor(mapper, rawToolMajor);
6886 processMTSync(mapper);
6887 processPosition(mapper, rawX2, rawY2);
6888 processTouchMajor(mapper, rawTouchMajor);
6889 processToolMajor(mapper, rawToolMajor);
6890 processMTSync(mapper);
6891 processSync(mapper);
6892
6893 NotifyMotionArgs args;
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6896
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6898 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6899 args.action);
6900 ASSERT_EQ(size_t(2), args.pointerCount);
6901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6902 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6904 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6905}
6906
6907TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006908 addConfigurationProperty("touch.deviceType", "touchScreen");
6909 prepareDisplay(DISPLAY_ORIENTATION_0);
6910 prepareAxes(POSITION | TOUCH | TOOL);
6911 addConfigurationProperty("touch.size.calibration", "area");
6912 addConfigurationProperty("touch.size.scale", "43");
6913 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006914 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006915
6916 // These calculations are based on the input device calibration documentation.
6917 int32_t rawX = 100;
6918 int32_t rawY = 200;
6919 int32_t rawTouchMajor = 5;
6920 int32_t rawToolMajor = 8;
6921
6922 float x = toDisplayX(rawX);
6923 float y = toDisplayY(rawY);
6924 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6925 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6926 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6927
6928 processPosition(mapper, rawX, rawY);
6929 processTouchMajor(mapper, rawTouchMajor);
6930 processToolMajor(mapper, rawToolMajor);
6931 processMTSync(mapper);
6932 processSync(mapper);
6933
6934 NotifyMotionArgs args;
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6937 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6938}
6939
6940TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006941 addConfigurationProperty("touch.deviceType", "touchScreen");
6942 prepareDisplay(DISPLAY_ORIENTATION_0);
6943 prepareAxes(POSITION | PRESSURE);
6944 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6945 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006946 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947
Michael Wrightaa449c92017-12-13 21:21:43 +00006948 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006949 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006950 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6951 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6952 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6953
Michael Wrightd02c5b62014-02-10 15:10:22 -08006954 // These calculations are based on the input device calibration documentation.
6955 int32_t rawX = 100;
6956 int32_t rawY = 200;
6957 int32_t rawPressure = 60;
6958
6959 float x = toDisplayX(rawX);
6960 float y = toDisplayY(rawY);
6961 float pressure = float(rawPressure) * 0.01f;
6962
6963 processPosition(mapper, rawX, rawY);
6964 processPressure(mapper, rawPressure);
6965 processMTSync(mapper);
6966 processSync(mapper);
6967
6968 NotifyMotionArgs args;
6969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6971 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6972}
6973
6974TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975 addConfigurationProperty("touch.deviceType", "touchScreen");
6976 prepareDisplay(DISPLAY_ORIENTATION_0);
6977 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006978 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006979
6980 NotifyMotionArgs motionArgs;
6981 NotifyKeyArgs keyArgs;
6982
6983 processId(mapper, 1);
6984 processPosition(mapper, 100, 200);
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6988 ASSERT_EQ(0, motionArgs.buttonState);
6989
6990 // press BTN_LEFT, release BTN_LEFT
6991 processKey(mapper, BTN_LEFT, 1);
6992 processSync(mapper);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6995 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6996
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6998 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6999 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7000
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001 processKey(mapper, BTN_LEFT, 0);
7002 processSync(mapper);
7003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007006
7007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007009 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007010
7011 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7012 processKey(mapper, BTN_RIGHT, 1);
7013 processKey(mapper, BTN_MIDDLE, 1);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7017 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7018 motionArgs.buttonState);
7019
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7022 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7023
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7025 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7026 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7027 motionArgs.buttonState);
7028
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029 processKey(mapper, BTN_RIGHT, 0);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007034
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007037 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007038
7039 processKey(mapper, BTN_MIDDLE, 0);
7040 processSync(mapper);
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007044
7045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007047 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007048
7049 // press BTN_BACK, release BTN_BACK
7050 processKey(mapper, BTN_BACK, 1);
7051 processSync(mapper);
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7053 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7054 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007055
Michael Wrightd02c5b62014-02-10 15:10:22 -08007056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007058 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7059
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7062 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063
7064 processKey(mapper, BTN_BACK, 0);
7065 processSync(mapper);
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007069
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007072 ASSERT_EQ(0, motionArgs.buttonState);
7073
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7075 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7076 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7077
7078 // press BTN_SIDE, release BTN_SIDE
7079 processKey(mapper, BTN_SIDE, 1);
7080 processSync(mapper);
7081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7083 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007084
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007087 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7088
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7091 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092
7093 processKey(mapper, BTN_SIDE, 0);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007098
7099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007101 ASSERT_EQ(0, motionArgs.buttonState);
7102
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7104 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7105 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7106
7107 // press BTN_FORWARD, release BTN_FORWARD
7108 processKey(mapper, BTN_FORWARD, 1);
7109 processSync(mapper);
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7111 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7112 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007113
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007116 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7117
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7120 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007121
7122 processKey(mapper, BTN_FORWARD, 0);
7123 processSync(mapper);
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007130 ASSERT_EQ(0, motionArgs.buttonState);
7131
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7133 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7134 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7135
7136 // press BTN_EXTRA, release BTN_EXTRA
7137 processKey(mapper, BTN_EXTRA, 1);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7140 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7141 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007142
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007145 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7146
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7149 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150
7151 processKey(mapper, BTN_EXTRA, 0);
7152 processSync(mapper);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007156
7157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007159 ASSERT_EQ(0, motionArgs.buttonState);
7160
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7162 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7163 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7164
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7166
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167 // press BTN_STYLUS, release BTN_STYLUS
7168 processKey(mapper, BTN_STYLUS, 1);
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007172 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7173
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7176 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177
7178 processKey(mapper, BTN_STYLUS, 0);
7179 processSync(mapper);
7180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007183
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007186 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187
7188 // press BTN_STYLUS2, release BTN_STYLUS2
7189 processKey(mapper, BTN_STYLUS2, 1);
7190 processSync(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007193 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7194
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7196 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7197 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198
7199 processKey(mapper, BTN_STYLUS2, 0);
7200 processSync(mapper);
7201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007204
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007207 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208
7209 // release touch
7210 processId(mapper, -1);
7211 processSync(mapper);
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7213 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7214 ASSERT_EQ(0, motionArgs.buttonState);
7215}
7216
7217TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218 addConfigurationProperty("touch.deviceType", "touchScreen");
7219 prepareDisplay(DISPLAY_ORIENTATION_0);
7220 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007221 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007222
7223 NotifyMotionArgs motionArgs;
7224
7225 // default tool type is finger
7226 processId(mapper, 1);
7227 processPosition(mapper, 100, 200);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7232
7233 // eraser
7234 processKey(mapper, BTN_TOOL_RUBBER, 1);
7235 processSync(mapper);
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7239
7240 // stylus
7241 processKey(mapper, BTN_TOOL_RUBBER, 0);
7242 processKey(mapper, BTN_TOOL_PEN, 1);
7243 processSync(mapper);
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7247
7248 // brush
7249 processKey(mapper, BTN_TOOL_PEN, 0);
7250 processKey(mapper, BTN_TOOL_BRUSH, 1);
7251 processSync(mapper);
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7255
7256 // pencil
7257 processKey(mapper, BTN_TOOL_BRUSH, 0);
7258 processKey(mapper, BTN_TOOL_PENCIL, 1);
7259 processSync(mapper);
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7263
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007264 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007265 processKey(mapper, BTN_TOOL_PENCIL, 0);
7266 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7267 processSync(mapper);
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7271
7272 // mouse
7273 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7274 processKey(mapper, BTN_TOOL_MOUSE, 1);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7279
7280 // lens
7281 processKey(mapper, BTN_TOOL_MOUSE, 0);
7282 processKey(mapper, BTN_TOOL_LENS, 1);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7287
7288 // double-tap
7289 processKey(mapper, BTN_TOOL_LENS, 0);
7290 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7295
7296 // triple-tap
7297 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7298 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7299 processSync(mapper);
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7303
7304 // quad-tap
7305 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7306 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7307 processSync(mapper);
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7311
7312 // finger
7313 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7314 processKey(mapper, BTN_TOOL_FINGER, 1);
7315 processSync(mapper);
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7319
7320 // stylus trumps finger
7321 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
7326
7327 // eraser trumps stylus
7328 processKey(mapper, BTN_TOOL_RUBBER, 1);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7333
7334 // mouse trumps eraser
7335 processKey(mapper, BTN_TOOL_MOUSE, 1);
7336 processSync(mapper);
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7340
7341 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7342 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7343 processSync(mapper);
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7347
7348 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7349 processToolType(mapper, MT_TOOL_PEN);
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7354
7355 // back to default tool type
7356 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7357 processKey(mapper, BTN_TOOL_MOUSE, 0);
7358 processKey(mapper, BTN_TOOL_RUBBER, 0);
7359 processKey(mapper, BTN_TOOL_PEN, 0);
7360 processKey(mapper, BTN_TOOL_FINGER, 0);
7361 processSync(mapper);
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7365}
7366
7367TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007368 addConfigurationProperty("touch.deviceType", "touchScreen");
7369 prepareDisplay(DISPLAY_ORIENTATION_0);
7370 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007371 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007372 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007373
7374 NotifyMotionArgs motionArgs;
7375
7376 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7377 processId(mapper, 1);
7378 processPosition(mapper, 100, 200);
7379 processSync(mapper);
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7381 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7383 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7384
7385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7388 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7389
7390 // move a little
7391 processPosition(mapper, 150, 250);
7392 processSync(mapper);
7393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7396 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7397
7398 // down when BTN_TOUCH is pressed, pressure defaults to 1
7399 processKey(mapper, BTN_TOUCH, 1);
7400 processSync(mapper);
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7404 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7405
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7407 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7409 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7410
7411 // up when BTN_TOUCH is released, hover restored
7412 processKey(mapper, BTN_TOUCH, 0);
7413 processSync(mapper);
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7415 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7417 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7418
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7422 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7423
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 // exit hover when pointer goes away
7430 processId(mapper, -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
7438TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 addConfigurationProperty("touch.deviceType", "touchScreen");
7440 prepareDisplay(DISPLAY_ORIENTATION_0);
7441 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007442 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443
7444 NotifyMotionArgs motionArgs;
7445
7446 // initially hovering because pressure is 0
7447 processId(mapper, 1);
7448 processPosition(mapper, 100, 200);
7449 processPressure(mapper, 0);
7450 processSync(mapper);
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7454 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7455
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7457 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7459 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7460
7461 // move a little
7462 processPosition(mapper, 150, 250);
7463 processSync(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7467 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7468
7469 // down when pressure becomes non-zero
7470 processPressure(mapper, RAW_PRESSURE_MAX);
7471 processSync(mapper);
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7475 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7476
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7480 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7481
7482 // up when pressure becomes 0, hover restored
7483 processPressure(mapper, 0);
7484 processSync(mapper);
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7489
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7493 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7494
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 // exit hover when pointer goes away
7501 processId(mapper, -1);
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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007509/**
7510 * Set the input device port <--> display port associations, and check that the
7511 * events are routed to the display that matches the display port.
7512 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7513 */
7514TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007515 const std::string usb2 = "USB2";
7516 const uint8_t hdmi1 = 0;
7517 const uint8_t hdmi2 = 1;
7518 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007519 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007520
7521 addConfigurationProperty("touch.deviceType", "touchScreen");
7522 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007523 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007524
7525 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7526 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7527
7528 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7529 // for this input device is specified, and the matching viewport is not present,
7530 // the input device should be disabled (at the mapper level).
7531
7532 // Add viewport for display 2 on hdmi2
7533 prepareSecondaryDisplay(type, hdmi2);
7534 // Send a touch event
7535 processPosition(mapper, 100, 100);
7536 processSync(mapper);
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7538
7539 // Add viewport for display 1 on hdmi1
7540 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7541 // Send a touch event again
7542 processPosition(mapper, 100, 100);
7543 processSync(mapper);
7544
7545 NotifyMotionArgs args;
7546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7547 ASSERT_EQ(DISPLAY_ID, args.displayId);
7548}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007550TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007551 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007552 std::shared_ptr<FakePointerController> fakePointerController =
7553 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007554 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007555 fakePointerController->setPosition(100, 200);
7556 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007557 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7558
Garfield Tan888a6a42020-01-09 11:39:16 -08007559 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007560 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007561
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007562 prepareDisplay(DISPLAY_ORIENTATION_0);
7563 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007564 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007565
7566 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007567 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007568
7569 NotifyMotionArgs motionArgs;
7570 processPosition(mapper, 100, 100);
7571 processSync(mapper);
7572
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7575 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7576}
7577
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007578/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007579 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7580 */
7581TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7582 addConfigurationProperty("touch.deviceType", "touchScreen");
7583 prepareAxes(POSITION);
7584 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7585
7586 prepareDisplay(DISPLAY_ORIENTATION_0);
7587 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7588 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7589 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7590 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7591
7592 NotifyMotionArgs args;
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7594 ASSERT_EQ(26, args.readTime);
7595
7596 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7597 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7598 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7599
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7601 ASSERT_EQ(33, args.readTime);
7602}
7603
7604/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007605 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7606 * events should not be delivered to the listener.
7607 */
7608TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7609 addConfigurationProperty("touch.deviceType", "touchScreen");
7610 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7611 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7612 ViewportType::INTERNAL);
7613 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7614 prepareAxes(POSITION);
7615 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7616
7617 NotifyMotionArgs motionArgs;
7618 processPosition(mapper, 100, 100);
7619 processSync(mapper);
7620
7621 mFakeListener->assertNotifyMotionWasNotCalled();
7622}
7623
Garfield Tanc734e4f2021-01-15 20:01:39 -08007624TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7625 addConfigurationProperty("touch.deviceType", "touchScreen");
7626 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7627 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7628 ViewportType::INTERNAL);
7629 std::optional<DisplayViewport> optionalDisplayViewport =
7630 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7631 ASSERT_TRUE(optionalDisplayViewport.has_value());
7632 DisplayViewport displayViewport = *optionalDisplayViewport;
7633
7634 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7635 prepareAxes(POSITION);
7636 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7637
7638 // Finger down
7639 int32_t x = 100, y = 100;
7640 processPosition(mapper, x, y);
7641 processSync(mapper);
7642
7643 NotifyMotionArgs motionArgs;
7644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7645 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7646
7647 // Deactivate display viewport
7648 displayViewport.isActive = false;
7649 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7650 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7651
7652 // Finger move
7653 x += 10, y += 10;
7654 processPosition(mapper, x, y);
7655 processSync(mapper);
7656
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7658 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7659
7660 // Reactivate display viewport
7661 displayViewport.isActive = true;
7662 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7663 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7664
7665 // Finger move again
7666 x += 10, y += 10;
7667 processPosition(mapper, x, y);
7668 processSync(mapper);
7669
7670 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7671 // no pointer on the touch device.
7672 mFakeListener->assertNotifyMotionWasNotCalled();
7673}
7674
Arthur Hung7c645402019-01-25 17:45:42 +08007675TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7676 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007677 prepareAxes(POSITION | ID | SLOT);
7678 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007679 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007680
7681 // Create the second touch screen device, and enable multi fingers.
7682 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007683 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007684 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007685 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007686 std::shared_ptr<InputDevice> device2 =
7687 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7688 Flags<InputDeviceClass>(0));
7689
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007690 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7691 0 /*flat*/, 0 /*fuzz*/);
7692 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7693 0 /*flat*/, 0 /*fuzz*/);
7694 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7695 0 /*flat*/, 0 /*fuzz*/);
7696 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7697 0 /*flat*/, 0 /*fuzz*/);
7698 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7699 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7700 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007701
7702 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007703 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007704 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7705 device2->reset(ARBITRARY_TIME);
7706
7707 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007708 std::shared_ptr<FakePointerController> fakePointerController =
7709 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007710 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7711 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7712
7713 // Setup policy for associated displays and show touches.
7714 const uint8_t hdmi1 = 0;
7715 const uint8_t hdmi2 = 1;
7716 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7717 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7718 mFakePolicy->setShowTouches(true);
7719
7720 // Create displays.
7721 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007722 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007723
7724 // Default device will reconfigure above, need additional reconfiguration for another device.
7725 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007726 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007727
7728 // Two fingers down at default display.
7729 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7730 processPosition(mapper, x1, y1);
7731 processId(mapper, 1);
7732 processSlot(mapper, 1);
7733 processPosition(mapper, x2, y2);
7734 processId(mapper, 2);
7735 processSync(mapper);
7736
7737 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7738 fakePointerController->getSpots().find(DISPLAY_ID);
7739 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7740 ASSERT_EQ(size_t(2), iter->second.size());
7741
7742 // Two fingers down at second display.
7743 processPosition(mapper2, x1, y1);
7744 processId(mapper2, 1);
7745 processSlot(mapper2, 1);
7746 processPosition(mapper2, x2, y2);
7747 processId(mapper2, 2);
7748 processSync(mapper2);
7749
7750 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7751 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7752 ASSERT_EQ(size_t(2), iter->second.size());
7753}
7754
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007755TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007756 prepareAxes(POSITION);
7757 addConfigurationProperty("touch.deviceType", "touchScreen");
7758 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007760
7761 NotifyMotionArgs motionArgs;
7762 // Unrotated video frame
7763 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7764 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007765 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007766 processPosition(mapper, 100, 200);
7767 processSync(mapper);
7768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7769 ASSERT_EQ(frames, motionArgs.videoFrames);
7770
7771 // Subsequent touch events should not have any videoframes
7772 // This is implemented separately in FakeEventHub,
7773 // but that should match the behaviour of TouchVideoDevice.
7774 processPosition(mapper, 200, 200);
7775 processSync(mapper);
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7777 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7778}
7779
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007780TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007781 prepareAxes(POSITION);
7782 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007783 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007784 // Unrotated video frame
7785 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7786 NotifyMotionArgs motionArgs;
7787
7788 // Test all 4 orientations
7789 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7790 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7791 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7792 clearViewports();
7793 prepareDisplay(orientation);
7794 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007795 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007796 processPosition(mapper, 100, 200);
7797 processSync(mapper);
7798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7799 frames[0].rotate(orientation);
7800 ASSERT_EQ(frames, motionArgs.videoFrames);
7801 }
7802}
7803
7804TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007805 prepareAxes(POSITION);
7806 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007807 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007808 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7809 // so mix these.
7810 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7811 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7812 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7813 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7814 NotifyMotionArgs motionArgs;
7815
7816 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007817 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007818 processPosition(mapper, 100, 200);
7819 processSync(mapper);
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7821 std::for_each(frames.begin(), frames.end(),
7822 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7823 ASSERT_EQ(frames, motionArgs.videoFrames);
7824}
7825
Arthur Hung9da14732019-09-02 16:16:58 +08007826/**
7827 * If we had defined port associations, but the viewport is not ready, the touch device would be
7828 * expected to be disabled, and it should be enabled after the viewport has found.
7829 */
7830TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007831 constexpr uint8_t hdmi2 = 1;
7832 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007833 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007834
7835 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7836
7837 addConfigurationProperty("touch.deviceType", "touchScreen");
7838 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007840
7841 ASSERT_EQ(mDevice->isEnabled(), false);
7842
7843 // Add display on hdmi2, the device should be enabled and can receive touch event.
7844 prepareSecondaryDisplay(type, hdmi2);
7845 ASSERT_EQ(mDevice->isEnabled(), true);
7846
7847 // Send a touch event.
7848 processPosition(mapper, 100, 100);
7849 processSync(mapper);
7850
7851 NotifyMotionArgs args;
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7853 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7854}
7855
Arthur Hung6cd19a42019-08-30 19:04:12 +08007856
Arthur Hung6cd19a42019-08-30 19:04:12 +08007857
Arthur Hung421eb1c2020-01-16 00:09:42 +08007858TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007859 addConfigurationProperty("touch.deviceType", "touchScreen");
7860 prepareDisplay(DISPLAY_ORIENTATION_0);
7861 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007862 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007863
7864 NotifyMotionArgs motionArgs;
7865
7866 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7867 // finger down
7868 processId(mapper, 1);
7869 processPosition(mapper, x1, y1);
7870 processSync(mapper);
7871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7872 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7874
7875 // finger move
7876 processId(mapper, 1);
7877 processPosition(mapper, x2, y2);
7878 processSync(mapper);
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7882
7883 // finger up.
7884 processId(mapper, -1);
7885 processSync(mapper);
7886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7887 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7889
7890 // new finger down
7891 processId(mapper, 1);
7892 processPosition(mapper, x3, y3);
7893 processSync(mapper);
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7897}
7898
7899/**
arthurhungcc7f9802020-04-30 17:55:40 +08007900 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7901 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007902 */
arthurhungcc7f9802020-04-30 17:55:40 +08007903TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007904 addConfigurationProperty("touch.deviceType", "touchScreen");
7905 prepareDisplay(DISPLAY_ORIENTATION_0);
7906 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007907 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007908
7909 NotifyMotionArgs motionArgs;
7910
7911 // default tool type is finger
7912 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007913 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007914 processPosition(mapper, x1, y1);
7915 processSync(mapper);
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7917 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7919
7920 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7921 processToolType(mapper, MT_TOOL_PALM);
7922 processSync(mapper);
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7924 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7925
7926 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007927 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007928 processPosition(mapper, x2, y2);
7929 processSync(mapper);
7930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7931
7932 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007933 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007934 processSync(mapper);
7935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7936
7937 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007938 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007939 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007940 processPosition(mapper, x3, y3);
7941 processSync(mapper);
7942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7943 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7945}
7946
arthurhungbf89a482020-04-17 17:37:55 +08007947/**
arthurhungcc7f9802020-04-30 17:55:40 +08007948 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7949 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007950 */
arthurhungcc7f9802020-04-30 17:55:40 +08007951TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007952 addConfigurationProperty("touch.deviceType", "touchScreen");
7953 prepareDisplay(DISPLAY_ORIENTATION_0);
7954 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7955 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7956
7957 NotifyMotionArgs motionArgs;
7958
7959 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007960 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7961 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007962 processPosition(mapper, x1, y1);
7963 processSync(mapper);
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7965 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7967
7968 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007969 processSlot(mapper, SECOND_SLOT);
7970 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007971 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007972 processSync(mapper);
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7974 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7975 motionArgs.action);
7976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7977
7978 // If the tool type of the first finger changes to MT_TOOL_PALM,
7979 // we expect to receive ACTION_POINTER_UP with cancel flag.
7980 processSlot(mapper, FIRST_SLOT);
7981 processId(mapper, FIRST_TRACKING_ID);
7982 processToolType(mapper, MT_TOOL_PALM);
7983 processSync(mapper);
7984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7985 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7986 motionArgs.action);
7987 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7988
7989 // The following MOVE events of second finger should be processed.
7990 processSlot(mapper, SECOND_SLOT);
7991 processId(mapper, SECOND_TRACKING_ID);
7992 processPosition(mapper, x2 + 1, y2 + 1);
7993 processSync(mapper);
7994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7996 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7997
7998 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7999 // it. Second finger receive move.
8000 processSlot(mapper, FIRST_SLOT);
8001 processId(mapper, INVALID_TRACKING_ID);
8002 processSync(mapper);
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8005 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8006
8007 // Second finger keeps moving.
8008 processSlot(mapper, SECOND_SLOT);
8009 processId(mapper, SECOND_TRACKING_ID);
8010 processPosition(mapper, x2 + 2, y2 + 2);
8011 processSync(mapper);
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8014 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8015
8016 // Second finger up.
8017 processId(mapper, INVALID_TRACKING_ID);
8018 processSync(mapper);
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8020 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8021 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8022}
8023
8024/**
8025 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8026 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8027 */
8028TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8029 addConfigurationProperty("touch.deviceType", "touchScreen");
8030 prepareDisplay(DISPLAY_ORIENTATION_0);
8031 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8032 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8033
8034 NotifyMotionArgs motionArgs;
8035
8036 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8037 // First finger down.
8038 processId(mapper, FIRST_TRACKING_ID);
8039 processPosition(mapper, x1, y1);
8040 processSync(mapper);
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8044
8045 // Second finger down.
8046 processSlot(mapper, SECOND_SLOT);
8047 processId(mapper, SECOND_TRACKING_ID);
8048 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008049 processSync(mapper);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8051 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8052 motionArgs.action);
8053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8054
arthurhungcc7f9802020-04-30 17:55:40 +08008055 // If the tool type of the first finger changes to MT_TOOL_PALM,
8056 // we expect to receive ACTION_POINTER_UP with cancel flag.
8057 processSlot(mapper, FIRST_SLOT);
8058 processId(mapper, FIRST_TRACKING_ID);
8059 processToolType(mapper, MT_TOOL_PALM);
8060 processSync(mapper);
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8062 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8063 motionArgs.action);
8064 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8065
8066 // Second finger keeps moving.
8067 processSlot(mapper, SECOND_SLOT);
8068 processId(mapper, SECOND_TRACKING_ID);
8069 processPosition(mapper, x2 + 1, y2 + 1);
8070 processSync(mapper);
8071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8073
8074 // second finger becomes palm, receive cancel due to only 1 finger is active.
8075 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008076 processToolType(mapper, MT_TOOL_PALM);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8079 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8080
arthurhungcc7f9802020-04-30 17:55:40 +08008081 // third finger down.
8082 processSlot(mapper, THIRD_SLOT);
8083 processId(mapper, THIRD_TRACKING_ID);
8084 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008085 processPosition(mapper, x3, y3);
8086 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008090 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8091
8092 // third finger move
8093 processId(mapper, THIRD_TRACKING_ID);
8094 processPosition(mapper, x3 + 1, y3 + 1);
8095 processSync(mapper);
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8098
8099 // first finger up, third finger receive move.
8100 processSlot(mapper, FIRST_SLOT);
8101 processId(mapper, INVALID_TRACKING_ID);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8105 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8106
8107 // second finger up, third finger receive move.
8108 processSlot(mapper, SECOND_SLOT);
8109 processId(mapper, INVALID_TRACKING_ID);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8113 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8114
8115 // third finger up.
8116 processSlot(mapper, THIRD_SLOT);
8117 processId(mapper, INVALID_TRACKING_ID);
8118 processSync(mapper);
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8121 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8122}
8123
8124/**
8125 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8126 * and the active finger could still be allowed to receive the events
8127 */
8128TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8129 addConfigurationProperty("touch.deviceType", "touchScreen");
8130 prepareDisplay(DISPLAY_ORIENTATION_0);
8131 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8132 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8133
8134 NotifyMotionArgs motionArgs;
8135
8136 // default tool type is finger
8137 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8138 processId(mapper, FIRST_TRACKING_ID);
8139 processPosition(mapper, x1, y1);
8140 processSync(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8144
8145 // Second finger down.
8146 processSlot(mapper, SECOND_SLOT);
8147 processId(mapper, SECOND_TRACKING_ID);
8148 processPosition(mapper, x2, y2);
8149 processSync(mapper);
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8152 motionArgs.action);
8153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8154
8155 // If the tool type of the second finger changes to MT_TOOL_PALM,
8156 // we expect to receive ACTION_POINTER_UP with cancel flag.
8157 processId(mapper, SECOND_TRACKING_ID);
8158 processToolType(mapper, MT_TOOL_PALM);
8159 processSync(mapper);
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8161 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8162 motionArgs.action);
8163 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8164
8165 // The following MOVE event should be processed.
8166 processSlot(mapper, FIRST_SLOT);
8167 processId(mapper, FIRST_TRACKING_ID);
8168 processPosition(mapper, x1 + 1, y1 + 1);
8169 processSync(mapper);
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8172 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8173
8174 // second finger up.
8175 processSlot(mapper, SECOND_SLOT);
8176 processId(mapper, INVALID_TRACKING_ID);
8177 processSync(mapper);
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8180
8181 // first finger keep moving
8182 processSlot(mapper, FIRST_SLOT);
8183 processId(mapper, FIRST_TRACKING_ID);
8184 processPosition(mapper, x1 + 2, y1 + 2);
8185 processSync(mapper);
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8188
8189 // first finger up.
8190 processId(mapper, INVALID_TRACKING_ID);
8191 processSync(mapper);
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8193 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8194 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008195}
8196
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008197// --- MultiTouchInputMapperTest_ExternalDevice ---
8198
8199class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8200protected:
Chris Yea52ade12020-08-27 16:49:20 -07008201 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008202};
8203
8204/**
8205 * Expect fallback to internal viewport if device is external and external viewport is not present.
8206 */
8207TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8208 prepareAxes(POSITION);
8209 addConfigurationProperty("touch.deviceType", "touchScreen");
8210 prepareDisplay(DISPLAY_ORIENTATION_0);
8211 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8212
8213 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8214
8215 NotifyMotionArgs motionArgs;
8216
8217 // Expect the event to be sent to the internal viewport,
8218 // because an external viewport is not present.
8219 processPosition(mapper, 100, 100);
8220 processSync(mapper);
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8222 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8223
8224 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008225 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008226 processPosition(mapper, 100, 100);
8227 processSync(mapper);
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8230}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008231
8232/**
8233 * Test touch should not work if outside of surface.
8234 */
8235class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8236protected:
8237 void halfDisplayToCenterHorizontal(int32_t orientation) {
8238 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008239 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008240
8241 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8242 internalViewport->orientation = orientation;
8243 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8244 internalViewport->logicalLeft = 0;
8245 internalViewport->logicalTop = 0;
8246 internalViewport->logicalRight = DISPLAY_HEIGHT;
8247 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8248
8249 internalViewport->physicalLeft = 0;
8250 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8251 internalViewport->physicalRight = DISPLAY_HEIGHT;
8252 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8253
8254 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8255 internalViewport->deviceHeight = DISPLAY_WIDTH;
8256 } else {
8257 internalViewport->logicalLeft = 0;
8258 internalViewport->logicalTop = 0;
8259 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8260 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8261
8262 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8263 internalViewport->physicalTop = 0;
8264 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8265 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8266
8267 internalViewport->deviceWidth = DISPLAY_WIDTH;
8268 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8269 }
8270
8271 mFakePolicy->updateViewport(internalViewport.value());
8272 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8273 }
8274
arthurhung5d547942020-12-14 17:04:45 +08008275 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8276 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008277 int32_t yExpected) {
8278 // touch on outside area should not work.
8279 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8280 processSync(mapper);
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8282
8283 // touch on inside area should receive the event.
8284 NotifyMotionArgs args;
8285 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8286 processSync(mapper);
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8288 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8289 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8290
8291 // Reset.
8292 mapper.reset(ARBITRARY_TIME);
8293 }
8294};
8295
arthurhung5d547942020-12-14 17:04:45 +08008296TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008297 addConfigurationProperty("touch.deviceType", "touchScreen");
8298 prepareDisplay(DISPLAY_ORIENTATION_0);
8299 prepareAxes(POSITION);
8300 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8301
8302 // Touch on center of normal display should work.
8303 const int32_t x = DISPLAY_WIDTH / 4;
8304 const int32_t y = DISPLAY_HEIGHT / 2;
8305 processPosition(mapper, toRawX(x), toRawY(y));
8306 processSync(mapper);
8307 NotifyMotionArgs args;
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8310 0.0f, 0.0f, 0.0f, 0.0f));
8311 // Reset.
8312 mapper.reset(ARBITRARY_TIME);
8313
8314 // Let physical display be different to device, and make surface and physical could be 1:1.
8315 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8316
8317 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8318 const int32_t yExpected = y;
8319 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8320}
8321
arthurhung5d547942020-12-14 17:04:45 +08008322TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008323 addConfigurationProperty("touch.deviceType", "touchScreen");
8324 prepareDisplay(DISPLAY_ORIENTATION_0);
8325 prepareAxes(POSITION);
8326 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8327
8328 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8329 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8330
8331 const int32_t x = DISPLAY_WIDTH / 4;
8332 const int32_t y = DISPLAY_HEIGHT / 2;
8333
8334 // expect x/y = swap x/y then reverse y.
8335 const int32_t xExpected = y;
8336 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8337 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8338}
8339
arthurhung5d547942020-12-14 17:04:45 +08008340TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008341 addConfigurationProperty("touch.deviceType", "touchScreen");
8342 prepareDisplay(DISPLAY_ORIENTATION_0);
8343 prepareAxes(POSITION);
8344 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8345
8346 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8347 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8348
8349 const int32_t x = DISPLAY_WIDTH / 4;
8350 const int32_t y = DISPLAY_HEIGHT / 2;
8351
8352 // expect x/y = swap x/y then reverse x.
8353 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8354 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8355 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8356}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008357
arthurhunga36b28e2020-12-29 20:28:15 +08008358TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8359 addConfigurationProperty("touch.deviceType", "touchScreen");
8360 prepareDisplay(DISPLAY_ORIENTATION_0);
8361 prepareAxes(POSITION);
8362 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8363
8364 const int32_t x = 0;
8365 const int32_t y = 0;
8366
8367 const int32_t xExpected = x;
8368 const int32_t yExpected = y;
8369 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8370
8371 clearViewports();
8372 prepareDisplay(DISPLAY_ORIENTATION_90);
8373 // expect x/y = swap x/y then reverse y.
8374 const int32_t xExpected90 = y;
8375 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8376 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8377
8378 clearViewports();
8379 prepareDisplay(DISPLAY_ORIENTATION_270);
8380 // expect x/y = swap x/y then reverse x.
8381 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8382 const int32_t yExpected270 = x;
8383 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8384}
8385
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008386TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8387 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8388 std::shared_ptr<FakePointerController> fakePointerController =
8389 std::make_shared<FakePointerController>();
8390 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8391 fakePointerController->setPosition(0, 0);
8392 fakePointerController->setButtonState(0);
8393
8394 // prepare device and capture
8395 prepareDisplay(DISPLAY_ORIENTATION_0);
8396 prepareAxes(POSITION | ID | SLOT);
8397 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8398 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8399 mFakePolicy->setPointerCapture(true);
8400 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8401 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8402
8403 // captured touchpad should be a touchpad source
8404 NotifyDeviceResetArgs resetArgs;
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8406 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8407
Chris Yef74dc422020-09-02 22:41:50 -07008408 InputDeviceInfo deviceInfo;
8409 mDevice->getDeviceInfo(&deviceInfo);
8410
8411 const InputDeviceInfo::MotionRange* relRangeX =
8412 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8413 ASSERT_NE(relRangeX, nullptr);
8414 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8415 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8416 const InputDeviceInfo::MotionRange* relRangeY =
8417 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8418 ASSERT_NE(relRangeY, nullptr);
8419 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8420 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8421
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008422 // run captured pointer tests - note that this is unscaled, so input listener events should be
8423 // identical to what the hardware sends (accounting for any
8424 // calibration).
8425 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008426 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008427 processId(mapper, 1);
8428 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8429 processKey(mapper, BTN_TOUCH, 1);
8430 processSync(mapper);
8431
8432 // expect coord[0] to contain initial location of touch 0
8433 NotifyMotionArgs args;
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8436 ASSERT_EQ(1U, args.pointerCount);
8437 ASSERT_EQ(0, args.pointerProperties[0].id);
8438 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8439 ASSERT_NO_FATAL_FAILURE(
8440 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8441
8442 // FINGER 1 DOWN
8443 processSlot(mapper, 1);
8444 processId(mapper, 2);
8445 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8446 processSync(mapper);
8447
8448 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008450 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8451 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008452 ASSERT_EQ(2U, args.pointerCount);
8453 ASSERT_EQ(0, args.pointerProperties[0].id);
8454 ASSERT_EQ(1, args.pointerProperties[1].id);
8455 ASSERT_NO_FATAL_FAILURE(
8456 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8457 ASSERT_NO_FATAL_FAILURE(
8458 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8459
8460 // FINGER 1 MOVE
8461 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8462 processSync(mapper);
8463
8464 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8465 // from move
8466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8468 ASSERT_NO_FATAL_FAILURE(
8469 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8470 ASSERT_NO_FATAL_FAILURE(
8471 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8472
8473 // FINGER 0 MOVE
8474 processSlot(mapper, 0);
8475 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8476 processSync(mapper);
8477
8478 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8481 ASSERT_NO_FATAL_FAILURE(
8482 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8483 ASSERT_NO_FATAL_FAILURE(
8484 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8485
8486 // BUTTON DOWN
8487 processKey(mapper, BTN_LEFT, 1);
8488 processSync(mapper);
8489
8490 // touchinputmapper design sends a move before button press
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8495
8496 // BUTTON UP
8497 processKey(mapper, BTN_LEFT, 0);
8498 processSync(mapper);
8499
8500 // touchinputmapper design sends a move after button release
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8505
8506 // FINGER 0 UP
8507 processId(mapper, -1);
8508 processSync(mapper);
8509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8510 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8511
8512 // FINGER 1 MOVE
8513 processSlot(mapper, 1);
8514 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8515 processSync(mapper);
8516
8517 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8520 ASSERT_EQ(1U, args.pointerCount);
8521 ASSERT_EQ(1, args.pointerProperties[0].id);
8522 ASSERT_NO_FATAL_FAILURE(
8523 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8524
8525 // FINGER 1 UP
8526 processId(mapper, -1);
8527 processKey(mapper, BTN_TOUCH, 0);
8528 processSync(mapper);
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8531
8532 // non captured touchpad should be a mouse source
8533 mFakePolicy->setPointerCapture(false);
8534 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8536 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8537}
8538
8539TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8540 std::shared_ptr<FakePointerController> fakePointerController =
8541 std::make_shared<FakePointerController>();
8542 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8543 fakePointerController->setPosition(0, 0);
8544 fakePointerController->setButtonState(0);
8545
8546 // prepare device and capture
8547 prepareDisplay(DISPLAY_ORIENTATION_0);
8548 prepareAxes(POSITION | ID | SLOT);
8549 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8550 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8551 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8552 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8553 // run uncaptured pointer tests - pushes out generic events
8554 // FINGER 0 DOWN
8555 processId(mapper, 3);
8556 processPosition(mapper, 100, 100);
8557 processKey(mapper, BTN_TOUCH, 1);
8558 processSync(mapper);
8559
8560 // start at (100,100), cursor should be at (0,0) * scale
8561 NotifyMotionArgs args;
8562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8563 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8564 ASSERT_NO_FATAL_FAILURE(
8565 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8566
8567 // FINGER 0 MOVE
8568 processPosition(mapper, 200, 200);
8569 processSync(mapper);
8570
8571 // compute scaling to help with touch position checking
8572 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8573 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8574 float scale =
8575 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8576
8577 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8581 0, 0, 0, 0, 0, 0, 0));
8582}
8583
8584TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8585 std::shared_ptr<FakePointerController> fakePointerController =
8586 std::make_shared<FakePointerController>();
8587
8588 prepareDisplay(DISPLAY_ORIENTATION_0);
8589 prepareAxes(POSITION | ID | SLOT);
8590 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8591 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8592 mFakePolicy->setPointerCapture(false);
8593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8594
8595 // uncaptured touchpad should be a pointer device
8596 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8597
8598 // captured touchpad should be a touchpad device
8599 mFakePolicy->setPointerCapture(true);
8600 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8601 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8602}
8603
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008604// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008605
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008606class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008607protected:
8608 static const char* DEVICE_NAME;
8609 static const char* DEVICE_LOCATION;
8610 static const int32_t DEVICE_ID;
8611 static const int32_t DEVICE_GENERATION;
8612 static const int32_t DEVICE_CONTROLLER_NUMBER;
8613 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8614 static const int32_t EVENTHUB_ID;
8615
8616 std::shared_ptr<FakeEventHub> mFakeEventHub;
8617 sp<FakeInputReaderPolicy> mFakePolicy;
8618 sp<TestInputListener> mFakeListener;
8619 std::unique_ptr<InstrumentedInputReader> mReader;
8620 std::shared_ptr<InputDevice> mDevice;
8621
8622 virtual void SetUp(Flags<InputDeviceClass> classes) {
8623 mFakeEventHub = std::make_unique<FakeEventHub>();
8624 mFakePolicy = new FakeInputReaderPolicy();
8625 mFakeListener = new TestInputListener();
8626 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8627 mFakeListener);
8628 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8629 }
8630
8631 void SetUp() override { SetUp(DEVICE_CLASSES); }
8632
8633 void TearDown() override {
8634 mFakeListener.clear();
8635 mFakePolicy.clear();
8636 }
8637
8638 void configureDevice(uint32_t changes) {
8639 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8640 mReader->requestRefreshConfiguration(changes);
8641 mReader->loopOnce();
8642 }
8643 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8644 }
8645
8646 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8647 const std::string& location, int32_t eventHubId,
8648 Flags<InputDeviceClass> classes) {
8649 InputDeviceIdentifier identifier;
8650 identifier.name = name;
8651 identifier.location = location;
8652 std::shared_ptr<InputDevice> device =
8653 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8654 identifier);
8655 mReader->pushNextDevice(device);
8656 mFakeEventHub->addDevice(eventHubId, name, classes);
8657 mReader->loopOnce();
8658 return device;
8659 }
8660
8661 template <class T, typename... Args>
8662 T& addControllerAndConfigure(Args... args) {
8663 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8664
8665 return controller;
8666 }
8667};
8668
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008669const char* PeripheralControllerTest::DEVICE_NAME = "device";
8670const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8671const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8672const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8673const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8674const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008675 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008676const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008677
8678// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008679class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008680protected:
8681 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008682 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008683 }
8684};
8685
8686TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008687 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008688
8689 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8690 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8691}
8692
8693TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008694 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008695
8696 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8697 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8698}
8699
8700// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008701class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008702protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008703 void SetUp() override {
8704 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8705 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008706};
8707
8708TEST_F(LightControllerTest, SingleLight) {
8709 RawLightInfo infoSingle = {.id = 1,
8710 .name = "Mono",
8711 .maxBrightness = 255,
8712 .flags = InputLightClass::BRIGHTNESS,
8713 .path = ""};
8714 mFakeEventHub->addRawLightInfo(infoSingle.id, std::move(infoSingle));
8715
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008716 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008717 InputDeviceInfo info;
8718 controller.populateDeviceInfo(&info);
8719 const auto& ids = info.getLightIds();
8720 ASSERT_EQ(1UL, ids.size());
8721 ASSERT_EQ(InputDeviceLightType::SINGLE, info.getLightInfo(ids[0])->type);
8722
8723 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_BRIGHTNESS));
8724 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_BRIGHTNESS);
8725}
8726
8727TEST_F(LightControllerTest, RGBLight) {
8728 RawLightInfo infoRed = {.id = 1,
8729 .name = "red",
8730 .maxBrightness = 255,
8731 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8732 .path = ""};
8733 RawLightInfo infoGreen = {.id = 2,
8734 .name = "green",
8735 .maxBrightness = 255,
8736 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8737 .path = ""};
8738 RawLightInfo infoBlue = {.id = 3,
8739 .name = "blue",
8740 .maxBrightness = 255,
8741 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8742 .path = ""};
8743 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8744 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8745 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8746
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008747 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008748 InputDeviceInfo info;
8749 controller.populateDeviceInfo(&info);
8750 const auto& ids = info.getLightIds();
8751 ASSERT_EQ(1UL, ids.size());
8752 ASSERT_EQ(InputDeviceLightType::RGB, info.getLightInfo(ids[0])->type);
8753
8754 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_COLOR));
8755 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
8756}
8757
8758TEST_F(LightControllerTest, MultiColorRGBLight) {
8759 RawLightInfo infoColor = {.id = 1,
8760 .name = "red",
8761 .maxBrightness = 255,
8762 .flags = InputLightClass::BRIGHTNESS |
8763 InputLightClass::MULTI_INTENSITY |
8764 InputLightClass::MULTI_INDEX,
8765 .path = ""};
8766
8767 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8768
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008769 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008770 InputDeviceInfo info;
8771 controller.populateDeviceInfo(&info);
8772 const auto& ids = info.getLightIds();
8773 ASSERT_EQ(1UL, ids.size());
8774 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, info.getLightInfo(ids[0])->type);
8775
8776 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_COLOR));
8777 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
8778}
8779
8780TEST_F(LightControllerTest, PlayerIdLight) {
8781 RawLightInfo info1 = {.id = 1,
8782 .name = "player1",
8783 .maxBrightness = 255,
8784 .flags = InputLightClass::BRIGHTNESS,
8785 .path = ""};
8786 RawLightInfo info2 = {.id = 2,
8787 .name = "player2",
8788 .maxBrightness = 255,
8789 .flags = InputLightClass::BRIGHTNESS,
8790 .path = ""};
8791 RawLightInfo info3 = {.id = 3,
8792 .name = "player3",
8793 .maxBrightness = 255,
8794 .flags = InputLightClass::BRIGHTNESS,
8795 .path = ""};
8796 RawLightInfo info4 = {.id = 4,
8797 .name = "player4",
8798 .maxBrightness = 255,
8799 .flags = InputLightClass::BRIGHTNESS,
8800 .path = ""};
8801 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8802 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8803 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8804 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8805
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008806 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008807 InputDeviceInfo info;
8808 controller.populateDeviceInfo(&info);
8809 const auto& ids = info.getLightIds();
8810 ASSERT_EQ(1UL, ids.size());
8811 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, info.getLightInfo(ids[0])->type);
8812
8813 ASSERT_FALSE(controller.setLightColor(ids[0], LIGHT_COLOR));
8814 ASSERT_TRUE(controller.setLightPlayerId(ids[0], LIGHT_PLAYER_ID));
8815 ASSERT_EQ(controller.getLightPlayerId(ids[0]).value_or(-1), LIGHT_PLAYER_ID);
8816}
8817
Michael Wrightd02c5b62014-02-10 15:10:22 -08008818} // namespace android