blob: d69bb6a303e2f3db1456c1eec630cc210dd461eb [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
Kim Low03ea0352020-11-06 12:45:07 -080017#include <BatteryInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070018#include <CursorInputMapper.h>
19#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>
Chris Ye3fdbfef2021-01-06 18:45:18 -080025#include <LightInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070026#include <MultiTouchInputMapper.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <SingleTouchInputMapper.h>
29#include <SwitchInputMapper.h>
30#include <TestInputListener.h>
31#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080032#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000033#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070034#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080036#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <math.h>
38
Michael Wright17db18e2020-06-26 20:51:44 +010039#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080040#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070046using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070047using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048
49// Timeout for waiting for an expected event
50static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
51
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000053static constexpr nsecs_t ARBITRARY_TIME = 1234;
54static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
56// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080057static constexpr int32_t DISPLAY_ID = 0;
58static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
59static constexpr int32_t DISPLAY_WIDTH = 480;
60static constexpr int32_t DISPLAY_HEIGHT = 800;
61static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
62static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
63static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070064static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070065static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080066
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t FIRST_SLOT = 0;
68static constexpr int32_t SECOND_SLOT = 1;
69static constexpr int32_t THIRD_SLOT = 2;
70static constexpr int32_t INVALID_TRACKING_ID = -1;
71static constexpr int32_t FIRST_TRACKING_ID = 0;
72static constexpr int32_t SECOND_TRACKING_ID = 1;
73static constexpr int32_t THIRD_TRACKING_ID = 2;
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
Kim Low03ea0352020-11-06 12:45:07 -0800898 std::optional<int32_t> getBatteryCapacity(int32_t) const override { return BATTERY_CAPACITY; }
899
900 std::optional<int32_t> getBatteryStatus(int32_t) const override { return BATTERY_STATUS; }
901
Chris Ye3fdbfef2021-01-06 18:45:18 -0800902 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
903 std::vector<int32_t> ids;
904 for (const auto& [rawId, info] : mRawLightInfos) {
905 ids.push_back(rawId);
906 }
907 return ids;
908 }
909
910 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
911 auto it = mRawLightInfos.find(lightId);
912 if (it == mRawLightInfos.end()) {
913 return std::nullopt;
914 }
915 return it->second;
916 }
917
918 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
919 mLightBrightness.emplace(lightId, brightness);
920 }
921
922 void setLightIntensities(int32_t deviceId, int32_t lightId,
923 std::unordered_map<LightColor, int32_t> intensities) override {
924 mLightIntensities.emplace(lightId, intensities);
925 };
926
927 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
928 auto lightIt = mLightBrightness.find(lightId);
929 if (lightIt == mLightBrightness.end()) {
930 return std::nullopt;
931 }
932 return lightIt->second;
933 }
934
935 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
936 int32_t deviceId, int32_t lightId) override {
937 auto lightIt = mLightIntensities.find(lightId);
938 if (lightIt == mLightIntensities.end()) {
939 return std::nullopt;
940 }
941 return lightIt->second;
942 };
943
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100944 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 return false;
946 }
947
Chris Yea52ade12020-08-27 16:49:20 -0700948 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949
Chris Yea52ade12020-08-27 16:49:20 -0700950 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951
Chris Yea52ade12020-08-27 16:49:20 -0700952 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953
Chris Yea52ade12020-08-27 16:49:20 -0700954 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955};
956
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957// --- FakeInputMapper ---
958
959class FakeInputMapper : public InputMapper {
960 uint32_t mSources;
961 int32_t mKeyboardType;
962 int32_t mMetaState;
963 KeyedVector<int32_t, int32_t> mKeyCodeStates;
964 KeyedVector<int32_t, int32_t> mScanCodeStates;
965 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800966 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700968 std::mutex mLock;
969 std::condition_variable mStateChangedCondition;
970 bool mConfigureWasCalled GUARDED_BY(mLock);
971 bool mResetWasCalled GUARDED_BY(mLock);
972 bool mProcessWasCalled GUARDED_BY(mLock);
973 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974
Arthur Hungc23540e2018-11-29 20:42:11 +0800975 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800977 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
978 : InputMapper(deviceContext),
979 mSources(sources),
980 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800982 mConfigureWasCalled(false),
983 mResetWasCalled(false),
984 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985
Chris Yea52ade12020-08-27 16:49:20 -0700986 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987
988 void setKeyboardType(int32_t keyboardType) {
989 mKeyboardType = keyboardType;
990 }
991
992 void setMetaState(int32_t metaState) {
993 mMetaState = metaState;
994 }
995
996 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700997 std::unique_lock<std::mutex> lock(mLock);
998 base::ScopedLockAssertion assumeLocked(mLock);
999 const bool configureCalled =
1000 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1001 return mConfigureWasCalled;
1002 });
1003 if (!configureCalled) {
1004 FAIL() << "Expected configure() to have been called.";
1005 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001006 mConfigureWasCalled = false;
1007 }
1008
1009 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001010 std::unique_lock<std::mutex> lock(mLock);
1011 base::ScopedLockAssertion assumeLocked(mLock);
1012 const bool resetCalled =
1013 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1014 return mResetWasCalled;
1015 });
1016 if (!resetCalled) {
1017 FAIL() << "Expected reset() to have been called.";
1018 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 mResetWasCalled = false;
1020 }
1021
Yi Kong9b14ac62018-07-17 13:48:38 -07001022 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001023 std::unique_lock<std::mutex> lock(mLock);
1024 base::ScopedLockAssertion assumeLocked(mLock);
1025 const bool processCalled =
1026 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1027 return mProcessWasCalled;
1028 });
1029 if (!processCalled) {
1030 FAIL() << "Expected process() to have been called.";
1031 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 if (outLastEvent) {
1033 *outLastEvent = mLastEvent;
1034 }
1035 mProcessWasCalled = false;
1036 }
1037
1038 void setKeyCodeState(int32_t keyCode, int32_t state) {
1039 mKeyCodeStates.replaceValueFor(keyCode, state);
1040 }
1041
1042 void setScanCodeState(int32_t scanCode, int32_t state) {
1043 mScanCodeStates.replaceValueFor(scanCode, state);
1044 }
1045
1046 void setSwitchState(int32_t switchCode, int32_t state) {
1047 mSwitchStates.replaceValueFor(switchCode, state);
1048 }
1049
1050 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001051 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 }
1053
1054private:
Chris Yea52ade12020-08-27 16:49:20 -07001055 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056
Chris Yea52ade12020-08-27 16:49:20 -07001057 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058 InputMapper::populateDeviceInfo(deviceInfo);
1059
1060 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1061 deviceInfo->setKeyboardType(mKeyboardType);
1062 }
1063 }
1064
Chris Yea52ade12020-08-27 16:49:20 -07001065 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001066 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001068
1069 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001070 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001071 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1072 mViewport = config->getDisplayViewportByPort(*displayPort);
1073 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001074
1075 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076 }
1077
Chris Yea52ade12020-08-27 16:49:20 -07001078 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001079 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 }
1083
Chris Yea52ade12020-08-27 16:49:20 -07001084 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001085 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 mLastEvent = *rawEvent;
1087 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001088 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089 }
1090
Chris Yea52ade12020-08-27 16:49:20 -07001091 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1093 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1094 }
1095
Chris Yea52ade12020-08-27 16:49:20 -07001096 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1098 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1099 }
1100
Chris Yea52ade12020-08-27 16:49:20 -07001101 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1103 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1104 }
1105
Chris Yea52ade12020-08-27 16:49:20 -07001106 // Return true if the device has non-empty key layout.
1107 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1108 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 for (size_t i = 0; i < numCodes; i++) {
1110 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1111 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1112 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113 }
1114 }
1115 }
Chris Yea52ade12020-08-27 16:49:20 -07001116 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 return result;
1118 }
1119
1120 virtual int32_t getMetaState() {
1121 return mMetaState;
1122 }
1123
1124 virtual void fadePointer() {
1125 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001126
1127 virtual std::optional<int32_t> getAssociatedDisplay() {
1128 if (mViewport) {
1129 return std::make_optional(mViewport->displayId);
1130 }
1131 return std::nullopt;
1132 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133};
1134
1135
1136// --- InstrumentedInputReader ---
1137
1138class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001139 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140
1141public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001142 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1143 const sp<InputReaderPolicyInterface>& policy,
1144 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001145 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001147 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001149 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001151 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001152 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 InputDeviceIdentifier identifier;
1154 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001155 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001157 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 }
1159
Prabir Pradhan28efc192019-11-05 01:10:04 +00001160 // Make the protected loopOnce method accessible to tests.
1161 using InputReader::loopOnce;
1162
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001164 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1165 const InputDeviceIdentifier& identifier)
1166 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001167 if (!mNextDevices.empty()) {
1168 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1169 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170 return device;
1171 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001172 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 }
1174
arthurhungdcef2dc2020-08-11 14:47:50 +08001175 // --- FakeInputReaderContext ---
1176 class FakeInputReaderContext : public ContextImpl {
1177 int32_t mGlobalMetaState;
1178 bool mUpdateGlobalMetaStateWasCalled;
1179 int32_t mGeneration;
1180
1181 public:
1182 FakeInputReaderContext(InputReader* reader)
1183 : ContextImpl(reader),
1184 mGlobalMetaState(0),
1185 mUpdateGlobalMetaStateWasCalled(false),
1186 mGeneration(1) {}
1187
1188 virtual ~FakeInputReaderContext() {}
1189
1190 void assertUpdateGlobalMetaStateWasCalled() {
1191 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1192 << "Expected updateGlobalMetaState() to have been called.";
1193 mUpdateGlobalMetaStateWasCalled = false;
1194 }
1195
1196 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1197
1198 uint32_t getGeneration() { return mGeneration; }
1199
1200 void updateGlobalMetaState() override {
1201 mUpdateGlobalMetaStateWasCalled = true;
1202 ContextImpl::updateGlobalMetaState();
1203 }
1204
1205 int32_t getGlobalMetaState() override {
1206 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1207 }
1208
1209 int32_t bumpGeneration() override {
1210 mGeneration = ContextImpl::bumpGeneration();
1211 return mGeneration;
1212 }
1213 } mFakeContext;
1214
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001216
1217public:
1218 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219};
1220
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221// --- InputReaderPolicyTest ---
1222class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001223protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001224 sp<FakeInputReaderPolicy> mFakePolicy;
1225
Chris Yea52ade12020-08-27 16:49:20 -07001226 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1227 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001228};
1229
1230/**
1231 * Check that empty set of viewports is an acceptable configuration.
1232 * Also try to get internal viewport two different ways - by type and by uniqueId.
1233 *
1234 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1235 * Such configuration is not currently allowed.
1236 */
1237TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001238 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239
1240 // We didn't add any viewports yet, so there shouldn't be any.
1241 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001242 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001243 ASSERT_FALSE(internalViewport);
1244
1245 // Add an internal viewport, then clear it
1246 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001247 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001248 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001249
1250 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001251 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001253 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254
1255 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001256 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001257 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001258 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001259
1260 mFakePolicy->clearViewports();
1261 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001262 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001263 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001264 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001265 ASSERT_FALSE(internalViewport);
1266}
1267
1268TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1269 const std::string internalUniqueId = "local:0";
1270 const std::string externalUniqueId = "local:1";
1271 const std::string virtualUniqueId1 = "virtual:2";
1272 const std::string virtualUniqueId2 = "virtual:3";
1273 constexpr int32_t virtualDisplayId1 = 2;
1274 constexpr int32_t virtualDisplayId2 = 3;
1275
1276 // Add an internal viewport
1277 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001278 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1279 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001280 // Add an external viewport
1281 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001282 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1283 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001284 // Add an virtual viewport
1285 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001286 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1287 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001288 // Add another virtual viewport
1289 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001290 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1291 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292
1293 // Check matching by type for internal
1294 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001295 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001296 ASSERT_TRUE(internalViewport);
1297 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1298
1299 // Check matching by type for external
1300 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001301 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001302 ASSERT_TRUE(externalViewport);
1303 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1304
1305 // Check matching by uniqueId for virtual viewport #1
1306 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001307 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001308 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001309 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001310 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1311 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1312
1313 // Check matching by uniqueId for virtual viewport #2
1314 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001315 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001316 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001317 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001318 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1319 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1320}
1321
1322
1323/**
1324 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1325 * that lookup works by checking display id.
1326 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1327 */
1328TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1329 const std::string uniqueId1 = "uniqueId1";
1330 const std::string uniqueId2 = "uniqueId2";
1331 constexpr int32_t displayId1 = 2;
1332 constexpr int32_t displayId2 = 3;
1333
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001334 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1335 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001336 for (const ViewportType& type : types) {
1337 mFakePolicy->clearViewports();
1338 // Add a viewport
1339 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001340 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1341 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001342 // Add another viewport
1343 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001344 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1345 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346
1347 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001348 std::optional<DisplayViewport> viewport1 =
1349 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001350 ASSERT_TRUE(viewport1);
1351 ASSERT_EQ(displayId1, viewport1->displayId);
1352 ASSERT_EQ(type, viewport1->type);
1353
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001354 std::optional<DisplayViewport> viewport2 =
1355 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001356 ASSERT_TRUE(viewport2);
1357 ASSERT_EQ(displayId2, viewport2->displayId);
1358 ASSERT_EQ(type, viewport2->type);
1359
1360 // When there are multiple viewports of the same kind, and uniqueId is not specified
1361 // in the call to getDisplayViewport, then that situation is not supported.
1362 // The viewports can be stored in any order, so we cannot rely on the order, since that
1363 // is just implementation detail.
1364 // However, we can check that it still returns *a* viewport, we just cannot assert
1365 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001366 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001367 ASSERT_TRUE(someViewport);
1368 }
1369}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001371/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001372 * When we have multiple internal displays make sure we always return the default display when
1373 * querying by type.
1374 */
1375TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1376 const std::string uniqueId1 = "uniqueId1";
1377 const std::string uniqueId2 = "uniqueId2";
1378 constexpr int32_t nonDefaultDisplayId = 2;
1379 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1380 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1381
1382 // Add the default display first and ensure it gets returned.
1383 mFakePolicy->clearViewports();
1384 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001385 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001386 ViewportType::INTERNAL);
1387 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001388 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001389 ViewportType::INTERNAL);
1390
1391 std::optional<DisplayViewport> viewport =
1392 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1393 ASSERT_TRUE(viewport);
1394 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1395 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1396
1397 // Add the default display second to make sure order doesn't matter.
1398 mFakePolicy->clearViewports();
1399 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001400 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001401 ViewportType::INTERNAL);
1402 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001403 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001404 ViewportType::INTERNAL);
1405
1406 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1407 ASSERT_TRUE(viewport);
1408 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1409 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1410}
1411
1412/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001413 * Check getDisplayViewportByPort
1414 */
1415TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001416 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001417 const std::string uniqueId1 = "uniqueId1";
1418 const std::string uniqueId2 = "uniqueId2";
1419 constexpr int32_t displayId1 = 1;
1420 constexpr int32_t displayId2 = 2;
1421 const uint8_t hdmi1 = 0;
1422 const uint8_t hdmi2 = 1;
1423 const uint8_t hdmi3 = 2;
1424
1425 mFakePolicy->clearViewports();
1426 // Add a viewport that's associated with some display port that's not of interest.
1427 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001428 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1429 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001430 // Add another viewport, connected to HDMI1 port
1431 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001432 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1433 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001434
1435 // Check that correct display viewport was returned by comparing the display ports.
1436 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1437 ASSERT_TRUE(hdmi1Viewport);
1438 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1439 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1440
1441 // Check that we can still get the same viewport using the uniqueId
1442 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1443 ASSERT_TRUE(hdmi1Viewport);
1444 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1445 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1446 ASSERT_EQ(type, hdmi1Viewport->type);
1447
1448 // Check that we cannot find a port with "HDMI2", because we never added one
1449 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1450 ASSERT_FALSE(hdmi2Viewport);
1451}
1452
Michael Wrightd02c5b62014-02-10 15:10:22 -08001453// --- InputReaderTest ---
1454
1455class InputReaderTest : public testing::Test {
1456protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001457 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001459 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001460 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001461
Chris Yea52ade12020-08-27 16:49:20 -07001462 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001463 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001464 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001465 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001466
Prabir Pradhan28efc192019-11-05 01:10:04 +00001467 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1468 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001469 }
1470
Chris Yea52ade12020-08-27 16:49:20 -07001471 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001472 mFakeListener.clear();
1473 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001474 }
1475
Chris Ye1b0c7342020-07-28 21:57:03 -07001476 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001477 const PropertyMap* configuration) {
1478 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479
1480 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001481 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001482 }
1483 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001484 mReader->loopOnce();
1485 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001486 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1487 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001488 }
1489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001490 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001491 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001492 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001493 }
1494
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001495 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001496 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001497 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001498 }
1499
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001500 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001501 const std::string& name,
1502 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001503 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001504 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1505 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001506 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001507 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508 return mapper;
1509 }
1510};
1511
Chris Ye98d3f532020-10-01 21:48:59 -07001512TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001513 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1514 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1515 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516
Chris Ye98d3f532020-10-01 21:48:59 -07001517 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001520 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001521 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1522 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1523 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001524}
1525
1526TEST_F(InputReaderTest, PolicyGetInputDevices) {
1527 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1528 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1529 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001530
1531 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001532 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001533 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001534 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001535 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1537 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1538 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1539}
1540
Chris Yee7310032020-09-22 15:36:28 -07001541TEST_F(InputReaderTest, GetMergedInputDevices) {
1542 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1543 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1544 // Add two subdevices to device
1545 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1546 // Must add at least one mapper or the device will be ignored!
1547 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1548 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1549
1550 // Push same device instance for next device to be added, so they'll have same identifier.
1551 mReader->pushNextDevice(device);
1552 mReader->pushNextDevice(device);
1553 ASSERT_NO_FATAL_FAILURE(
1554 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1555 ASSERT_NO_FATAL_FAILURE(
1556 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1557
1558 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001559 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001560}
1561
Chris Yee14523a2020-12-19 13:46:00 -08001562TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1563 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1564 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1565 // Add two subdevices to device
1566 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1567 // Must add at least one mapper or the device will be ignored!
1568 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1569 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1570
1571 // Push same device instance for next device to be added, so they'll have same identifier.
1572 mReader->pushNextDevice(device);
1573 mReader->pushNextDevice(device);
1574 // Sensor device is initially disabled
1575 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1576 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1577 nullptr));
1578 // Device is disabled because the only sub device is a sensor device and disabled initially.
1579 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1580 ASSERT_FALSE(device->isEnabled());
1581 ASSERT_NO_FATAL_FAILURE(
1582 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1583 // The merged device is enabled if any sub device is enabled
1584 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1585 ASSERT_TRUE(device->isEnabled());
1586}
1587
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001588TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001589 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001590 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 constexpr int32_t eventHubId = 1;
1592 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001593 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001594 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001595 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001596 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001597
Yi Kong9b14ac62018-07-17 13:48:38 -07001598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001599
1600 NotifyDeviceResetArgs resetArgs;
1601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602 ASSERT_EQ(deviceId, resetArgs.deviceId);
1603
1604 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001605 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001606 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001609 ASSERT_EQ(deviceId, resetArgs.deviceId);
1610 ASSERT_EQ(device->isEnabled(), false);
1611
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001612 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001613 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616 ASSERT_EQ(device->isEnabled(), false);
1617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001618 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001619 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621 ASSERT_EQ(deviceId, resetArgs.deviceId);
1622 ASSERT_EQ(device->isEnabled(), true);
1623}
1624
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001627 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001628 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001631 AINPUT_SOURCE_KEYBOARD, nullptr);
1632 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633
1634 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1635 AINPUT_SOURCE_ANY, AKEYCODE_A))
1636 << "Should return unknown when the device id is >= 0 but unknown.";
1637
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1639 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1640 << "Should return unknown when the device id is valid but the sources are not "
1641 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 ASSERT_EQ(AKEY_STATE_DOWN,
1644 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1645 AKEYCODE_A))
1646 << "Should return value provided by mapper when device id is valid and the device "
1647 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648
1649 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1650 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1651 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1652
1653 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1654 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1655 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1656}
1657
1658TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001659 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001660 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001661 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001664 AINPUT_SOURCE_KEYBOARD, nullptr);
1665 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001666
1667 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1668 AINPUT_SOURCE_ANY, KEY_A))
1669 << "Should return unknown when the device id is >= 0 but unknown.";
1670
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001671 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1672 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1673 << "Should return unknown when the device id is valid but the sources are not "
1674 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 ASSERT_EQ(AKEY_STATE_DOWN,
1677 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1678 KEY_A))
1679 << "Should return value provided by mapper when device id is valid and the device "
1680 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681
1682 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1683 AINPUT_SOURCE_TRACKBALL, KEY_A))
1684 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1685
1686 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1687 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1688 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1689}
1690
1691TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001692 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001693 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001694 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001697 AINPUT_SOURCE_KEYBOARD, nullptr);
1698 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001699
1700 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1701 AINPUT_SOURCE_ANY, SW_LID))
1702 << "Should return unknown when the device id is >= 0 but unknown.";
1703
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001704 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1705 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1706 << "Should return unknown when the device id is valid but the sources are not "
1707 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 ASSERT_EQ(AKEY_STATE_DOWN,
1710 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1711 SW_LID))
1712 << "Should return value provided by mapper when device id is valid and the device "
1713 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714
1715 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1716 AINPUT_SOURCE_TRACKBALL, SW_LID))
1717 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1718
1719 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1720 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1721 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1722}
1723
1724TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001725 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001726 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001728 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001729 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001731
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001732 mapper.addSupportedKeyCode(AKEYCODE_A);
1733 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001734
1735 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1736 uint8_t flags[4] = { 0, 0, 0, 1 };
1737
1738 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1739 << "Should return false when device id is >= 0 but unknown.";
1740 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1741
1742 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001743 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1744 << "Should return false when device id is valid but the sources are not supported by "
1745 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001746 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1747
1748 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1750 keyCodes, flags))
1751 << "Should return value provided by mapper when device id is valid and the device "
1752 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001753 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1754
1755 flags[3] = 1;
1756 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1757 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1758 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1759
1760 flags[3] = 1;
1761 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1762 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1763 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1764}
1765
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001766TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001767 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001768 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001769
1770 NotifyConfigurationChangedArgs args;
1771
1772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1773 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1774}
1775
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001776TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001778 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001779 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001780 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001781 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001782 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001783 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001784 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001785
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001786 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001787 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001788 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1789
1790 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001791 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001792 ASSERT_EQ(when, event.when);
1793 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001795 ASSERT_EQ(EV_KEY, event.type);
1796 ASSERT_EQ(KEY_A, event.code);
1797 ASSERT_EQ(1, event.value);
1798}
1799
Garfield Tan1c7bc862020-01-28 13:24:04 -08001800TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001801 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001802 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 constexpr int32_t eventHubId = 1;
1804 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001805 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001807 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001809
1810 NotifyDeviceResetArgs resetArgs;
1811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001812 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001813
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001815 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001817 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001818 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001820 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001821 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001823 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001824 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001826 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001827 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001829 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001830 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001831}
1832
Garfield Tan1c7bc862020-01-28 13:24:04 -08001833TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1834 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001835 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001836 constexpr int32_t eventHubId = 1;
1837 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1838 // Must add at least one mapper or the device will be ignored!
1839 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001840 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001841 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1842
1843 NotifyDeviceResetArgs resetArgs;
1844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1845 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1846}
1847
Arthur Hungc23540e2018-11-29 20:42:11 +08001848TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001850 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001852 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001853 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1854 FakeInputMapper& mapper =
1855 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001856 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001857
1858 const uint8_t hdmi1 = 1;
1859
1860 // Associated touch screen with second display.
1861 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1862
1863 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001864 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001865 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001866 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001867 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001868 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001869 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001870 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001871 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001872 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001873
1874 // Add the device, and make sure all of the callbacks are triggered.
1875 // The device is added after the input port associations are processed since
1876 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001880 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001881
Arthur Hung2c9a3342019-07-23 14:18:59 +08001882 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001883 ASSERT_EQ(deviceId, device->getId());
1884 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1885 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001886
1887 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001888 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001889 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001890 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001891}
1892
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001893TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1894 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1895 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1896 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1897 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1898 // Must add at least one mapper or the device will be ignored!
1899 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1900 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1901 mReader->pushNextDevice(device);
1902 mReader->pushNextDevice(device);
1903 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1904 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1905
1906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1907
1908 NotifyDeviceResetArgs resetArgs;
1909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1910 ASSERT_EQ(deviceId, resetArgs.deviceId);
1911 ASSERT_TRUE(device->isEnabled());
1912 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1913 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1914
1915 disableDevice(deviceId);
1916 mReader->loopOnce();
1917
1918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1919 ASSERT_EQ(deviceId, resetArgs.deviceId);
1920 ASSERT_FALSE(device->isEnabled());
1921 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1922 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1923
1924 enableDevice(deviceId);
1925 mReader->loopOnce();
1926
1927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1928 ASSERT_EQ(deviceId, resetArgs.deviceId);
1929 ASSERT_TRUE(device->isEnabled());
1930 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1931 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1932}
1933
1934TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1935 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1936 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1937 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1938 // Add two subdevices to device
1939 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1940 FakeInputMapper& mapperDevice1 =
1941 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1942 FakeInputMapper& mapperDevice2 =
1943 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1944 mReader->pushNextDevice(device);
1945 mReader->pushNextDevice(device);
1946 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1947 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1948
1949 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1950 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1951
1952 ASSERT_EQ(AKEY_STATE_DOWN,
1953 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1954 ASSERT_EQ(AKEY_STATE_DOWN,
1955 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1956 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1957 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1958}
1959
Prabir Pradhan7e186182020-11-10 13:56:45 -08001960TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1961 NotifyPointerCaptureChangedArgs args;
1962
1963 mFakePolicy->setPointerCapture(true);
1964 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1965 mReader->loopOnce();
1966 mFakeListener->assertNotifyCaptureWasCalled(&args);
1967 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1968
1969 mFakePolicy->setPointerCapture(false);
1970 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1971 mReader->loopOnce();
1972 mFakeListener->assertNotifyCaptureWasCalled(&args);
1973 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1974
1975 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1976 // does not change.
1977 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1978 mReader->loopOnce();
1979 mFakeListener->assertNotifyCaptureWasCalled(&args);
1980 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1981}
1982
Chris Ye87143712020-11-10 05:05:58 +00001983class FakeVibratorInputMapper : public FakeInputMapper {
1984public:
1985 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1986 : FakeInputMapper(deviceContext, sources) {}
1987
1988 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1989};
1990
1991TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1992 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1993 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1994 constexpr int32_t eventHubId = 1;
1995 const char* DEVICE_LOCATION = "BLUETOOTH";
1996 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1997 FakeVibratorInputMapper& mapper =
1998 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1999 mReader->pushNextDevice(device);
2000
2001 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2002 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2003
2004 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2005 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2006}
2007
Kim Low03ea0352020-11-06 12:45:07 -08002008class FakeBatteryInputMapper : public FakeInputMapper {
2009public:
2010 FakeBatteryInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2011 : FakeInputMapper(deviceContext, sources) {}
2012
2013 std::optional<int32_t> getBatteryCapacity() override {
2014 return getDeviceContext().getBatteryCapacity();
2015 }
2016
2017 std::optional<int32_t> getBatteryStatus() override {
2018 return getDeviceContext().getBatteryStatus();
2019 }
2020};
2021
2022TEST_F(InputReaderTest, BatteryGetCapacity) {
2023 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2024 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2025 constexpr int32_t eventHubId = 1;
2026 const char* DEVICE_LOCATION = "BLUETOOTH";
2027 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2028 FakeBatteryInputMapper& mapper =
2029 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2030 mReader->pushNextDevice(device);
2031
2032 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2033 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2034
2035 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2036}
2037
2038TEST_F(InputReaderTest, BatteryGetStatus) {
2039 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2040 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2041 constexpr int32_t eventHubId = 1;
2042 const char* DEVICE_LOCATION = "BLUETOOTH";
2043 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2044 FakeBatteryInputMapper& mapper =
2045 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2046 mReader->pushNextDevice(device);
2047
2048 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2049 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2050
2051 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2052}
2053
Chris Ye3fdbfef2021-01-06 18:45:18 -08002054class FakeLightInputMapper : public FakeInputMapper {
2055public:
2056 FakeLightInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2057 : FakeInputMapper(deviceContext, sources) {}
2058
2059 bool setLightColor(int32_t lightId, int32_t color) override {
2060 getDeviceContext().setLightBrightness(lightId, color >> 24);
2061 return true;
2062 }
2063
2064 std::optional<int32_t> getLightColor(int32_t lightId) override {
2065 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2066 if (!result.has_value()) {
2067 return std::nullopt;
2068 }
2069 return result.value() << 24;
2070 }
2071};
2072
2073TEST_F(InputReaderTest, LightGetColor) {
2074 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2075 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2076 constexpr int32_t eventHubId = 1;
2077 const char* DEVICE_LOCATION = "BLUETOOTH";
2078 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2079 FakeLightInputMapper& mapper =
2080 device->addMapper<FakeLightInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2081 mReader->pushNextDevice(device);
2082 RawLightInfo info = {.id = 1,
2083 .name = "Mono",
2084 .maxBrightness = 255,
2085 .flags = InputLightClass::BRIGHTNESS,
2086 .path = ""};
2087 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2088 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2089
2090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2091 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2092
2093 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2094 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2095}
2096
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002097// --- InputReaderIntegrationTest ---
2098
2099// These tests create and interact with the InputReader only through its interface.
2100// The InputReader is started during SetUp(), which starts its processing in its own
2101// thread. The tests use linux uinput to emulate input devices.
2102// NOTE: Interacting with the physical device while these tests are running may cause
2103// the tests to fail.
2104class InputReaderIntegrationTest : public testing::Test {
2105protected:
2106 sp<TestInputListener> mTestListener;
2107 sp<FakeInputReaderPolicy> mFakePolicy;
2108 sp<InputReaderInterface> mReader;
2109
Chris Yea52ade12020-08-27 16:49:20 -07002110 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002111 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002112 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2113 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002114
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002115 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002116 ASSERT_EQ(mReader->start(), OK);
2117
2118 // Since this test is run on a real device, all the input devices connected
2119 // to the test device will show up in mReader. We wait for those input devices to
2120 // show up before beginning the tests.
2121 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2122 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2123 }
2124
Chris Yea52ade12020-08-27 16:49:20 -07002125 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002126 ASSERT_EQ(mReader->stop(), OK);
2127 mTestListener.clear();
2128 mFakePolicy.clear();
2129 }
2130};
2131
2132TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2133 // An invalid input device that is only used for this test.
2134 class InvalidUinputDevice : public UinputDevice {
2135 public:
2136 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2137
2138 private:
2139 void configureDevice(int fd, uinput_user_dev* device) override {}
2140 };
2141
2142 const size_t numDevices = mFakePolicy->getInputDevices().size();
2143
2144 // UinputDevice does not set any event or key bits, so InputReader should not
2145 // consider it as a valid device.
2146 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2147 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2148 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2149 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2150
2151 invalidDevice.reset();
2152 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2153 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2154 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2155}
2156
2157TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2158 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2159
2160 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2161 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2162 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2163 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2164
2165 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002166 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2167 const auto& it =
2168 std::find_if(inputDevices.begin(), inputDevices.end(),
2169 [&keyboard](const InputDeviceInfo& info) {
2170 return info.getIdentifier().name == keyboard->getName();
2171 });
2172
2173 ASSERT_NE(it, inputDevices.end());
2174 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2175 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2176 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002177
2178 keyboard.reset();
2179 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2180 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2181 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2182}
2183
2184TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2185 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2186 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2187
2188 NotifyConfigurationChangedArgs configChangedArgs;
2189 ASSERT_NO_FATAL_FAILURE(
2190 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002191 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002192 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2193
2194 NotifyKeyArgs keyArgs;
2195 keyboard->pressAndReleaseHomeKey();
2196 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2197 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002198 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002199 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002200 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002201 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002202 prevTimestamp = keyArgs.eventTime;
2203
2204 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2205 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002206 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002207 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002208 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002209}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002211/**
2212 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2213 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2214 * are passed to the listener.
2215 */
2216static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2217TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2218 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2219 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2220 NotifyKeyArgs keyArgs;
2221
2222 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2223 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2224 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2225 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2226
2227 controller->pressAndReleaseKey(BTN_GEAR_UP);
2228 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2229 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2230 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2231}
2232
Arthur Hungaab25622020-01-16 11:22:11 +08002233// --- TouchProcessTest ---
2234class TouchIntegrationTest : public InputReaderIntegrationTest {
2235protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002236 const std::string UNIQUE_ID = "local:0";
2237
Chris Yea52ade12020-08-27 16:49:20 -07002238 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002239 InputReaderIntegrationTest::SetUp();
2240 // At least add an internal display.
2241 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2242 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002243 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002244
2245 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2246 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2247 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2248 }
2249
2250 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2251 int32_t orientation, const std::string& uniqueId,
2252 std::optional<uint8_t> physicalPort,
2253 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002254 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2255 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002256 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2257 }
2258
2259 std::unique_ptr<UinputTouchScreen> mDevice;
2260};
2261
2262TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2263 NotifyMotionArgs args;
2264 const Point centerPoint = mDevice->getCenterPoint();
2265
2266 // ACTION_DOWN
2267 mDevice->sendDown(centerPoint);
2268 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2270
2271 // ACTION_MOVE
2272 mDevice->sendMove(centerPoint + Point(1, 1));
2273 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2275
2276 // ACTION_UP
2277 mDevice->sendUp();
2278 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2279 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2280}
2281
2282TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2283 NotifyMotionArgs args;
2284 const Point centerPoint = mDevice->getCenterPoint();
2285
2286 // ACTION_DOWN
2287 mDevice->sendDown(centerPoint);
2288 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2290
2291 // ACTION_POINTER_DOWN (Second slot)
2292 const Point secondPoint = centerPoint + Point(100, 100);
2293 mDevice->sendSlot(SECOND_SLOT);
2294 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2295 mDevice->sendDown(secondPoint + Point(1, 1));
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2297 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2298 args.action);
2299
2300 // ACTION_MOVE (Second slot)
2301 mDevice->sendMove(secondPoint);
2302 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2304
2305 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002306 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002307 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002308 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002309 args.action);
2310
2311 // ACTION_UP
2312 mDevice->sendSlot(FIRST_SLOT);
2313 mDevice->sendUp();
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2315 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2316}
2317
2318TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2319 NotifyMotionArgs args;
2320 const Point centerPoint = mDevice->getCenterPoint();
2321
2322 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002323 mDevice->sendSlot(FIRST_SLOT);
2324 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002325 mDevice->sendDown(centerPoint);
2326 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2327 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2328
arthurhungcc7f9802020-04-30 17:55:40 +08002329 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002330 const Point secondPoint = centerPoint + Point(100, 100);
2331 mDevice->sendSlot(SECOND_SLOT);
2332 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2333 mDevice->sendDown(secondPoint);
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2335 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2336 args.action);
2337
arthurhungcc7f9802020-04-30 17:55:40 +08002338 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002339 mDevice->sendMove(secondPoint + Point(1, 1));
2340 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2342
arthurhungcc7f9802020-04-30 17:55:40 +08002343 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2344 // a palm event.
2345 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002346 mDevice->sendToolType(MT_TOOL_PALM);
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002348 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2349 args.action);
2350 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002351
arthurhungcc7f9802020-04-30 17:55:40 +08002352 // Send up to second slot, expect first slot send moving.
2353 mDevice->sendPointerUp();
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002356
arthurhungcc7f9802020-04-30 17:55:40 +08002357 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002358 mDevice->sendSlot(FIRST_SLOT);
2359 mDevice->sendUp();
2360
arthurhungcc7f9802020-04-30 17:55:40 +08002361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2362 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002363}
2364
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002366class InputDeviceTest : public testing::Test {
2367protected:
2368 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002369 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370 static const int32_t DEVICE_ID;
2371 static const int32_t DEVICE_GENERATION;
2372 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002373 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002374 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002376 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002378 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002379 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002380 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381
Chris Yea52ade12020-08-27 16:49:20 -07002382 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002383 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002385 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002386 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2387 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002388 InputDeviceIdentifier identifier;
2389 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002390 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002391 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002392 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002393 mReader->pushNextDevice(mDevice);
2394 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2395 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396 }
2397
Chris Yea52ade12020-08-27 16:49:20 -07002398 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399 mFakeListener.clear();
2400 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 }
2402};
2403
2404const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002405const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002406const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2408const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002409const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2410 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002411const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412
2413TEST_F(InputDeviceTest, ImmutableProperties) {
2414 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002415 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002416 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417}
2418
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002419TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2420 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002421}
2422
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2424 // Configuration.
2425 InputReaderConfiguration config;
2426 mDevice->configure(ARBITRARY_TIME, &config, 0);
2427
2428 // Reset.
2429 mDevice->reset(ARBITRARY_TIME);
2430
2431 NotifyDeviceResetArgs resetArgs;
2432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2433 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2434 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2435
2436 // Metadata.
2437 ASSERT_TRUE(mDevice->isIgnored());
2438 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2439
2440 InputDeviceInfo info;
2441 mDevice->getDeviceInfo(&info);
2442 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002443 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2445 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2446
2447 // State queries.
2448 ASSERT_EQ(0, mDevice->getMetaState());
2449
2450 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2451 << "Ignored device should return unknown key code state.";
2452 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2453 << "Ignored device should return unknown scan code state.";
2454 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2455 << "Ignored device should return unknown switch state.";
2456
2457 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2458 uint8_t flags[2] = { 0, 1 };
2459 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2460 << "Ignored device should never mark any key codes.";
2461 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2462 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2463}
2464
2465TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2466 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002467 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002469 FakeInputMapper& mapper1 =
2470 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002471 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2472 mapper1.setMetaState(AMETA_ALT_ON);
2473 mapper1.addSupportedKeyCode(AKEYCODE_A);
2474 mapper1.addSupportedKeyCode(AKEYCODE_B);
2475 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2476 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2477 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2478 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2479 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002481 FakeInputMapper& mapper2 =
2482 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002483 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002484
2485 InputReaderConfiguration config;
2486 mDevice->configure(ARBITRARY_TIME, &config, 0);
2487
2488 String8 propertyValue;
2489 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2490 << "Device should have read configuration during configuration phase.";
2491 ASSERT_STREQ("value", propertyValue.string());
2492
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002493 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2494 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495
2496 // Reset
2497 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002498 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2499 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500
2501 NotifyDeviceResetArgs resetArgs;
2502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2503 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2504 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2505
2506 // Metadata.
2507 ASSERT_FALSE(mDevice->isIgnored());
2508 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2509
2510 InputDeviceInfo info;
2511 mDevice->getDeviceInfo(&info);
2512 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002513 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2515 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2516
2517 // State queries.
2518 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2519 << "Should query mappers and combine meta states.";
2520
2521 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2522 << "Should return unknown key code state when source not supported.";
2523 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2524 << "Should return unknown scan code state when source not supported.";
2525 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2526 << "Should return unknown switch state when source not supported.";
2527
2528 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2529 << "Should query mapper when source is supported.";
2530 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2531 << "Should query mapper when source is supported.";
2532 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2533 << "Should query mapper when source is supported.";
2534
2535 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2536 uint8_t flags[4] = { 0, 0, 0, 1 };
2537 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2538 << "Should do nothing when source is unsupported.";
2539 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2540 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2541 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2542 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2543
2544 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2545 << "Should query mapper when source is supported.";
2546 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2547 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2548 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2549 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2550
2551 // Event handling.
2552 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002553 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002554 mDevice->process(&event, 1);
2555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002556 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2557 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558}
2559
Arthur Hung2c9a3342019-07-23 14:18:59 +08002560// A single input device is associated with a specific display. Check that:
2561// 1. Device is disabled if the viewport corresponding to the associated display is not found
2562// 2. Device is disabled when setEnabled API is called
2563TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002564 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002565
2566 // First Configuration.
2567 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2568
2569 // Device should be enabled by default.
2570 ASSERT_TRUE(mDevice->isEnabled());
2571
2572 // Prepare associated info.
2573 constexpr uint8_t hdmi = 1;
2574 const std::string UNIQUE_ID = "local:1";
2575
2576 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2577 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2578 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2579 // Device should be disabled because it is associated with a specific display via
2580 // input port <-> display port association, but the corresponding display is not found
2581 ASSERT_FALSE(mDevice->isEnabled());
2582
2583 // Prepare displays.
2584 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002585 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2586 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002587 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2588 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2589 ASSERT_TRUE(mDevice->isEnabled());
2590
2591 // Device should be disabled after set disable.
2592 mFakePolicy->addDisabledDevice(mDevice->getId());
2593 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2594 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2595 ASSERT_FALSE(mDevice->isEnabled());
2596
2597 // Device should still be disabled even found the associated display.
2598 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2599 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2600 ASSERT_FALSE(mDevice->isEnabled());
2601}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602
2603// --- InputMapperTest ---
2604
2605class InputMapperTest : public testing::Test {
2606protected:
2607 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002608 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 static const int32_t DEVICE_ID;
2610 static const int32_t DEVICE_GENERATION;
2611 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002612 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002613 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002615 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002617 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002618 std::unique_ptr<InstrumentedInputReader> mReader;
2619 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620
Chris Ye1b0c7342020-07-28 21:57:03 -07002621 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002622 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002623 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002624 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002625 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2626 mFakeListener);
2627 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628 }
2629
Chris Yea52ade12020-08-27 16:49:20 -07002630 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002631
Chris Yea52ade12020-08-27 16:49:20 -07002632 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633 mFakeListener.clear();
2634 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 }
2636
2637 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002638 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 }
2640
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002641 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002642 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002643 mReader->requestRefreshConfiguration(changes);
2644 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002645 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002646 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2647 }
2648
arthurhungdcef2dc2020-08-11 14:47:50 +08002649 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2650 const std::string& location, int32_t eventHubId,
2651 Flags<InputDeviceClass> classes) {
2652 InputDeviceIdentifier identifier;
2653 identifier.name = name;
2654 identifier.location = location;
2655 std::shared_ptr<InputDevice> device =
2656 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2657 identifier);
2658 mReader->pushNextDevice(device);
2659 mFakeEventHub->addDevice(eventHubId, name, classes);
2660 mReader->loopOnce();
2661 return device;
2662 }
2663
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002664 template <class T, typename... Args>
2665 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002666 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002667 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002669 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002670 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 }
2672
2673 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002674 int32_t orientation, const std::string& uniqueId,
2675 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002676 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2677 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002678 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2679 }
2680
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002681 void clearViewports() {
2682 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683 }
2684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002685 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2686 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 RawEvent event;
2688 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002689 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002690 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 event.type = type;
2692 event.code = code;
2693 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002694 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002695 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 }
2697
2698 static void assertMotionRange(const InputDeviceInfo& info,
2699 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2700 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002701 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2703 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2704 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2705 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2706 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2707 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2708 }
2709
2710 static void assertPointerCoords(const PointerCoords& coords,
2711 float x, float y, float pressure, float size,
2712 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2713 float orientation, float distance) {
2714 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2715 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2716 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2717 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2718 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2719 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2720 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2721 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2722 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2723 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2724 }
2725
Michael Wright17db18e2020-06-26 20:51:44 +01002726 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002728 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 ASSERT_NEAR(x, actualX, 1);
2730 ASSERT_NEAR(y, actualY, 1);
2731 }
2732};
2733
2734const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002735const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2738const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002739const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2740 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002741const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742
2743// --- SwitchInputMapperTest ---
2744
2745class SwitchInputMapperTest : public InputMapperTest {
2746protected:
2747};
2748
2749TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002750 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753}
2754
2755TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002756 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002758 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002759 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002761 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002762 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763}
2764
2765TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772
2773 NotifySwitchArgs args;
2774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2775 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002776 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2777 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778 args.switchMask);
2779 ASSERT_EQ(uint32_t(0), args.policyFlags);
2780}
2781
Chris Ye87143712020-11-10 05:05:58 +00002782// --- VibratorInputMapperTest ---
2783class VibratorInputMapperTest : public InputMapperTest {
2784protected:
2785 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2786};
2787
2788TEST_F(VibratorInputMapperTest, GetSources) {
2789 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2790
2791 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2792}
2793
2794TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2795 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2796
2797 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2798}
2799
2800TEST_F(VibratorInputMapperTest, Vibrate) {
2801 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002802 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002803 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2804
2805 VibrationElement pattern(2);
2806 VibrationSequence sequence(2);
2807 pattern.duration = std::chrono::milliseconds(200);
2808 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2809 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2810 sequence.addElement(pattern);
2811 pattern.duration = std::chrono::milliseconds(500);
2812 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2813 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2814 sequence.addElement(pattern);
2815
2816 std::vector<int64_t> timings = {0, 1};
2817 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2818
2819 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002820 // Start vibrating
2821 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002822 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002823 // Verify vibrator state listener was notified.
2824 mReader->loopOnce();
2825 NotifyVibratorStateArgs args;
2826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2827 ASSERT_EQ(DEVICE_ID, args.deviceId);
2828 ASSERT_TRUE(args.isOn);
2829 // Stop vibrating
2830 mapper.cancelVibrate(VIBRATION_TOKEN);
2831 ASSERT_FALSE(mapper.isVibrating());
2832 // Verify vibrator state listener was notified.
2833 mReader->loopOnce();
2834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2835 ASSERT_EQ(DEVICE_ID, args.deviceId);
2836 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002837}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838
Chris Yef59a2f42020-10-16 12:55:26 -07002839// --- SensorInputMapperTest ---
2840
2841class SensorInputMapperTest : public InputMapperTest {
2842protected:
2843 static const int32_t ACCEL_RAW_MIN;
2844 static const int32_t ACCEL_RAW_MAX;
2845 static const int32_t ACCEL_RAW_FUZZ;
2846 static const int32_t ACCEL_RAW_FLAT;
2847 static const int32_t ACCEL_RAW_RESOLUTION;
2848
2849 static const int32_t GYRO_RAW_MIN;
2850 static const int32_t GYRO_RAW_MAX;
2851 static const int32_t GYRO_RAW_FUZZ;
2852 static const int32_t GYRO_RAW_FLAT;
2853 static const int32_t GYRO_RAW_RESOLUTION;
2854
2855 static const float GRAVITY_MS2_UNIT;
2856 static const float DEGREE_RADIAN_UNIT;
2857
2858 void prepareAccelAxes();
2859 void prepareGyroAxes();
2860 void setAccelProperties();
2861 void setGyroProperties();
2862 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2863};
2864
2865const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2866const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2867const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2868const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2869const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2870
2871const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2872const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2873const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2874const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2875const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2876
2877const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2878const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2879
2880void SensorInputMapperTest::prepareAccelAxes() {
2881 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2882 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2884 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2885 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2886 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2887}
2888
2889void SensorInputMapperTest::prepareGyroAxes() {
2890 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2891 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2892 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2893 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2894 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2895 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2896}
2897
2898void SensorInputMapperTest::setAccelProperties() {
2899 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2900 /* sensorDataIndex */ 0);
2901 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2902 /* sensorDataIndex */ 1);
2903 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2904 /* sensorDataIndex */ 2);
2905 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2906 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2907 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2908 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2909 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2910}
2911
2912void SensorInputMapperTest::setGyroProperties() {
2913 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2914 /* sensorDataIndex */ 0);
2915 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2916 /* sensorDataIndex */ 1);
2917 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2918 /* sensorDataIndex */ 2);
2919 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2920 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2921 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2922 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2923 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2924}
2925
2926TEST_F(SensorInputMapperTest, GetSources) {
2927 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2928
2929 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2930}
2931
2932TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2933 setAccelProperties();
2934 prepareAccelAxes();
2935 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2936
2937 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2938 std::chrono::microseconds(10000),
2939 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002940 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002946
2947 NotifySensorArgs args;
2948 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2949 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2950 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2951
2952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2953 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2954 ASSERT_EQ(args.deviceId, DEVICE_ID);
2955 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2956 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2957 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2958 ASSERT_EQ(args.values, values);
2959 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2960}
2961
2962TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2963 setGyroProperties();
2964 prepareGyroAxes();
2965 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2966
2967 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2968 std::chrono::microseconds(10000),
2969 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002970 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002976
2977 NotifySensorArgs args;
2978 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2979 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2980 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2981
2982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2983 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2984 ASSERT_EQ(args.deviceId, DEVICE_ID);
2985 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2986 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2987 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2988 ASSERT_EQ(args.values, values);
2989 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2990}
2991
Kim Low03ea0352020-11-06 12:45:07 -08002992// --- BatteryInputMapperTest ---
2993class BatteryInputMapperTest : public InputMapperTest {
2994protected:
2995 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY); }
2996};
2997
2998TEST_F(BatteryInputMapperTest, GetSources) {
2999 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
3000
3001 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3002}
3003
3004TEST_F(BatteryInputMapperTest, GetBatteryCapacity) {
3005 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
3006
3007 ASSERT_TRUE(mapper.getBatteryCapacity());
Chris Ye3fdbfef2021-01-06 18:45:18 -08003008 ASSERT_EQ(mapper.getBatteryCapacity().value_or(-1), BATTERY_CAPACITY);
Kim Low03ea0352020-11-06 12:45:07 -08003009}
3010
3011TEST_F(BatteryInputMapperTest, GetBatteryStatus) {
3012 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
3013
3014 ASSERT_TRUE(mapper.getBatteryStatus());
Chris Ye3fdbfef2021-01-06 18:45:18 -08003015 ASSERT_EQ(mapper.getBatteryStatus().value_or(-1), BATTERY_STATUS);
3016}
3017
3018// --- LightInputMapperTest ---
3019class LightInputMapperTest : public InputMapperTest {
3020protected:
3021 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT); }
3022};
3023
3024TEST_F(LightInputMapperTest, GetSources) {
3025 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3026
3027 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3028}
3029
3030TEST_F(LightInputMapperTest, SingleLight) {
3031 RawLightInfo infoSingle = {.id = 1,
3032 .name = "Mono",
3033 .maxBrightness = 255,
3034 .flags = InputLightClass::BRIGHTNESS,
3035 .path = ""};
3036 mFakeEventHub->addRawLightInfo(infoSingle.id, std::move(infoSingle));
3037
3038 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3039 InputDeviceInfo info;
3040 mapper.populateDeviceInfo(&info);
3041 const auto& ids = info.getLightIds();
3042 ASSERT_EQ(1UL, ids.size());
3043 ASSERT_EQ(InputDeviceLightType::SINGLE, info.getLightInfo(ids[0])->type);
3044
3045 ASSERT_TRUE(mapper.setLightColor(ids[0], LIGHT_BRIGHTNESS));
3046 ASSERT_EQ(mapper.getLightColor(ids[0]).value_or(-1), LIGHT_BRIGHTNESS);
3047}
3048
3049TEST_F(LightInputMapperTest, RGBLight) {
3050 RawLightInfo infoRed = {.id = 1,
3051 .name = "red",
3052 .maxBrightness = 255,
3053 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
3054 .path = ""};
3055 RawLightInfo infoGreen = {.id = 2,
3056 .name = "green",
3057 .maxBrightness = 255,
3058 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
3059 .path = ""};
3060 RawLightInfo infoBlue = {.id = 3,
3061 .name = "blue",
3062 .maxBrightness = 255,
3063 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
3064 .path = ""};
3065 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
3066 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
3067 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
3068
3069 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3070 InputDeviceInfo info;
3071 mapper.populateDeviceInfo(&info);
3072 const auto& ids = info.getLightIds();
3073 ASSERT_EQ(1UL, ids.size());
3074 ASSERT_EQ(InputDeviceLightType::RGB, info.getLightInfo(ids[0])->type);
3075
3076 ASSERT_TRUE(mapper.setLightColor(ids[0], LIGHT_COLOR));
3077 ASSERT_EQ(mapper.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
3078}
3079
3080TEST_F(LightInputMapperTest, MultiColorRGBLight) {
3081 RawLightInfo infoColor = {.id = 1,
3082 .name = "red",
3083 .maxBrightness = 255,
3084 .flags = InputLightClass::BRIGHTNESS |
3085 InputLightClass::MULTI_INTENSITY |
3086 InputLightClass::MULTI_INDEX,
3087 .path = ""};
3088
3089 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
3090
3091 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3092 InputDeviceInfo info;
3093 mapper.populateDeviceInfo(&info);
3094 const auto& ids = info.getLightIds();
3095 ASSERT_EQ(1UL, ids.size());
3096 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, info.getLightInfo(ids[0])->type);
3097
3098 ASSERT_TRUE(mapper.setLightColor(ids[0], LIGHT_COLOR));
3099 ASSERT_EQ(mapper.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
3100}
3101
3102TEST_F(LightInputMapperTest, PlayerIdLight) {
3103 RawLightInfo info1 = {.id = 1,
3104 .name = "player1",
3105 .maxBrightness = 255,
3106 .flags = InputLightClass::BRIGHTNESS,
3107 .path = ""};
3108 RawLightInfo info2 = {.id = 2,
3109 .name = "player2",
3110 .maxBrightness = 255,
3111 .flags = InputLightClass::BRIGHTNESS,
3112 .path = ""};
3113 RawLightInfo info3 = {.id = 3,
3114 .name = "player3",
3115 .maxBrightness = 255,
3116 .flags = InputLightClass::BRIGHTNESS,
3117 .path = ""};
3118 RawLightInfo info4 = {.id = 4,
3119 .name = "player4",
3120 .maxBrightness = 255,
3121 .flags = InputLightClass::BRIGHTNESS,
3122 .path = ""};
3123 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
3124 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
3125 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
3126 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
3127
3128 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3129 InputDeviceInfo info;
3130 mapper.populateDeviceInfo(&info);
3131 const auto& ids = info.getLightIds();
3132 ASSERT_EQ(1UL, ids.size());
3133 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, info.getLightInfo(ids[0])->type);
3134
3135 ASSERT_FALSE(mapper.setLightColor(ids[0], LIGHT_COLOR));
3136 ASSERT_TRUE(mapper.setLightPlayerId(ids[0], LIGHT_PLAYER_ID));
3137 ASSERT_EQ(mapper.getLightPlayerId(ids[0]).value_or(-1), LIGHT_PLAYER_ID);
Kim Low03ea0352020-11-06 12:45:07 -08003138}
3139
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140// --- KeyboardInputMapperTest ---
3141
3142class KeyboardInputMapperTest : public InputMapperTest {
3143protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003144 const std::string UNIQUE_ID = "local:0";
3145
3146 void prepareDisplay(int32_t orientation);
3147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003148 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003149 int32_t originalKeyCode, int32_t rotatedKeyCode,
3150 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151};
3152
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003153/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3154 * orientation.
3155 */
3156void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003157 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3158 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003159}
3160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003161void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003162 int32_t originalScanCode, int32_t originalKeyCode,
3163 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164 NotifyKeyArgs args;
3165
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3168 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3169 ASSERT_EQ(originalScanCode, args.scanCode);
3170 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003171 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3175 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3176 ASSERT_EQ(originalScanCode, args.scanCode);
3177 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003178 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179}
3180
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003182 KeyboardInputMapper& mapper =
3183 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3184 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003186 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187}
3188
3189TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3190 const int32_t USAGE_A = 0x070004;
3191 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3193 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003194 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3195 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3196 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003198 KeyboardInputMapper& mapper =
3199 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3200 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003201 // Initial metastate to AMETA_NONE.
3202 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3203 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204
3205 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 NotifyKeyArgs args;
3208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3209 ASSERT_EQ(DEVICE_ID, args.deviceId);
3210 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3211 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3212 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3213 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3214 ASSERT_EQ(KEY_HOME, args.scanCode);
3215 ASSERT_EQ(AMETA_NONE, args.metaState);
3216 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3217 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3218 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3219
3220 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003221 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3223 ASSERT_EQ(DEVICE_ID, args.deviceId);
3224 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3225 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3227 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3228 ASSERT_EQ(KEY_HOME, args.scanCode);
3229 ASSERT_EQ(AMETA_NONE, args.metaState);
3230 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3231 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3232 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3233
3234 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3238 ASSERT_EQ(DEVICE_ID, args.deviceId);
3239 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3240 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3241 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3242 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3243 ASSERT_EQ(0, args.scanCode);
3244 ASSERT_EQ(AMETA_NONE, args.metaState);
3245 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3246 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3247 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3248
3249 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3251 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3253 ASSERT_EQ(DEVICE_ID, args.deviceId);
3254 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3255 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3256 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3257 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3258 ASSERT_EQ(0, args.scanCode);
3259 ASSERT_EQ(AMETA_NONE, args.metaState);
3260 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3261 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3262 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3263
3264 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3268 ASSERT_EQ(DEVICE_ID, args.deviceId);
3269 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3270 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3271 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3272 ASSERT_EQ(0, args.keyCode);
3273 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3274 ASSERT_EQ(AMETA_NONE, args.metaState);
3275 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3276 ASSERT_EQ(0U, args.policyFlags);
3277 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3278
3279 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3281 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3283 ASSERT_EQ(DEVICE_ID, args.deviceId);
3284 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3285 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3286 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3287 ASSERT_EQ(0, args.keyCode);
3288 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3289 ASSERT_EQ(AMETA_NONE, args.metaState);
3290 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3291 ASSERT_EQ(0U, args.policyFlags);
3292 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3293}
3294
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003295/**
3296 * Ensure that the readTime is set to the time when the EV_KEY is received.
3297 */
3298TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3300
3301 KeyboardInputMapper& mapper =
3302 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3303 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3304 NotifyKeyArgs args;
3305
3306 // Key down
3307 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3309 ASSERT_EQ(12, args.readTime);
3310
3311 // Key up
3312 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3314 ASSERT_EQ(15, args.readTime);
3315}
3316
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003318 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3319 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003320 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3321 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3322 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003323
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003324 KeyboardInputMapper& mapper =
3325 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3326 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327
arthurhungc903df12020-08-11 15:08:42 +08003328 // Initial metastate to AMETA_NONE.
3329 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3330 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331
3332 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 NotifyKeyArgs args;
3335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003337 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003338 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339
3340 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003341 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
3346 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
3352 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003353 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3355 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003357 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358}
3359
3360TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003361 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3362 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3363 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3364 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003366 KeyboardInputMapper& mapper =
3367 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3368 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003370 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3372 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3373 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3374 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3375 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3376 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3377 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3378 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3379}
3380
3381TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003382 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3383 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3384 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3385 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 KeyboardInputMapper& mapper =
3389 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003392 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003393 ASSERT_NO_FATAL_FAILURE(
3394 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3395 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3396 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3397 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3398 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3399 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3400 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003402 clearViewports();
3403 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003404 ASSERT_NO_FATAL_FAILURE(
3405 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3406 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3407 AKEYCODE_DPAD_UP, DISPLAY_ID));
3408 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3409 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3410 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3411 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003413 clearViewports();
3414 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003415 ASSERT_NO_FATAL_FAILURE(
3416 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3417 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3418 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3419 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3420 AKEYCODE_DPAD_UP, DISPLAY_ID));
3421 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3422 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003424 clearViewports();
3425 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003426 ASSERT_NO_FATAL_FAILURE(
3427 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3428 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3429 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3430 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3431 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3432 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3433 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434
3435 // Special case: if orientation changes while key is down, we still emit the same keycode
3436 // in the key up as we did in the key down.
3437 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003438 clearViewports();
3439 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3442 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3443 ASSERT_EQ(KEY_UP, args.scanCode);
3444 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3445
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003446 clearViewports();
3447 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3450 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3451 ASSERT_EQ(KEY_UP, args.scanCode);
3452 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3453}
3454
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003455TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3456 // If the keyboard is not orientation aware,
3457 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003458 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 KeyboardInputMapper& mapper =
3461 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3462 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003463 NotifyKeyArgs args;
3464
3465 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3470 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3471
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003472 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3477 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3478}
3479
3480TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3481 // If the keyboard is orientation aware,
3482 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003483 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003484
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003485 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003486 KeyboardInputMapper& mapper =
3487 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3488 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003489 NotifyKeyArgs args;
3490
3491 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3492 // ^--- already checked by the previous test
3493
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003494 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003495 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3500 ASSERT_EQ(DISPLAY_ID, args.displayId);
3501
3502 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003503 clearViewports();
3504 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003505 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3510 ASSERT_EQ(newDisplayId, args.displayId);
3511}
3512
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003514 KeyboardInputMapper& mapper =
3515 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3516 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003518 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003519 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523}
3524
3525TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 KeyboardInputMapper& mapper =
3527 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3528 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003530 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003531 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003533 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003534 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535}
3536
3537TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003538 KeyboardInputMapper& mapper =
3539 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3540 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
3544 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3545 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547 ASSERT_TRUE(flags[0]);
3548 ASSERT_FALSE(flags[1]);
3549}
3550
3551TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003552 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3553 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3554 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3555 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003559 KeyboardInputMapper& mapper =
3560 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3561 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003562 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003563 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3564 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
3566 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003567 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3568 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3569 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570
3571 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003574 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3575 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3576 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003577 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578
3579 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003582 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3583 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3584 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586
3587 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003590 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3591 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3592 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003593 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594
3595 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003598 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3599 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3600 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003601 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
3603 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003606 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3607 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3608 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003609 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610
3611 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003614 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3615 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3616 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003617 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618}
3619
Chris Yea52ade12020-08-27 16:49:20 -07003620TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3621 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3622 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3623 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3624 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3625
3626 KeyboardInputMapper& mapper =
3627 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3628 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3629
3630 // Initial metastate should be AMETA_NONE as no meta keys added.
3631 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3632 // Meta state should be AMETA_NONE after reset
3633 mapper.reset(ARBITRARY_TIME);
3634 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3635 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3636 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3637 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3638
3639 NotifyKeyArgs args;
3640 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3643 ASSERT_EQ(AMETA_NONE, args.metaState);
3644 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3645 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3646 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3647
3648 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3651 ASSERT_EQ(AMETA_NONE, args.metaState);
3652 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3653 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3654 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3655}
3656
Arthur Hung2c9a3342019-07-23 14:18:59 +08003657TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3658 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003659 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3660 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3661 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3662 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003663
3664 // keyboard 2.
3665 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003666 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003667 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003668 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003669 std::shared_ptr<InputDevice> device2 =
3670 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3671 Flags<InputDeviceClass>(0));
3672
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003673 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3674 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3675 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3676 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003677
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003678 KeyboardInputMapper& mapper =
3679 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3680 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003682 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003683 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003684 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003685 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3686 device2->reset(ARBITRARY_TIME);
3687
3688 // Prepared displays and associated info.
3689 constexpr uint8_t hdmi1 = 0;
3690 constexpr uint8_t hdmi2 = 1;
3691 const std::string SECONDARY_UNIQUE_ID = "local:1";
3692
3693 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3694 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3695
3696 // No associated display viewport found, should disable the device.
3697 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3698 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3699 ASSERT_FALSE(device2->isEnabled());
3700
3701 // Prepare second display.
3702 constexpr int32_t newDisplayId = 2;
3703 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003704 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003705 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003706 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003707 // Default device will reconfigure above, need additional reconfiguration for another device.
3708 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3709 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3710
3711 // Device should be enabled after the associated display is found.
3712 ASSERT_TRUE(mDevice->isEnabled());
3713 ASSERT_TRUE(device2->isEnabled());
3714
3715 // Test pad key events
3716 ASSERT_NO_FATAL_FAILURE(
3717 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3718 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3719 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3720 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3721 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3722 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3723 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3724
3725 ASSERT_NO_FATAL_FAILURE(
3726 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3727 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3728 AKEYCODE_DPAD_RIGHT, newDisplayId));
3729 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3730 AKEYCODE_DPAD_DOWN, newDisplayId));
3731 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3732 AKEYCODE_DPAD_LEFT, newDisplayId));
3733}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734
arthurhungc903df12020-08-11 15:08:42 +08003735TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3736 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3737 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3738 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3739 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3740 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3741 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3742
3743 KeyboardInputMapper& mapper =
3744 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3745 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3746 // Initial metastate to AMETA_NONE.
3747 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3748 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3749
3750 // Initialization should have turned all of the lights off.
3751 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3752 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3753 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3754
3755 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003758 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3759 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3760
3761 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003764 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3765 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3766
3767 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003770 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3771 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3772
3773 mFakeEventHub->removeDevice(EVENTHUB_ID);
3774 mReader->loopOnce();
3775
3776 // keyboard 2 should default toggle keys.
3777 const std::string USB2 = "USB2";
3778 const std::string DEVICE_NAME2 = "KEYBOARD2";
3779 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3780 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3781 std::shared_ptr<InputDevice> device2 =
3782 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3783 Flags<InputDeviceClass>(0));
3784 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3785 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3786 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3787 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3788 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3789 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3790
arthurhung6fe95782020-10-05 22:41:16 +08003791 KeyboardInputMapper& mapper2 =
3792 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3793 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003794 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3795 device2->reset(ARBITRARY_TIME);
3796
3797 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3798 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3799 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003800 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3801 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003802}
3803
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003804// --- KeyboardInputMapperTest_ExternalDevice ---
3805
3806class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3807protected:
Chris Yea52ade12020-08-27 16:49:20 -07003808 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003809};
3810
3811TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3813 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003814
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003815 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3816 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3817 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3818 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003820 KeyboardInputMapper& mapper =
3821 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3822 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003823
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003825 NotifyKeyArgs args;
3826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3827 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003829 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3831 ASSERT_EQ(uint32_t(0), args.policyFlags);
3832
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(uint32_t(0), args.policyFlags);
3836
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3839 ASSERT_EQ(uint32_t(0), args.policyFlags);
3840
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3844
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3847 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3848}
3849
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003850TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003851 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003852
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003853 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3855 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003856
Powei Fengd041c5d2019-05-03 17:11:33 -07003857 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 KeyboardInputMapper& mapper =
3859 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3860 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003861
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003863 NotifyKeyArgs args;
3864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3865 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3866
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003867 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3869 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3870
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3873 ASSERT_EQ(uint32_t(0), args.policyFlags);
3874
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003875 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3877 ASSERT_EQ(uint32_t(0), args.policyFlags);
3878
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3881 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3882
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003883 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3885 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3886}
3887
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888// --- CursorInputMapperTest ---
3889
3890class CursorInputMapperTest : public InputMapperTest {
3891protected:
3892 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3893
Michael Wright17db18e2020-06-26 20:51:44 +01003894 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895
Chris Yea52ade12020-08-27 16:49:20 -07003896 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897 InputMapperTest::SetUp();
3898
Michael Wright17db18e2020-06-26 20:51:44 +01003899 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003900 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901 }
3902
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003903 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3904 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003905
3906 void prepareDisplay(int32_t orientation) {
3907 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003908 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003909 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3910 orientation, uniqueId, NO_PORT, viewportType);
3911 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912};
3913
3914const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003916void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3917 int32_t originalY, int32_t rotatedX,
3918 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919 NotifyMotionArgs args;
3920
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3927 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3928 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3929 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3930}
3931
3932TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003934 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003936 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937}
3938
3939TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003941 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003943 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944}
3945
3946TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003948 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949
3950 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003951 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003952
3953 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003954 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3955 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3957 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3958
3959 // When the bounds are set, then there should be a valid motion range.
3960 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3961
3962 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003963 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964
3965 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3966 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3967 1, 800 - 1, 0.0f, 0.0f));
3968 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3969 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3970 2, 480 - 1, 0.0f, 0.0f));
3971 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3972 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3973 0.0f, 1.0f, 0.0f, 0.0f));
3974}
3975
3976TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003978 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979
3980 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003981 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982
3983 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3984 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3985 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3986 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3987 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3988 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3989 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3990 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3991 0.0f, 1.0f, 0.0f, 0.0f));
3992}
3993
3994TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003996 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
arthurhungdcef2dc2020-08-11 14:47:50 +08003998 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999
4000 NotifyMotionArgs args;
4001
4002 // Button press.
4003 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4007 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4008 ASSERT_EQ(DEVICE_ID, args.deviceId);
4009 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4010 ASSERT_EQ(uint32_t(0), args.policyFlags);
4011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4012 ASSERT_EQ(0, args.flags);
4013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4014 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4015 ASSERT_EQ(0, args.edgeFlags);
4016 ASSERT_EQ(uint32_t(1), args.pointerCount);
4017 ASSERT_EQ(0, args.pointerProperties[0].id);
4018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
4019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4020 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4021 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4022 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4023 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4024
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4026 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4027 ASSERT_EQ(DEVICE_ID, args.deviceId);
4028 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4029 ASSERT_EQ(uint32_t(0), args.policyFlags);
4030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4031 ASSERT_EQ(0, args.flags);
4032 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4033 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4034 ASSERT_EQ(0, args.edgeFlags);
4035 ASSERT_EQ(uint32_t(1), args.pointerCount);
4036 ASSERT_EQ(0, args.pointerProperties[0].id);
4037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
4038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4039 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4040 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4041 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4042 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4043
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004045 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4046 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4048 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4049 ASSERT_EQ(DEVICE_ID, args.deviceId);
4050 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4051 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004052 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4053 ASSERT_EQ(0, args.flags);
4054 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4055 ASSERT_EQ(0, args.buttonState);
4056 ASSERT_EQ(0, args.edgeFlags);
4057 ASSERT_EQ(uint32_t(1), args.pointerCount);
4058 ASSERT_EQ(0, args.pointerProperties[0].id);
4059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
4060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4061 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4062 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4063 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4064 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4065
4066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4067 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4068 ASSERT_EQ(DEVICE_ID, args.deviceId);
4069 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4070 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4072 ASSERT_EQ(0, args.flags);
4073 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4074 ASSERT_EQ(0, args.buttonState);
4075 ASSERT_EQ(0, args.edgeFlags);
4076 ASSERT_EQ(uint32_t(1), args.pointerCount);
4077 ASSERT_EQ(0, args.pointerProperties[0].id);
4078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
4079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4080 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4081 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4082 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4083 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4084}
4085
4086TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004088 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089
4090 NotifyMotionArgs args;
4091
4092 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4098 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4099
4100 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4106 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4107}
4108
4109TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004111 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112
4113 NotifyMotionArgs args;
4114
4115 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4119 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4121 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4122
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4126 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4127
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4134 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4135
4136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4139 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4140}
4141
4142TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004144 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145
4146 NotifyMotionArgs args;
4147
4148 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4154 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4156 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4157 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4158
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4162 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4163 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4164
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4172 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4173 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4174
4175 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4181 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4182
4183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4186 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4187}
4188
4189TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004191 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004193 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4202}
4203
4204TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 addConfigurationProperty("cursor.mode", "navigation");
4206 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004207 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004209 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4218
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004219 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4222 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4223 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4225 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4226 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4227 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4228
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004229 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4231 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4232 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4233 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4234 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4235 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4236 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4237 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4238
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004239 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4241 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4242 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4243 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4244 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4245 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4246 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4247 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4248}
4249
4250TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004252 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253
4254 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4255 mFakePointerController->setPosition(100, 200);
4256 mFakePointerController->setButtonState(0);
4257
4258 NotifyMotionArgs motionArgs;
4259 NotifyKeyArgs keyArgs;
4260
4261 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4266 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4267 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4269 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4270
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4273 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4274 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4276 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4277
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_EQ(0, motionArgs.buttonState);
4283 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4285 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004288 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_EQ(0, motionArgs.buttonState);
4290 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4292 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4293
4294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004296 ASSERT_EQ(0, motionArgs.buttonState);
4297 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4299 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4300
4301 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4306 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4307 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4308 motionArgs.buttonState);
4309 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4310 mFakePointerController->getButtonState());
4311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4312 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4313
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4316 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4317 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4318 mFakePointerController->getButtonState());
4319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4320 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4321
4322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4324 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4325 motionArgs.buttonState);
4326 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4327 mFakePointerController->getButtonState());
4328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4329 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4330
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4336 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4338 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4339
4340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004342 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4343 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4345 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4351 ASSERT_EQ(0, motionArgs.buttonState);
4352 ASSERT_EQ(0, mFakePointerController->getButtonState());
4353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4354 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357
4358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_EQ(0, motionArgs.buttonState);
4360 ASSERT_EQ(0, mFakePointerController->getButtonState());
4361 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4363 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 -08004364
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4366 ASSERT_EQ(0, motionArgs.buttonState);
4367 ASSERT_EQ(0, mFakePointerController->getButtonState());
4368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4370 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4371
4372 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4376 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4377 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4382 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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
4386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4388 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4389 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4391 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4392
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 ASSERT_EQ(0, motionArgs.buttonState);
4398 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004399 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
4402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004404 ASSERT_EQ(0, motionArgs.buttonState);
4405 ASSERT_EQ(0, mFakePointerController->getButtonState());
4406
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4408 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4410 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4411 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4412
4413 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4417 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4418 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4423 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4425 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4426
4427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4429 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4430 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4432 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4433
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 ASSERT_EQ(0, motionArgs.buttonState);
4439 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4441 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 -08004442
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4445 ASSERT_EQ(0, motionArgs.buttonState);
4446 ASSERT_EQ(0, mFakePointerController->getButtonState());
4447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4448 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4449
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4451 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4452 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4453
4454 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4458 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4459 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004460
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4464 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4466 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4467
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4470 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4471 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4473 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4474
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004478 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_EQ(0, motionArgs.buttonState);
4480 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4482 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 -08004483
4484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4486 ASSERT_EQ(0, motionArgs.buttonState);
4487 ASSERT_EQ(0, mFakePointerController->getButtonState());
4488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4489 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4490
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4492 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4493 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4494
4495 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4499 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4500 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004501
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4505 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4507 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4508
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4511 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4512 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4514 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4515
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004519 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 ASSERT_EQ(0, motionArgs.buttonState);
4521 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4523 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 -08004524
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4526 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4527 ASSERT_EQ(0, motionArgs.buttonState);
4528 ASSERT_EQ(0, mFakePointerController->getButtonState());
4529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4530 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4531
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4533 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4534 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4535}
4536
4537TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004539 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540
4541 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4542 mFakePointerController->setPosition(100, 200);
4543 mFakePointerController->setButtonState(0);
4544
4545 NotifyMotionArgs args;
4546
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004551 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4554 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 +01004555 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004556}
4557
4558TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004559 addConfigurationProperty("cursor.mode", "pointer");
4560 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004561 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004562
4563 NotifyDeviceResetArgs resetArgs;
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4565 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4566 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4567
4568 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4569 mFakePointerController->setPosition(100, 200);
4570 mFakePointerController->setButtonState(0);
4571
4572 NotifyMotionArgs args;
4573
4574 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4579 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4582 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 +01004583 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004584
4585 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4592 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4594 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4597 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4598
4599 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004600 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4601 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4603 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4606 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4608 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4609 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4611 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4612
4613 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4618 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4621 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 +01004622 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004623
4624 // Disable pointer capture and check that the device generation got bumped
4625 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004626 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004627 mFakePolicy->setPointerCapture(false);
4628 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004629 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004630
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4632 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4633 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4634
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4639 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4642 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 +01004643 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644}
4645
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004646TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004647 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004648
Garfield Tan888a6a42020-01-09 11:39:16 -08004649 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004650 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004651 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4652 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004653 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4654 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004655 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4656 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4657
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004658 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4659 mFakePointerController->setPosition(100, 200);
4660 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004661
4662 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4667 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4670 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 +01004671 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004672 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4673}
4674
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675// --- TouchInputMapperTest ---
4676
4677class TouchInputMapperTest : public InputMapperTest {
4678protected:
4679 static const int32_t RAW_X_MIN;
4680 static const int32_t RAW_X_MAX;
4681 static const int32_t RAW_Y_MIN;
4682 static const int32_t RAW_Y_MAX;
4683 static const int32_t RAW_TOUCH_MIN;
4684 static const int32_t RAW_TOUCH_MAX;
4685 static const int32_t RAW_TOOL_MIN;
4686 static const int32_t RAW_TOOL_MAX;
4687 static const int32_t RAW_PRESSURE_MIN;
4688 static const int32_t RAW_PRESSURE_MAX;
4689 static const int32_t RAW_ORIENTATION_MIN;
4690 static const int32_t RAW_ORIENTATION_MAX;
4691 static const int32_t RAW_DISTANCE_MIN;
4692 static const int32_t RAW_DISTANCE_MAX;
4693 static const int32_t RAW_TILT_MIN;
4694 static const int32_t RAW_TILT_MAX;
4695 static const int32_t RAW_ID_MIN;
4696 static const int32_t RAW_ID_MAX;
4697 static const int32_t RAW_SLOT_MIN;
4698 static const int32_t RAW_SLOT_MAX;
4699 static const float X_PRECISION;
4700 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004701 static const float X_PRECISION_VIRTUAL;
4702 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703
4704 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004705 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706
4707 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4708
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004709 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004710 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004711
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 enum Axes {
4713 POSITION = 1 << 0,
4714 TOUCH = 1 << 1,
4715 TOOL = 1 << 2,
4716 PRESSURE = 1 << 3,
4717 ORIENTATION = 1 << 4,
4718 MINOR = 1 << 5,
4719 ID = 1 << 6,
4720 DISTANCE = 1 << 7,
4721 TILT = 1 << 8,
4722 SLOT = 1 << 9,
4723 TOOL_TYPE = 1 << 10,
4724 };
4725
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004726 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4727 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004728 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004730 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 int32_t toRawX(float displayX);
4732 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004733 float toCookedX(float rawX, float rawY);
4734 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004736 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004738 float toDisplayY(int32_t rawY, int32_t displayHeight);
4739
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740};
4741
4742const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4743const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4744const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4745const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4746const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4747const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4748const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4749const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004750const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4751const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4753const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4754const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4755const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4756const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4757const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4758const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4759const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4760const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4761const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4762const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4763const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004764const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4765 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4766const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4767 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004768const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4769 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770
4771const float TouchInputMapperTest::GEOMETRIC_SCALE =
4772 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4773 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4774
4775const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4776 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4777 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4778};
4779
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004780void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004781 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4782 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004783}
4784
4785void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4786 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4787 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788}
4789
Santos Cordonfa5cf462017-04-05 10:37:00 -07004790void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004791 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4792 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4793 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004794}
4795
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004797 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4798 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4799 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4800 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801}
4802
Jason Gerecke489fda82012-09-07 17:19:40 -07004803void TouchInputMapperTest::prepareLocationCalibration() {
4804 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4805}
4806
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807int32_t TouchInputMapperTest::toRawX(float displayX) {
4808 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4809}
4810
4811int32_t TouchInputMapperTest::toRawY(float displayY) {
4812 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4813}
4814
Jason Gerecke489fda82012-09-07 17:19:40 -07004815float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4816 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4817 return rawX;
4818}
4819
4820float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4821 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4822 return rawY;
4823}
4824
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004826 return toDisplayX(rawX, DISPLAY_WIDTH);
4827}
4828
4829float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4830 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831}
4832
4833float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004834 return toDisplayY(rawY, DISPLAY_HEIGHT);
4835}
4836
4837float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4838 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839}
4840
4841
4842// --- SingleTouchInputMapperTest ---
4843
4844class SingleTouchInputMapperTest : public TouchInputMapperTest {
4845protected:
4846 void prepareButtons();
4847 void prepareAxes(int axes);
4848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004849 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4850 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4851 void processUp(SingleTouchInputMapper& mappery);
4852 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4853 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4854 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4855 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4856 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4857 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858};
4859
4860void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004861 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862}
4863
4864void SingleTouchInputMapperTest::prepareAxes(int axes) {
4865 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004866 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4867 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 }
4869 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004870 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4871 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 }
4873 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004874 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4875 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 }
4877 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004878 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4879 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 }
4881 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004882 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 }
4885}
4886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004887void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891}
4892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004893void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896}
4897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004898void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900}
4901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904}
4905
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004906void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4907 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909}
4910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004911void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913}
4914
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004915void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4916 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919}
4920
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004921void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4922 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924}
4925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004926void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928}
4929
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 prepareButtons();
4932 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004933 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004935 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936}
4937
4938TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004939 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4940 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941 prepareButtons();
4942 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004943 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004945 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946}
4947
4948TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949 prepareButtons();
4950 prepareAxes(POSITION);
4951 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004952 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004954 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955}
4956
4957TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 prepareButtons();
4959 prepareAxes(POSITION);
4960 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004961 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004963 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964}
4965
4966TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 addConfigurationProperty("touch.deviceType", "touchScreen");
4968 prepareDisplay(DISPLAY_ORIENTATION_0);
4969 prepareButtons();
4970 prepareAxes(POSITION);
4971 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004972 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973
4974 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004975 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976
4977 // Virtual key is down.
4978 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4979 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4980 processDown(mapper, x, y);
4981 processSync(mapper);
4982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004984 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985
4986 // Virtual key is up.
4987 processUp(mapper);
4988 processSync(mapper);
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004991 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992}
4993
4994TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 addConfigurationProperty("touch.deviceType", "touchScreen");
4996 prepareDisplay(DISPLAY_ORIENTATION_0);
4997 prepareButtons();
4998 prepareAxes(POSITION);
4999 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005000 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001
5002 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005003 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004
5005 // Virtual key is down.
5006 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5007 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5008 processDown(mapper, x, y);
5009 processSync(mapper);
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005012 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013
5014 // Virtual key is up.
5015 processUp(mapper);
5016 processSync(mapper);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020}
5021
5022TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 addConfigurationProperty("touch.deviceType", "touchScreen");
5024 prepareDisplay(DISPLAY_ORIENTATION_0);
5025 prepareButtons();
5026 prepareAxes(POSITION);
5027 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005028 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029
5030 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5031 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005032 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033 ASSERT_TRUE(flags[0]);
5034 ASSERT_FALSE(flags[1]);
5035}
5036
5037TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 addConfigurationProperty("touch.deviceType", "touchScreen");
5039 prepareDisplay(DISPLAY_ORIENTATION_0);
5040 prepareButtons();
5041 prepareAxes(POSITION);
5042 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005043 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005044
arthurhungdcef2dc2020-08-11 14:47:50 +08005045 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046
5047 NotifyKeyArgs args;
5048
5049 // Press virtual key.
5050 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5051 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5052 processDown(mapper, x, y);
5053 processSync(mapper);
5054
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5056 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5057 ASSERT_EQ(DEVICE_ID, args.deviceId);
5058 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5059 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5060 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5061 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5062 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5063 ASSERT_EQ(KEY_HOME, args.scanCode);
5064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5065 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5066
5067 // Release virtual key.
5068 processUp(mapper);
5069 processSync(mapper);
5070
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5072 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5073 ASSERT_EQ(DEVICE_ID, args.deviceId);
5074 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5075 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5076 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5077 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5078 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5079 ASSERT_EQ(KEY_HOME, args.scanCode);
5080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5081 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5082
5083 // Should not have sent any motions.
5084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5085}
5086
5087TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088 addConfigurationProperty("touch.deviceType", "touchScreen");
5089 prepareDisplay(DISPLAY_ORIENTATION_0);
5090 prepareButtons();
5091 prepareAxes(POSITION);
5092 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005093 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094
arthurhungdcef2dc2020-08-11 14:47:50 +08005095 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096
5097 NotifyKeyArgs keyArgs;
5098
5099 // Press virtual key.
5100 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5101 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5102 processDown(mapper, x, y);
5103 processSync(mapper);
5104
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5106 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5107 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5108 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5109 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5110 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5111 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5112 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5113 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5115 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5116
5117 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5118 // into the display area.
5119 y -= 100;
5120 processMove(mapper, x, y);
5121 processSync(mapper);
5122
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5124 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5125 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5126 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5127 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5128 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5129 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5130 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5131 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5132 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5133 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5134 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5135
5136 NotifyMotionArgs motionArgs;
5137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5138 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5139 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5140 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5141 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5143 ASSERT_EQ(0, motionArgs.flags);
5144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5145 ASSERT_EQ(0, motionArgs.buttonState);
5146 ASSERT_EQ(0, motionArgs.edgeFlags);
5147 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5148 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5151 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5152 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5153 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5154 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5155
5156 // Keep moving out of bounds. Should generate a pointer move.
5157 y -= 50;
5158 processMove(mapper, x, y);
5159 processSync(mapper);
5160
5161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5162 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5163 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5164 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5165 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5167 ASSERT_EQ(0, motionArgs.flags);
5168 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5169 ASSERT_EQ(0, motionArgs.buttonState);
5170 ASSERT_EQ(0, motionArgs.edgeFlags);
5171 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5172 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5175 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5176 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5177 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5178 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5179
5180 // Release out of bounds. Should generate a pointer up.
5181 processUp(mapper);
5182 processSync(mapper);
5183
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5186 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5187 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5188 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5189 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5190 ASSERT_EQ(0, motionArgs.flags);
5191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5192 ASSERT_EQ(0, motionArgs.buttonState);
5193 ASSERT_EQ(0, motionArgs.edgeFlags);
5194 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5195 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5198 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5199 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5200 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5201 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5202
5203 // Should not have sent any more keys or motions.
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5206}
5207
5208TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209 addConfigurationProperty("touch.deviceType", "touchScreen");
5210 prepareDisplay(DISPLAY_ORIENTATION_0);
5211 prepareButtons();
5212 prepareAxes(POSITION);
5213 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005214 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215
arthurhungdcef2dc2020-08-11 14:47:50 +08005216 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217
5218 NotifyMotionArgs motionArgs;
5219
5220 // Initially go down out of bounds.
5221 int32_t x = -10;
5222 int32_t y = -10;
5223 processDown(mapper, x, y);
5224 processSync(mapper);
5225
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5227
5228 // Move into the display area. Should generate a pointer down.
5229 x = 50;
5230 y = 75;
5231 processMove(mapper, x, y);
5232 processSync(mapper);
5233
5234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5235 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5236 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5237 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5238 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5239 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5240 ASSERT_EQ(0, motionArgs.flags);
5241 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5242 ASSERT_EQ(0, motionArgs.buttonState);
5243 ASSERT_EQ(0, motionArgs.edgeFlags);
5244 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5245 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5248 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5249 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5250 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5252
5253 // Release. Should generate a pointer up.
5254 processUp(mapper);
5255 processSync(mapper);
5256
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5261 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5263 ASSERT_EQ(0, motionArgs.flags);
5264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5265 ASSERT_EQ(0, motionArgs.buttonState);
5266 ASSERT_EQ(0, motionArgs.edgeFlags);
5267 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5271 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5272 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5273 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5275
5276 // Should not have sent any more keys or motions.
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5279}
5280
Santos Cordonfa5cf462017-04-05 10:37:00 -07005281TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005282 addConfigurationProperty("touch.deviceType", "touchScreen");
5283 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5284
5285 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5286 prepareButtons();
5287 prepareAxes(POSITION);
5288 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005289 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005290
arthurhungdcef2dc2020-08-11 14:47:50 +08005291 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005292
5293 NotifyMotionArgs motionArgs;
5294
5295 // Down.
5296 int32_t x = 100;
5297 int32_t y = 125;
5298 processDown(mapper, x, y);
5299 processSync(mapper);
5300
5301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5303 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5304 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5306 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5308 ASSERT_EQ(0, motionArgs.flags);
5309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5310 ASSERT_EQ(0, motionArgs.buttonState);
5311 ASSERT_EQ(0, motionArgs.edgeFlags);
5312 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5313 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5316 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5317 1, 0, 0, 0, 0, 0, 0, 0));
5318 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5319 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5321
5322 // Move.
5323 x += 50;
5324 y += 75;
5325 processMove(mapper, x, y);
5326 processSync(mapper);
5327
5328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5330 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5331 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5332 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5333 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5335 ASSERT_EQ(0, motionArgs.flags);
5336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5337 ASSERT_EQ(0, motionArgs.buttonState);
5338 ASSERT_EQ(0, motionArgs.edgeFlags);
5339 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5343 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5344 1, 0, 0, 0, 0, 0, 0, 0));
5345 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5346 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5348
5349 // Up.
5350 processUp(mapper);
5351 processSync(mapper);
5352
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5355 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5356 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5357 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5358 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5359 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5360 ASSERT_EQ(0, motionArgs.flags);
5361 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5362 ASSERT_EQ(0, motionArgs.buttonState);
5363 ASSERT_EQ(0, motionArgs.edgeFlags);
5364 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5365 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5368 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5369 1, 0, 0, 0, 0, 0, 0, 0));
5370 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5371 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5372 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5373
5374 // Should not have sent any more keys or motions.
5375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5377}
5378
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380 addConfigurationProperty("touch.deviceType", "touchScreen");
5381 prepareDisplay(DISPLAY_ORIENTATION_0);
5382 prepareButtons();
5383 prepareAxes(POSITION);
5384 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005385 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386
arthurhungdcef2dc2020-08-11 14:47:50 +08005387 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388
5389 NotifyMotionArgs motionArgs;
5390
5391 // Down.
5392 int32_t x = 100;
5393 int32_t y = 125;
5394 processDown(mapper, x, y);
5395 processSync(mapper);
5396
5397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5399 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5400 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5401 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5402 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5403 ASSERT_EQ(0, motionArgs.flags);
5404 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5405 ASSERT_EQ(0, motionArgs.buttonState);
5406 ASSERT_EQ(0, motionArgs.edgeFlags);
5407 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5408 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5411 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5412 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5413 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5415
5416 // Move.
5417 x += 50;
5418 y += 75;
5419 processMove(mapper, x, y);
5420 processSync(mapper);
5421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5424 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5425 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5426 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5428 ASSERT_EQ(0, motionArgs.flags);
5429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5430 ASSERT_EQ(0, motionArgs.buttonState);
5431 ASSERT_EQ(0, motionArgs.edgeFlags);
5432 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5433 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5436 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5437 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5438 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5440
5441 // Up.
5442 processUp(mapper);
5443 processSync(mapper);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5447 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5448 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5449 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5450 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5451 ASSERT_EQ(0, motionArgs.flags);
5452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5453 ASSERT_EQ(0, motionArgs.buttonState);
5454 ASSERT_EQ(0, motionArgs.edgeFlags);
5455 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5456 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5459 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5460 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5461 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5462 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5463
5464 // Should not have sent any more keys or motions.
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5467}
5468
5469TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 addConfigurationProperty("touch.deviceType", "touchScreen");
5471 prepareButtons();
5472 prepareAxes(POSITION);
5473 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005474 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475
5476 NotifyMotionArgs args;
5477
5478 // Rotation 90.
5479 prepareDisplay(DISPLAY_ORIENTATION_90);
5480 processDown(mapper, toRawX(50), toRawY(75));
5481 processSync(mapper);
5482
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5484 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5485 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5486
5487 processUp(mapper);
5488 processSync(mapper);
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5490}
5491
5492TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493 addConfigurationProperty("touch.deviceType", "touchScreen");
5494 prepareButtons();
5495 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005496 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497
5498 NotifyMotionArgs args;
5499
5500 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005501 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502 prepareDisplay(DISPLAY_ORIENTATION_0);
5503 processDown(mapper, toRawX(50), toRawY(75));
5504 processSync(mapper);
5505
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5507 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5508 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5509
5510 processUp(mapper);
5511 processSync(mapper);
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5513
5514 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005515 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 prepareDisplay(DISPLAY_ORIENTATION_90);
5517 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5518 processSync(mapper);
5519
5520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5521 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5522 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5523
5524 processUp(mapper);
5525 processSync(mapper);
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5527
5528 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005529 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 prepareDisplay(DISPLAY_ORIENTATION_180);
5531 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5532 processSync(mapper);
5533
5534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5535 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5536 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5537
5538 processUp(mapper);
5539 processSync(mapper);
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5541
5542 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005543 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 prepareDisplay(DISPLAY_ORIENTATION_270);
5545 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5546 processSync(mapper);
5547
5548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5549 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5550 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5551
5552 processUp(mapper);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5555}
5556
5557TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 addConfigurationProperty("touch.deviceType", "touchScreen");
5559 prepareDisplay(DISPLAY_ORIENTATION_0);
5560 prepareButtons();
5561 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005562 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563
5564 // These calculations are based on the input device calibration documentation.
5565 int32_t rawX = 100;
5566 int32_t rawY = 200;
5567 int32_t rawPressure = 10;
5568 int32_t rawToolMajor = 12;
5569 int32_t rawDistance = 2;
5570 int32_t rawTiltX = 30;
5571 int32_t rawTiltY = 110;
5572
5573 float x = toDisplayX(rawX);
5574 float y = toDisplayY(rawY);
5575 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5576 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5577 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5578 float distance = float(rawDistance);
5579
5580 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5581 float tiltScale = M_PI / 180;
5582 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5583 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5584 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5585 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5586
5587 processDown(mapper, rawX, rawY);
5588 processPressure(mapper, rawPressure);
5589 processToolMajor(mapper, rawToolMajor);
5590 processDistance(mapper, rawDistance);
5591 processTilt(mapper, rawTiltX, rawTiltY);
5592 processSync(mapper);
5593
5594 NotifyMotionArgs args;
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5597 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5598 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5599}
5600
Jason Gerecke489fda82012-09-07 17:19:40 -07005601TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005602 addConfigurationProperty("touch.deviceType", "touchScreen");
5603 prepareDisplay(DISPLAY_ORIENTATION_0);
5604 prepareLocationCalibration();
5605 prepareButtons();
5606 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005607 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005608
5609 int32_t rawX = 100;
5610 int32_t rawY = 200;
5611
5612 float x = toDisplayX(toCookedX(rawX, rawY));
5613 float y = toDisplayY(toCookedY(rawX, rawY));
5614
5615 processDown(mapper, rawX, rawY);
5616 processSync(mapper);
5617
5618 NotifyMotionArgs args;
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5621 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5622}
5623
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 addConfigurationProperty("touch.deviceType", "touchScreen");
5626 prepareDisplay(DISPLAY_ORIENTATION_0);
5627 prepareButtons();
5628 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005629 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005630
5631 NotifyMotionArgs motionArgs;
5632 NotifyKeyArgs keyArgs;
5633
5634 processDown(mapper, 100, 200);
5635 processSync(mapper);
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5637 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5638 ASSERT_EQ(0, motionArgs.buttonState);
5639
5640 // press BTN_LEFT, release BTN_LEFT
5641 processKey(mapper, BTN_LEFT, 1);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5645 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5646
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5649 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5650
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 processKey(mapper, BTN_LEFT, 0);
5652 processSync(mapper);
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005659 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660
5661 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5662 processKey(mapper, BTN_RIGHT, 1);
5663 processKey(mapper, BTN_MIDDLE, 1);
5664 processSync(mapper);
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5667 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5668 motionArgs.buttonState);
5669
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5672 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5673
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5676 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5677 motionArgs.buttonState);
5678
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 processKey(mapper, BTN_RIGHT, 0);
5680 processSync(mapper);
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005682 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005687 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688
5689 processKey(mapper, BTN_MIDDLE, 0);
5690 processSync(mapper);
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005697 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698
5699 // press BTN_BACK, release BTN_BACK
5700 processKey(mapper, BTN_BACK, 1);
5701 processSync(mapper);
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5703 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5704 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005705
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713
5714 processKey(mapper, BTN_BACK, 0);
5715 processSync(mapper);
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005719
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005722 ASSERT_EQ(0, motionArgs.buttonState);
5723
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5725 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5726 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5727
5728 // press BTN_SIDE, release BTN_SIDE
5729 processKey(mapper, BTN_SIDE, 1);
5730 processSync(mapper);
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5732 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5733 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005734
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005737 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5738
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5741 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742
5743 processKey(mapper, BTN_SIDE, 0);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005746 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005751 ASSERT_EQ(0, motionArgs.buttonState);
5752
Michael Wrightd02c5b62014-02-10 15:10:22 -08005753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5754 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5755 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5756
5757 // press BTN_FORWARD, release BTN_FORWARD
5758 processKey(mapper, BTN_FORWARD, 1);
5759 processSync(mapper);
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5761 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5762 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005763
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005766 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5767
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5769 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5770 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771
5772 processKey(mapper, BTN_FORWARD, 0);
5773 processSync(mapper);
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005775 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005777
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005780 ASSERT_EQ(0, motionArgs.buttonState);
5781
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5783 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5784 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5785
5786 // press BTN_EXTRA, release BTN_EXTRA
5787 processKey(mapper, BTN_EXTRA, 1);
5788 processSync(mapper);
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5790 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5791 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005792
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5796
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5798 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5799 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800
5801 processKey(mapper, BTN_EXTRA, 0);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005806
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005809 ASSERT_EQ(0, motionArgs.buttonState);
5810
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5812 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5813 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5814
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5816
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817 // press BTN_STYLUS, release BTN_STYLUS
5818 processKey(mapper, BTN_STYLUS, 1);
5819 processSync(mapper);
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005822 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5823
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5825 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5826 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827
5828 processKey(mapper, BTN_STYLUS, 0);
5829 processSync(mapper);
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005831 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005836 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837
5838 // press BTN_STYLUS2, release BTN_STYLUS2
5839 processKey(mapper, BTN_STYLUS2, 1);
5840 processSync(mapper);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5844
5845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5846 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5847 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848
5849 processKey(mapper, BTN_STYLUS2, 0);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005854
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005857 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858
5859 // release touch
5860 processUp(mapper);
5861 processSync(mapper);
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5863 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5864 ASSERT_EQ(0, motionArgs.buttonState);
5865}
5866
5867TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868 addConfigurationProperty("touch.deviceType", "touchScreen");
5869 prepareDisplay(DISPLAY_ORIENTATION_0);
5870 prepareButtons();
5871 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005872 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873
5874 NotifyMotionArgs motionArgs;
5875
5876 // default tool type is finger
5877 processDown(mapper, 100, 200);
5878 processSync(mapper);
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5882
5883 // eraser
5884 processKey(mapper, BTN_TOOL_RUBBER, 1);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5889
5890 // stylus
5891 processKey(mapper, BTN_TOOL_RUBBER, 0);
5892 processKey(mapper, BTN_TOOL_PEN, 1);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5897
5898 // brush
5899 processKey(mapper, BTN_TOOL_PEN, 0);
5900 processKey(mapper, BTN_TOOL_BRUSH, 1);
5901 processSync(mapper);
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5905
5906 // pencil
5907 processKey(mapper, BTN_TOOL_BRUSH, 0);
5908 processKey(mapper, BTN_TOOL_PENCIL, 1);
5909 processSync(mapper);
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5913
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005914 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915 processKey(mapper, BTN_TOOL_PENCIL, 0);
5916 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5917 processSync(mapper);
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5921
5922 // mouse
5923 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5924 processKey(mapper, BTN_TOOL_MOUSE, 1);
5925 processSync(mapper);
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5929
5930 // lens
5931 processKey(mapper, BTN_TOOL_MOUSE, 0);
5932 processKey(mapper, BTN_TOOL_LENS, 1);
5933 processSync(mapper);
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5937
5938 // double-tap
5939 processKey(mapper, BTN_TOOL_LENS, 0);
5940 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5945
5946 // triple-tap
5947 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5948 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5953
5954 // quad-tap
5955 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5956 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5957 processSync(mapper);
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5961
5962 // finger
5963 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5964 processKey(mapper, BTN_TOOL_FINGER, 1);
5965 processSync(mapper);
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5969
5970 // stylus trumps finger
5971 processKey(mapper, BTN_TOOL_PEN, 1);
5972 processSync(mapper);
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5976
5977 // eraser trumps stylus
5978 processKey(mapper, BTN_TOOL_RUBBER, 1);
5979 processSync(mapper);
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5983
5984 // mouse trumps eraser
5985 processKey(mapper, BTN_TOOL_MOUSE, 1);
5986 processSync(mapper);
5987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5990
5991 // back to default tool type
5992 processKey(mapper, BTN_TOOL_MOUSE, 0);
5993 processKey(mapper, BTN_TOOL_RUBBER, 0);
5994 processKey(mapper, BTN_TOOL_PEN, 0);
5995 processKey(mapper, BTN_TOOL_FINGER, 0);
5996 processSync(mapper);
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6000}
6001
6002TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 addConfigurationProperty("touch.deviceType", "touchScreen");
6004 prepareDisplay(DISPLAY_ORIENTATION_0);
6005 prepareButtons();
6006 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006007 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006008 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009
6010 NotifyMotionArgs motionArgs;
6011
6012 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6013 processKey(mapper, BTN_TOOL_FINGER, 1);
6014 processMove(mapper, 100, 200);
6015 processSync(mapper);
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6017 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6019 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6020
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6022 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6024 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6025
6026 // move a little
6027 processMove(mapper, 150, 250);
6028 processSync(mapper);
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6032 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6033
6034 // down when BTN_TOUCH is pressed, pressure defaults to 1
6035 processKey(mapper, BTN_TOUCH, 1);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6040 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6041
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6043 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6045 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6046
6047 // up when BTN_TOUCH is released, hover restored
6048 processKey(mapper, BTN_TOUCH, 0);
6049 processSync(mapper);
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6053 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6054
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6056 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6058 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6059
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6061 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6063 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6064
6065 // exit hover when pointer goes away
6066 processKey(mapper, BTN_TOOL_FINGER, 0);
6067 processSync(mapper);
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6071 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6072}
6073
6074TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 addConfigurationProperty("touch.deviceType", "touchScreen");
6076 prepareDisplay(DISPLAY_ORIENTATION_0);
6077 prepareButtons();
6078 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006079 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080
6081 NotifyMotionArgs motionArgs;
6082
6083 // initially hovering because pressure is 0
6084 processDown(mapper, 100, 200);
6085 processPressure(mapper, 0);
6086 processSync(mapper);
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6088 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6090 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6091
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6093 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6095 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6096
6097 // move a little
6098 processMove(mapper, 150, 250);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6103 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6104
6105 // down when pressure is non-zero
6106 processPressure(mapper, RAW_PRESSURE_MAX);
6107 processSync(mapper);
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6109 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6111 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6112
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6114 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6116 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6117
6118 // up when pressure becomes 0, hover restored
6119 processPressure(mapper, 0);
6120 processSync(mapper);
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6124 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6125
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6127 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6129 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6130
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6134 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6135
6136 // exit hover when pointer goes away
6137 processUp(mapper);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6142 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6143}
6144
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145// --- MultiTouchInputMapperTest ---
6146
6147class MultiTouchInputMapperTest : public TouchInputMapperTest {
6148protected:
6149 void prepareAxes(int axes);
6150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006151 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6152 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6153 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6154 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6155 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6156 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6157 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6158 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6159 void processId(MultiTouchInputMapper& mapper, int32_t id);
6160 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6161 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6162 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6163 void processMTSync(MultiTouchInputMapper& mapper);
6164 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165};
6166
6167void MultiTouchInputMapperTest::prepareAxes(int axes) {
6168 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006169 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6170 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171 }
6172 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006173 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6174 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006176 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6177 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 }
6179 }
6180 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006181 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6182 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006184 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6185 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186 }
6187 }
6188 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006189 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6190 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 }
6192 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006193 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6194 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195 }
6196 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006197 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6198 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199 }
6200 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006201 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6202 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 }
6204 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006205 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6206 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207 }
6208 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006209 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 }
6211}
6212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006213void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6214 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217}
6218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006219void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6220 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222}
6223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006224void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6225 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227}
6228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006229void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231}
6232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006233void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235}
6236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006237void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6238 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240}
6241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006242void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244}
6245
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006246void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248}
6249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006250void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252}
6253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006254void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256}
6257
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006258void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260}
6261
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006262void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6263 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265}
6266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006267void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269}
6270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006271void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273}
6274
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 addConfigurationProperty("touch.deviceType", "touchScreen");
6277 prepareDisplay(DISPLAY_ORIENTATION_0);
6278 prepareAxes(POSITION);
6279 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006280 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281
arthurhungdcef2dc2020-08-11 14:47:50 +08006282 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283
6284 NotifyMotionArgs motionArgs;
6285
6286 // Two fingers down at once.
6287 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6288 processPosition(mapper, x1, y1);
6289 processMTSync(mapper);
6290 processPosition(mapper, x2, y2);
6291 processMTSync(mapper);
6292 processSync(mapper);
6293
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6296 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6297 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6298 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6300 ASSERT_EQ(0, motionArgs.flags);
6301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6302 ASSERT_EQ(0, motionArgs.buttonState);
6303 ASSERT_EQ(0, motionArgs.edgeFlags);
6304 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6305 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6308 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6309 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6310 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6312
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6315 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6316 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6317 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6319 motionArgs.action);
6320 ASSERT_EQ(0, motionArgs.flags);
6321 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6322 ASSERT_EQ(0, motionArgs.buttonState);
6323 ASSERT_EQ(0, motionArgs.edgeFlags);
6324 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6325 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6327 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6330 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6332 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6333 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6334 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6336
6337 // Move.
6338 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6339 processPosition(mapper, x1, y1);
6340 processMTSync(mapper);
6341 processPosition(mapper, x2, y2);
6342 processMTSync(mapper);
6343 processSync(mapper);
6344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6347 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6348 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6349 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6351 ASSERT_EQ(0, motionArgs.flags);
6352 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6353 ASSERT_EQ(0, motionArgs.buttonState);
6354 ASSERT_EQ(0, motionArgs.edgeFlags);
6355 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6356 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6358 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6361 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6363 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6367
6368 // First finger up.
6369 x2 += 15; y2 -= 20;
6370 processPosition(mapper, x2, y2);
6371 processMTSync(mapper);
6372 processSync(mapper);
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_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6380 motionArgs.action);
6381 ASSERT_EQ(0, motionArgs.flags);
6382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6383 ASSERT_EQ(0, motionArgs.buttonState);
6384 ASSERT_EQ(0, motionArgs.edgeFlags);
6385 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6388 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6391 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6393 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6394 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6395 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6397
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6400 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6401 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6402 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6404 ASSERT_EQ(0, motionArgs.flags);
6405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6406 ASSERT_EQ(0, motionArgs.buttonState);
6407 ASSERT_EQ(0, motionArgs.edgeFlags);
6408 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6409 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6412 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6416
6417 // Move.
6418 x2 += 20; y2 -= 25;
6419 processPosition(mapper, x2, y2);
6420 processMTSync(mapper);
6421 processSync(mapper);
6422
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6425 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6426 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6427 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6429 ASSERT_EQ(0, motionArgs.flags);
6430 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6431 ASSERT_EQ(0, motionArgs.buttonState);
6432 ASSERT_EQ(0, motionArgs.edgeFlags);
6433 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6434 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6437 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6438 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6439 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6441
6442 // New finger down.
6443 int32_t x3 = 700, y3 = 300;
6444 processPosition(mapper, x2, y2);
6445 processMTSync(mapper);
6446 processPosition(mapper, x3, y3);
6447 processMTSync(mapper);
6448 processSync(mapper);
6449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6452 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6453 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6454 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6456 motionArgs.action);
6457 ASSERT_EQ(0, motionArgs.flags);
6458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6459 ASSERT_EQ(0, motionArgs.buttonState);
6460 ASSERT_EQ(0, motionArgs.edgeFlags);
6461 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6464 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6467 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6469 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6470 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6471 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6473
6474 // Second finger up.
6475 x3 += 30; y3 -= 20;
6476 processPosition(mapper, x3, y3);
6477 processMTSync(mapper);
6478 processSync(mapper);
6479
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6482 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6483 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6484 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6486 motionArgs.action);
6487 ASSERT_EQ(0, motionArgs.flags);
6488 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6489 ASSERT_EQ(0, motionArgs.buttonState);
6490 ASSERT_EQ(0, motionArgs.edgeFlags);
6491 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6492 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6494 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6497 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6499 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6500 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6501 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6503
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6506 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6507 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6508 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6510 ASSERT_EQ(0, motionArgs.flags);
6511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6512 ASSERT_EQ(0, motionArgs.buttonState);
6513 ASSERT_EQ(0, motionArgs.edgeFlags);
6514 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6518 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6519 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6520 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6522
6523 // Last finger up.
6524 processMTSync(mapper);
6525 processSync(mapper);
6526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6529 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6530 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6531 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6533 ASSERT_EQ(0, motionArgs.flags);
6534 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6535 ASSERT_EQ(0, motionArgs.buttonState);
6536 ASSERT_EQ(0, motionArgs.edgeFlags);
6537 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6538 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6541 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6542 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6543 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6545
6546 // Should not have sent any more keys or motions.
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6549}
6550
6551TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552 addConfigurationProperty("touch.deviceType", "touchScreen");
6553 prepareDisplay(DISPLAY_ORIENTATION_0);
6554 prepareAxes(POSITION | ID);
6555 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006556 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557
arthurhungdcef2dc2020-08-11 14:47:50 +08006558 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559
6560 NotifyMotionArgs motionArgs;
6561
6562 // Two fingers down at once.
6563 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6564 processPosition(mapper, x1, y1);
6565 processId(mapper, 1);
6566 processMTSync(mapper);
6567 processPosition(mapper, x2, y2);
6568 processId(mapper, 2);
6569 processMTSync(mapper);
6570 processSync(mapper);
6571
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6574 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6575 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6578 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6579
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6582 motionArgs.action);
6583 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6586 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6589 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6591 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6592
6593 // Move.
6594 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6595 processPosition(mapper, x1, y1);
6596 processId(mapper, 1);
6597 processMTSync(mapper);
6598 processPosition(mapper, x2, y2);
6599 processId(mapper, 2);
6600 processMTSync(mapper);
6601 processSync(mapper);
6602
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6605 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6608 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6614
6615 // First finger up.
6616 x2 += 15; y2 -= 20;
6617 processPosition(mapper, x2, y2);
6618 processId(mapper, 2);
6619 processMTSync(mapper);
6620 processSync(mapper);
6621
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6623 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6624 motionArgs.action);
6625 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6626 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6628 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6631 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6633 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6634
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6637 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6638 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6641 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6642
6643 // Move.
6644 x2 += 20; y2 -= 25;
6645 processPosition(mapper, x2, y2);
6646 processId(mapper, 2);
6647 processMTSync(mapper);
6648 processSync(mapper);
6649
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6652 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6653 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6656 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6657
6658 // New finger down.
6659 int32_t x3 = 700, y3 = 300;
6660 processPosition(mapper, x2, y2);
6661 processId(mapper, 2);
6662 processMTSync(mapper);
6663 processPosition(mapper, x3, y3);
6664 processId(mapper, 3);
6665 processMTSync(mapper);
6666 processSync(mapper);
6667
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6670 motionArgs.action);
6671 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6672 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6674 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6679 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6680
6681 // Second finger up.
6682 x3 += 30; y3 -= 20;
6683 processPosition(mapper, x3, y3);
6684 processId(mapper, 3);
6685 processMTSync(mapper);
6686 processSync(mapper);
6687
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6690 motionArgs.action);
6691 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6700
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6703 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6704 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6705 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6707 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6708
6709 // Last finger up.
6710 processMTSync(mapper);
6711 processSync(mapper);
6712
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6715 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6716 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6719 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6720
6721 // Should not have sent any more keys or motions.
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6724}
6725
6726TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006727 addConfigurationProperty("touch.deviceType", "touchScreen");
6728 prepareDisplay(DISPLAY_ORIENTATION_0);
6729 prepareAxes(POSITION | ID | SLOT);
6730 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006731 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006732
arthurhungdcef2dc2020-08-11 14:47:50 +08006733 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006734
6735 NotifyMotionArgs motionArgs;
6736
6737 // Two fingers down at once.
6738 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6739 processPosition(mapper, x1, y1);
6740 processId(mapper, 1);
6741 processSlot(mapper, 1);
6742 processPosition(mapper, x2, y2);
6743 processId(mapper, 2);
6744 processSync(mapper);
6745
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6748 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6752 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6753
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6755 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6756 motionArgs.action);
6757 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6758 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6760 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6765 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6766
6767 // Move.
6768 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6769 processSlot(mapper, 0);
6770 processPosition(mapper, x1, y1);
6771 processSlot(mapper, 1);
6772 processPosition(mapper, x2, y2);
6773 processSync(mapper);
6774
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6777 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6778 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6780 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6783 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6785 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6786
6787 // First finger up.
6788 x2 += 15; y2 -= 20;
6789 processSlot(mapper, 0);
6790 processId(mapper, -1);
6791 processSlot(mapper, 1);
6792 processPosition(mapper, x2, y2);
6793 processSync(mapper);
6794
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6796 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6797 motionArgs.action);
6798 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6801 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6804 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6806 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6807
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6810 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6811 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6814 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6815
6816 // Move.
6817 x2 += 20; y2 -= 25;
6818 processPosition(mapper, x2, y2);
6819 processSync(mapper);
6820
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6823 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6824 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6827 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6828
6829 // New finger down.
6830 int32_t x3 = 700, y3 = 300;
6831 processPosition(mapper, x2, y2);
6832 processSlot(mapper, 0);
6833 processId(mapper, 3);
6834 processPosition(mapper, x3, y3);
6835 processSync(mapper);
6836
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6838 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6839 motionArgs.action);
6840 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6841 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6843 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6846 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6848 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6849
6850 // Second finger up.
6851 x3 += 30; y3 -= 20;
6852 processSlot(mapper, 1);
6853 processId(mapper, -1);
6854 processSlot(mapper, 0);
6855 processPosition(mapper, x3, y3);
6856 processSync(mapper);
6857
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6859 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6860 motionArgs.action);
6861 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6862 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6864 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6867 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6869 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6870
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6873 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6874 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6877 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6878
6879 // Last finger up.
6880 processId(mapper, -1);
6881 processSync(mapper);
6882
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6884 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6885 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6886 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6889 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6890
6891 // Should not have sent any more keys or motions.
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6894}
6895
6896TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897 addConfigurationProperty("touch.deviceType", "touchScreen");
6898 prepareDisplay(DISPLAY_ORIENTATION_0);
6899 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006900 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006901
6902 // These calculations are based on the input device calibration documentation.
6903 int32_t rawX = 100;
6904 int32_t rawY = 200;
6905 int32_t rawTouchMajor = 7;
6906 int32_t rawTouchMinor = 6;
6907 int32_t rawToolMajor = 9;
6908 int32_t rawToolMinor = 8;
6909 int32_t rawPressure = 11;
6910 int32_t rawDistance = 0;
6911 int32_t rawOrientation = 3;
6912 int32_t id = 5;
6913
6914 float x = toDisplayX(rawX);
6915 float y = toDisplayY(rawY);
6916 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6917 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6918 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6919 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6920 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6921 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6922 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6923 float distance = float(rawDistance);
6924
6925 processPosition(mapper, rawX, rawY);
6926 processTouchMajor(mapper, rawTouchMajor);
6927 processTouchMinor(mapper, rawTouchMinor);
6928 processToolMajor(mapper, rawToolMajor);
6929 processToolMinor(mapper, rawToolMinor);
6930 processPressure(mapper, rawPressure);
6931 processOrientation(mapper, rawOrientation);
6932 processDistance(mapper, rawDistance);
6933 processId(mapper, id);
6934 processMTSync(mapper);
6935 processSync(mapper);
6936
6937 NotifyMotionArgs args;
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6939 ASSERT_EQ(0, args.pointerProperties[0].id);
6940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6941 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6942 orientation, distance));
6943}
6944
6945TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006946 addConfigurationProperty("touch.deviceType", "touchScreen");
6947 prepareDisplay(DISPLAY_ORIENTATION_0);
6948 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6949 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006950 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006951
6952 // These calculations are based on the input device calibration documentation.
6953 int32_t rawX = 100;
6954 int32_t rawY = 200;
6955 int32_t rawTouchMajor = 140;
6956 int32_t rawTouchMinor = 120;
6957 int32_t rawToolMajor = 180;
6958 int32_t rawToolMinor = 160;
6959
6960 float x = toDisplayX(rawX);
6961 float y = toDisplayY(rawY);
6962 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6963 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6964 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6965 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6966 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6967
6968 processPosition(mapper, rawX, rawY);
6969 processTouchMajor(mapper, rawTouchMajor);
6970 processTouchMinor(mapper, rawTouchMinor);
6971 processToolMajor(mapper, rawToolMajor);
6972 processToolMinor(mapper, rawToolMinor);
6973 processMTSync(mapper);
6974 processSync(mapper);
6975
6976 NotifyMotionArgs args;
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6979 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6980}
6981
6982TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983 addConfigurationProperty("touch.deviceType", "touchScreen");
6984 prepareDisplay(DISPLAY_ORIENTATION_0);
6985 prepareAxes(POSITION | TOUCH | TOOL);
6986 addConfigurationProperty("touch.size.calibration", "diameter");
6987 addConfigurationProperty("touch.size.scale", "10");
6988 addConfigurationProperty("touch.size.bias", "160");
6989 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006990 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006991
6992 // These calculations are based on the input device calibration documentation.
6993 // Note: We only provide a single common touch/tool value because the device is assumed
6994 // not to emit separate values for each pointer (isSummed = 1).
6995 int32_t rawX = 100;
6996 int32_t rawY = 200;
6997 int32_t rawX2 = 150;
6998 int32_t rawY2 = 250;
6999 int32_t rawTouchMajor = 5;
7000 int32_t rawToolMajor = 8;
7001
7002 float x = toDisplayX(rawX);
7003 float y = toDisplayY(rawY);
7004 float x2 = toDisplayX(rawX2);
7005 float y2 = toDisplayY(rawY2);
7006 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7007 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7008 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7009
7010 processPosition(mapper, rawX, rawY);
7011 processTouchMajor(mapper, rawTouchMajor);
7012 processToolMajor(mapper, rawToolMajor);
7013 processMTSync(mapper);
7014 processPosition(mapper, rawX2, rawY2);
7015 processTouchMajor(mapper, rawTouchMajor);
7016 processToolMajor(mapper, rawToolMajor);
7017 processMTSync(mapper);
7018 processSync(mapper);
7019
7020 NotifyMotionArgs args;
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7022 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7023
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7025 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7026 args.action);
7027 ASSERT_EQ(size_t(2), args.pointerCount);
7028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7029 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7031 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7032}
7033
7034TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035 addConfigurationProperty("touch.deviceType", "touchScreen");
7036 prepareDisplay(DISPLAY_ORIENTATION_0);
7037 prepareAxes(POSITION | TOUCH | TOOL);
7038 addConfigurationProperty("touch.size.calibration", "area");
7039 addConfigurationProperty("touch.size.scale", "43");
7040 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042
7043 // These calculations are based on the input device calibration documentation.
7044 int32_t rawX = 100;
7045 int32_t rawY = 200;
7046 int32_t rawTouchMajor = 5;
7047 int32_t rawToolMajor = 8;
7048
7049 float x = toDisplayX(rawX);
7050 float y = toDisplayY(rawY);
7051 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7052 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7053 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7054
7055 processPosition(mapper, rawX, rawY);
7056 processTouchMajor(mapper, rawTouchMajor);
7057 processToolMajor(mapper, rawToolMajor);
7058 processMTSync(mapper);
7059 processSync(mapper);
7060
7061 NotifyMotionArgs args;
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7064 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7065}
7066
7067TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 addConfigurationProperty("touch.deviceType", "touchScreen");
7069 prepareDisplay(DISPLAY_ORIENTATION_0);
7070 prepareAxes(POSITION | PRESSURE);
7071 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7072 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007073 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074
Michael Wrightaa449c92017-12-13 21:21:43 +00007075 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007076 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007077 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7078 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7079 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7080
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 // These calculations are based on the input device calibration documentation.
7082 int32_t rawX = 100;
7083 int32_t rawY = 200;
7084 int32_t rawPressure = 60;
7085
7086 float x = toDisplayX(rawX);
7087 float y = toDisplayY(rawY);
7088 float pressure = float(rawPressure) * 0.01f;
7089
7090 processPosition(mapper, rawX, rawY);
7091 processPressure(mapper, rawPressure);
7092 processMTSync(mapper);
7093 processSync(mapper);
7094
7095 NotifyMotionArgs args;
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7098 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7099}
7100
7101TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 addConfigurationProperty("touch.deviceType", "touchScreen");
7103 prepareDisplay(DISPLAY_ORIENTATION_0);
7104 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007105 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106
7107 NotifyMotionArgs motionArgs;
7108 NotifyKeyArgs keyArgs;
7109
7110 processId(mapper, 1);
7111 processPosition(mapper, 100, 200);
7112 processSync(mapper);
7113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7114 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7115 ASSERT_EQ(0, motionArgs.buttonState);
7116
7117 // press BTN_LEFT, release BTN_LEFT
7118 processKey(mapper, BTN_LEFT, 1);
7119 processSync(mapper);
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7122 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7123
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7126 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7127
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 processKey(mapper, BTN_LEFT, 0);
7129 processSync(mapper);
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007133
7134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007136 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137
7138 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7139 processKey(mapper, BTN_RIGHT, 1);
7140 processKey(mapper, BTN_MIDDLE, 1);
7141 processSync(mapper);
7142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7144 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7145 motionArgs.buttonState);
7146
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7149 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7150
7151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7152 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7153 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7154 motionArgs.buttonState);
7155
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156 processKey(mapper, BTN_RIGHT, 0);
7157 processSync(mapper);
7158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007161
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007164 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165
7166 processKey(mapper, BTN_MIDDLE, 0);
7167 processSync(mapper);
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007169 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007171
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007174 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175
7176 // press BTN_BACK, release BTN_BACK
7177 processKey(mapper, BTN_BACK, 1);
7178 processSync(mapper);
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7180 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7181 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007182
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007185 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7186
7187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7189 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190
7191 processKey(mapper, BTN_BACK, 0);
7192 processSync(mapper);
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007196
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007199 ASSERT_EQ(0, motionArgs.buttonState);
7200
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7202 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7203 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7204
7205 // press BTN_SIDE, release BTN_SIDE
7206 processKey(mapper, BTN_SIDE, 1);
7207 processSync(mapper);
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7209 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7210 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007211
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007214 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7215
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7218 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219
7220 processKey(mapper, BTN_SIDE, 0);
7221 processSync(mapper);
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007224 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007225
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007228 ASSERT_EQ(0, motionArgs.buttonState);
7229
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7231 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7232 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7233
7234 // press BTN_FORWARD, release BTN_FORWARD
7235 processKey(mapper, BTN_FORWARD, 1);
7236 processSync(mapper);
7237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7238 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7239 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007240
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007243 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7244
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7247 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007248
7249 processKey(mapper, BTN_FORWARD, 0);
7250 processSync(mapper);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007253 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007254
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007257 ASSERT_EQ(0, motionArgs.buttonState);
7258
Michael Wrightd02c5b62014-02-10 15:10:22 -08007259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7260 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7261 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7262
7263 // press BTN_EXTRA, release BTN_EXTRA
7264 processKey(mapper, BTN_EXTRA, 1);
7265 processSync(mapper);
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7267 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7268 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007269
Michael Wrightd02c5b62014-02-10 15:10:22 -08007270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007272 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7273
7274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7276 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007277
7278 processKey(mapper, BTN_EXTRA, 0);
7279 processSync(mapper);
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007282 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007283
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007286 ASSERT_EQ(0, motionArgs.buttonState);
7287
Michael Wrightd02c5b62014-02-10 15:10:22 -08007288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7289 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7290 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7291
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7293
Michael Wrightd02c5b62014-02-10 15:10:22 -08007294 // press BTN_STYLUS, release BTN_STYLUS
7295 processKey(mapper, BTN_STYLUS, 1);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007299 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7300
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7303 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304
7305 processKey(mapper, BTN_STYLUS, 0);
7306 processSync(mapper);
7307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007308 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007309 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007310
7311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007313 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314
7315 // press BTN_STYLUS2, release BTN_STYLUS2
7316 processKey(mapper, BTN_STYLUS2, 1);
7317 processSync(mapper);
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007320 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7321
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7324 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007325
7326 processKey(mapper, BTN_STYLUS2, 0);
7327 processSync(mapper);
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007330 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007331
7332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007334 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335
7336 // release touch
7337 processId(mapper, -1);
7338 processSync(mapper);
7339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7341 ASSERT_EQ(0, motionArgs.buttonState);
7342}
7343
7344TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007345 addConfigurationProperty("touch.deviceType", "touchScreen");
7346 prepareDisplay(DISPLAY_ORIENTATION_0);
7347 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007348 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349
7350 NotifyMotionArgs motionArgs;
7351
7352 // default tool type is finger
7353 processId(mapper, 1);
7354 processPosition(mapper, 100, 200);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7359
7360 // eraser
7361 processKey(mapper, BTN_TOOL_RUBBER, 1);
7362 processSync(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7366
7367 // stylus
7368 processKey(mapper, BTN_TOOL_RUBBER, 0);
7369 processKey(mapper, BTN_TOOL_PEN, 1);
7370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7374
7375 // brush
7376 processKey(mapper, BTN_TOOL_PEN, 0);
7377 processKey(mapper, BTN_TOOL_BRUSH, 1);
7378 processSync(mapper);
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7382
7383 // pencil
7384 processKey(mapper, BTN_TOOL_BRUSH, 0);
7385 processKey(mapper, BTN_TOOL_PENCIL, 1);
7386 processSync(mapper);
7387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7390
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007391 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007392 processKey(mapper, BTN_TOOL_PENCIL, 0);
7393 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7394 processSync(mapper);
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7398
7399 // mouse
7400 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7401 processKey(mapper, BTN_TOOL_MOUSE, 1);
7402 processSync(mapper);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7406
7407 // lens
7408 processKey(mapper, BTN_TOOL_MOUSE, 0);
7409 processKey(mapper, BTN_TOOL_LENS, 1);
7410 processSync(mapper);
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7414
7415 // double-tap
7416 processKey(mapper, BTN_TOOL_LENS, 0);
7417 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7418 processSync(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7422
7423 // triple-tap
7424 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7425 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7430
7431 // quad-tap
7432 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7433 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7434 processSync(mapper);
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7438
7439 // finger
7440 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7441 processKey(mapper, BTN_TOOL_FINGER, 1);
7442 processSync(mapper);
7443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7446
7447 // stylus trumps finger
7448 processKey(mapper, BTN_TOOL_PEN, 1);
7449 processSync(mapper);
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7453
7454 // eraser trumps stylus
7455 processKey(mapper, BTN_TOOL_RUBBER, 1);
7456 processSync(mapper);
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7460
7461 // mouse trumps eraser
7462 processKey(mapper, BTN_TOOL_MOUSE, 1);
7463 processSync(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7467
7468 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7469 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7470 processSync(mapper);
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7474
7475 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7476 processToolType(mapper, MT_TOOL_PEN);
7477 processSync(mapper);
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7481
7482 // back to default tool type
7483 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7484 processKey(mapper, BTN_TOOL_MOUSE, 0);
7485 processKey(mapper, BTN_TOOL_RUBBER, 0);
7486 processKey(mapper, BTN_TOOL_PEN, 0);
7487 processKey(mapper, BTN_TOOL_FINGER, 0);
7488 processSync(mapper);
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7492}
7493
7494TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007495 addConfigurationProperty("touch.deviceType", "touchScreen");
7496 prepareDisplay(DISPLAY_ORIENTATION_0);
7497 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007498 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007499 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007500
7501 NotifyMotionArgs motionArgs;
7502
7503 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7504 processId(mapper, 1);
7505 processPosition(mapper, 100, 200);
7506 processSync(mapper);
7507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7508 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7510 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7511
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7515 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7516
7517 // move a little
7518 processPosition(mapper, 150, 250);
7519 processSync(mapper);
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7523 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7524
7525 // down when BTN_TOUCH is pressed, pressure defaults to 1
7526 processKey(mapper, BTN_TOUCH, 1);
7527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7531 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7532
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7534 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7536 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7537
7538 // up when BTN_TOUCH is released, hover restored
7539 processKey(mapper, BTN_TOUCH, 0);
7540 processSync(mapper);
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7542 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7544 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7545
7546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7547 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7549 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7550
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7554 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7555
7556 // exit hover when pointer goes away
7557 processId(mapper, -1);
7558 processSync(mapper);
7559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7562 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7563}
7564
7565TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 addConfigurationProperty("touch.deviceType", "touchScreen");
7567 prepareDisplay(DISPLAY_ORIENTATION_0);
7568 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007569 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570
7571 NotifyMotionArgs motionArgs;
7572
7573 // initially hovering because pressure is 0
7574 processId(mapper, 1);
7575 processPosition(mapper, 100, 200);
7576 processPressure(mapper, 0);
7577 processSync(mapper);
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7581 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7582
7583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7586 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7587
7588 // move a little
7589 processPosition(mapper, 150, 250);
7590 processSync(mapper);
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7594 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7595
7596 // down when pressure becomes non-zero
7597 processPressure(mapper, RAW_PRESSURE_MAX);
7598 processSync(mapper);
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7602 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7603
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7607 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7608
7609 // up when pressure becomes 0, hover restored
7610 processPressure(mapper, 0);
7611 processSync(mapper);
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7613 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7615 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7616
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7620 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7621
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7625 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7626
7627 // exit hover when pointer goes away
7628 processId(mapper, -1);
7629 processSync(mapper);
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7633 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7634}
7635
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007636/**
7637 * Set the input device port <--> display port associations, and check that the
7638 * events are routed to the display that matches the display port.
7639 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7640 */
7641TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007642 const std::string usb2 = "USB2";
7643 const uint8_t hdmi1 = 0;
7644 const uint8_t hdmi2 = 1;
7645 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007646 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007647
7648 addConfigurationProperty("touch.deviceType", "touchScreen");
7649 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007650 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007651
7652 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7653 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7654
7655 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7656 // for this input device is specified, and the matching viewport is not present,
7657 // the input device should be disabled (at the mapper level).
7658
7659 // Add viewport for display 2 on hdmi2
7660 prepareSecondaryDisplay(type, hdmi2);
7661 // Send a touch event
7662 processPosition(mapper, 100, 100);
7663 processSync(mapper);
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7665
7666 // Add viewport for display 1 on hdmi1
7667 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7668 // Send a touch event again
7669 processPosition(mapper, 100, 100);
7670 processSync(mapper);
7671
7672 NotifyMotionArgs args;
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7674 ASSERT_EQ(DISPLAY_ID, args.displayId);
7675}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007677TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007678 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007679 std::shared_ptr<FakePointerController> fakePointerController =
7680 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007681 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007682 fakePointerController->setPosition(100, 200);
7683 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007684 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7685
Garfield Tan888a6a42020-01-09 11:39:16 -08007686 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007687 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007688
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007689 prepareDisplay(DISPLAY_ORIENTATION_0);
7690 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007692
7693 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007694 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007695
7696 NotifyMotionArgs motionArgs;
7697 processPosition(mapper, 100, 100);
7698 processSync(mapper);
7699
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7702 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7703}
7704
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007705/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007706 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7707 */
7708TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7709 addConfigurationProperty("touch.deviceType", "touchScreen");
7710 prepareAxes(POSITION);
7711 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7712
7713 prepareDisplay(DISPLAY_ORIENTATION_0);
7714 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7715 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7716 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7717 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7718
7719 NotifyMotionArgs args;
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7721 ASSERT_EQ(26, args.readTime);
7722
7723 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7724 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7725 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7726
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7728 ASSERT_EQ(33, args.readTime);
7729}
7730
7731/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007732 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7733 * events should not be delivered to the listener.
7734 */
7735TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7736 addConfigurationProperty("touch.deviceType", "touchScreen");
7737 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7738 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7739 ViewportType::INTERNAL);
7740 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7741 prepareAxes(POSITION);
7742 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7743
7744 NotifyMotionArgs motionArgs;
7745 processPosition(mapper, 100, 100);
7746 processSync(mapper);
7747
7748 mFakeListener->assertNotifyMotionWasNotCalled();
7749}
7750
Garfield Tanc734e4f2021-01-15 20:01:39 -08007751TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7752 addConfigurationProperty("touch.deviceType", "touchScreen");
7753 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7754 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7755 ViewportType::INTERNAL);
7756 std::optional<DisplayViewport> optionalDisplayViewport =
7757 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7758 ASSERT_TRUE(optionalDisplayViewport.has_value());
7759 DisplayViewport displayViewport = *optionalDisplayViewport;
7760
7761 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7762 prepareAxes(POSITION);
7763 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7764
7765 // Finger down
7766 int32_t x = 100, y = 100;
7767 processPosition(mapper, x, y);
7768 processSync(mapper);
7769
7770 NotifyMotionArgs motionArgs;
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7772 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7773
7774 // Deactivate display viewport
7775 displayViewport.isActive = false;
7776 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7777 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7778
7779 // Finger move
7780 x += 10, y += 10;
7781 processPosition(mapper, x, y);
7782 processSync(mapper);
7783
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7785 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7786
7787 // Reactivate display viewport
7788 displayViewport.isActive = true;
7789 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7790 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7791
7792 // Finger move again
7793 x += 10, y += 10;
7794 processPosition(mapper, x, y);
7795 processSync(mapper);
7796
7797 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7798 // no pointer on the touch device.
7799 mFakeListener->assertNotifyMotionWasNotCalled();
7800}
7801
Arthur Hung7c645402019-01-25 17:45:42 +08007802TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7803 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007804 prepareAxes(POSITION | ID | SLOT);
7805 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007806 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007807
7808 // Create the second touch screen device, and enable multi fingers.
7809 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007810 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007811 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007812 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007813 std::shared_ptr<InputDevice> device2 =
7814 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7815 Flags<InputDeviceClass>(0));
7816
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007817 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7818 0 /*flat*/, 0 /*fuzz*/);
7819 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7820 0 /*flat*/, 0 /*fuzz*/);
7821 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7822 0 /*flat*/, 0 /*fuzz*/);
7823 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7824 0 /*flat*/, 0 /*fuzz*/);
7825 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7826 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7827 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007828
7829 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007830 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007831 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7832 device2->reset(ARBITRARY_TIME);
7833
7834 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007835 std::shared_ptr<FakePointerController> fakePointerController =
7836 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007837 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7838 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7839
7840 // Setup policy for associated displays and show touches.
7841 const uint8_t hdmi1 = 0;
7842 const uint8_t hdmi2 = 1;
7843 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7844 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7845 mFakePolicy->setShowTouches(true);
7846
7847 // Create displays.
7848 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007849 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007850
7851 // Default device will reconfigure above, need additional reconfiguration for another device.
7852 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007853 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007854
7855 // Two fingers down at default display.
7856 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7857 processPosition(mapper, x1, y1);
7858 processId(mapper, 1);
7859 processSlot(mapper, 1);
7860 processPosition(mapper, x2, y2);
7861 processId(mapper, 2);
7862 processSync(mapper);
7863
7864 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7865 fakePointerController->getSpots().find(DISPLAY_ID);
7866 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7867 ASSERT_EQ(size_t(2), iter->second.size());
7868
7869 // Two fingers down at second display.
7870 processPosition(mapper2, x1, y1);
7871 processId(mapper2, 1);
7872 processSlot(mapper2, 1);
7873 processPosition(mapper2, x2, y2);
7874 processId(mapper2, 2);
7875 processSync(mapper2);
7876
7877 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7878 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7879 ASSERT_EQ(size_t(2), iter->second.size());
7880}
7881
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007882TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007883 prepareAxes(POSITION);
7884 addConfigurationProperty("touch.deviceType", "touchScreen");
7885 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007887
7888 NotifyMotionArgs motionArgs;
7889 // Unrotated video frame
7890 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7891 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007892 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007893 processPosition(mapper, 100, 200);
7894 processSync(mapper);
7895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7896 ASSERT_EQ(frames, motionArgs.videoFrames);
7897
7898 // Subsequent touch events should not have any videoframes
7899 // This is implemented separately in FakeEventHub,
7900 // but that should match the behaviour of TouchVideoDevice.
7901 processPosition(mapper, 200, 200);
7902 processSync(mapper);
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7904 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7905}
7906
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007907TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007908 prepareAxes(POSITION);
7909 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007911 // Unrotated video frame
7912 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7913 NotifyMotionArgs motionArgs;
7914
7915 // Test all 4 orientations
7916 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7917 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7918 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7919 clearViewports();
7920 prepareDisplay(orientation);
7921 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007922 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007923 processPosition(mapper, 100, 200);
7924 processSync(mapper);
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7926 frames[0].rotate(orientation);
7927 ASSERT_EQ(frames, motionArgs.videoFrames);
7928 }
7929}
7930
7931TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007932 prepareAxes(POSITION);
7933 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007934 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007935 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7936 // so mix these.
7937 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7938 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7939 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7940 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7941 NotifyMotionArgs motionArgs;
7942
7943 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007944 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007945 processPosition(mapper, 100, 200);
7946 processSync(mapper);
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7948 std::for_each(frames.begin(), frames.end(),
7949 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7950 ASSERT_EQ(frames, motionArgs.videoFrames);
7951}
7952
Arthur Hung9da14732019-09-02 16:16:58 +08007953/**
7954 * If we had defined port associations, but the viewport is not ready, the touch device would be
7955 * expected to be disabled, and it should be enabled after the viewport has found.
7956 */
7957TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007958 constexpr uint8_t hdmi2 = 1;
7959 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007960 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007961
7962 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7963
7964 addConfigurationProperty("touch.deviceType", "touchScreen");
7965 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007966 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007967
7968 ASSERT_EQ(mDevice->isEnabled(), false);
7969
7970 // Add display on hdmi2, the device should be enabled and can receive touch event.
7971 prepareSecondaryDisplay(type, hdmi2);
7972 ASSERT_EQ(mDevice->isEnabled(), true);
7973
7974 // Send a touch event.
7975 processPosition(mapper, 100, 100);
7976 processSync(mapper);
7977
7978 NotifyMotionArgs args;
7979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7980 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7981}
7982
Arthur Hung6cd19a42019-08-30 19:04:12 +08007983
Arthur Hung6cd19a42019-08-30 19:04:12 +08007984
Arthur Hung421eb1c2020-01-16 00:09:42 +08007985TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007986 addConfigurationProperty("touch.deviceType", "touchScreen");
7987 prepareDisplay(DISPLAY_ORIENTATION_0);
7988 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007989 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007990
7991 NotifyMotionArgs motionArgs;
7992
7993 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7994 // finger down
7995 processId(mapper, 1);
7996 processPosition(mapper, x1, y1);
7997 processSync(mapper);
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8001
8002 // finger move
8003 processId(mapper, 1);
8004 processPosition(mapper, x2, y2);
8005 processSync(mapper);
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8009
8010 // finger up.
8011 processId(mapper, -1);
8012 processSync(mapper);
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8016
8017 // new finger down
8018 processId(mapper, 1);
8019 processPosition(mapper, x3, y3);
8020 processSync(mapper);
8021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8022 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8024}
8025
8026/**
arthurhungcc7f9802020-04-30 17:55:40 +08008027 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8028 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008029 */
arthurhungcc7f9802020-04-30 17:55:40 +08008030TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008031 addConfigurationProperty("touch.deviceType", "touchScreen");
8032 prepareDisplay(DISPLAY_ORIENTATION_0);
8033 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008034 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008035
8036 NotifyMotionArgs motionArgs;
8037
8038 // default tool type is finger
8039 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008040 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008041 processPosition(mapper, x1, y1);
8042 processSync(mapper);
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8046
8047 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8048 processToolType(mapper, MT_TOOL_PALM);
8049 processSync(mapper);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8051 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8052
8053 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008054 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008055 processPosition(mapper, x2, y2);
8056 processSync(mapper);
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8058
8059 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008060 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008061 processSync(mapper);
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8063
8064 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008065 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008066 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008067 processPosition(mapper, x3, y3);
8068 processSync(mapper);
8069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8070 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8072}
8073
arthurhungbf89a482020-04-17 17:37:55 +08008074/**
arthurhungcc7f9802020-04-30 17:55:40 +08008075 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8076 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008077 */
arthurhungcc7f9802020-04-30 17:55:40 +08008078TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008079 addConfigurationProperty("touch.deviceType", "touchScreen");
8080 prepareDisplay(DISPLAY_ORIENTATION_0);
8081 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8083
8084 NotifyMotionArgs motionArgs;
8085
8086 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008087 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8088 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008089 processPosition(mapper, x1, y1);
8090 processSync(mapper);
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8094
8095 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008096 processSlot(mapper, SECOND_SLOT);
8097 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008098 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008099 processSync(mapper);
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8101 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8102 motionArgs.action);
8103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8104
8105 // If the tool type of the first finger changes to MT_TOOL_PALM,
8106 // we expect to receive ACTION_POINTER_UP with cancel flag.
8107 processSlot(mapper, FIRST_SLOT);
8108 processId(mapper, FIRST_TRACKING_ID);
8109 processToolType(mapper, MT_TOOL_PALM);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8113 motionArgs.action);
8114 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8115
8116 // The following MOVE events of second finger should be processed.
8117 processSlot(mapper, SECOND_SLOT);
8118 processId(mapper, SECOND_TRACKING_ID);
8119 processPosition(mapper, x2 + 1, y2 + 1);
8120 processSync(mapper);
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8123 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8124
8125 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8126 // it. Second finger receive move.
8127 processSlot(mapper, FIRST_SLOT);
8128 processId(mapper, INVALID_TRACKING_ID);
8129 processSync(mapper);
8130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8132 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8133
8134 // Second finger keeps moving.
8135 processSlot(mapper, SECOND_SLOT);
8136 processId(mapper, SECOND_TRACKING_ID);
8137 processPosition(mapper, x2 + 2, y2 + 2);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8141 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8142
8143 // Second finger up.
8144 processId(mapper, INVALID_TRACKING_ID);
8145 processSync(mapper);
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8147 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8148 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8149}
8150
8151/**
8152 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8153 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8154 */
8155TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8156 addConfigurationProperty("touch.deviceType", "touchScreen");
8157 prepareDisplay(DISPLAY_ORIENTATION_0);
8158 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8159 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8160
8161 NotifyMotionArgs motionArgs;
8162
8163 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8164 // First finger down.
8165 processId(mapper, FIRST_TRACKING_ID);
8166 processPosition(mapper, x1, y1);
8167 processSync(mapper);
8168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8171
8172 // Second finger down.
8173 processSlot(mapper, SECOND_SLOT);
8174 processId(mapper, SECOND_TRACKING_ID);
8175 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008176 processSync(mapper);
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8178 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8179 motionArgs.action);
8180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8181
arthurhungcc7f9802020-04-30 17:55:40 +08008182 // If the tool type of the first finger changes to MT_TOOL_PALM,
8183 // we expect to receive ACTION_POINTER_UP with cancel flag.
8184 processSlot(mapper, FIRST_SLOT);
8185 processId(mapper, FIRST_TRACKING_ID);
8186 processToolType(mapper, MT_TOOL_PALM);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8189 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8190 motionArgs.action);
8191 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8192
8193 // Second finger keeps moving.
8194 processSlot(mapper, SECOND_SLOT);
8195 processId(mapper, SECOND_TRACKING_ID);
8196 processPosition(mapper, x2 + 1, y2 + 1);
8197 processSync(mapper);
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8200
8201 // second finger becomes palm, receive cancel due to only 1 finger is active.
8202 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008203 processToolType(mapper, MT_TOOL_PALM);
8204 processSync(mapper);
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8206 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8207
arthurhungcc7f9802020-04-30 17:55:40 +08008208 // third finger down.
8209 processSlot(mapper, THIRD_SLOT);
8210 processId(mapper, THIRD_TRACKING_ID);
8211 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008212 processPosition(mapper, x3, y3);
8213 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8215 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008217 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8218
8219 // third finger move
8220 processId(mapper, THIRD_TRACKING_ID);
8221 processPosition(mapper, x3 + 1, y3 + 1);
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8225
8226 // first finger up, third finger receive move.
8227 processSlot(mapper, FIRST_SLOT);
8228 processId(mapper, INVALID_TRACKING_ID);
8229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8232 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8233
8234 // second finger up, third finger receive move.
8235 processSlot(mapper, SECOND_SLOT);
8236 processId(mapper, INVALID_TRACKING_ID);
8237 processSync(mapper);
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8240 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8241
8242 // third finger up.
8243 processSlot(mapper, THIRD_SLOT);
8244 processId(mapper, INVALID_TRACKING_ID);
8245 processSync(mapper);
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8247 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8248 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8249}
8250
8251/**
8252 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8253 * and the active finger could still be allowed to receive the events
8254 */
8255TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8256 addConfigurationProperty("touch.deviceType", "touchScreen");
8257 prepareDisplay(DISPLAY_ORIENTATION_0);
8258 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8259 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8260
8261 NotifyMotionArgs motionArgs;
8262
8263 // default tool type is finger
8264 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8265 processId(mapper, FIRST_TRACKING_ID);
8266 processPosition(mapper, x1, y1);
8267 processSync(mapper);
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8271
8272 // Second finger down.
8273 processSlot(mapper, SECOND_SLOT);
8274 processId(mapper, SECOND_TRACKING_ID);
8275 processPosition(mapper, x2, y2);
8276 processSync(mapper);
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8278 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8279 motionArgs.action);
8280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8281
8282 // If the tool type of the second finger changes to MT_TOOL_PALM,
8283 // we expect to receive ACTION_POINTER_UP with cancel flag.
8284 processId(mapper, SECOND_TRACKING_ID);
8285 processToolType(mapper, MT_TOOL_PALM);
8286 processSync(mapper);
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8289 motionArgs.action);
8290 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8291
8292 // The following MOVE event should be processed.
8293 processSlot(mapper, FIRST_SLOT);
8294 processId(mapper, FIRST_TRACKING_ID);
8295 processPosition(mapper, x1 + 1, y1 + 1);
8296 processSync(mapper);
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8299 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8300
8301 // second finger up.
8302 processSlot(mapper, SECOND_SLOT);
8303 processId(mapper, INVALID_TRACKING_ID);
8304 processSync(mapper);
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8307
8308 // first finger keep moving
8309 processSlot(mapper, FIRST_SLOT);
8310 processId(mapper, FIRST_TRACKING_ID);
8311 processPosition(mapper, x1 + 2, y1 + 2);
8312 processSync(mapper);
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8315
8316 // first finger up.
8317 processId(mapper, INVALID_TRACKING_ID);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8321 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008322}
8323
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008324// --- MultiTouchInputMapperTest_ExternalDevice ---
8325
8326class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8327protected:
Chris Yea52ade12020-08-27 16:49:20 -07008328 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008329};
8330
8331/**
8332 * Expect fallback to internal viewport if device is external and external viewport is not present.
8333 */
8334TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8335 prepareAxes(POSITION);
8336 addConfigurationProperty("touch.deviceType", "touchScreen");
8337 prepareDisplay(DISPLAY_ORIENTATION_0);
8338 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8339
8340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8341
8342 NotifyMotionArgs motionArgs;
8343
8344 // Expect the event to be sent to the internal viewport,
8345 // because an external viewport is not present.
8346 processPosition(mapper, 100, 100);
8347 processSync(mapper);
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8349 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8350
8351 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008352 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008353 processPosition(mapper, 100, 100);
8354 processSync(mapper);
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8356 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8357}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008358
8359/**
8360 * Test touch should not work if outside of surface.
8361 */
8362class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8363protected:
8364 void halfDisplayToCenterHorizontal(int32_t orientation) {
8365 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008366 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008367
8368 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8369 internalViewport->orientation = orientation;
8370 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8371 internalViewport->logicalLeft = 0;
8372 internalViewport->logicalTop = 0;
8373 internalViewport->logicalRight = DISPLAY_HEIGHT;
8374 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8375
8376 internalViewport->physicalLeft = 0;
8377 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8378 internalViewport->physicalRight = DISPLAY_HEIGHT;
8379 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8380
8381 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8382 internalViewport->deviceHeight = DISPLAY_WIDTH;
8383 } else {
8384 internalViewport->logicalLeft = 0;
8385 internalViewport->logicalTop = 0;
8386 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8387 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8388
8389 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8390 internalViewport->physicalTop = 0;
8391 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8392 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8393
8394 internalViewport->deviceWidth = DISPLAY_WIDTH;
8395 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8396 }
8397
8398 mFakePolicy->updateViewport(internalViewport.value());
8399 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8400 }
8401
arthurhung5d547942020-12-14 17:04:45 +08008402 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8403 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008404 int32_t yExpected) {
8405 // touch on outside area should not work.
8406 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8407 processSync(mapper);
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8409
8410 // touch on inside area should receive the event.
8411 NotifyMotionArgs args;
8412 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8413 processSync(mapper);
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8415 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8416 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8417
8418 // Reset.
8419 mapper.reset(ARBITRARY_TIME);
8420 }
8421};
8422
arthurhung5d547942020-12-14 17:04:45 +08008423TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008424 addConfigurationProperty("touch.deviceType", "touchScreen");
8425 prepareDisplay(DISPLAY_ORIENTATION_0);
8426 prepareAxes(POSITION);
8427 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8428
8429 // Touch on center of normal display should work.
8430 const int32_t x = DISPLAY_WIDTH / 4;
8431 const int32_t y = DISPLAY_HEIGHT / 2;
8432 processPosition(mapper, toRawX(x), toRawY(y));
8433 processSync(mapper);
8434 NotifyMotionArgs args;
8435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8437 0.0f, 0.0f, 0.0f, 0.0f));
8438 // Reset.
8439 mapper.reset(ARBITRARY_TIME);
8440
8441 // Let physical display be different to device, and make surface and physical could be 1:1.
8442 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8443
8444 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8445 const int32_t yExpected = y;
8446 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8447}
8448
arthurhung5d547942020-12-14 17:04:45 +08008449TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008450 addConfigurationProperty("touch.deviceType", "touchScreen");
8451 prepareDisplay(DISPLAY_ORIENTATION_0);
8452 prepareAxes(POSITION);
8453 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8454
8455 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8456 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8457
8458 const int32_t x = DISPLAY_WIDTH / 4;
8459 const int32_t y = DISPLAY_HEIGHT / 2;
8460
8461 // expect x/y = swap x/y then reverse y.
8462 const int32_t xExpected = y;
8463 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8464 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8465}
8466
arthurhung5d547942020-12-14 17:04:45 +08008467TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008468 addConfigurationProperty("touch.deviceType", "touchScreen");
8469 prepareDisplay(DISPLAY_ORIENTATION_0);
8470 prepareAxes(POSITION);
8471 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8472
8473 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8474 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8475
8476 const int32_t x = DISPLAY_WIDTH / 4;
8477 const int32_t y = DISPLAY_HEIGHT / 2;
8478
8479 // expect x/y = swap x/y then reverse x.
8480 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8481 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8482 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8483}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008484
arthurhunga36b28e2020-12-29 20:28:15 +08008485TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8486 addConfigurationProperty("touch.deviceType", "touchScreen");
8487 prepareDisplay(DISPLAY_ORIENTATION_0);
8488 prepareAxes(POSITION);
8489 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8490
8491 const int32_t x = 0;
8492 const int32_t y = 0;
8493
8494 const int32_t xExpected = x;
8495 const int32_t yExpected = y;
8496 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8497
8498 clearViewports();
8499 prepareDisplay(DISPLAY_ORIENTATION_90);
8500 // expect x/y = swap x/y then reverse y.
8501 const int32_t xExpected90 = y;
8502 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8503 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8504
8505 clearViewports();
8506 prepareDisplay(DISPLAY_ORIENTATION_270);
8507 // expect x/y = swap x/y then reverse x.
8508 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8509 const int32_t yExpected270 = x;
8510 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8511}
8512
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008513TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8514 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8515 std::shared_ptr<FakePointerController> fakePointerController =
8516 std::make_shared<FakePointerController>();
8517 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8518 fakePointerController->setPosition(0, 0);
8519 fakePointerController->setButtonState(0);
8520
8521 // prepare device and capture
8522 prepareDisplay(DISPLAY_ORIENTATION_0);
8523 prepareAxes(POSITION | ID | SLOT);
8524 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8525 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8526 mFakePolicy->setPointerCapture(true);
8527 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8528 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8529
8530 // captured touchpad should be a touchpad source
8531 NotifyDeviceResetArgs resetArgs;
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8533 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8534
Chris Yef74dc422020-09-02 22:41:50 -07008535 InputDeviceInfo deviceInfo;
8536 mDevice->getDeviceInfo(&deviceInfo);
8537
8538 const InputDeviceInfo::MotionRange* relRangeX =
8539 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8540 ASSERT_NE(relRangeX, nullptr);
8541 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8542 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8543 const InputDeviceInfo::MotionRange* relRangeY =
8544 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8545 ASSERT_NE(relRangeY, nullptr);
8546 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8547 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8548
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008549 // run captured pointer tests - note that this is unscaled, so input listener events should be
8550 // identical to what the hardware sends (accounting for any
8551 // calibration).
8552 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008553 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008554 processId(mapper, 1);
8555 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8556 processKey(mapper, BTN_TOUCH, 1);
8557 processSync(mapper);
8558
8559 // expect coord[0] to contain initial location of touch 0
8560 NotifyMotionArgs args;
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8562 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8563 ASSERT_EQ(1U, args.pointerCount);
8564 ASSERT_EQ(0, args.pointerProperties[0].id);
8565 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8566 ASSERT_NO_FATAL_FAILURE(
8567 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8568
8569 // FINGER 1 DOWN
8570 processSlot(mapper, 1);
8571 processId(mapper, 2);
8572 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8573 processSync(mapper);
8574
8575 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008577 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8578 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008579 ASSERT_EQ(2U, args.pointerCount);
8580 ASSERT_EQ(0, args.pointerProperties[0].id);
8581 ASSERT_EQ(1, args.pointerProperties[1].id);
8582 ASSERT_NO_FATAL_FAILURE(
8583 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8584 ASSERT_NO_FATAL_FAILURE(
8585 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8586
8587 // FINGER 1 MOVE
8588 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8589 processSync(mapper);
8590
8591 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8592 // from move
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8595 ASSERT_NO_FATAL_FAILURE(
8596 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8597 ASSERT_NO_FATAL_FAILURE(
8598 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8599
8600 // FINGER 0 MOVE
8601 processSlot(mapper, 0);
8602 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8603 processSync(mapper);
8604
8605 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8608 ASSERT_NO_FATAL_FAILURE(
8609 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8610 ASSERT_NO_FATAL_FAILURE(
8611 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8612
8613 // BUTTON DOWN
8614 processKey(mapper, BTN_LEFT, 1);
8615 processSync(mapper);
8616
8617 // touchinputmapper design sends a move before button press
8618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8622
8623 // BUTTON UP
8624 processKey(mapper, BTN_LEFT, 0);
8625 processSync(mapper);
8626
8627 // touchinputmapper design sends a move after button release
8628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8632
8633 // FINGER 0 UP
8634 processId(mapper, -1);
8635 processSync(mapper);
8636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8637 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8638
8639 // FINGER 1 MOVE
8640 processSlot(mapper, 1);
8641 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8642 processSync(mapper);
8643
8644 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8647 ASSERT_EQ(1U, args.pointerCount);
8648 ASSERT_EQ(1, args.pointerProperties[0].id);
8649 ASSERT_NO_FATAL_FAILURE(
8650 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8651
8652 // FINGER 1 UP
8653 processId(mapper, -1);
8654 processKey(mapper, BTN_TOUCH, 0);
8655 processSync(mapper);
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8657 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8658
8659 // non captured touchpad should be a mouse source
8660 mFakePolicy->setPointerCapture(false);
8661 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8663 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8664}
8665
8666TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8667 std::shared_ptr<FakePointerController> fakePointerController =
8668 std::make_shared<FakePointerController>();
8669 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8670 fakePointerController->setPosition(0, 0);
8671 fakePointerController->setButtonState(0);
8672
8673 // prepare device and capture
8674 prepareDisplay(DISPLAY_ORIENTATION_0);
8675 prepareAxes(POSITION | ID | SLOT);
8676 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8677 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8678 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8679 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8680 // run uncaptured pointer tests - pushes out generic events
8681 // FINGER 0 DOWN
8682 processId(mapper, 3);
8683 processPosition(mapper, 100, 100);
8684 processKey(mapper, BTN_TOUCH, 1);
8685 processSync(mapper);
8686
8687 // start at (100,100), cursor should be at (0,0) * scale
8688 NotifyMotionArgs args;
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8691 ASSERT_NO_FATAL_FAILURE(
8692 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8693
8694 // FINGER 0 MOVE
8695 processPosition(mapper, 200, 200);
8696 processSync(mapper);
8697
8698 // compute scaling to help with touch position checking
8699 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8700 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8701 float scale =
8702 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8703
8704 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8706 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8708 0, 0, 0, 0, 0, 0, 0));
8709}
8710
8711TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8712 std::shared_ptr<FakePointerController> fakePointerController =
8713 std::make_shared<FakePointerController>();
8714
8715 prepareDisplay(DISPLAY_ORIENTATION_0);
8716 prepareAxes(POSITION | ID | SLOT);
8717 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8718 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8719 mFakePolicy->setPointerCapture(false);
8720 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8721
8722 // uncaptured touchpad should be a pointer device
8723 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8724
8725 // captured touchpad should be a touchpad device
8726 mFakePolicy->setPointerCapture(true);
8727 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8728 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8729}
8730
Michael Wrightd02c5b62014-02-10 15:10:22 -08008731} // namespace android