blob: 3d99a6b863d8efee52cf87c502f24df1669d2c56 [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>
Chris Yee2b1e5c2021-03-10 22:45:12 -080018#include <InputController.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070019#include <InputDevice.h>
20#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.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 Yee2b1e5c2021-03-10 22:45:12 -08002018// --- FakeInputController ---
Kim Low03ea0352020-11-06 12:45:07 -08002019
Chris Yee2b1e5c2021-03-10 22:45:12 -08002020class FakeInputController : public InputControllerInterface {
2021public:
2022 FakeInputController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
2023
2024 ~FakeInputController() override {}
2025
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);
2067 FakeInputController& controller = device->addController<FakeInputController>(eventHubId);
2068 mReader->pushNextDevice(device);
2069
2070 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2071
2072 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2073 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2074}
2075
2076TEST_F(InputReaderTest, BatteryGetStatus) {
2077 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2078 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2079 constexpr int32_t eventHubId = 1;
2080 const char* DEVICE_LOCATION = "BLUETOOTH";
2081 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2082 FakeInputController& controller = device->addController<FakeInputController>(eventHubId);
2083 mReader->pushNextDevice(device);
2084
2085 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2086
2087 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2088 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2089}
2090
Chris Ye3fdbfef2021-01-06 18:45:18 -08002091TEST_F(InputReaderTest, LightGetColor) {
2092 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2093 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2094 constexpr int32_t eventHubId = 1;
2095 const char* DEVICE_LOCATION = "BLUETOOTH";
2096 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002097 FakeInputController& controller = device->addController<FakeInputController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002098 mReader->pushNextDevice(device);
2099 RawLightInfo info = {.id = 1,
2100 .name = "Mono",
2101 .maxBrightness = 255,
2102 .flags = InputLightClass::BRIGHTNESS,
2103 .path = ""};
2104 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2105 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2106
2107 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002108
Chris Yee2b1e5c2021-03-10 22:45:12 -08002109 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2110 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002111 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2112 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2113}
2114
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002115// --- InputReaderIntegrationTest ---
2116
2117// These tests create and interact with the InputReader only through its interface.
2118// The InputReader is started during SetUp(), which starts its processing in its own
2119// thread. The tests use linux uinput to emulate input devices.
2120// NOTE: Interacting with the physical device while these tests are running may cause
2121// the tests to fail.
2122class InputReaderIntegrationTest : public testing::Test {
2123protected:
2124 sp<TestInputListener> mTestListener;
2125 sp<FakeInputReaderPolicy> mFakePolicy;
2126 sp<InputReaderInterface> mReader;
2127
Chris Yea52ade12020-08-27 16:49:20 -07002128 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002129 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002130 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2131 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002132
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002133 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002134 ASSERT_EQ(mReader->start(), OK);
2135
2136 // Since this test is run on a real device, all the input devices connected
2137 // to the test device will show up in mReader. We wait for those input devices to
2138 // show up before beginning the tests.
2139 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2140 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2141 }
2142
Chris Yea52ade12020-08-27 16:49:20 -07002143 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002144 ASSERT_EQ(mReader->stop(), OK);
2145 mTestListener.clear();
2146 mFakePolicy.clear();
2147 }
2148};
2149
2150TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2151 // An invalid input device that is only used for this test.
2152 class InvalidUinputDevice : public UinputDevice {
2153 public:
2154 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2155
2156 private:
2157 void configureDevice(int fd, uinput_user_dev* device) override {}
2158 };
2159
2160 const size_t numDevices = mFakePolicy->getInputDevices().size();
2161
2162 // UinputDevice does not set any event or key bits, so InputReader should not
2163 // consider it as a valid device.
2164 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2165 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2166 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2167 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2168
2169 invalidDevice.reset();
2170 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2171 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2172 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2173}
2174
2175TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2176 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2177
2178 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2179 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2180 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2181 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2182
2183 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002184 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2185 const auto& it =
2186 std::find_if(inputDevices.begin(), inputDevices.end(),
2187 [&keyboard](const InputDeviceInfo& info) {
2188 return info.getIdentifier().name == keyboard->getName();
2189 });
2190
2191 ASSERT_NE(it, inputDevices.end());
2192 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2193 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2194 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002195
2196 keyboard.reset();
2197 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2198 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2199 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2200}
2201
2202TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2203 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2204 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2205
2206 NotifyConfigurationChangedArgs configChangedArgs;
2207 ASSERT_NO_FATAL_FAILURE(
2208 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002209 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002210 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2211
2212 NotifyKeyArgs keyArgs;
2213 keyboard->pressAndReleaseHomeKey();
2214 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2215 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002216 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002217 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002218 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002219 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002220 prevTimestamp = keyArgs.eventTime;
2221
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2223 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002224 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002225 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002226 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002227}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002228
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002229/**
2230 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2231 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2232 * are passed to the listener.
2233 */
2234static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2235TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2236 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2237 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2238 NotifyKeyArgs keyArgs;
2239
2240 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2241 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2242 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2243 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2244
2245 controller->pressAndReleaseKey(BTN_GEAR_UP);
2246 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2247 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2248 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2249}
2250
Arthur Hungaab25622020-01-16 11:22:11 +08002251// --- TouchProcessTest ---
2252class TouchIntegrationTest : public InputReaderIntegrationTest {
2253protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002254 const std::string UNIQUE_ID = "local:0";
2255
Chris Yea52ade12020-08-27 16:49:20 -07002256 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002257 InputReaderIntegrationTest::SetUp();
2258 // At least add an internal display.
2259 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2260 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002261 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002262
2263 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2264 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2265 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2266 }
2267
2268 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2269 int32_t orientation, const std::string& uniqueId,
2270 std::optional<uint8_t> physicalPort,
2271 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002272 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2273 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002274 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2275 }
2276
2277 std::unique_ptr<UinputTouchScreen> mDevice;
2278};
2279
2280TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2281 NotifyMotionArgs args;
2282 const Point centerPoint = mDevice->getCenterPoint();
2283
2284 // ACTION_DOWN
2285 mDevice->sendDown(centerPoint);
2286 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2287 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2288
2289 // ACTION_MOVE
2290 mDevice->sendMove(centerPoint + Point(1, 1));
2291 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2293
2294 // ACTION_UP
2295 mDevice->sendUp();
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2297 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2298}
2299
2300TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2301 NotifyMotionArgs args;
2302 const Point centerPoint = mDevice->getCenterPoint();
2303
2304 // ACTION_DOWN
2305 mDevice->sendDown(centerPoint);
2306 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2308
2309 // ACTION_POINTER_DOWN (Second slot)
2310 const Point secondPoint = centerPoint + Point(100, 100);
2311 mDevice->sendSlot(SECOND_SLOT);
2312 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2313 mDevice->sendDown(secondPoint + Point(1, 1));
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2315 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2316 args.action);
2317
2318 // ACTION_MOVE (Second slot)
2319 mDevice->sendMove(secondPoint);
2320 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2322
2323 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002324 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002325 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002326 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002327 args.action);
2328
2329 // ACTION_UP
2330 mDevice->sendSlot(FIRST_SLOT);
2331 mDevice->sendUp();
2332 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2333 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2334}
2335
2336TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2337 NotifyMotionArgs args;
2338 const Point centerPoint = mDevice->getCenterPoint();
2339
2340 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002341 mDevice->sendSlot(FIRST_SLOT);
2342 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002343 mDevice->sendDown(centerPoint);
2344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2345 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2346
arthurhungcc7f9802020-04-30 17:55:40 +08002347 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002348 const Point secondPoint = centerPoint + Point(100, 100);
2349 mDevice->sendSlot(SECOND_SLOT);
2350 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2351 mDevice->sendDown(secondPoint);
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2353 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2354 args.action);
2355
arthurhungcc7f9802020-04-30 17:55:40 +08002356 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002357 mDevice->sendMove(secondPoint + Point(1, 1));
2358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2360
arthurhungcc7f9802020-04-30 17:55:40 +08002361 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2362 // a palm event.
2363 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002364 mDevice->sendToolType(MT_TOOL_PALM);
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002366 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2367 args.action);
2368 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002369
arthurhungcc7f9802020-04-30 17:55:40 +08002370 // Send up to second slot, expect first slot send moving.
2371 mDevice->sendPointerUp();
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002374
arthurhungcc7f9802020-04-30 17:55:40 +08002375 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002376 mDevice->sendSlot(FIRST_SLOT);
2377 mDevice->sendUp();
2378
arthurhungcc7f9802020-04-30 17:55:40 +08002379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2380 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002381}
2382
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384class InputDeviceTest : public testing::Test {
2385protected:
2386 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002387 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002388 static const int32_t DEVICE_ID;
2389 static const int32_t DEVICE_GENERATION;
2390 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002391 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002392 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002394 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002396 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002397 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002398 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399
Chris Yea52ade12020-08-27 16:49:20 -07002400 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002401 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002403 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002404 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2405 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406 InputDeviceIdentifier identifier;
2407 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002408 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002409 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002410 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002411 mReader->pushNextDevice(mDevice);
2412 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2413 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414 }
2415
Chris Yea52ade12020-08-27 16:49:20 -07002416 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417 mFakeListener.clear();
2418 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 }
2420};
2421
2422const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002423const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002424const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2426const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002427const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2428 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002429const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430
2431TEST_F(InputDeviceTest, ImmutableProperties) {
2432 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002433 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002434 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435}
2436
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002437TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2438 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002439}
2440
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2442 // Configuration.
2443 InputReaderConfiguration config;
2444 mDevice->configure(ARBITRARY_TIME, &config, 0);
2445
2446 // Reset.
2447 mDevice->reset(ARBITRARY_TIME);
2448
2449 NotifyDeviceResetArgs resetArgs;
2450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2451 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2452 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2453
2454 // Metadata.
2455 ASSERT_TRUE(mDevice->isIgnored());
2456 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2457
2458 InputDeviceInfo info;
2459 mDevice->getDeviceInfo(&info);
2460 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002461 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002462 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2463 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2464
2465 // State queries.
2466 ASSERT_EQ(0, mDevice->getMetaState());
2467
2468 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2469 << "Ignored device should return unknown key code state.";
2470 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2471 << "Ignored device should return unknown scan code state.";
2472 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2473 << "Ignored device should return unknown switch state.";
2474
2475 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2476 uint8_t flags[2] = { 0, 1 };
2477 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2478 << "Ignored device should never mark any key codes.";
2479 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2480 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2481}
2482
2483TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2484 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002485 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002486
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002487 FakeInputMapper& mapper1 =
2488 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002489 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2490 mapper1.setMetaState(AMETA_ALT_ON);
2491 mapper1.addSupportedKeyCode(AKEYCODE_A);
2492 mapper1.addSupportedKeyCode(AKEYCODE_B);
2493 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2494 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2495 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2496 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2497 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002498
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002499 FakeInputMapper& mapper2 =
2500 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002501 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502
2503 InputReaderConfiguration config;
2504 mDevice->configure(ARBITRARY_TIME, &config, 0);
2505
2506 String8 propertyValue;
2507 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2508 << "Device should have read configuration during configuration phase.";
2509 ASSERT_STREQ("value", propertyValue.string());
2510
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002511 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2512 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513
2514 // Reset
2515 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002516 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2517 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518
2519 NotifyDeviceResetArgs resetArgs;
2520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2521 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2522 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2523
2524 // Metadata.
2525 ASSERT_FALSE(mDevice->isIgnored());
2526 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2527
2528 InputDeviceInfo info;
2529 mDevice->getDeviceInfo(&info);
2530 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002531 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002532 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2533 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2534
2535 // State queries.
2536 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2537 << "Should query mappers and combine meta states.";
2538
2539 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2540 << "Should return unknown key code state when source not supported.";
2541 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2542 << "Should return unknown scan code state when source not supported.";
2543 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2544 << "Should return unknown switch state when source not supported.";
2545
2546 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2547 << "Should query mapper when source is supported.";
2548 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2549 << "Should query mapper when source is supported.";
2550 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2551 << "Should query mapper when source is supported.";
2552
2553 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2554 uint8_t flags[4] = { 0, 0, 0, 1 };
2555 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2556 << "Should do nothing when source is unsupported.";
2557 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2558 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2559 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2560 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2561
2562 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2563 << "Should query mapper when source is supported.";
2564 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2565 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2566 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2567 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2568
2569 // Event handling.
2570 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002571 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572 mDevice->process(&event, 1);
2573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002574 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2575 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576}
2577
Arthur Hung2c9a3342019-07-23 14:18:59 +08002578// A single input device is associated with a specific display. Check that:
2579// 1. Device is disabled if the viewport corresponding to the associated display is not found
2580// 2. Device is disabled when setEnabled API is called
2581TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002582 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002583
2584 // First Configuration.
2585 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2586
2587 // Device should be enabled by default.
2588 ASSERT_TRUE(mDevice->isEnabled());
2589
2590 // Prepare associated info.
2591 constexpr uint8_t hdmi = 1;
2592 const std::string UNIQUE_ID = "local:1";
2593
2594 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2595 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2596 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2597 // Device should be disabled because it is associated with a specific display via
2598 // input port <-> display port association, but the corresponding display is not found
2599 ASSERT_FALSE(mDevice->isEnabled());
2600
2601 // Prepare displays.
2602 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002603 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2604 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002605 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2606 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2607 ASSERT_TRUE(mDevice->isEnabled());
2608
2609 // Device should be disabled after set disable.
2610 mFakePolicy->addDisabledDevice(mDevice->getId());
2611 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2612 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2613 ASSERT_FALSE(mDevice->isEnabled());
2614
2615 // Device should still be disabled even found the associated display.
2616 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2617 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2618 ASSERT_FALSE(mDevice->isEnabled());
2619}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620
2621// --- InputMapperTest ---
2622
2623class InputMapperTest : public testing::Test {
2624protected:
2625 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002626 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002627 static const int32_t DEVICE_ID;
2628 static const int32_t DEVICE_GENERATION;
2629 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002630 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002631 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002633 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002635 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002636 std::unique_ptr<InstrumentedInputReader> mReader;
2637 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638
Chris Ye1b0c7342020-07-28 21:57:03 -07002639 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002640 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002642 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002643 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2644 mFakeListener);
2645 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646 }
2647
Chris Yea52ade12020-08-27 16:49:20 -07002648 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002649
Chris Yea52ade12020-08-27 16:49:20 -07002650 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651 mFakeListener.clear();
2652 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653 }
2654
2655 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002656 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657 }
2658
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002659 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002660 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002661 mReader->requestRefreshConfiguration(changes);
2662 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002663 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002664 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2665 }
2666
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2668 const std::string& location, int32_t eventHubId,
2669 Flags<InputDeviceClass> classes) {
2670 InputDeviceIdentifier identifier;
2671 identifier.name = name;
2672 identifier.location = location;
2673 std::shared_ptr<InputDevice> device =
2674 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2675 identifier);
2676 mReader->pushNextDevice(device);
2677 mFakeEventHub->addDevice(eventHubId, name, classes);
2678 mReader->loopOnce();
2679 return device;
2680 }
2681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002682 template <class T, typename... Args>
2683 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002684 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002685 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002687 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002688 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 }
2690
2691 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002692 int32_t orientation, const std::string& uniqueId,
2693 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002694 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2695 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002696 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2697 }
2698
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002699 void clearViewports() {
2700 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701 }
2702
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002703 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2704 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705 RawEvent event;
2706 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002707 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002708 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 event.type = type;
2710 event.code = code;
2711 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002712 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002713 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714 }
2715
2716 static void assertMotionRange(const InputDeviceInfo& info,
2717 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2718 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002719 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2721 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2722 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2723 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2724 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2725 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2726 }
2727
2728 static void assertPointerCoords(const PointerCoords& coords,
2729 float x, float y, float pressure, float size,
2730 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2731 float orientation, float distance) {
2732 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2733 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2734 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2735 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2736 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2737 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2738 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2739 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2740 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2741 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2742 }
2743
Michael Wright17db18e2020-06-26 20:51:44 +01002744 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002745 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002746 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747 ASSERT_NEAR(x, actualX, 1);
2748 ASSERT_NEAR(y, actualY, 1);
2749 }
2750};
2751
2752const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002753const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002754const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2756const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002757const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2758 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002759const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760
2761// --- SwitchInputMapperTest ---
2762
2763class SwitchInputMapperTest : public InputMapperTest {
2764protected:
2765};
2766
2767TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002768 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002770 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771}
2772
2773TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002774 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002776 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002777 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002779 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002780 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781}
2782
2783TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002784 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790
2791 NotifySwitchArgs args;
2792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2793 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002794 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2795 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796 args.switchMask);
2797 ASSERT_EQ(uint32_t(0), args.policyFlags);
2798}
2799
Chris Ye87143712020-11-10 05:05:58 +00002800// --- VibratorInputMapperTest ---
2801class VibratorInputMapperTest : public InputMapperTest {
2802protected:
2803 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2804};
2805
2806TEST_F(VibratorInputMapperTest, GetSources) {
2807 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2808
2809 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2810}
2811
2812TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2813 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2814
2815 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2816}
2817
2818TEST_F(VibratorInputMapperTest, Vibrate) {
2819 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002820 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002821 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2822
2823 VibrationElement pattern(2);
2824 VibrationSequence sequence(2);
2825 pattern.duration = std::chrono::milliseconds(200);
2826 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2827 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2828 sequence.addElement(pattern);
2829 pattern.duration = std::chrono::milliseconds(500);
2830 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2831 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2832 sequence.addElement(pattern);
2833
2834 std::vector<int64_t> timings = {0, 1};
2835 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2836
2837 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002838 // Start vibrating
2839 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002840 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002841 // Verify vibrator state listener was notified.
2842 mReader->loopOnce();
2843 NotifyVibratorStateArgs args;
2844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2845 ASSERT_EQ(DEVICE_ID, args.deviceId);
2846 ASSERT_TRUE(args.isOn);
2847 // Stop vibrating
2848 mapper.cancelVibrate(VIBRATION_TOKEN);
2849 ASSERT_FALSE(mapper.isVibrating());
2850 // Verify vibrator state listener was notified.
2851 mReader->loopOnce();
2852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2853 ASSERT_EQ(DEVICE_ID, args.deviceId);
2854 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002855}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856
Chris Yef59a2f42020-10-16 12:55:26 -07002857// --- SensorInputMapperTest ---
2858
2859class SensorInputMapperTest : public InputMapperTest {
2860protected:
2861 static const int32_t ACCEL_RAW_MIN;
2862 static const int32_t ACCEL_RAW_MAX;
2863 static const int32_t ACCEL_RAW_FUZZ;
2864 static const int32_t ACCEL_RAW_FLAT;
2865 static const int32_t ACCEL_RAW_RESOLUTION;
2866
2867 static const int32_t GYRO_RAW_MIN;
2868 static const int32_t GYRO_RAW_MAX;
2869 static const int32_t GYRO_RAW_FUZZ;
2870 static const int32_t GYRO_RAW_FLAT;
2871 static const int32_t GYRO_RAW_RESOLUTION;
2872
2873 static const float GRAVITY_MS2_UNIT;
2874 static const float DEGREE_RADIAN_UNIT;
2875
2876 void prepareAccelAxes();
2877 void prepareGyroAxes();
2878 void setAccelProperties();
2879 void setGyroProperties();
2880 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2881};
2882
2883const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2884const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2885const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2886const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2887const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2888
2889const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2890const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2891const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2892const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2893const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2894
2895const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2896const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2897
2898void SensorInputMapperTest::prepareAccelAxes() {
2899 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2900 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2901 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2902 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2904 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2905}
2906
2907void SensorInputMapperTest::prepareGyroAxes() {
2908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2909 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2911 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2912 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2913 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2914}
2915
2916void SensorInputMapperTest::setAccelProperties() {
2917 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2918 /* sensorDataIndex */ 0);
2919 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2920 /* sensorDataIndex */ 1);
2921 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2922 /* sensorDataIndex */ 2);
2923 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2924 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2925 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2926 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2927 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2928}
2929
2930void SensorInputMapperTest::setGyroProperties() {
2931 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2932 /* sensorDataIndex */ 0);
2933 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2934 /* sensorDataIndex */ 1);
2935 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2936 /* sensorDataIndex */ 2);
2937 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2938 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2939 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2940 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2941 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2942}
2943
2944TEST_F(SensorInputMapperTest, GetSources) {
2945 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2946
2947 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2948}
2949
2950TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2951 setAccelProperties();
2952 prepareAccelAxes();
2953 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2954
2955 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2956 std::chrono::microseconds(10000),
2957 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002958 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002964
2965 NotifySensorArgs args;
2966 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2967 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2968 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2969
2970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2971 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2972 ASSERT_EQ(args.deviceId, DEVICE_ID);
2973 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2974 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2975 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2976 ASSERT_EQ(args.values, values);
2977 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2978}
2979
2980TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2981 setGyroProperties();
2982 prepareGyroAxes();
2983 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2984
2985 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2986 std::chrono::microseconds(10000),
2987 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002988 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002994
2995 NotifySensorArgs args;
2996 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2997 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2998 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2999
3000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3001 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3002 ASSERT_EQ(args.deviceId, DEVICE_ID);
3003 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3004 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3005 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3006 ASSERT_EQ(args.values, values);
3007 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3008}
3009
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010// --- KeyboardInputMapperTest ---
3011
3012class KeyboardInputMapperTest : public InputMapperTest {
3013protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003014 const std::string UNIQUE_ID = "local:0";
3015
3016 void prepareDisplay(int32_t orientation);
3017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003018 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003019 int32_t originalKeyCode, int32_t rotatedKeyCode,
3020 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021};
3022
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003023/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3024 * orientation.
3025 */
3026void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003027 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3028 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003029}
3030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003031void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003032 int32_t originalScanCode, int32_t originalKeyCode,
3033 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 NotifyKeyArgs args;
3035
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3038 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3039 ASSERT_EQ(originalScanCode, args.scanCode);
3040 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003041 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3045 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3046 ASSERT_EQ(originalScanCode, args.scanCode);
3047 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003048 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049}
3050
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003052 KeyboardInputMapper& mapper =
3053 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3054 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003056 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057}
3058
3059TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3060 const int32_t USAGE_A = 0x070004;
3061 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003062 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3063 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003064 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3065 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3066 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003068 KeyboardInputMapper& mapper =
3069 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3070 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003071 // Initial metastate to AMETA_NONE.
3072 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3073 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
3075 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 NotifyKeyArgs args;
3078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3079 ASSERT_EQ(DEVICE_ID, args.deviceId);
3080 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3081 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3083 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3084 ASSERT_EQ(KEY_HOME, args.scanCode);
3085 ASSERT_EQ(AMETA_NONE, args.metaState);
3086 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3087 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3088 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3089
3090 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003091 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3093 ASSERT_EQ(DEVICE_ID, args.deviceId);
3094 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3095 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3096 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3097 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3098 ASSERT_EQ(KEY_HOME, args.scanCode);
3099 ASSERT_EQ(AMETA_NONE, args.metaState);
3100 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3101 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3102 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3103
3104 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3108 ASSERT_EQ(DEVICE_ID, args.deviceId);
3109 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3110 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3111 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3112 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3113 ASSERT_EQ(0, args.scanCode);
3114 ASSERT_EQ(AMETA_NONE, args.metaState);
3115 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3116 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3117 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3118
3119 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3121 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3123 ASSERT_EQ(DEVICE_ID, args.deviceId);
3124 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3125 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3126 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3127 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3128 ASSERT_EQ(0, args.scanCode);
3129 ASSERT_EQ(AMETA_NONE, args.metaState);
3130 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3131 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3132 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3133
3134 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3138 ASSERT_EQ(DEVICE_ID, args.deviceId);
3139 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3140 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3141 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3142 ASSERT_EQ(0, args.keyCode);
3143 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3144 ASSERT_EQ(AMETA_NONE, args.metaState);
3145 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3146 ASSERT_EQ(0U, args.policyFlags);
3147 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3148
3149 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3151 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3153 ASSERT_EQ(DEVICE_ID, args.deviceId);
3154 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3155 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3156 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3157 ASSERT_EQ(0, args.keyCode);
3158 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3159 ASSERT_EQ(AMETA_NONE, args.metaState);
3160 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3161 ASSERT_EQ(0U, args.policyFlags);
3162 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3163}
3164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003165/**
3166 * Ensure that the readTime is set to the time when the EV_KEY is received.
3167 */
3168TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3170
3171 KeyboardInputMapper& mapper =
3172 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3173 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3174 NotifyKeyArgs args;
3175
3176 // Key down
3177 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3179 ASSERT_EQ(12, args.readTime);
3180
3181 // Key up
3182 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3184 ASSERT_EQ(15, args.readTime);
3185}
3186
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003188 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003190 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3191 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3192 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003194 KeyboardInputMapper& mapper =
3195 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3196 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197
arthurhungc903df12020-08-11 15:08:42 +08003198 // Initial metastate to AMETA_NONE.
3199 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3200 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201
3202 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 NotifyKeyArgs args;
3205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3206 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003207 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003208 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209
3210 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003211 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3213 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215
3216 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003217 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221
3222 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3225 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003226 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003227 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228}
3229
3230TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003231 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3233 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3234 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003236 KeyboardInputMapper& mapper =
3237 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3238 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003240 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3242 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3243 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3244 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3245 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3246 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3247 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3248 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3249}
3250
3251TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003252 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3253 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3254 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3255 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003258 KeyboardInputMapper& mapper =
3259 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3260 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003262 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003263 ASSERT_NO_FATAL_FAILURE(
3264 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3265 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3266 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3267 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3268 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3269 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3270 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003272 clearViewports();
3273 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003274 ASSERT_NO_FATAL_FAILURE(
3275 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3276 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3277 AKEYCODE_DPAD_UP, DISPLAY_ID));
3278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3279 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3280 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3281 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003283 clearViewports();
3284 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003285 ASSERT_NO_FATAL_FAILURE(
3286 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3287 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3288 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3289 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3290 AKEYCODE_DPAD_UP, DISPLAY_ID));
3291 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3292 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003294 clearViewports();
3295 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003296 ASSERT_NO_FATAL_FAILURE(
3297 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3298 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3299 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3300 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3301 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3302 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3303 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304
3305 // Special case: if orientation changes while key is down, we still emit the same keycode
3306 // in the key up as we did in the key down.
3307 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003308 clearViewports();
3309 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3312 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3313 ASSERT_EQ(KEY_UP, args.scanCode);
3314 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3315
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003316 clearViewports();
3317 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3321 ASSERT_EQ(KEY_UP, args.scanCode);
3322 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3323}
3324
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003325TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3326 // If the keyboard is not orientation aware,
3327 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003328 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003329
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 KeyboardInputMapper& mapper =
3331 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3332 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003333 NotifyKeyArgs args;
3334
3335 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3340 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3341
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003342 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3347 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3348}
3349
3350TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3351 // If the keyboard is orientation aware,
3352 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003353 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003354
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003355 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 KeyboardInputMapper& mapper =
3357 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3358 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003359 NotifyKeyArgs args;
3360
3361 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3362 // ^--- already checked by the previous test
3363
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003364 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003365 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3370 ASSERT_EQ(DISPLAY_ID, args.displayId);
3371
3372 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003373 clearViewports();
3374 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003375 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3380 ASSERT_EQ(newDisplayId, args.displayId);
3381}
3382
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003384 KeyboardInputMapper& mapper =
3385 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3386 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003388 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003389 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003391 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003392 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393}
3394
3395TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003396 KeyboardInputMapper& mapper =
3397 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3398 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003400 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003403 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003404 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405}
3406
3407TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003408 KeyboardInputMapper& mapper =
3409 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3410 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003412 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413
3414 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3415 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003416 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417 ASSERT_TRUE(flags[0]);
3418 ASSERT_FALSE(flags[1]);
3419}
3420
3421TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003422 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3423 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3424 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3425 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3426 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003429 KeyboardInputMapper& mapper =
3430 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3431 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003432 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003433 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3434 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435
3436 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003437 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3438 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3439 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440
3441 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003444 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3445 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3446 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
3449 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003452 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3453 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3454 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003455 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456
3457 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003460 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3461 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3462 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464
3465 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003468 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3469 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3470 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003471 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
3473 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003476 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3477 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3478 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003479 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
3481 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3485 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3486 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003487 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488}
3489
Chris Yea52ade12020-08-27 16:49:20 -07003490TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3491 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3492 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3493 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3494 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3495
3496 KeyboardInputMapper& mapper =
3497 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3498 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3499
3500 // Initial metastate should be AMETA_NONE as no meta keys added.
3501 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3502 // Meta state should be AMETA_NONE after reset
3503 mapper.reset(ARBITRARY_TIME);
3504 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3505 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3506 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3507 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3508
3509 NotifyKeyArgs args;
3510 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3513 ASSERT_EQ(AMETA_NONE, args.metaState);
3514 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3515 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3516 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3517
3518 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(AMETA_NONE, args.metaState);
3522 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3523 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3524 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3525}
3526
Arthur Hung2c9a3342019-07-23 14:18:59 +08003527TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3528 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003529 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3530 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3531 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3532 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003533
3534 // keyboard 2.
3535 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003536 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003537 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003538 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003539 std::shared_ptr<InputDevice> device2 =
3540 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3541 Flags<InputDeviceClass>(0));
3542
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003543 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3544 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3545 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3546 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003547
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 KeyboardInputMapper& mapper =
3549 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3550 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003553 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003555 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3556 device2->reset(ARBITRARY_TIME);
3557
3558 // Prepared displays and associated info.
3559 constexpr uint8_t hdmi1 = 0;
3560 constexpr uint8_t hdmi2 = 1;
3561 const std::string SECONDARY_UNIQUE_ID = "local:1";
3562
3563 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3564 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3565
3566 // No associated display viewport found, should disable the device.
3567 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3568 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3569 ASSERT_FALSE(device2->isEnabled());
3570
3571 // Prepare second display.
3572 constexpr int32_t newDisplayId = 2;
3573 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003574 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003575 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003576 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003577 // Default device will reconfigure above, need additional reconfiguration for another device.
3578 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3579 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3580
3581 // Device should be enabled after the associated display is found.
3582 ASSERT_TRUE(mDevice->isEnabled());
3583 ASSERT_TRUE(device2->isEnabled());
3584
3585 // Test pad key events
3586 ASSERT_NO_FATAL_FAILURE(
3587 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3589 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3590 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3591 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3593 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3594
3595 ASSERT_NO_FATAL_FAILURE(
3596 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3597 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3598 AKEYCODE_DPAD_RIGHT, newDisplayId));
3599 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3600 AKEYCODE_DPAD_DOWN, newDisplayId));
3601 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3602 AKEYCODE_DPAD_LEFT, newDisplayId));
3603}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604
arthurhungc903df12020-08-11 15:08:42 +08003605TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3606 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3607 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3608 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3609 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3611 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3612
3613 KeyboardInputMapper& mapper =
3614 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3615 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3616 // Initial metastate to AMETA_NONE.
3617 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3618 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3619
3620 // Initialization should have turned all of the lights off.
3621 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3622 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3623 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3624
3625 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003628 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3629 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3630
3631 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003634 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3635 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3636
3637 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003640 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3641 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3642
3643 mFakeEventHub->removeDevice(EVENTHUB_ID);
3644 mReader->loopOnce();
3645
3646 // keyboard 2 should default toggle keys.
3647 const std::string USB2 = "USB2";
3648 const std::string DEVICE_NAME2 = "KEYBOARD2";
3649 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3650 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3651 std::shared_ptr<InputDevice> device2 =
3652 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3653 Flags<InputDeviceClass>(0));
3654 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3655 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3656 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3657 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3658 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3659 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3660
arthurhung6fe95782020-10-05 22:41:16 +08003661 KeyboardInputMapper& mapper2 =
3662 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3663 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003664 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3665 device2->reset(ARBITRARY_TIME);
3666
3667 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3668 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3669 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003670 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3671 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003672}
3673
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003674// --- KeyboardInputMapperTest_ExternalDevice ---
3675
3676class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3677protected:
Chris Yea52ade12020-08-27 16:49:20 -07003678 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003679};
3680
3681TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003682 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3683 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003684
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3686 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3687 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3688 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003689
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003690 KeyboardInputMapper& mapper =
3691 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3692 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003693
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003695 NotifyKeyArgs args;
3696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3697 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3698
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003699 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3701 ASSERT_EQ(uint32_t(0), args.policyFlags);
3702
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3705 ASSERT_EQ(uint32_t(0), args.policyFlags);
3706
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003707 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3709 ASSERT_EQ(uint32_t(0), args.policyFlags);
3710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3713 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3714
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003715 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3717 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3718}
3719
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003720TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003721 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003723 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3724 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3725 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003726
Powei Fengd041c5d2019-05-03 17:11:33 -07003727 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 KeyboardInputMapper& mapper =
3729 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3730 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003731
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003733 NotifyKeyArgs args;
3734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3735 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3736
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003737 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3739 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3743 ASSERT_EQ(uint32_t(0), args.policyFlags);
3744
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003745 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3747 ASSERT_EQ(uint32_t(0), args.policyFlags);
3748
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3751 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003753 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3755 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3756}
3757
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758// --- CursorInputMapperTest ---
3759
3760class CursorInputMapperTest : public InputMapperTest {
3761protected:
3762 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3763
Michael Wright17db18e2020-06-26 20:51:44 +01003764 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765
Chris Yea52ade12020-08-27 16:49:20 -07003766 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767 InputMapperTest::SetUp();
3768
Michael Wright17db18e2020-06-26 20:51:44 +01003769 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003770 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771 }
3772
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003773 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3774 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003775
3776 void prepareDisplay(int32_t orientation) {
3777 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003778 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003779 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3780 orientation, uniqueId, NO_PORT, viewportType);
3781 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782};
3783
3784const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003786void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3787 int32_t originalY, int32_t rotatedX,
3788 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789 NotifyMotionArgs args;
3790
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3797 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3798 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3799 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3800}
3801
3802TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807}
3808
3809TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003813 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814}
3815
3816TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003818 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819
3820 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003821 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822
3823 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003824 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3825 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3827 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3828
3829 // When the bounds are set, then there should be a valid motion range.
3830 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3831
3832 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003833 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834
3835 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3836 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3837 1, 800 - 1, 0.0f, 0.0f));
3838 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3839 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3840 2, 480 - 1, 0.0f, 0.0f));
3841 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3842 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3843 0.0f, 1.0f, 0.0f, 0.0f));
3844}
3845
3846TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003848 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849
3850 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003851 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852
3853 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3854 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3855 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3856 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3857 AINPUT_MOTION_RANGE_Y, 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_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3861 0.0f, 1.0f, 0.0f, 0.0f));
3862}
3863
3864TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867
arthurhungdcef2dc2020-08-11 14:47:50 +08003868 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869
3870 NotifyMotionArgs args;
3871
3872 // Button press.
3873 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3877 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3878 ASSERT_EQ(DEVICE_ID, args.deviceId);
3879 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3880 ASSERT_EQ(uint32_t(0), args.policyFlags);
3881 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3882 ASSERT_EQ(0, args.flags);
3883 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3884 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3885 ASSERT_EQ(0, args.edgeFlags);
3886 ASSERT_EQ(uint32_t(1), args.pointerCount);
3887 ASSERT_EQ(0, args.pointerProperties[0].id);
3888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3890 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3891 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3892 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3893 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3894
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3896 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3897 ASSERT_EQ(DEVICE_ID, args.deviceId);
3898 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3899 ASSERT_EQ(uint32_t(0), args.policyFlags);
3900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3901 ASSERT_EQ(0, args.flags);
3902 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3903 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3904 ASSERT_EQ(0, args.edgeFlags);
3905 ASSERT_EQ(uint32_t(1), args.pointerCount);
3906 ASSERT_EQ(0, args.pointerProperties[0].id);
3907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3909 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3910 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3911 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3912 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3913
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003915 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3916 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3918 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3919 ASSERT_EQ(DEVICE_ID, args.deviceId);
3920 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3921 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3923 ASSERT_EQ(0, args.flags);
3924 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3925 ASSERT_EQ(0, args.buttonState);
3926 ASSERT_EQ(0, args.edgeFlags);
3927 ASSERT_EQ(uint32_t(1), args.pointerCount);
3928 ASSERT_EQ(0, args.pointerProperties[0].id);
3929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3931 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3932 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3933 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3934 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3935
3936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3937 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3938 ASSERT_EQ(DEVICE_ID, args.deviceId);
3939 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3940 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3942 ASSERT_EQ(0, args.flags);
3943 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3944 ASSERT_EQ(0, args.buttonState);
3945 ASSERT_EQ(0, args.edgeFlags);
3946 ASSERT_EQ(uint32_t(1), args.pointerCount);
3947 ASSERT_EQ(0, args.pointerProperties[0].id);
3948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3950 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3951 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3952 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3953 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3954}
3955
3956TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003958 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959
3960 NotifyMotionArgs args;
3961
3962 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3968 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3969
3970 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3976 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3977}
3978
3979TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003981 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982
3983 NotifyMotionArgs args;
3984
3985 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3991 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3992
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3996 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3997
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4004 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4005
4006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4009 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4010}
4011
4012TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004014 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015
4016 NotifyMotionArgs args;
4017
4018 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4024 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4026 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4027 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4028
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4032 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4033 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4034
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4042 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4043 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4044
4045 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4051 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4052
4053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4056 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4057}
4058
4059TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004061 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004063 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4065 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4066 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4067 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4071 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4072}
4073
4074TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 addConfigurationProperty("cursor.mode", "navigation");
4076 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004077 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004079 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4082 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4083 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4084 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4085 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4086 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4087 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4088
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004089 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4091 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4098
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004099 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4108
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004109 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4118}
4119
4120TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004122 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123
4124 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4125 mFakePointerController->setPosition(100, 200);
4126 mFakePointerController->setButtonState(0);
4127
4128 NotifyMotionArgs motionArgs;
4129 NotifyKeyArgs keyArgs;
4130
4131 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4135 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4136 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4137 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4139 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4140
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4143 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4144 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4146 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4147
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004151 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 ASSERT_EQ(0, motionArgs.buttonState);
4153 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4155 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4156
4157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004158 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 ASSERT_EQ(0, motionArgs.buttonState);
4160 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4162 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4163
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004166 ASSERT_EQ(0, motionArgs.buttonState);
4167 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4169 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4170
4171 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4177 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4178 motionArgs.buttonState);
4179 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4180 mFakePointerController->getButtonState());
4181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4182 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4183
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4186 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4187 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4188 mFakePointerController->getButtonState());
4189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4190 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4191
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4194 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4195 motionArgs.buttonState);
4196 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4197 mFakePointerController->getButtonState());
4198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4199 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4200
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4206 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4208 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4209
4210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004212 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4213 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4215 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4216
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4221 ASSERT_EQ(0, motionArgs.buttonState);
4222 ASSERT_EQ(0, mFakePointerController->getButtonState());
4223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4224 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 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227
4228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 ASSERT_EQ(0, motionArgs.buttonState);
4230 ASSERT_EQ(0, mFakePointerController->getButtonState());
4231 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4233 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 -08004234
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(0, motionArgs.buttonState);
4237 ASSERT_EQ(0, mFakePointerController->getButtonState());
4238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4240 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4241
4242 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4246 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4247 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4252 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4254 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4255
4256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4258 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4259 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4261 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4262
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_EQ(0, motionArgs.buttonState);
4268 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4270 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4271
4272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004274 ASSERT_EQ(0, motionArgs.buttonState);
4275 ASSERT_EQ(0, mFakePointerController->getButtonState());
4276
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4278 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4280 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4281 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4282
4283 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4287 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4288 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004289
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4293 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4295 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4296
4297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4299 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4300 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4302 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4303
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_EQ(0, motionArgs.buttonState);
4309 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4311 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 -08004312
4313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4315 ASSERT_EQ(0, motionArgs.buttonState);
4316 ASSERT_EQ(0, mFakePointerController->getButtonState());
4317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4318 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4319
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4321 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4322 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4323
4324 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4328 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4329 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004332 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4334 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4336 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4337
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4340 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4341 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4343 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4344
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_EQ(0, motionArgs.buttonState);
4350 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4352 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 -08004353
4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4356 ASSERT_EQ(0, motionArgs.buttonState);
4357 ASSERT_EQ(0, mFakePointerController->getButtonState());
4358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4359 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4360
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4362 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4363 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4364
4365 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4369 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4370 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4375 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4377 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4378
4379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4381 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4382 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4384 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4385
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_EQ(0, motionArgs.buttonState);
4391 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4393 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 -08004394
4395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4397 ASSERT_EQ(0, motionArgs.buttonState);
4398 ASSERT_EQ(0, mFakePointerController->getButtonState());
4399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4400 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4401
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4403 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4404 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4405}
4406
4407TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004409 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410
4411 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4412 mFakePointerController->setPosition(100, 200);
4413 mFakePointerController->setButtonState(0);
4414
4415 NotifyMotionArgs args;
4416
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004421 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4424 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 +01004425 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004426}
4427
4428TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004429 addConfigurationProperty("cursor.mode", "pointer");
4430 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004431 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004432
4433 NotifyDeviceResetArgs resetArgs;
4434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4435 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4436 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4437
4438 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4439 mFakePointerController->setPosition(100, 200);
4440 mFakePointerController->setButtonState(0);
4441
4442 NotifyMotionArgs args;
4443
4444 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4452 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 +01004453 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004454
4455 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4459 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4460 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4462 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4464 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4467 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4468
4469 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004470 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4471 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4473 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4476 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4478 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4481 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4482
4483 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4488 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4491 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 +01004492 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004493
4494 // Disable pointer capture and check that the device generation got bumped
4495 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004496 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004497 mFakePolicy->setPointerCapture(false);
4498 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004499 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004500
4501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4502 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4503 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4509 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4512 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 +01004513 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514}
4515
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004516TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004517 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004518
Garfield Tan888a6a42020-01-09 11:39:16 -08004519 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004520 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004521 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4522 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004523 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4524 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004525 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4526 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4527
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004528 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4529 mFakePointerController->setPosition(100, 200);
4530 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004531
4532 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4537 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4540 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004541 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004542 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4543}
4544
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545// --- TouchInputMapperTest ---
4546
4547class TouchInputMapperTest : public InputMapperTest {
4548protected:
4549 static const int32_t RAW_X_MIN;
4550 static const int32_t RAW_X_MAX;
4551 static const int32_t RAW_Y_MIN;
4552 static const int32_t RAW_Y_MAX;
4553 static const int32_t RAW_TOUCH_MIN;
4554 static const int32_t RAW_TOUCH_MAX;
4555 static const int32_t RAW_TOOL_MIN;
4556 static const int32_t RAW_TOOL_MAX;
4557 static const int32_t RAW_PRESSURE_MIN;
4558 static const int32_t RAW_PRESSURE_MAX;
4559 static const int32_t RAW_ORIENTATION_MIN;
4560 static const int32_t RAW_ORIENTATION_MAX;
4561 static const int32_t RAW_DISTANCE_MIN;
4562 static const int32_t RAW_DISTANCE_MAX;
4563 static const int32_t RAW_TILT_MIN;
4564 static const int32_t RAW_TILT_MAX;
4565 static const int32_t RAW_ID_MIN;
4566 static const int32_t RAW_ID_MAX;
4567 static const int32_t RAW_SLOT_MIN;
4568 static const int32_t RAW_SLOT_MAX;
4569 static const float X_PRECISION;
4570 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004571 static const float X_PRECISION_VIRTUAL;
4572 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573
4574 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004575 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576
4577 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4578
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004579 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004580 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004581
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 enum Axes {
4583 POSITION = 1 << 0,
4584 TOUCH = 1 << 1,
4585 TOOL = 1 << 2,
4586 PRESSURE = 1 << 3,
4587 ORIENTATION = 1 << 4,
4588 MINOR = 1 << 5,
4589 ID = 1 << 6,
4590 DISTANCE = 1 << 7,
4591 TILT = 1 << 8,
4592 SLOT = 1 << 9,
4593 TOOL_TYPE = 1 << 10,
4594 };
4595
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004596 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4597 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004598 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004600 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 int32_t toRawX(float displayX);
4602 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004603 float toCookedX(float rawX, float rawY);
4604 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004606 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004608 float toDisplayY(int32_t rawY, int32_t displayHeight);
4609
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610};
4611
4612const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4613const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4614const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4615const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4616const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4617const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4618const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4619const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004620const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4621const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4623const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4624const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4625const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4626const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4627const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4628const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4629const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4630const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4631const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4632const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4633const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004634const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4635 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4636const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4637 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004638const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4639 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640
4641const float TouchInputMapperTest::GEOMETRIC_SCALE =
4642 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4643 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4644
4645const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4646 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4647 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4648};
4649
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004650void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004651 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4652 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004653}
4654
4655void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4656 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4657 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658}
4659
Santos Cordonfa5cf462017-04-05 10:37:00 -07004660void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004661 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4662 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4663 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004664}
4665
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004667 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4668 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4669 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4670 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671}
4672
Jason Gerecke489fda82012-09-07 17:19:40 -07004673void TouchInputMapperTest::prepareLocationCalibration() {
4674 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4675}
4676
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677int32_t TouchInputMapperTest::toRawX(float displayX) {
4678 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4679}
4680
4681int32_t TouchInputMapperTest::toRawY(float displayY) {
4682 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4683}
4684
Jason Gerecke489fda82012-09-07 17:19:40 -07004685float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4686 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4687 return rawX;
4688}
4689
4690float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4691 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4692 return rawY;
4693}
4694
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004696 return toDisplayX(rawX, DISPLAY_WIDTH);
4697}
4698
4699float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4700 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701}
4702
4703float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004704 return toDisplayY(rawY, DISPLAY_HEIGHT);
4705}
4706
4707float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4708 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709}
4710
4711
4712// --- SingleTouchInputMapperTest ---
4713
4714class SingleTouchInputMapperTest : public TouchInputMapperTest {
4715protected:
4716 void prepareButtons();
4717 void prepareAxes(int axes);
4718
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004719 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4720 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4721 void processUp(SingleTouchInputMapper& mappery);
4722 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4723 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4724 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4725 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4726 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4727 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728};
4729
4730void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004731 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732}
4733
4734void SingleTouchInputMapperTest::prepareAxes(int axes) {
4735 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004736 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4737 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 }
4739 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004740 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4741 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 }
4743 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004744 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4745 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004746 }
4747 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004748 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4749 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750 }
4751 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004752 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4753 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 }
4755}
4756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004757void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761}
4762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004763void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766}
4767
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004768void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770}
4771
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004772void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774}
4775
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004776void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4777 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779}
4780
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004781void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783}
4784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004785void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4786 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789}
4790
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004791void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4792 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794}
4795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004796void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798}
4799
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 prepareButtons();
4802 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004803 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004805 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806}
4807
4808TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004809 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4810 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 prepareButtons();
4812 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004813 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004815 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816}
4817
4818TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 prepareButtons();
4820 prepareAxes(POSITION);
4821 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004822 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004824 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825}
4826
4827TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 prepareButtons();
4829 prepareAxes(POSITION);
4830 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004831 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004833 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834}
4835
4836TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 addConfigurationProperty("touch.deviceType", "touchScreen");
4838 prepareDisplay(DISPLAY_ORIENTATION_0);
4839 prepareButtons();
4840 prepareAxes(POSITION);
4841 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004842 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843
4844 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004845 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846
4847 // Virtual key is down.
4848 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4849 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4850 processDown(mapper, x, y);
4851 processSync(mapper);
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004854 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855
4856 // Virtual key is up.
4857 processUp(mapper);
4858 processSync(mapper);
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4860
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004861 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862}
4863
4864TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 addConfigurationProperty("touch.deviceType", "touchScreen");
4866 prepareDisplay(DISPLAY_ORIENTATION_0);
4867 prepareButtons();
4868 prepareAxes(POSITION);
4869 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004870 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871
4872 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004873 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874
4875 // Virtual key is down.
4876 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4877 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4878 processDown(mapper, x, y);
4879 processSync(mapper);
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004882 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
4884 // Virtual key is up.
4885 processUp(mapper);
4886 processSync(mapper);
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004889 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890}
4891
4892TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 addConfigurationProperty("touch.deviceType", "touchScreen");
4894 prepareDisplay(DISPLAY_ORIENTATION_0);
4895 prepareButtons();
4896 prepareAxes(POSITION);
4897 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004898 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899
4900 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4901 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903 ASSERT_TRUE(flags[0]);
4904 ASSERT_FALSE(flags[1]);
4905}
4906
4907TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 addConfigurationProperty("touch.deviceType", "touchScreen");
4909 prepareDisplay(DISPLAY_ORIENTATION_0);
4910 prepareButtons();
4911 prepareAxes(POSITION);
4912 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004913 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914
arthurhungdcef2dc2020-08-11 14:47:50 +08004915 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916
4917 NotifyKeyArgs args;
4918
4919 // Press virtual key.
4920 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4921 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4922 processDown(mapper, x, y);
4923 processSync(mapper);
4924
4925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4926 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4927 ASSERT_EQ(DEVICE_ID, args.deviceId);
4928 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4929 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4930 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4931 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4932 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4933 ASSERT_EQ(KEY_HOME, args.scanCode);
4934 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4935 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4936
4937 // Release virtual key.
4938 processUp(mapper);
4939 processSync(mapper);
4940
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4942 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4943 ASSERT_EQ(DEVICE_ID, args.deviceId);
4944 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4945 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4946 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4947 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4948 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4949 ASSERT_EQ(KEY_HOME, args.scanCode);
4950 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4951 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4952
4953 // Should not have sent any motions.
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4955}
4956
4957TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 addConfigurationProperty("touch.deviceType", "touchScreen");
4959 prepareDisplay(DISPLAY_ORIENTATION_0);
4960 prepareButtons();
4961 prepareAxes(POSITION);
4962 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004963 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964
arthurhungdcef2dc2020-08-11 14:47:50 +08004965 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966
4967 NotifyKeyArgs keyArgs;
4968
4969 // Press virtual key.
4970 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4971 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4972 processDown(mapper, x, y);
4973 processSync(mapper);
4974
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4976 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4977 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4978 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4979 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4980 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4981 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4982 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4983 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4985 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4986
4987 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4988 // into the display area.
4989 y -= 100;
4990 processMove(mapper, x, y);
4991 processSync(mapper);
4992
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4994 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4995 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4996 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4997 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4998 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4999 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5000 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5001 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5002 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5004 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5005
5006 NotifyMotionArgs motionArgs;
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5009 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5010 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5011 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5013 ASSERT_EQ(0, motionArgs.flags);
5014 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5015 ASSERT_EQ(0, motionArgs.buttonState);
5016 ASSERT_EQ(0, motionArgs.edgeFlags);
5017 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5018 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5021 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5022 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5023 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5024 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5025
5026 // Keep moving out of bounds. Should generate a pointer move.
5027 y -= 50;
5028 processMove(mapper, x, y);
5029 processSync(mapper);
5030
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5032 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5033 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5034 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5035 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5037 ASSERT_EQ(0, motionArgs.flags);
5038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5039 ASSERT_EQ(0, motionArgs.buttonState);
5040 ASSERT_EQ(0, motionArgs.edgeFlags);
5041 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5042 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5045 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5046 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5047 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5049
5050 // Release out of bounds. Should generate a pointer up.
5051 processUp(mapper);
5052 processSync(mapper);
5053
5054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5055 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5056 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5057 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5058 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5059 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5060 ASSERT_EQ(0, motionArgs.flags);
5061 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5062 ASSERT_EQ(0, motionArgs.buttonState);
5063 ASSERT_EQ(0, motionArgs.edgeFlags);
5064 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5065 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5068 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5069 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5070 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5071 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5072
5073 // Should not have sent any more keys or motions.
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5076}
5077
5078TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079 addConfigurationProperty("touch.deviceType", "touchScreen");
5080 prepareDisplay(DISPLAY_ORIENTATION_0);
5081 prepareButtons();
5082 prepareAxes(POSITION);
5083 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005084 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085
arthurhungdcef2dc2020-08-11 14:47:50 +08005086 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087
5088 NotifyMotionArgs motionArgs;
5089
5090 // Initially go down out of bounds.
5091 int32_t x = -10;
5092 int32_t y = -10;
5093 processDown(mapper, x, y);
5094 processSync(mapper);
5095
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5097
5098 // Move into the display area. Should generate a pointer down.
5099 x = 50;
5100 y = 75;
5101 processMove(mapper, x, y);
5102 processSync(mapper);
5103
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5105 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5106 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5107 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5108 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5109 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5110 ASSERT_EQ(0, motionArgs.flags);
5111 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5112 ASSERT_EQ(0, motionArgs.buttonState);
5113 ASSERT_EQ(0, motionArgs.edgeFlags);
5114 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5115 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5118 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5119 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5120 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5121 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5122
5123 // Release. Should generate a pointer up.
5124 processUp(mapper);
5125 processSync(mapper);
5126
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5129 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5130 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5131 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5132 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5133 ASSERT_EQ(0, motionArgs.flags);
5134 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5135 ASSERT_EQ(0, motionArgs.buttonState);
5136 ASSERT_EQ(0, motionArgs.edgeFlags);
5137 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5138 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5141 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5142 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5143 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5144 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5145
5146 // Should not have sent any more keys or motions.
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5149}
5150
Santos Cordonfa5cf462017-04-05 10:37:00 -07005151TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005152 addConfigurationProperty("touch.deviceType", "touchScreen");
5153 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5154
5155 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5156 prepareButtons();
5157 prepareAxes(POSITION);
5158 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005159 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005160
arthurhungdcef2dc2020-08-11 14:47:50 +08005161 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005162
5163 NotifyMotionArgs motionArgs;
5164
5165 // Down.
5166 int32_t x = 100;
5167 int32_t y = 125;
5168 processDown(mapper, x, y);
5169 processSync(mapper);
5170
5171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5173 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5174 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5175 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5176 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5178 ASSERT_EQ(0, motionArgs.flags);
5179 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5180 ASSERT_EQ(0, motionArgs.buttonState);
5181 ASSERT_EQ(0, motionArgs.edgeFlags);
5182 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5183 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5186 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5187 1, 0, 0, 0, 0, 0, 0, 0));
5188 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5189 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5190 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5191
5192 // Move.
5193 x += 50;
5194 y += 75;
5195 processMove(mapper, x, y);
5196 processSync(mapper);
5197
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5199 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5200 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5201 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5202 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5203 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5205 ASSERT_EQ(0, motionArgs.flags);
5206 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5207 ASSERT_EQ(0, motionArgs.buttonState);
5208 ASSERT_EQ(0, motionArgs.edgeFlags);
5209 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5210 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5213 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5214 1, 0, 0, 0, 0, 0, 0, 0));
5215 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5216 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5218
5219 // Up.
5220 processUp(mapper);
5221 processSync(mapper);
5222
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5225 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5226 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5227 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5228 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5229 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5230 ASSERT_EQ(0, motionArgs.flags);
5231 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5232 ASSERT_EQ(0, motionArgs.buttonState);
5233 ASSERT_EQ(0, motionArgs.edgeFlags);
5234 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5235 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5238 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5239 1, 0, 0, 0, 0, 0, 0, 0));
5240 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5241 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5242 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5243
5244 // Should not have sent any more keys or motions.
5245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5247}
5248
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250 addConfigurationProperty("touch.deviceType", "touchScreen");
5251 prepareDisplay(DISPLAY_ORIENTATION_0);
5252 prepareButtons();
5253 prepareAxes(POSITION);
5254 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005255 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256
arthurhungdcef2dc2020-08-11 14:47:50 +08005257 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258
5259 NotifyMotionArgs motionArgs;
5260
5261 // Down.
5262 int32_t x = 100;
5263 int32_t y = 125;
5264 processDown(mapper, x, y);
5265 processSync(mapper);
5266
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5269 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5270 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5271 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5272 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5273 ASSERT_EQ(0, motionArgs.flags);
5274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5275 ASSERT_EQ(0, motionArgs.buttonState);
5276 ASSERT_EQ(0, motionArgs.edgeFlags);
5277 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5278 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5281 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5282 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5283 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5285
5286 // Move.
5287 x += 50;
5288 y += 75;
5289 processMove(mapper, x, y);
5290 processSync(mapper);
5291
5292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5298 ASSERT_EQ(0, motionArgs.flags);
5299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5300 ASSERT_EQ(0, motionArgs.buttonState);
5301 ASSERT_EQ(0, motionArgs.edgeFlags);
5302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5306 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5310
5311 // Up.
5312 processUp(mapper);
5313 processSync(mapper);
5314
5315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5317 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5318 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5319 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5320 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5321 ASSERT_EQ(0, motionArgs.flags);
5322 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5323 ASSERT_EQ(0, motionArgs.buttonState);
5324 ASSERT_EQ(0, motionArgs.edgeFlags);
5325 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5329 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5330 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5331 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5333
5334 // Should not have sent any more keys or motions.
5335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5337}
5338
5339TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 addConfigurationProperty("touch.deviceType", "touchScreen");
5341 prepareButtons();
5342 prepareAxes(POSITION);
5343 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345
5346 NotifyMotionArgs args;
5347
5348 // Rotation 90.
5349 prepareDisplay(DISPLAY_ORIENTATION_90);
5350 processDown(mapper, toRawX(50), toRawY(75));
5351 processSync(mapper);
5352
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5354 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5355 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5356
5357 processUp(mapper);
5358 processSync(mapper);
5359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5360}
5361
5362TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363 addConfigurationProperty("touch.deviceType", "touchScreen");
5364 prepareButtons();
5365 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367
5368 NotifyMotionArgs args;
5369
5370 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005371 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372 prepareDisplay(DISPLAY_ORIENTATION_0);
5373 processDown(mapper, toRawX(50), toRawY(75));
5374 processSync(mapper);
5375
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5377 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5378 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5379
5380 processUp(mapper);
5381 processSync(mapper);
5382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5383
5384 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005385 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386 prepareDisplay(DISPLAY_ORIENTATION_90);
5387 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5388 processSync(mapper);
5389
5390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5391 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5392 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5393
5394 processUp(mapper);
5395 processSync(mapper);
5396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5397
5398 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005399 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 prepareDisplay(DISPLAY_ORIENTATION_180);
5401 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5402 processSync(mapper);
5403
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5405 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5406 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5407
5408 processUp(mapper);
5409 processSync(mapper);
5410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5411
5412 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005413 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 prepareDisplay(DISPLAY_ORIENTATION_270);
5415 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5416 processSync(mapper);
5417
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5419 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5420 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5421
5422 processUp(mapper);
5423 processSync(mapper);
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5425}
5426
5427TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 addConfigurationProperty("touch.deviceType", "touchScreen");
5429 prepareDisplay(DISPLAY_ORIENTATION_0);
5430 prepareButtons();
5431 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005432 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433
5434 // These calculations are based on the input device calibration documentation.
5435 int32_t rawX = 100;
5436 int32_t rawY = 200;
5437 int32_t rawPressure = 10;
5438 int32_t rawToolMajor = 12;
5439 int32_t rawDistance = 2;
5440 int32_t rawTiltX = 30;
5441 int32_t rawTiltY = 110;
5442
5443 float x = toDisplayX(rawX);
5444 float y = toDisplayY(rawY);
5445 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5446 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5447 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5448 float distance = float(rawDistance);
5449
5450 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5451 float tiltScale = M_PI / 180;
5452 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5453 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5454 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5455 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5456
5457 processDown(mapper, rawX, rawY);
5458 processPressure(mapper, rawPressure);
5459 processToolMajor(mapper, rawToolMajor);
5460 processDistance(mapper, rawDistance);
5461 processTilt(mapper, rawTiltX, rawTiltY);
5462 processSync(mapper);
5463
5464 NotifyMotionArgs args;
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5467 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5468 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5469}
5470
Jason Gerecke489fda82012-09-07 17:19:40 -07005471TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005472 addConfigurationProperty("touch.deviceType", "touchScreen");
5473 prepareDisplay(DISPLAY_ORIENTATION_0);
5474 prepareLocationCalibration();
5475 prepareButtons();
5476 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005477 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005478
5479 int32_t rawX = 100;
5480 int32_t rawY = 200;
5481
5482 float x = toDisplayX(toCookedX(rawX, rawY));
5483 float y = toDisplayY(toCookedY(rawX, rawY));
5484
5485 processDown(mapper, rawX, rawY);
5486 processSync(mapper);
5487
5488 NotifyMotionArgs args;
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5491 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5492}
5493
Michael Wrightd02c5b62014-02-10 15:10:22 -08005494TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495 addConfigurationProperty("touch.deviceType", "touchScreen");
5496 prepareDisplay(DISPLAY_ORIENTATION_0);
5497 prepareButtons();
5498 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005499 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500
5501 NotifyMotionArgs motionArgs;
5502 NotifyKeyArgs keyArgs;
5503
5504 processDown(mapper, 100, 200);
5505 processSync(mapper);
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5507 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5508 ASSERT_EQ(0, motionArgs.buttonState);
5509
5510 // press BTN_LEFT, release BTN_LEFT
5511 processKey(mapper, BTN_LEFT, 1);
5512 processSync(mapper);
5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5515 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5516
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5518 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5519 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5520
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521 processKey(mapper, BTN_LEFT, 0);
5522 processSync(mapper);
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005525 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005526
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005529 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530
5531 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5532 processKey(mapper, BTN_RIGHT, 1);
5533 processKey(mapper, BTN_MIDDLE, 1);
5534 processSync(mapper);
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5537 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5538 motionArgs.buttonState);
5539
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5542 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5543
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5546 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5547 motionArgs.buttonState);
5548
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549 processKey(mapper, BTN_RIGHT, 0);
5550 processSync(mapper);
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005554
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558
5559 processKey(mapper, BTN_MIDDLE, 0);
5560 processSync(mapper);
5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005562 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005564
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005567 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568
5569 // press BTN_BACK, release BTN_BACK
5570 processKey(mapper, BTN_BACK, 1);
5571 processSync(mapper);
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5573 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5574 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005575
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005578 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5582 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583
5584 processKey(mapper, BTN_BACK, 0);
5585 processSync(mapper);
5586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005589
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005592 ASSERT_EQ(0, motionArgs.buttonState);
5593
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5595 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5596 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5597
5598 // press BTN_SIDE, release BTN_SIDE
5599 processKey(mapper, BTN_SIDE, 1);
5600 processSync(mapper);
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5602 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5603 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005604
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005607 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5611 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612
5613 processKey(mapper, BTN_SIDE, 0);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005618
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005621 ASSERT_EQ(0, motionArgs.buttonState);
5622
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5624 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5625 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5626
5627 // press BTN_FORWARD, release BTN_FORWARD
5628 processKey(mapper, BTN_FORWARD, 1);
5629 processSync(mapper);
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5631 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5632 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005633
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005636 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5637
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5640 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641
5642 processKey(mapper, BTN_FORWARD, 0);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005645 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005647
5648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005650 ASSERT_EQ(0, motionArgs.buttonState);
5651
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5653 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5654 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5655
5656 // press BTN_EXTRA, release BTN_EXTRA
5657 processKey(mapper, BTN_EXTRA, 1);
5658 processSync(mapper);
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5660 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5661 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005662
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005665 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5666
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5669 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670
5671 processKey(mapper, BTN_EXTRA, 0);
5672 processSync(mapper);
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005676
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005679 ASSERT_EQ(0, motionArgs.buttonState);
5680
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5682 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5683 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5684
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5686
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687 // press BTN_STYLUS, release BTN_STYLUS
5688 processKey(mapper, BTN_STYLUS, 1);
5689 processSync(mapper);
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5693
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5696 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
5698 processKey(mapper, BTN_STYLUS, 0);
5699 processSync(mapper);
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005701 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005703
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707
5708 // press BTN_STYLUS2, release BTN_STYLUS2
5709 processKey(mapper, BTN_STYLUS2, 1);
5710 processSync(mapper);
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005713 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5714
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5717 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718
5719 processKey(mapper, BTN_STYLUS2, 0);
5720 processSync(mapper);
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005722 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005723 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005724
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005727 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728
5729 // release touch
5730 processUp(mapper);
5731 processSync(mapper);
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5733 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5734 ASSERT_EQ(0, motionArgs.buttonState);
5735}
5736
5737TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 addConfigurationProperty("touch.deviceType", "touchScreen");
5739 prepareDisplay(DISPLAY_ORIENTATION_0);
5740 prepareButtons();
5741 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005742 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005743
5744 NotifyMotionArgs motionArgs;
5745
5746 // default tool type is finger
5747 processDown(mapper, 100, 200);
5748 processSync(mapper);
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5750 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5752
5753 // eraser
5754 processKey(mapper, BTN_TOOL_RUBBER, 1);
5755 processSync(mapper);
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5759
5760 // stylus
5761 processKey(mapper, BTN_TOOL_RUBBER, 0);
5762 processKey(mapper, BTN_TOOL_PEN, 1);
5763 processSync(mapper);
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5767
5768 // brush
5769 processKey(mapper, BTN_TOOL_PEN, 0);
5770 processKey(mapper, BTN_TOOL_BRUSH, 1);
5771 processSync(mapper);
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5775
5776 // pencil
5777 processKey(mapper, BTN_TOOL_BRUSH, 0);
5778 processKey(mapper, BTN_TOOL_PENCIL, 1);
5779 processSync(mapper);
5780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5783
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005784 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785 processKey(mapper, BTN_TOOL_PENCIL, 0);
5786 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5787 processSync(mapper);
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5791
5792 // mouse
5793 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5794 processKey(mapper, BTN_TOOL_MOUSE, 1);
5795 processSync(mapper);
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5799
5800 // lens
5801 processKey(mapper, BTN_TOOL_MOUSE, 0);
5802 processKey(mapper, BTN_TOOL_LENS, 1);
5803 processSync(mapper);
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5807
5808 // double-tap
5809 processKey(mapper, BTN_TOOL_LENS, 0);
5810 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5811 processSync(mapper);
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5815
5816 // triple-tap
5817 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5818 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5819 processSync(mapper);
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5823
5824 // quad-tap
5825 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5826 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5827 processSync(mapper);
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5831
5832 // finger
5833 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5834 processKey(mapper, BTN_TOOL_FINGER, 1);
5835 processSync(mapper);
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5839
5840 // stylus trumps finger
5841 processKey(mapper, BTN_TOOL_PEN, 1);
5842 processSync(mapper);
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5846
5847 // eraser trumps stylus
5848 processKey(mapper, BTN_TOOL_RUBBER, 1);
5849 processSync(mapper);
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5853
5854 // mouse trumps eraser
5855 processKey(mapper, BTN_TOOL_MOUSE, 1);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5860
5861 // back to default tool type
5862 processKey(mapper, BTN_TOOL_MOUSE, 0);
5863 processKey(mapper, BTN_TOOL_RUBBER, 0);
5864 processKey(mapper, BTN_TOOL_PEN, 0);
5865 processKey(mapper, BTN_TOOL_FINGER, 0);
5866 processSync(mapper);
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5870}
5871
5872TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873 addConfigurationProperty("touch.deviceType", "touchScreen");
5874 prepareDisplay(DISPLAY_ORIENTATION_0);
5875 prepareButtons();
5876 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005877 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005878 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 NotifyMotionArgs motionArgs;
5881
5882 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5883 processKey(mapper, BTN_TOOL_FINGER, 1);
5884 processMove(mapper, 100, 200);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5887 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5889 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5890
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5894 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5895
5896 // move a little
5897 processMove(mapper, 150, 250);
5898 processSync(mapper);
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5902 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5903
5904 // down when BTN_TOUCH is pressed, pressure defaults to 1
5905 processKey(mapper, BTN_TOUCH, 1);
5906 processSync(mapper);
5907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5908 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5910 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5911
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5913 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5915 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5916
5917 // up when BTN_TOUCH is released, hover restored
5918 processKey(mapper, BTN_TOUCH, 0);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5923 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5928 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5934
5935 // exit hover when pointer goes away
5936 processKey(mapper, BTN_TOOL_FINGER, 0);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5941 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5942}
5943
5944TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 addConfigurationProperty("touch.deviceType", "touchScreen");
5946 prepareDisplay(DISPLAY_ORIENTATION_0);
5947 prepareButtons();
5948 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005949 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950
5951 NotifyMotionArgs motionArgs;
5952
5953 // initially hovering because pressure is 0
5954 processDown(mapper, 100, 200);
5955 processPressure(mapper, 0);
5956 processSync(mapper);
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5960 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5961
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5965 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5966
5967 // move a little
5968 processMove(mapper, 150, 250);
5969 processSync(mapper);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5973 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5974
5975 // down when pressure is non-zero
5976 processPressure(mapper, RAW_PRESSURE_MAX);
5977 processSync(mapper);
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5979 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5981 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5986 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5987
5988 // up when pressure becomes 0, hover restored
5989 processPressure(mapper, 0);
5990 processSync(mapper);
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5994 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5995
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5999 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6000
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6004 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6005
6006 // exit hover when pointer goes away
6007 processUp(mapper);
6008 processSync(mapper);
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6012 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6013}
6014
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015// --- MultiTouchInputMapperTest ---
6016
6017class MultiTouchInputMapperTest : public TouchInputMapperTest {
6018protected:
6019 void prepareAxes(int axes);
6020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006021 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6022 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6023 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6024 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6025 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6026 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6027 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6028 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6029 void processId(MultiTouchInputMapper& mapper, int32_t id);
6030 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6031 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6032 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6033 void processMTSync(MultiTouchInputMapper& mapper);
6034 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035};
6036
6037void MultiTouchInputMapperTest::prepareAxes(int axes) {
6038 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006039 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6040 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 }
6042 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006043 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6044 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006046 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6047 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 }
6049 }
6050 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006051 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6052 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6055 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 }
6057 }
6058 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006059 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6060 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061 }
6062 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006063 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6064 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 }
6066 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006067 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6068 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069 }
6070 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6072 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 }
6074 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6076 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077 }
6078 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 }
6081}
6082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006083void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6084 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087}
6088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006089void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6090 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092}
6093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006094void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6095 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097}
6098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006099void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101}
6102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006103void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105}
6106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006107void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6108 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110}
6111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006112void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114}
6115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006116void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118}
6119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006120void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122}
6123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006124void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126}
6127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006128void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130}
6131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006132void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6133 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135}
6136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139}
6140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006141void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143}
6144
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 addConfigurationProperty("touch.deviceType", "touchScreen");
6147 prepareDisplay(DISPLAY_ORIENTATION_0);
6148 prepareAxes(POSITION);
6149 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006150 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151
arthurhungdcef2dc2020-08-11 14:47:50 +08006152 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153
6154 NotifyMotionArgs motionArgs;
6155
6156 // Two fingers down at once.
6157 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6158 processPosition(mapper, x1, y1);
6159 processMTSync(mapper);
6160 processPosition(mapper, x2, y2);
6161 processMTSync(mapper);
6162 processSync(mapper);
6163
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6165 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6166 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6167 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6168 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6170 ASSERT_EQ(0, motionArgs.flags);
6171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6172 ASSERT_EQ(0, motionArgs.buttonState);
6173 ASSERT_EQ(0, motionArgs.edgeFlags);
6174 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6178 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6179 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6180 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6182
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6185 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6186 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6187 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6188 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6189 motionArgs.action);
6190 ASSERT_EQ(0, motionArgs.flags);
6191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6192 ASSERT_EQ(0, motionArgs.buttonState);
6193 ASSERT_EQ(0, motionArgs.edgeFlags);
6194 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6195 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6197 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6200 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6206
6207 // Move.
6208 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6209 processPosition(mapper, x1, y1);
6210 processMTSync(mapper);
6211 processPosition(mapper, x2, y2);
6212 processMTSync(mapper);
6213 processSync(mapper);
6214
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6221 ASSERT_EQ(0, motionArgs.flags);
6222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6223 ASSERT_EQ(0, motionArgs.buttonState);
6224 ASSERT_EQ(0, motionArgs.edgeFlags);
6225 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6228 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6231 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6233 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6234 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6235 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6237
6238 // First finger up.
6239 x2 += 15; y2 -= 20;
6240 processPosition(mapper, x2, y2);
6241 processMTSync(mapper);
6242 processSync(mapper);
6243
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6246 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6247 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6248 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6250 motionArgs.action);
6251 ASSERT_EQ(0, motionArgs.flags);
6252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6253 ASSERT_EQ(0, motionArgs.buttonState);
6254 ASSERT_EQ(0, motionArgs.edgeFlags);
6255 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6258 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6261 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6263 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6264 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6265 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6267
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6270 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6274 ASSERT_EQ(0, motionArgs.flags);
6275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6276 ASSERT_EQ(0, motionArgs.buttonState);
6277 ASSERT_EQ(0, motionArgs.edgeFlags);
6278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6279 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6282 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6286
6287 // Move.
6288 x2 += 20; y2 -= 25;
6289 processPosition(mapper, x2, y2);
6290 processMTSync(mapper);
6291 processSync(mapper);
6292
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6295 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6296 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6297 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6299 ASSERT_EQ(0, motionArgs.flags);
6300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6301 ASSERT_EQ(0, motionArgs.buttonState);
6302 ASSERT_EQ(0, motionArgs.edgeFlags);
6303 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6304 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6307 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6308 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6309 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6311
6312 // New finger down.
6313 int32_t x3 = 700, y3 = 300;
6314 processPosition(mapper, x2, y2);
6315 processMTSync(mapper);
6316 processPosition(mapper, x3, y3);
6317 processMTSync(mapper);
6318 processSync(mapper);
6319
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6326 motionArgs.action);
6327 ASSERT_EQ(0, motionArgs.flags);
6328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6329 ASSERT_EQ(0, motionArgs.buttonState);
6330 ASSERT_EQ(0, motionArgs.edgeFlags);
6331 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6334 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6337 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6339 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6340 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6341 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6343
6344 // Second finger up.
6345 x3 += 30; y3 -= 20;
6346 processPosition(mapper, x3, y3);
6347 processMTSync(mapper);
6348 processSync(mapper);
6349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6352 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6353 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6354 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6356 motionArgs.action);
6357 ASSERT_EQ(0, motionArgs.flags);
6358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6359 ASSERT_EQ(0, motionArgs.buttonState);
6360 ASSERT_EQ(0, motionArgs.edgeFlags);
6361 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6364 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6367 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6369 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6370 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6371 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6372 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6373
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6380 ASSERT_EQ(0, motionArgs.flags);
6381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6382 ASSERT_EQ(0, motionArgs.buttonState);
6383 ASSERT_EQ(0, motionArgs.edgeFlags);
6384 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6388 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6389 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6390 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6392
6393 // Last finger up.
6394 processMTSync(mapper);
6395 processSync(mapper);
6396
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6399 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6400 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6401 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6403 ASSERT_EQ(0, motionArgs.flags);
6404 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6405 ASSERT_EQ(0, motionArgs.buttonState);
6406 ASSERT_EQ(0, motionArgs.edgeFlags);
6407 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6408 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6411 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6412 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6413 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6415
6416 // Should not have sent any more keys or motions.
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6419}
6420
6421TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 addConfigurationProperty("touch.deviceType", "touchScreen");
6423 prepareDisplay(DISPLAY_ORIENTATION_0);
6424 prepareAxes(POSITION | ID);
6425 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006426 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427
arthurhungdcef2dc2020-08-11 14:47:50 +08006428 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429
6430 NotifyMotionArgs motionArgs;
6431
6432 // Two fingers down at once.
6433 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6434 processPosition(mapper, x1, y1);
6435 processId(mapper, 1);
6436 processMTSync(mapper);
6437 processPosition(mapper, x2, y2);
6438 processId(mapper, 2);
6439 processMTSync(mapper);
6440 processSync(mapper);
6441
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6444 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6445 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6448 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6452 motionArgs.action);
6453 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6454 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6456 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6459 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6461 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6462
6463 // Move.
6464 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6465 processPosition(mapper, x1, y1);
6466 processId(mapper, 1);
6467 processMTSync(mapper);
6468 processPosition(mapper, x2, y2);
6469 processId(mapper, 2);
6470 processMTSync(mapper);
6471 processSync(mapper);
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6475 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6476 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6478 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6481 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6483 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6484
6485 // First finger up.
6486 x2 += 15; y2 -= 20;
6487 processPosition(mapper, x2, y2);
6488 processId(mapper, 2);
6489 processMTSync(mapper);
6490 processSync(mapper);
6491
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6494 motionArgs.action);
6495 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6496 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6498 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6501 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6503 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6508 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6511 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6512
6513 // Move.
6514 x2 += 20; y2 -= 25;
6515 processPosition(mapper, x2, y2);
6516 processId(mapper, 2);
6517 processMTSync(mapper);
6518 processSync(mapper);
6519
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6522 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6523 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6526 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6527
6528 // New finger down.
6529 int32_t x3 = 700, y3 = 300;
6530 processPosition(mapper, x2, y2);
6531 processId(mapper, 2);
6532 processMTSync(mapper);
6533 processPosition(mapper, x3, y3);
6534 processId(mapper, 3);
6535 processMTSync(mapper);
6536 processSync(mapper);
6537
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6539 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6540 motionArgs.action);
6541 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6544 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6547 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6549 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6550
6551 // Second finger up.
6552 x3 += 30; y3 -= 20;
6553 processPosition(mapper, x3, y3);
6554 processId(mapper, 3);
6555 processMTSync(mapper);
6556 processSync(mapper);
6557
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6560 motionArgs.action);
6561 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6564 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6567 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6569 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6570
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6573 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6574 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6577 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6578
6579 // Last finger up.
6580 processMTSync(mapper);
6581 processSync(mapper);
6582
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6585 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6589 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6590
6591 // Should not have sent any more keys or motions.
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6594}
6595
6596TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006597 addConfigurationProperty("touch.deviceType", "touchScreen");
6598 prepareDisplay(DISPLAY_ORIENTATION_0);
6599 prepareAxes(POSITION | ID | SLOT);
6600 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006601 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006602
arthurhungdcef2dc2020-08-11 14:47:50 +08006603 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006604
6605 NotifyMotionArgs motionArgs;
6606
6607 // Two fingers down at once.
6608 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6609 processPosition(mapper, x1, y1);
6610 processId(mapper, 1);
6611 processSlot(mapper, 1);
6612 processPosition(mapper, x2, y2);
6613 processId(mapper, 2);
6614 processSync(mapper);
6615
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6618 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6619 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6622 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6623
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6626 motionArgs.action);
6627 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6628 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6630 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6633 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6635 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6636
6637 // Move.
6638 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6639 processSlot(mapper, 0);
6640 processPosition(mapper, x1, y1);
6641 processSlot(mapper, 1);
6642 processPosition(mapper, x2, y2);
6643 processSync(mapper);
6644
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6647 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6650 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6655 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6656
6657 // First finger up.
6658 x2 += 15; y2 -= 20;
6659 processSlot(mapper, 0);
6660 processId(mapper, -1);
6661 processSlot(mapper, 1);
6662 processPosition(mapper, x2, y2);
6663 processSync(mapper);
6664
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6666 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6667 motionArgs.action);
6668 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6669 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6671 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6674 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6676 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6677
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6680 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6681 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6685
6686 // Move.
6687 x2 += 20; y2 -= 25;
6688 processPosition(mapper, x2, y2);
6689 processSync(mapper);
6690
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6693 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6694 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6698
6699 // New finger down.
6700 int32_t x3 = 700, y3 = 300;
6701 processPosition(mapper, x2, y2);
6702 processSlot(mapper, 0);
6703 processId(mapper, 3);
6704 processPosition(mapper, x3, y3);
6705 processSync(mapper);
6706
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6709 motionArgs.action);
6710 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6711 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6713 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6716 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6718 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6719
6720 // Second finger up.
6721 x3 += 30; y3 -= 20;
6722 processSlot(mapper, 1);
6723 processId(mapper, -1);
6724 processSlot(mapper, 0);
6725 processPosition(mapper, x3, y3);
6726 processSync(mapper);
6727
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6730 motionArgs.action);
6731 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6734 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6740
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6744 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6747 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6748
6749 // Last finger up.
6750 processId(mapper, -1);
6751 processSync(mapper);
6752
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6755 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6756 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6760
6761 // Should not have sent any more keys or motions.
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6764}
6765
6766TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006767 addConfigurationProperty("touch.deviceType", "touchScreen");
6768 prepareDisplay(DISPLAY_ORIENTATION_0);
6769 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006770 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006771
6772 // These calculations are based on the input device calibration documentation.
6773 int32_t rawX = 100;
6774 int32_t rawY = 200;
6775 int32_t rawTouchMajor = 7;
6776 int32_t rawTouchMinor = 6;
6777 int32_t rawToolMajor = 9;
6778 int32_t rawToolMinor = 8;
6779 int32_t rawPressure = 11;
6780 int32_t rawDistance = 0;
6781 int32_t rawOrientation = 3;
6782 int32_t id = 5;
6783
6784 float x = toDisplayX(rawX);
6785 float y = toDisplayY(rawY);
6786 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6787 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6788 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6789 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6790 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6791 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6792 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6793 float distance = float(rawDistance);
6794
6795 processPosition(mapper, rawX, rawY);
6796 processTouchMajor(mapper, rawTouchMajor);
6797 processTouchMinor(mapper, rawTouchMinor);
6798 processToolMajor(mapper, rawToolMajor);
6799 processToolMinor(mapper, rawToolMinor);
6800 processPressure(mapper, rawPressure);
6801 processOrientation(mapper, rawOrientation);
6802 processDistance(mapper, rawDistance);
6803 processId(mapper, id);
6804 processMTSync(mapper);
6805 processSync(mapper);
6806
6807 NotifyMotionArgs args;
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6809 ASSERT_EQ(0, args.pointerProperties[0].id);
6810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6811 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6812 orientation, distance));
6813}
6814
6815TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006816 addConfigurationProperty("touch.deviceType", "touchScreen");
6817 prepareDisplay(DISPLAY_ORIENTATION_0);
6818 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6819 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006820 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821
6822 // These calculations are based on the input device calibration documentation.
6823 int32_t rawX = 100;
6824 int32_t rawY = 200;
6825 int32_t rawTouchMajor = 140;
6826 int32_t rawTouchMinor = 120;
6827 int32_t rawToolMajor = 180;
6828 int32_t rawToolMinor = 160;
6829
6830 float x = toDisplayX(rawX);
6831 float y = toDisplayY(rawY);
6832 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6833 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6834 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6835 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6836 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6837
6838 processPosition(mapper, rawX, rawY);
6839 processTouchMajor(mapper, rawTouchMajor);
6840 processTouchMinor(mapper, rawTouchMinor);
6841 processToolMajor(mapper, rawToolMajor);
6842 processToolMinor(mapper, rawToolMinor);
6843 processMTSync(mapper);
6844 processSync(mapper);
6845
6846 NotifyMotionArgs args;
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6849 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6850}
6851
6852TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006853 addConfigurationProperty("touch.deviceType", "touchScreen");
6854 prepareDisplay(DISPLAY_ORIENTATION_0);
6855 prepareAxes(POSITION | TOUCH | TOOL);
6856 addConfigurationProperty("touch.size.calibration", "diameter");
6857 addConfigurationProperty("touch.size.scale", "10");
6858 addConfigurationProperty("touch.size.bias", "160");
6859 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006860 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006861
6862 // These calculations are based on the input device calibration documentation.
6863 // Note: We only provide a single common touch/tool value because the device is assumed
6864 // not to emit separate values for each pointer (isSummed = 1).
6865 int32_t rawX = 100;
6866 int32_t rawY = 200;
6867 int32_t rawX2 = 150;
6868 int32_t rawY2 = 250;
6869 int32_t rawTouchMajor = 5;
6870 int32_t rawToolMajor = 8;
6871
6872 float x = toDisplayX(rawX);
6873 float y = toDisplayY(rawY);
6874 float x2 = toDisplayX(rawX2);
6875 float y2 = toDisplayY(rawY2);
6876 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6877 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6878 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6879
6880 processPosition(mapper, rawX, rawY);
6881 processTouchMajor(mapper, rawTouchMajor);
6882 processToolMajor(mapper, rawToolMajor);
6883 processMTSync(mapper);
6884 processPosition(mapper, rawX2, rawY2);
6885 processTouchMajor(mapper, rawTouchMajor);
6886 processToolMajor(mapper, rawToolMajor);
6887 processMTSync(mapper);
6888 processSync(mapper);
6889
6890 NotifyMotionArgs args;
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6892 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6893
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6895 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6896 args.action);
6897 ASSERT_EQ(size_t(2), args.pointerCount);
6898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6899 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6901 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6902}
6903
6904TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905 addConfigurationProperty("touch.deviceType", "touchScreen");
6906 prepareDisplay(DISPLAY_ORIENTATION_0);
6907 prepareAxes(POSITION | TOUCH | TOOL);
6908 addConfigurationProperty("touch.size.calibration", "area");
6909 addConfigurationProperty("touch.size.scale", "43");
6910 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006911 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006912
6913 // These calculations are based on the input device calibration documentation.
6914 int32_t rawX = 100;
6915 int32_t rawY = 200;
6916 int32_t rawTouchMajor = 5;
6917 int32_t rawToolMajor = 8;
6918
6919 float x = toDisplayX(rawX);
6920 float y = toDisplayY(rawY);
6921 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6922 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6923 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6924
6925 processPosition(mapper, rawX, rawY);
6926 processTouchMajor(mapper, rawTouchMajor);
6927 processToolMajor(mapper, rawToolMajor);
6928 processMTSync(mapper);
6929 processSync(mapper);
6930
6931 NotifyMotionArgs args;
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6934 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6935}
6936
6937TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006938 addConfigurationProperty("touch.deviceType", "touchScreen");
6939 prepareDisplay(DISPLAY_ORIENTATION_0);
6940 prepareAxes(POSITION | PRESSURE);
6941 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6942 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006943 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006944
Michael Wrightaa449c92017-12-13 21:21:43 +00006945 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006946 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006947 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6948 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6949 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6950
Michael Wrightd02c5b62014-02-10 15:10:22 -08006951 // These calculations are based on the input device calibration documentation.
6952 int32_t rawX = 100;
6953 int32_t rawY = 200;
6954 int32_t rawPressure = 60;
6955
6956 float x = toDisplayX(rawX);
6957 float y = toDisplayY(rawY);
6958 float pressure = float(rawPressure) * 0.01f;
6959
6960 processPosition(mapper, rawX, rawY);
6961 processPressure(mapper, rawPressure);
6962 processMTSync(mapper);
6963 processSync(mapper);
6964
6965 NotifyMotionArgs args;
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6968 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6969}
6970
6971TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972 addConfigurationProperty("touch.deviceType", "touchScreen");
6973 prepareDisplay(DISPLAY_ORIENTATION_0);
6974 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006975 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976
6977 NotifyMotionArgs motionArgs;
6978 NotifyKeyArgs keyArgs;
6979
6980 processId(mapper, 1);
6981 processPosition(mapper, 100, 200);
6982 processSync(mapper);
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6985 ASSERT_EQ(0, motionArgs.buttonState);
6986
6987 // press BTN_LEFT, release BTN_LEFT
6988 processKey(mapper, BTN_LEFT, 1);
6989 processSync(mapper);
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6992 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6993
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6996 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6997
Michael Wrightd02c5b62014-02-10 15:10:22 -08006998 processKey(mapper, BTN_LEFT, 0);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007002 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007003
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007006 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007007
7008 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7009 processKey(mapper, BTN_RIGHT, 1);
7010 processKey(mapper, BTN_MIDDLE, 1);
7011 processSync(mapper);
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7014 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7015 motionArgs.buttonState);
7016
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7019 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7020
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7022 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7023 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7024 motionArgs.buttonState);
7025
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026 processKey(mapper, BTN_RIGHT, 0);
7027 processSync(mapper);
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007030 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007031
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007034 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035
7036 processKey(mapper, BTN_MIDDLE, 0);
7037 processSync(mapper);
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007040 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007041
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007044 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007045
7046 // press BTN_BACK, release BTN_BACK
7047 processKey(mapper, BTN_BACK, 1);
7048 processSync(mapper);
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7050 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7051 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007052
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007055 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7056
7057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7059 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060
7061 processKey(mapper, BTN_BACK, 0);
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007066
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007069 ASSERT_EQ(0, motionArgs.buttonState);
7070
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7072 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7073 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7074
7075 // press BTN_SIDE, release BTN_SIDE
7076 processKey(mapper, BTN_SIDE, 1);
7077 processSync(mapper);
7078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7079 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7080 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007081
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007084 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7085
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7088 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007089
7090 processKey(mapper, BTN_SIDE, 0);
7091 processSync(mapper);
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007095
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007098 ASSERT_EQ(0, motionArgs.buttonState);
7099
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7101 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7102 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7103
7104 // press BTN_FORWARD, release BTN_FORWARD
7105 processKey(mapper, BTN_FORWARD, 1);
7106 processSync(mapper);
7107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7108 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7109 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007110
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007113 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7114
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7117 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118
7119 processKey(mapper, BTN_FORWARD, 0);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007124
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127 ASSERT_EQ(0, motionArgs.buttonState);
7128
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7130 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7131 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7132
7133 // press BTN_EXTRA, release BTN_EXTRA
7134 processKey(mapper, BTN_EXTRA, 1);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7137 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7138 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007139
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007142 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7143
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7146 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147
7148 processKey(mapper, BTN_EXTRA, 0);
7149 processSync(mapper);
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007151 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007153
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007156 ASSERT_EQ(0, motionArgs.buttonState);
7157
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7159 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7160 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7161
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7163
Michael Wrightd02c5b62014-02-10 15:10:22 -08007164 // press BTN_STYLUS, release BTN_STYLUS
7165 processKey(mapper, BTN_STYLUS, 1);
7166 processSync(mapper);
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007169 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7170
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7173 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007174
7175 processKey(mapper, BTN_STYLUS, 0);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007180
7181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007183 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184
7185 // press BTN_STYLUS2, release BTN_STYLUS2
7186 processKey(mapper, BTN_STYLUS2, 1);
7187 processSync(mapper);
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007190 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7191
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7194 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195
7196 processKey(mapper, BTN_STYLUS2, 0);
7197 processSync(mapper);
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007201
7202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007204 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007205
7206 // release touch
7207 processId(mapper, -1);
7208 processSync(mapper);
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7210 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7211 ASSERT_EQ(0, motionArgs.buttonState);
7212}
7213
7214TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215 addConfigurationProperty("touch.deviceType", "touchScreen");
7216 prepareDisplay(DISPLAY_ORIENTATION_0);
7217 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007218 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219
7220 NotifyMotionArgs motionArgs;
7221
7222 // default tool type is finger
7223 processId(mapper, 1);
7224 processPosition(mapper, 100, 200);
7225 processSync(mapper);
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7229
7230 // eraser
7231 processKey(mapper, BTN_TOOL_RUBBER, 1);
7232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7236
7237 // stylus
7238 processKey(mapper, BTN_TOOL_RUBBER, 0);
7239 processKey(mapper, BTN_TOOL_PEN, 1);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7244
7245 // brush
7246 processKey(mapper, BTN_TOOL_PEN, 0);
7247 processKey(mapper, BTN_TOOL_BRUSH, 1);
7248 processSync(mapper);
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7252
7253 // pencil
7254 processKey(mapper, BTN_TOOL_BRUSH, 0);
7255 processKey(mapper, BTN_TOOL_PENCIL, 1);
7256 processSync(mapper);
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7260
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007261 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007262 processKey(mapper, BTN_TOOL_PENCIL, 0);
7263 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7264 processSync(mapper);
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7268
7269 // mouse
7270 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7271 processKey(mapper, BTN_TOOL_MOUSE, 1);
7272 processSync(mapper);
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7276
7277 // lens
7278 processKey(mapper, BTN_TOOL_MOUSE, 0);
7279 processKey(mapper, BTN_TOOL_LENS, 1);
7280 processSync(mapper);
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7284
7285 // double-tap
7286 processKey(mapper, BTN_TOOL_LENS, 0);
7287 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7292
7293 // triple-tap
7294 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7295 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7300
7301 // quad-tap
7302 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7303 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7304 processSync(mapper);
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7308
7309 // finger
7310 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7311 processKey(mapper, BTN_TOOL_FINGER, 1);
7312 processSync(mapper);
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7316
7317 // stylus trumps finger
7318 processKey(mapper, BTN_TOOL_PEN, 1);
7319 processSync(mapper);
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7323
7324 // eraser trumps stylus
7325 processKey(mapper, BTN_TOOL_RUBBER, 1);
7326 processSync(mapper);
7327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7330
7331 // mouse trumps eraser
7332 processKey(mapper, BTN_TOOL_MOUSE, 1);
7333 processSync(mapper);
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7337
7338 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7339 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7344
7345 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7346 processToolType(mapper, MT_TOOL_PEN);
7347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7351
7352 // back to default tool type
7353 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7354 processKey(mapper, BTN_TOOL_MOUSE, 0);
7355 processKey(mapper, BTN_TOOL_RUBBER, 0);
7356 processKey(mapper, BTN_TOOL_PEN, 0);
7357 processKey(mapper, BTN_TOOL_FINGER, 0);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362}
7363
7364TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007365 addConfigurationProperty("touch.deviceType", "touchScreen");
7366 prepareDisplay(DISPLAY_ORIENTATION_0);
7367 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007368 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007369 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007370
7371 NotifyMotionArgs motionArgs;
7372
7373 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7374 processId(mapper, 1);
7375 processPosition(mapper, 100, 200);
7376 processSync(mapper);
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7380 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7381
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7383 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7385 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7386
7387 // move a little
7388 processPosition(mapper, 150, 250);
7389 processSync(mapper);
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7391 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7393 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7394
7395 // down when BTN_TOUCH is pressed, pressure defaults to 1
7396 processKey(mapper, BTN_TOUCH, 1);
7397 processSync(mapper);
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7401 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7402
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7406 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7407
7408 // up when BTN_TOUCH is released, hover restored
7409 processKey(mapper, BTN_TOUCH, 0);
7410 processSync(mapper);
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7414 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7415
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7419 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7420
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7424 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7425
7426 // exit hover when pointer goes away
7427 processId(mapper, -1);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7430 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7432 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7433}
7434
7435TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007436 addConfigurationProperty("touch.deviceType", "touchScreen");
7437 prepareDisplay(DISPLAY_ORIENTATION_0);
7438 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007439 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007440
7441 NotifyMotionArgs motionArgs;
7442
7443 // initially hovering because pressure is 0
7444 processId(mapper, 1);
7445 processPosition(mapper, 100, 200);
7446 processPressure(mapper, 0);
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7451 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7452
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7454 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7456 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7457
7458 // move a little
7459 processPosition(mapper, 150, 250);
7460 processSync(mapper);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7464 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7465
7466 // down when pressure becomes non-zero
7467 processPressure(mapper, RAW_PRESSURE_MAX);
7468 processSync(mapper);
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7470 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7472 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7473
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7477 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7478
7479 // up when pressure becomes 0, hover restored
7480 processPressure(mapper, 0);
7481 processSync(mapper);
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7486
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7490 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7491
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7495 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7496
7497 // exit hover when pointer goes away
7498 processId(mapper, -1);
7499 processSync(mapper);
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7503 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7504}
7505
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007506/**
7507 * Set the input device port <--> display port associations, and check that the
7508 * events are routed to the display that matches the display port.
7509 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7510 */
7511TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007512 const std::string usb2 = "USB2";
7513 const uint8_t hdmi1 = 0;
7514 const uint8_t hdmi2 = 1;
7515 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007516 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007517
7518 addConfigurationProperty("touch.deviceType", "touchScreen");
7519 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007520 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007521
7522 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7523 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7524
7525 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7526 // for this input device is specified, and the matching viewport is not present,
7527 // the input device should be disabled (at the mapper level).
7528
7529 // Add viewport for display 2 on hdmi2
7530 prepareSecondaryDisplay(type, hdmi2);
7531 // Send a touch event
7532 processPosition(mapper, 100, 100);
7533 processSync(mapper);
7534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7535
7536 // Add viewport for display 1 on hdmi1
7537 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7538 // Send a touch event again
7539 processPosition(mapper, 100, 100);
7540 processSync(mapper);
7541
7542 NotifyMotionArgs args;
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7544 ASSERT_EQ(DISPLAY_ID, args.displayId);
7545}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007547TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007548 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007549 std::shared_ptr<FakePointerController> fakePointerController =
7550 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007551 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007552 fakePointerController->setPosition(100, 200);
7553 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007554 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7555
Garfield Tan888a6a42020-01-09 11:39:16 -08007556 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007557 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007558
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007559 prepareDisplay(DISPLAY_ORIENTATION_0);
7560 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007561 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007562
7563 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007564 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007565
7566 NotifyMotionArgs motionArgs;
7567 processPosition(mapper, 100, 100);
7568 processSync(mapper);
7569
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7572 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7573}
7574
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007575/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007576 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7577 */
7578TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7579 addConfigurationProperty("touch.deviceType", "touchScreen");
7580 prepareAxes(POSITION);
7581 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7582
7583 prepareDisplay(DISPLAY_ORIENTATION_0);
7584 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7585 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7586 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7587 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7588
7589 NotifyMotionArgs args;
7590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7591 ASSERT_EQ(26, args.readTime);
7592
7593 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7594 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7595 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7598 ASSERT_EQ(33, args.readTime);
7599}
7600
7601/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007602 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7603 * events should not be delivered to the listener.
7604 */
7605TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7606 addConfigurationProperty("touch.deviceType", "touchScreen");
7607 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7608 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7609 ViewportType::INTERNAL);
7610 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7611 prepareAxes(POSITION);
7612 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7613
7614 NotifyMotionArgs motionArgs;
7615 processPosition(mapper, 100, 100);
7616 processSync(mapper);
7617
7618 mFakeListener->assertNotifyMotionWasNotCalled();
7619}
7620
Garfield Tanc734e4f2021-01-15 20:01:39 -08007621TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7622 addConfigurationProperty("touch.deviceType", "touchScreen");
7623 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7624 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7625 ViewportType::INTERNAL);
7626 std::optional<DisplayViewport> optionalDisplayViewport =
7627 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7628 ASSERT_TRUE(optionalDisplayViewport.has_value());
7629 DisplayViewport displayViewport = *optionalDisplayViewport;
7630
7631 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7632 prepareAxes(POSITION);
7633 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7634
7635 // Finger down
7636 int32_t x = 100, y = 100;
7637 processPosition(mapper, x, y);
7638 processSync(mapper);
7639
7640 NotifyMotionArgs motionArgs;
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7642 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7643
7644 // Deactivate display viewport
7645 displayViewport.isActive = false;
7646 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7647 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7648
7649 // Finger move
7650 x += 10, y += 10;
7651 processPosition(mapper, x, y);
7652 processSync(mapper);
7653
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7655 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7656
7657 // Reactivate display viewport
7658 displayViewport.isActive = true;
7659 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7660 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7661
7662 // Finger move again
7663 x += 10, y += 10;
7664 processPosition(mapper, x, y);
7665 processSync(mapper);
7666
7667 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7668 // no pointer on the touch device.
7669 mFakeListener->assertNotifyMotionWasNotCalled();
7670}
7671
Arthur Hung7c645402019-01-25 17:45:42 +08007672TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7673 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007674 prepareAxes(POSITION | ID | SLOT);
7675 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007676 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007677
7678 // Create the second touch screen device, and enable multi fingers.
7679 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007680 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007681 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007682 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007683 std::shared_ptr<InputDevice> device2 =
7684 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7685 Flags<InputDeviceClass>(0));
7686
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007687 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7688 0 /*flat*/, 0 /*fuzz*/);
7689 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7690 0 /*flat*/, 0 /*fuzz*/);
7691 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7692 0 /*flat*/, 0 /*fuzz*/);
7693 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7694 0 /*flat*/, 0 /*fuzz*/);
7695 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7696 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7697 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007698
7699 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007700 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007701 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7702 device2->reset(ARBITRARY_TIME);
7703
7704 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007705 std::shared_ptr<FakePointerController> fakePointerController =
7706 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007707 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7708 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7709
7710 // Setup policy for associated displays and show touches.
7711 const uint8_t hdmi1 = 0;
7712 const uint8_t hdmi2 = 1;
7713 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7714 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7715 mFakePolicy->setShowTouches(true);
7716
7717 // Create displays.
7718 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007719 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007720
7721 // Default device will reconfigure above, need additional reconfiguration for another device.
7722 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007723 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007724
7725 // Two fingers down at default display.
7726 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7727 processPosition(mapper, x1, y1);
7728 processId(mapper, 1);
7729 processSlot(mapper, 1);
7730 processPosition(mapper, x2, y2);
7731 processId(mapper, 2);
7732 processSync(mapper);
7733
7734 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7735 fakePointerController->getSpots().find(DISPLAY_ID);
7736 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7737 ASSERT_EQ(size_t(2), iter->second.size());
7738
7739 // Two fingers down at second display.
7740 processPosition(mapper2, x1, y1);
7741 processId(mapper2, 1);
7742 processSlot(mapper2, 1);
7743 processPosition(mapper2, x2, y2);
7744 processId(mapper2, 2);
7745 processSync(mapper2);
7746
7747 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7748 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7749 ASSERT_EQ(size_t(2), iter->second.size());
7750}
7751
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007752TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007753 prepareAxes(POSITION);
7754 addConfigurationProperty("touch.deviceType", "touchScreen");
7755 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007756 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007757
7758 NotifyMotionArgs motionArgs;
7759 // Unrotated video frame
7760 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7761 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007762 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007763 processPosition(mapper, 100, 200);
7764 processSync(mapper);
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7766 ASSERT_EQ(frames, motionArgs.videoFrames);
7767
7768 // Subsequent touch events should not have any videoframes
7769 // This is implemented separately in FakeEventHub,
7770 // but that should match the behaviour of TouchVideoDevice.
7771 processPosition(mapper, 200, 200);
7772 processSync(mapper);
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7774 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7775}
7776
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007777TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007778 prepareAxes(POSITION);
7779 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007780 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007781 // Unrotated video frame
7782 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7783 NotifyMotionArgs motionArgs;
7784
7785 // Test all 4 orientations
7786 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7787 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7788 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7789 clearViewports();
7790 prepareDisplay(orientation);
7791 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007792 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007793 processPosition(mapper, 100, 200);
7794 processSync(mapper);
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7796 frames[0].rotate(orientation);
7797 ASSERT_EQ(frames, motionArgs.videoFrames);
7798 }
7799}
7800
7801TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007802 prepareAxes(POSITION);
7803 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007804 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007805 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7806 // so mix these.
7807 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7808 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7809 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7810 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7811 NotifyMotionArgs motionArgs;
7812
7813 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007814 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007815 processPosition(mapper, 100, 200);
7816 processSync(mapper);
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7818 std::for_each(frames.begin(), frames.end(),
7819 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7820 ASSERT_EQ(frames, motionArgs.videoFrames);
7821}
7822
Arthur Hung9da14732019-09-02 16:16:58 +08007823/**
7824 * If we had defined port associations, but the viewport is not ready, the touch device would be
7825 * expected to be disabled, and it should be enabled after the viewport has found.
7826 */
7827TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007828 constexpr uint8_t hdmi2 = 1;
7829 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007830 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007831
7832 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7833
7834 addConfigurationProperty("touch.deviceType", "touchScreen");
7835 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007836 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007837
7838 ASSERT_EQ(mDevice->isEnabled(), false);
7839
7840 // Add display on hdmi2, the device should be enabled and can receive touch event.
7841 prepareSecondaryDisplay(type, hdmi2);
7842 ASSERT_EQ(mDevice->isEnabled(), true);
7843
7844 // Send a touch event.
7845 processPosition(mapper, 100, 100);
7846 processSync(mapper);
7847
7848 NotifyMotionArgs args;
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7850 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7851}
7852
Arthur Hung6cd19a42019-08-30 19:04:12 +08007853
Arthur Hung6cd19a42019-08-30 19:04:12 +08007854
Arthur Hung421eb1c2020-01-16 00:09:42 +08007855TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007856 addConfigurationProperty("touch.deviceType", "touchScreen");
7857 prepareDisplay(DISPLAY_ORIENTATION_0);
7858 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007859 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007860
7861 NotifyMotionArgs motionArgs;
7862
7863 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7864 // finger down
7865 processId(mapper, 1);
7866 processPosition(mapper, x1, y1);
7867 processSync(mapper);
7868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7869 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7870 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7871
7872 // finger move
7873 processId(mapper, 1);
7874 processPosition(mapper, x2, y2);
7875 processSync(mapper);
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7879
7880 // finger up.
7881 processId(mapper, -1);
7882 processSync(mapper);
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7884 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7886
7887 // new finger down
7888 processId(mapper, 1);
7889 processPosition(mapper, x3, y3);
7890 processSync(mapper);
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7892 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7894}
7895
7896/**
arthurhungcc7f9802020-04-30 17:55:40 +08007897 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7898 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007899 */
arthurhungcc7f9802020-04-30 17:55:40 +08007900TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007901 addConfigurationProperty("touch.deviceType", "touchScreen");
7902 prepareDisplay(DISPLAY_ORIENTATION_0);
7903 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007904 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007905
7906 NotifyMotionArgs motionArgs;
7907
7908 // default tool type is finger
7909 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007910 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007911 processPosition(mapper, x1, y1);
7912 processSync(mapper);
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7914 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7916
7917 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7918 processToolType(mapper, MT_TOOL_PALM);
7919 processSync(mapper);
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7921 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7922
7923 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007924 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007925 processPosition(mapper, x2, y2);
7926 processSync(mapper);
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7928
7929 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007930 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007931 processSync(mapper);
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7933
7934 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007935 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007936 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007937 processPosition(mapper, x3, y3);
7938 processSync(mapper);
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7942}
7943
arthurhungbf89a482020-04-17 17:37:55 +08007944/**
arthurhungcc7f9802020-04-30 17:55:40 +08007945 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7946 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007947 */
arthurhungcc7f9802020-04-30 17:55:40 +08007948TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007949 addConfigurationProperty("touch.deviceType", "touchScreen");
7950 prepareDisplay(DISPLAY_ORIENTATION_0);
7951 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7952 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7953
7954 NotifyMotionArgs motionArgs;
7955
7956 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007957 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7958 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007959 processPosition(mapper, x1, y1);
7960 processSync(mapper);
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7962 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7964
7965 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007966 processSlot(mapper, SECOND_SLOT);
7967 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007968 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007969 processSync(mapper);
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7972 motionArgs.action);
7973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7974
7975 // If the tool type of the first finger changes to MT_TOOL_PALM,
7976 // we expect to receive ACTION_POINTER_UP with cancel flag.
7977 processSlot(mapper, FIRST_SLOT);
7978 processId(mapper, FIRST_TRACKING_ID);
7979 processToolType(mapper, MT_TOOL_PALM);
7980 processSync(mapper);
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7982 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7983 motionArgs.action);
7984 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7985
7986 // The following MOVE events of second finger should be processed.
7987 processSlot(mapper, SECOND_SLOT);
7988 processId(mapper, SECOND_TRACKING_ID);
7989 processPosition(mapper, x2 + 1, y2 + 1);
7990 processSync(mapper);
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7993 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7994
7995 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7996 // it. Second finger receive move.
7997 processSlot(mapper, FIRST_SLOT);
7998 processId(mapper, INVALID_TRACKING_ID);
7999 processSync(mapper);
8000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8002 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8003
8004 // Second finger keeps moving.
8005 processSlot(mapper, SECOND_SLOT);
8006 processId(mapper, SECOND_TRACKING_ID);
8007 processPosition(mapper, x2 + 2, y2 + 2);
8008 processSync(mapper);
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8011 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8012
8013 // Second finger up.
8014 processId(mapper, INVALID_TRACKING_ID);
8015 processSync(mapper);
8016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8017 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8018 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8019}
8020
8021/**
8022 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8023 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8024 */
8025TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8026 addConfigurationProperty("touch.deviceType", "touchScreen");
8027 prepareDisplay(DISPLAY_ORIENTATION_0);
8028 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8029 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8030
8031 NotifyMotionArgs motionArgs;
8032
8033 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8034 // First finger down.
8035 processId(mapper, FIRST_TRACKING_ID);
8036 processPosition(mapper, x1, y1);
8037 processSync(mapper);
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8041
8042 // Second finger down.
8043 processSlot(mapper, SECOND_SLOT);
8044 processId(mapper, SECOND_TRACKING_ID);
8045 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8048 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8049 motionArgs.action);
8050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8051
arthurhungcc7f9802020-04-30 17:55:40 +08008052 // If the tool type of the first finger changes to MT_TOOL_PALM,
8053 // we expect to receive ACTION_POINTER_UP with cancel flag.
8054 processSlot(mapper, FIRST_SLOT);
8055 processId(mapper, FIRST_TRACKING_ID);
8056 processToolType(mapper, MT_TOOL_PALM);
8057 processSync(mapper);
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8060 motionArgs.action);
8061 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8062
8063 // Second finger keeps moving.
8064 processSlot(mapper, SECOND_SLOT);
8065 processId(mapper, SECOND_TRACKING_ID);
8066 processPosition(mapper, x2 + 1, y2 + 1);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8070
8071 // second finger becomes palm, receive cancel due to only 1 finger is active.
8072 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008073 processToolType(mapper, MT_TOOL_PALM);
8074 processSync(mapper);
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8076 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8077
arthurhungcc7f9802020-04-30 17:55:40 +08008078 // third finger down.
8079 processSlot(mapper, THIRD_SLOT);
8080 processId(mapper, THIRD_TRACKING_ID);
8081 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008082 processPosition(mapper, x3, y3);
8083 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008087 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8088
8089 // third finger move
8090 processId(mapper, THIRD_TRACKING_ID);
8091 processPosition(mapper, x3 + 1, y3 + 1);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8095
8096 // first finger up, third finger receive move.
8097 processSlot(mapper, FIRST_SLOT);
8098 processId(mapper, INVALID_TRACKING_ID);
8099 processSync(mapper);
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8102 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8103
8104 // second finger up, third finger receive move.
8105 processSlot(mapper, SECOND_SLOT);
8106 processId(mapper, INVALID_TRACKING_ID);
8107 processSync(mapper);
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8110 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8111
8112 // third finger up.
8113 processSlot(mapper, THIRD_SLOT);
8114 processId(mapper, INVALID_TRACKING_ID);
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8117 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8118 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8119}
8120
8121/**
8122 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8123 * and the active finger could still be allowed to receive the events
8124 */
8125TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8126 addConfigurationProperty("touch.deviceType", "touchScreen");
8127 prepareDisplay(DISPLAY_ORIENTATION_0);
8128 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8129 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8130
8131 NotifyMotionArgs motionArgs;
8132
8133 // default tool type is finger
8134 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8135 processId(mapper, FIRST_TRACKING_ID);
8136 processPosition(mapper, x1, y1);
8137 processSync(mapper);
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8139 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8141
8142 // Second finger down.
8143 processSlot(mapper, SECOND_SLOT);
8144 processId(mapper, SECOND_TRACKING_ID);
8145 processPosition(mapper, x2, y2);
8146 processSync(mapper);
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8149 motionArgs.action);
8150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8151
8152 // If the tool type of the second finger changes to MT_TOOL_PALM,
8153 // we expect to receive ACTION_POINTER_UP with cancel flag.
8154 processId(mapper, SECOND_TRACKING_ID);
8155 processToolType(mapper, MT_TOOL_PALM);
8156 processSync(mapper);
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8159 motionArgs.action);
8160 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8161
8162 // The following MOVE event should be processed.
8163 processSlot(mapper, FIRST_SLOT);
8164 processId(mapper, FIRST_TRACKING_ID);
8165 processPosition(mapper, x1 + 1, y1 + 1);
8166 processSync(mapper);
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8169 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8170
8171 // second finger up.
8172 processSlot(mapper, SECOND_SLOT);
8173 processId(mapper, INVALID_TRACKING_ID);
8174 processSync(mapper);
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8177
8178 // first finger keep moving
8179 processSlot(mapper, FIRST_SLOT);
8180 processId(mapper, FIRST_TRACKING_ID);
8181 processPosition(mapper, x1 + 2, y1 + 2);
8182 processSync(mapper);
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8185
8186 // first finger up.
8187 processId(mapper, INVALID_TRACKING_ID);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8191 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008192}
8193
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008194// --- MultiTouchInputMapperTest_ExternalDevice ---
8195
8196class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8197protected:
Chris Yea52ade12020-08-27 16:49:20 -07008198 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008199};
8200
8201/**
8202 * Expect fallback to internal viewport if device is external and external viewport is not present.
8203 */
8204TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8205 prepareAxes(POSITION);
8206 addConfigurationProperty("touch.deviceType", "touchScreen");
8207 prepareDisplay(DISPLAY_ORIENTATION_0);
8208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8209
8210 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8211
8212 NotifyMotionArgs motionArgs;
8213
8214 // Expect the event to be sent to the internal viewport,
8215 // because an external viewport is not present.
8216 processPosition(mapper, 100, 100);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8219 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8220
8221 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008222 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008223 processPosition(mapper, 100, 100);
8224 processSync(mapper);
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8226 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8227}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008228
8229/**
8230 * Test touch should not work if outside of surface.
8231 */
8232class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8233protected:
8234 void halfDisplayToCenterHorizontal(int32_t orientation) {
8235 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008236 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008237
8238 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8239 internalViewport->orientation = orientation;
8240 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8241 internalViewport->logicalLeft = 0;
8242 internalViewport->logicalTop = 0;
8243 internalViewport->logicalRight = DISPLAY_HEIGHT;
8244 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8245
8246 internalViewport->physicalLeft = 0;
8247 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8248 internalViewport->physicalRight = DISPLAY_HEIGHT;
8249 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8250
8251 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8252 internalViewport->deviceHeight = DISPLAY_WIDTH;
8253 } else {
8254 internalViewport->logicalLeft = 0;
8255 internalViewport->logicalTop = 0;
8256 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8257 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8258
8259 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8260 internalViewport->physicalTop = 0;
8261 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8262 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8263
8264 internalViewport->deviceWidth = DISPLAY_WIDTH;
8265 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8266 }
8267
8268 mFakePolicy->updateViewport(internalViewport.value());
8269 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8270 }
8271
arthurhung5d547942020-12-14 17:04:45 +08008272 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8273 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008274 int32_t yExpected) {
8275 // touch on outside area should not work.
8276 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8277 processSync(mapper);
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8279
8280 // touch on inside area should receive the event.
8281 NotifyMotionArgs args;
8282 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8283 processSync(mapper);
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8285 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8286 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8287
8288 // Reset.
8289 mapper.reset(ARBITRARY_TIME);
8290 }
8291};
8292
arthurhung5d547942020-12-14 17:04:45 +08008293TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008294 addConfigurationProperty("touch.deviceType", "touchScreen");
8295 prepareDisplay(DISPLAY_ORIENTATION_0);
8296 prepareAxes(POSITION);
8297 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8298
8299 // Touch on center of normal display should work.
8300 const int32_t x = DISPLAY_WIDTH / 4;
8301 const int32_t y = DISPLAY_HEIGHT / 2;
8302 processPosition(mapper, toRawX(x), toRawY(y));
8303 processSync(mapper);
8304 NotifyMotionArgs args;
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8307 0.0f, 0.0f, 0.0f, 0.0f));
8308 // Reset.
8309 mapper.reset(ARBITRARY_TIME);
8310
8311 // Let physical display be different to device, and make surface and physical could be 1:1.
8312 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8313
8314 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8315 const int32_t yExpected = y;
8316 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8317}
8318
arthurhung5d547942020-12-14 17:04:45 +08008319TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008320 addConfigurationProperty("touch.deviceType", "touchScreen");
8321 prepareDisplay(DISPLAY_ORIENTATION_0);
8322 prepareAxes(POSITION);
8323 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8324
8325 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8326 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8327
8328 const int32_t x = DISPLAY_WIDTH / 4;
8329 const int32_t y = DISPLAY_HEIGHT / 2;
8330
8331 // expect x/y = swap x/y then reverse y.
8332 const int32_t xExpected = y;
8333 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8334 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8335}
8336
arthurhung5d547942020-12-14 17:04:45 +08008337TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008338 addConfigurationProperty("touch.deviceType", "touchScreen");
8339 prepareDisplay(DISPLAY_ORIENTATION_0);
8340 prepareAxes(POSITION);
8341 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8342
8343 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8344 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8345
8346 const int32_t x = DISPLAY_WIDTH / 4;
8347 const int32_t y = DISPLAY_HEIGHT / 2;
8348
8349 // expect x/y = swap x/y then reverse x.
8350 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8351 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8352 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8353}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008354
arthurhunga36b28e2020-12-29 20:28:15 +08008355TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8356 addConfigurationProperty("touch.deviceType", "touchScreen");
8357 prepareDisplay(DISPLAY_ORIENTATION_0);
8358 prepareAxes(POSITION);
8359 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8360
8361 const int32_t x = 0;
8362 const int32_t y = 0;
8363
8364 const int32_t xExpected = x;
8365 const int32_t yExpected = y;
8366 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8367
8368 clearViewports();
8369 prepareDisplay(DISPLAY_ORIENTATION_90);
8370 // expect x/y = swap x/y then reverse y.
8371 const int32_t xExpected90 = y;
8372 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8373 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8374
8375 clearViewports();
8376 prepareDisplay(DISPLAY_ORIENTATION_270);
8377 // expect x/y = swap x/y then reverse x.
8378 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8379 const int32_t yExpected270 = x;
8380 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8381}
8382
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008383TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8384 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8385 std::shared_ptr<FakePointerController> fakePointerController =
8386 std::make_shared<FakePointerController>();
8387 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8388 fakePointerController->setPosition(0, 0);
8389 fakePointerController->setButtonState(0);
8390
8391 // prepare device and capture
8392 prepareDisplay(DISPLAY_ORIENTATION_0);
8393 prepareAxes(POSITION | ID | SLOT);
8394 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8395 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8396 mFakePolicy->setPointerCapture(true);
8397 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8398 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8399
8400 // captured touchpad should be a touchpad source
8401 NotifyDeviceResetArgs resetArgs;
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8403 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8404
Chris Yef74dc422020-09-02 22:41:50 -07008405 InputDeviceInfo deviceInfo;
8406 mDevice->getDeviceInfo(&deviceInfo);
8407
8408 const InputDeviceInfo::MotionRange* relRangeX =
8409 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8410 ASSERT_NE(relRangeX, nullptr);
8411 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8412 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8413 const InputDeviceInfo::MotionRange* relRangeY =
8414 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8415 ASSERT_NE(relRangeY, nullptr);
8416 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8417 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8418
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008419 // run captured pointer tests - note that this is unscaled, so input listener events should be
8420 // identical to what the hardware sends (accounting for any
8421 // calibration).
8422 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008423 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008424 processId(mapper, 1);
8425 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8426 processKey(mapper, BTN_TOUCH, 1);
8427 processSync(mapper);
8428
8429 // expect coord[0] to contain initial location of touch 0
8430 NotifyMotionArgs args;
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8433 ASSERT_EQ(1U, args.pointerCount);
8434 ASSERT_EQ(0, args.pointerProperties[0].id);
8435 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8436 ASSERT_NO_FATAL_FAILURE(
8437 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8438
8439 // FINGER 1 DOWN
8440 processSlot(mapper, 1);
8441 processId(mapper, 2);
8442 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8443 processSync(mapper);
8444
8445 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008447 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8448 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008449 ASSERT_EQ(2U, args.pointerCount);
8450 ASSERT_EQ(0, args.pointerProperties[0].id);
8451 ASSERT_EQ(1, args.pointerProperties[1].id);
8452 ASSERT_NO_FATAL_FAILURE(
8453 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8454 ASSERT_NO_FATAL_FAILURE(
8455 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8456
8457 // FINGER 1 MOVE
8458 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8459 processSync(mapper);
8460
8461 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8462 // from move
8463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8465 ASSERT_NO_FATAL_FAILURE(
8466 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8467 ASSERT_NO_FATAL_FAILURE(
8468 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8469
8470 // FINGER 0 MOVE
8471 processSlot(mapper, 0);
8472 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8473 processSync(mapper);
8474
8475 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8478 ASSERT_NO_FATAL_FAILURE(
8479 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8480 ASSERT_NO_FATAL_FAILURE(
8481 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8482
8483 // BUTTON DOWN
8484 processKey(mapper, BTN_LEFT, 1);
8485 processSync(mapper);
8486
8487 // touchinputmapper design sends a move before button press
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8492
8493 // BUTTON UP
8494 processKey(mapper, BTN_LEFT, 0);
8495 processSync(mapper);
8496
8497 // touchinputmapper design sends a move after button release
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8502
8503 // FINGER 0 UP
8504 processId(mapper, -1);
8505 processSync(mapper);
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8507 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8508
8509 // FINGER 1 MOVE
8510 processSlot(mapper, 1);
8511 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8512 processSync(mapper);
8513
8514 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8517 ASSERT_EQ(1U, args.pointerCount);
8518 ASSERT_EQ(1, args.pointerProperties[0].id);
8519 ASSERT_NO_FATAL_FAILURE(
8520 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8521
8522 // FINGER 1 UP
8523 processId(mapper, -1);
8524 processKey(mapper, BTN_TOUCH, 0);
8525 processSync(mapper);
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8528
8529 // non captured touchpad should be a mouse source
8530 mFakePolicy->setPointerCapture(false);
8531 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8533 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8534}
8535
8536TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8537 std::shared_ptr<FakePointerController> fakePointerController =
8538 std::make_shared<FakePointerController>();
8539 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8540 fakePointerController->setPosition(0, 0);
8541 fakePointerController->setButtonState(0);
8542
8543 // prepare device and capture
8544 prepareDisplay(DISPLAY_ORIENTATION_0);
8545 prepareAxes(POSITION | ID | SLOT);
8546 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8547 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8548 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8549 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8550 // run uncaptured pointer tests - pushes out generic events
8551 // FINGER 0 DOWN
8552 processId(mapper, 3);
8553 processPosition(mapper, 100, 100);
8554 processKey(mapper, BTN_TOUCH, 1);
8555 processSync(mapper);
8556
8557 // start at (100,100), cursor should be at (0,0) * scale
8558 NotifyMotionArgs args;
8559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8561 ASSERT_NO_FATAL_FAILURE(
8562 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8563
8564 // FINGER 0 MOVE
8565 processPosition(mapper, 200, 200);
8566 processSync(mapper);
8567
8568 // compute scaling to help with touch position checking
8569 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8570 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8571 float scale =
8572 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8573
8574 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8578 0, 0, 0, 0, 0, 0, 0));
8579}
8580
8581TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8582 std::shared_ptr<FakePointerController> fakePointerController =
8583 std::make_shared<FakePointerController>();
8584
8585 prepareDisplay(DISPLAY_ORIENTATION_0);
8586 prepareAxes(POSITION | ID | SLOT);
8587 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8588 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8589 mFakePolicy->setPointerCapture(false);
8590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8591
8592 // uncaptured touchpad should be a pointer device
8593 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8594
8595 // captured touchpad should be a touchpad device
8596 mFakePolicy->setPointerCapture(true);
8597 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8598 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8599}
8600
Chris Yee2b1e5c2021-03-10 22:45:12 -08008601// --- InputControllerTest ---
8602
8603class InputControllerTest : public testing::Test {
8604protected:
8605 static const char* DEVICE_NAME;
8606 static const char* DEVICE_LOCATION;
8607 static const int32_t DEVICE_ID;
8608 static const int32_t DEVICE_GENERATION;
8609 static const int32_t DEVICE_CONTROLLER_NUMBER;
8610 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8611 static const int32_t EVENTHUB_ID;
8612
8613 std::shared_ptr<FakeEventHub> mFakeEventHub;
8614 sp<FakeInputReaderPolicy> mFakePolicy;
8615 sp<TestInputListener> mFakeListener;
8616 std::unique_ptr<InstrumentedInputReader> mReader;
8617 std::shared_ptr<InputDevice> mDevice;
8618
8619 virtual void SetUp(Flags<InputDeviceClass> classes) {
8620 mFakeEventHub = std::make_unique<FakeEventHub>();
8621 mFakePolicy = new FakeInputReaderPolicy();
8622 mFakeListener = new TestInputListener();
8623 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8624 mFakeListener);
8625 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8626 }
8627
8628 void SetUp() override { SetUp(DEVICE_CLASSES); }
8629
8630 void TearDown() override {
8631 mFakeListener.clear();
8632 mFakePolicy.clear();
8633 }
8634
8635 void configureDevice(uint32_t changes) {
8636 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8637 mReader->requestRefreshConfiguration(changes);
8638 mReader->loopOnce();
8639 }
8640 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8641 }
8642
8643 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8644 const std::string& location, int32_t eventHubId,
8645 Flags<InputDeviceClass> classes) {
8646 InputDeviceIdentifier identifier;
8647 identifier.name = name;
8648 identifier.location = location;
8649 std::shared_ptr<InputDevice> device =
8650 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8651 identifier);
8652 mReader->pushNextDevice(device);
8653 mFakeEventHub->addDevice(eventHubId, name, classes);
8654 mReader->loopOnce();
8655 return device;
8656 }
8657
8658 template <class T, typename... Args>
8659 T& addControllerAndConfigure(Args... args) {
8660 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8661
8662 return controller;
8663 }
8664};
8665
8666const char* InputControllerTest::DEVICE_NAME = "device";
8667const char* InputControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8668const int32_t InputControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8669const int32_t InputControllerTest::DEVICE_GENERATION = 2;
8670const int32_t InputControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8671const Flags<InputDeviceClass> InputControllerTest::DEVICE_CLASSES =
8672 Flags<InputDeviceClass>(0); // not needed for current tests
8673const int32_t InputControllerTest::EVENTHUB_ID = 1;
8674
8675// --- BatteryControllerTest ---
8676class BatteryControllerTest : public InputControllerTest {
8677protected:
8678 void SetUp() override {
8679 InputControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
8680 }
8681};
8682
8683TEST_F(BatteryControllerTest, GetBatteryCapacity) {
8684 InputController& controller = addControllerAndConfigure<InputController>();
8685
8686 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8687 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8688}
8689
8690TEST_F(BatteryControllerTest, GetBatteryStatus) {
8691 InputController& controller = addControllerAndConfigure<InputController>();
8692
8693 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8694 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8695}
8696
8697// --- LightControllerTest ---
8698class LightControllerTest : public InputControllerTest {
8699protected:
8700 void SetUp() override { InputControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT); }
8701};
8702
8703TEST_F(LightControllerTest, SingleLight) {
8704 RawLightInfo infoSingle = {.id = 1,
8705 .name = "Mono",
8706 .maxBrightness = 255,
8707 .flags = InputLightClass::BRIGHTNESS,
8708 .path = ""};
8709 mFakeEventHub->addRawLightInfo(infoSingle.id, std::move(infoSingle));
8710
8711 InputController& controller = addControllerAndConfigure<InputController>();
8712 InputDeviceInfo info;
8713 controller.populateDeviceInfo(&info);
8714 const auto& ids = info.getLightIds();
8715 ASSERT_EQ(1UL, ids.size());
8716 ASSERT_EQ(InputDeviceLightType::SINGLE, info.getLightInfo(ids[0])->type);
8717
8718 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_BRIGHTNESS));
8719 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_BRIGHTNESS);
8720}
8721
8722TEST_F(LightControllerTest, RGBLight) {
8723 RawLightInfo infoRed = {.id = 1,
8724 .name = "red",
8725 .maxBrightness = 255,
8726 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8727 .path = ""};
8728 RawLightInfo infoGreen = {.id = 2,
8729 .name = "green",
8730 .maxBrightness = 255,
8731 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8732 .path = ""};
8733 RawLightInfo infoBlue = {.id = 3,
8734 .name = "blue",
8735 .maxBrightness = 255,
8736 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8737 .path = ""};
8738 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8739 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8740 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8741
8742 InputController& controller = addControllerAndConfigure<InputController>();
8743 InputDeviceInfo info;
8744 controller.populateDeviceInfo(&info);
8745 const auto& ids = info.getLightIds();
8746 ASSERT_EQ(1UL, ids.size());
8747 ASSERT_EQ(InputDeviceLightType::RGB, info.getLightInfo(ids[0])->type);
8748
8749 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_COLOR));
8750 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
8751}
8752
8753TEST_F(LightControllerTest, MultiColorRGBLight) {
8754 RawLightInfo infoColor = {.id = 1,
8755 .name = "red",
8756 .maxBrightness = 255,
8757 .flags = InputLightClass::BRIGHTNESS |
8758 InputLightClass::MULTI_INTENSITY |
8759 InputLightClass::MULTI_INDEX,
8760 .path = ""};
8761
8762 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8763
8764 InputController& controller = addControllerAndConfigure<InputController>();
8765 InputDeviceInfo info;
8766 controller.populateDeviceInfo(&info);
8767 const auto& ids = info.getLightIds();
8768 ASSERT_EQ(1UL, ids.size());
8769 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, info.getLightInfo(ids[0])->type);
8770
8771 ASSERT_TRUE(controller.setLightColor(ids[0], LIGHT_COLOR));
8772 ASSERT_EQ(controller.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
8773}
8774
8775TEST_F(LightControllerTest, PlayerIdLight) {
8776 RawLightInfo info1 = {.id = 1,
8777 .name = "player1",
8778 .maxBrightness = 255,
8779 .flags = InputLightClass::BRIGHTNESS,
8780 .path = ""};
8781 RawLightInfo info2 = {.id = 2,
8782 .name = "player2",
8783 .maxBrightness = 255,
8784 .flags = InputLightClass::BRIGHTNESS,
8785 .path = ""};
8786 RawLightInfo info3 = {.id = 3,
8787 .name = "player3",
8788 .maxBrightness = 255,
8789 .flags = InputLightClass::BRIGHTNESS,
8790 .path = ""};
8791 RawLightInfo info4 = {.id = 4,
8792 .name = "player4",
8793 .maxBrightness = 255,
8794 .flags = InputLightClass::BRIGHTNESS,
8795 .path = ""};
8796 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8797 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8798 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8799 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8800
8801 InputController& controller = addControllerAndConfigure<InputController>();
8802 InputDeviceInfo info;
8803 controller.populateDeviceInfo(&info);
8804 const auto& ids = info.getLightIds();
8805 ASSERT_EQ(1UL, ids.size());
8806 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, info.getLightInfo(ids[0])->type);
8807
8808 ASSERT_FALSE(controller.setLightColor(ids[0], LIGHT_COLOR));
8809 ASSERT_TRUE(controller.setLightPlayerId(ids[0], LIGHT_PLAYER_ID));
8810 ASSERT_EQ(controller.getLightPlayerId(ids[0]).value_or(-1), LIGHT_PLAYER_ID);
8811}
8812
Michael Wrightd02c5b62014-02-10 15:10:22 -08008813} // namespace android