blob: 778f6e65b07aa699fe715824b86f21c9f2431e35 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070025#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070026#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <SingleTouchInputMapper.h>
28#include <SwitchInputMapper.h>
29#include <TestInputListener.h>
30#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080031#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000032#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070033#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050035#include <gui/constants.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;
Chris Yee2b1e5c2021-03-10 22:45:12 -080074static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080075static constexpr int32_t BATTERY_STATUS = 4;
76static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080077static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
78static constexpr int32_t LIGHT_COLOR = 0x7F448866;
79static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080080
Michael Wrightd02c5b62014-02-10 15:10:22 -080081// Error tolerance for floating point assertions.
82static const float EPSILON = 0.001f;
83
84template<typename T>
85static inline T min(T a, T b) {
86 return a < b ? a : b;
87}
88
89static inline float avg(float x, float y) {
90 return (x + y) / 2;
91}
92
Chris Ye3fdbfef2021-01-06 18:45:18 -080093// Mapping for light color name and the light color
94const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
95 {"green", LightColor::GREEN},
96 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
98// --- FakePointerController ---
99
100class FakePointerController : public PointerControllerInterface {
101 bool mHaveBounds;
102 float mMinX, mMinY, mMaxX, mMaxY;
103 float mX, mY;
104 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800105 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107public:
108 FakePointerController() :
109 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800110 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111 }
112
Michael Wright17db18e2020-06-26 20:51:44 +0100113 virtual ~FakePointerController() {}
114
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115 void setBounds(float minX, float minY, float maxX, float maxY) {
116 mHaveBounds = true;
117 mMinX = minX;
118 mMinY = minY;
119 mMaxX = maxX;
120 mMaxY = maxY;
121 }
122
Chris Yea52ade12020-08-27 16:49:20 -0700123 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124 mX = x;
125 mY = y;
126 }
127
Chris Yea52ade12020-08-27 16:49:20 -0700128 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129
Chris Yea52ade12020-08-27 16:49:20 -0700130 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131
Chris Yea52ade12020-08-27 16:49:20 -0700132 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133 *outX = mX;
134 *outY = mY;
135 }
136
Chris Yea52ade12020-08-27 16:49:20 -0700137 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800138
Chris Yea52ade12020-08-27 16:49:20 -0700139 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800140 mDisplayId = viewport.displayId;
141 }
142
Arthur Hung7c645402019-01-25 17:45:42 +0800143 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
144 return mSpotsByDisplay;
145 }
146
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147private:
Chris Yea52ade12020-08-27 16:49:20 -0700148 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149 *outMinX = mMinX;
150 *outMinY = mMinY;
151 *outMaxX = mMaxX;
152 *outMaxY = mMaxY;
153 return mHaveBounds;
154 }
155
Chris Yea52ade12020-08-27 16:49:20 -0700156 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 mX += deltaX;
158 if (mX < mMinX) mX = mMinX;
159 if (mX > mMaxX) mX = mMaxX;
160 mY += deltaY;
161 if (mY < mMinY) mY = mMinY;
162 if (mY > mMaxY) mY = mMaxY;
163 }
164
Chris Yea52ade12020-08-27 16:49:20 -0700165 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
172 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800173 std::vector<int32_t> newSpots;
174 // Add spots for fingers that are down.
175 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
176 uint32_t id = idBits.clearFirstMarkedBit();
177 newSpots.push_back(id);
178 }
179
180 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 }
182
Chris Yea52ade12020-08-27 16:49:20 -0700183 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800184
185 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186};
187
188
189// --- FakeInputReaderPolicy ---
190
191class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700192 std::mutex mLock;
193 std::condition_variable mDevicesChangedCondition;
194
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100196 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700197 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
198 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100199 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700200 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201
202protected:
Chris Yea52ade12020-08-27 16:49:20 -0700203 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204
205public:
206 FakeInputReaderPolicy() {
207 }
208
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800210 waitForInputDevices([](bool devicesChanged) {
211 if (!devicesChanged) {
212 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
213 }
214 });
215 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700216
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800217 void assertInputDevicesNotChanged() {
218 waitForInputDevices([](bool devicesChanged) {
219 if (devicesChanged) {
220 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
221 }
222 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700223 }
224
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700225 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100226 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100227 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700228 }
229
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700230 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
231 return mConfig.getDisplayViewportByUniqueId(uniqueId);
232 }
233 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
234 return mConfig.getDisplayViewportByType(type);
235 }
236
237 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
238 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700239 }
240
241 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000242 bool isActive, const std::string& uniqueId,
243 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
244 const DisplayViewport viewport =
245 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
246 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700247 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100248 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 }
250
Arthur Hung6cd19a42019-08-30 19:04:12 +0800251 bool updateViewport(const DisplayViewport& viewport) {
252 size_t count = mViewports.size();
253 for (size_t i = 0; i < count; i++) {
254 const DisplayViewport& currentViewport = mViewports[i];
255 if (currentViewport.displayId == viewport.displayId) {
256 mViewports[i] = viewport;
257 mConfig.setDisplayViewports(mViewports);
258 return true;
259 }
260 }
261 // no viewport found.
262 return false;
263 }
264
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100265 void addExcludedDeviceName(const std::string& deviceName) {
266 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
270 mConfig.portAssociations.insert({inputPort, displayPort});
271 }
272
Christine Franks1ba71cc2021-04-07 14:37:42 -0700273 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
274 const std::string& displayUniqueId) {
275 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
276 }
277
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000278 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700279
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000280 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700281
Michael Wright17db18e2020-06-26 20:51:44 +0100282 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
283 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 }
285
286 const InputReaderConfiguration* getReaderConfiguration() const {
287 return &mConfig;
288 }
289
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800290 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 return mInputDevices;
292 }
293
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100294 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700295 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700296 return transform;
297 }
298
299 void setTouchAffineTransformation(const TouchAffineTransformation t) {
300 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800301 }
302
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800303 void setPointerCapture(bool enabled) {
304 mConfig.pointerCapture = enabled;
305 }
306
Arthur Hung7c645402019-01-25 17:45:42 +0800307 void setShowTouches(bool enabled) {
308 mConfig.showTouches = enabled;
309 }
310
Garfield Tan888a6a42020-01-09 11:39:16 -0800311 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
312 mConfig.defaultPointerDisplayId = pointerDisplayId;
313 }
314
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800315 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
316
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700318 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000319 int32_t orientation, bool isActive,
320 const std::string& uniqueId,
321 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700322 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
323 || orientation == DISPLAY_ORIENTATION_270);
324 DisplayViewport v;
325 v.displayId = displayId;
326 v.orientation = orientation;
327 v.logicalLeft = 0;
328 v.logicalTop = 0;
329 v.logicalRight = isRotated ? height : width;
330 v.logicalBottom = isRotated ? width : height;
331 v.physicalLeft = 0;
332 v.physicalTop = 0;
333 v.physicalRight = isRotated ? height : width;
334 v.physicalBottom = isRotated ? width : height;
335 v.deviceWidth = isRotated ? height : width;
336 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000337 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700338 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700339 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100340 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700341 return v;
342 }
343
Chris Yea52ade12020-08-27 16:49:20 -0700344 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 *outConfig = mConfig;
346 }
347
Chris Yea52ade12020-08-27 16:49:20 -0700348 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100349 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 }
351
Chris Yea52ade12020-08-27 16:49:20 -0700352 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700353 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700355 mInputDevicesChanged = true;
356 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357 }
358
Chris Yea52ade12020-08-27 16:49:20 -0700359 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
360 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700361 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362 }
363
Chris Yea52ade12020-08-27 16:49:20 -0700364 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800365
366 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
367 std::unique_lock<std::mutex> lock(mLock);
368 base::ScopedLockAssertion assumeLocked(mLock);
369
370 const bool devicesChanged =
371 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
372 return mInputDevicesChanged;
373 });
374 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
375 mInputDevicesChanged = false;
376 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377};
378
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379// --- FakeEventHub ---
380
381class FakeEventHub : public EventHubInterface {
382 struct KeyInfo {
383 int32_t keyCode;
384 uint32_t flags;
385 };
386
Chris Yef59a2f42020-10-16 12:55:26 -0700387 struct SensorInfo {
388 InputDeviceSensorType sensorType;
389 int32_t sensorDataIndex;
390 };
391
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392 struct Device {
393 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700394 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 PropertyMap configuration;
396 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
397 KeyedVector<int, bool> relativeAxes;
398 KeyedVector<int32_t, int32_t> keyCodeStates;
399 KeyedVector<int32_t, int32_t> scanCodeStates;
400 KeyedVector<int32_t, int32_t> switchStates;
401 KeyedVector<int32_t, int32_t> absoluteAxisValue;
402 KeyedVector<int32_t, KeyInfo> keysByScanCode;
403 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
404 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700405 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
406 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800407 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700408 bool enabled;
409
410 status_t enable() {
411 enabled = true;
412 return OK;
413 }
414
415 status_t disable() {
416 enabled = false;
417 return OK;
418 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419
Chris Ye1b0c7342020-07-28 21:57:03 -0700420 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421 };
422
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700423 std::mutex mLock;
424 std::condition_variable mEventsCondition;
425
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100427 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000428 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600429 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000430 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800431 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
432 // Simulates a device light brightness, from light id to light brightness.
433 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
434 // Simulates a device light intensities, from light id to light intensities map.
435 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
436 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700438public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 virtual ~FakeEventHub() {
440 for (size_t i = 0; i < mDevices.size(); i++) {
441 delete mDevices.valueAt(i);
442 }
443 }
444
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 FakeEventHub() { }
446
Chris Ye1b0c7342020-07-28 21:57:03 -0700447 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448 Device* device = new Device(classes);
449 device->identifier.name = name;
450 mDevices.add(deviceId, device);
451
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000452 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453 }
454
455 void removeDevice(int32_t deviceId) {
456 delete mDevices.valueFor(deviceId);
457 mDevices.removeItem(deviceId);
458
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000459 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 }
461
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700462 bool isDeviceEnabled(int32_t deviceId) {
463 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700464 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700465 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
466 return false;
467 }
468 return device->enabled;
469 }
470
471 status_t enableDevice(int32_t deviceId) {
472 status_t result;
473 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700474 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700475 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
476 return BAD_VALUE;
477 }
478 if (device->enabled) {
479 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
480 return OK;
481 }
482 result = device->enable();
483 return result;
484 }
485
486 status_t disableDevice(int32_t deviceId) {
487 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700488 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700489 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
490 return BAD_VALUE;
491 }
492 if (!device->enabled) {
493 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
494 return OK;
495 }
496 return device->disable();
497 }
498
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000500 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 }
502
503 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
504 Device* device = getDevice(deviceId);
505 device->configuration.addProperty(key, value);
506 }
507
508 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
509 Device* device = getDevice(deviceId);
510 device->configuration.addAll(configuration);
511 }
512
513 void addAbsoluteAxis(int32_t deviceId, int axis,
514 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
515 Device* device = getDevice(deviceId);
516
517 RawAbsoluteAxisInfo info;
518 info.valid = true;
519 info.minValue = minValue;
520 info.maxValue = maxValue;
521 info.flat = flat;
522 info.fuzz = fuzz;
523 info.resolution = resolution;
524 device->absoluteAxes.add(axis, info);
525 }
526
527 void addRelativeAxis(int32_t deviceId, int32_t axis) {
528 Device* device = getDevice(deviceId);
529 device->relativeAxes.add(axis, true);
530 }
531
532 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
533 Device* device = getDevice(deviceId);
534 device->keyCodeStates.replaceValueFor(keyCode, state);
535 }
536
537 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
538 Device* device = getDevice(deviceId);
539 device->scanCodeStates.replaceValueFor(scanCode, state);
540 }
541
542 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
543 Device* device = getDevice(deviceId);
544 device->switchStates.replaceValueFor(switchCode, state);
545 }
546
547 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
548 Device* device = getDevice(deviceId);
549 device->absoluteAxisValue.replaceValueFor(axis, value);
550 }
551
552 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
553 int32_t keyCode, uint32_t flags) {
554 Device* device = getDevice(deviceId);
555 KeyInfo info;
556 info.keyCode = keyCode;
557 info.flags = flags;
558 if (scanCode) {
559 device->keysByScanCode.add(scanCode, info);
560 }
561 if (usageCode) {
562 device->keysByUsageCode.add(usageCode, info);
563 }
564 }
565
566 void addLed(int32_t deviceId, int32_t led, bool initialState) {
567 Device* device = getDevice(deviceId);
568 device->leds.add(led, initialState);
569 }
570
Chris Yef59a2f42020-10-16 12:55:26 -0700571 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
572 int32_t sensorDataIndex) {
573 Device* device = getDevice(deviceId);
574 SensorInfo info;
575 info.sensorType = sensorType;
576 info.sensorDataIndex = sensorDataIndex;
577 device->sensorsByAbsCode.emplace(absCode, info);
578 }
579
580 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
581 Device* device = getDevice(deviceId);
582 typename BitArray<MSC_MAX>::Buffer buffer;
583 buffer[mscEvent / 32] = 1 << mscEvent % 32;
584 device->mscBitmask.loadFromBuffer(buffer);
585 }
586
Chris Ye3fdbfef2021-01-06 18:45:18 -0800587 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
588 mRawLightInfos.emplace(rawId, std::move(info));
589 }
590
591 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
592 mLightBrightness.emplace(rawId, brightness);
593 }
594
595 void fakeLightIntensities(int32_t rawId,
596 const std::unordered_map<LightColor, int32_t> intensities) {
597 mLightIntensities.emplace(rawId, std::move(intensities));
598 }
599
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 bool getLedState(int32_t deviceId, int32_t led) {
601 Device* device = getDevice(deviceId);
602 return device->leds.valueFor(led);
603 }
604
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100605 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 return mExcludedDevices;
607 }
608
609 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
610 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800611 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000614 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
615 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700616 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 RawEvent event;
618 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000619 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 event.deviceId = deviceId;
621 event.type = type;
622 event.code = code;
623 event.value = value;
624 mEvents.push_back(event);
625
626 if (type == EV_ABS) {
627 setAbsoluteAxisValue(deviceId, code, value);
628 }
629 }
630
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600631 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
632 std::vector<TouchVideoFrame>> videoFrames) {
633 mVideoFrames = std::move(videoFrames);
634 }
635
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700637 std::unique_lock<std::mutex> lock(mLock);
638 base::ScopedLockAssertion assumeLocked(mLock);
639 const bool queueIsEmpty =
640 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
641 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
642 if (!queueIsEmpty) {
643 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
644 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 }
646
647private:
648 Device* getDevice(int32_t deviceId) const {
649 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100650 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 }
652
Chris Yea52ade12020-08-27 16:49:20 -0700653 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700655 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 }
657
Chris Yea52ade12020-08-27 16:49:20 -0700658 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659 Device* device = getDevice(deviceId);
660 return device ? device->identifier : InputDeviceIdentifier();
661 }
662
Chris Yea52ade12020-08-27 16:49:20 -0700663 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664
Chris Yea52ade12020-08-27 16:49:20 -0700665 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 Device* device = getDevice(deviceId);
667 if (device) {
668 *outConfiguration = device->configuration;
669 }
670 }
671
Chris Yea52ade12020-08-27 16:49:20 -0700672 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
673 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800675 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 ssize_t index = device->absoluteAxes.indexOfKey(axis);
677 if (index >= 0) {
678 *outAxisInfo = device->absoluteAxes.valueAt(index);
679 return OK;
680 }
681 }
682 outAxisInfo->clear();
683 return -1;
684 }
685
Chris Yea52ade12020-08-27 16:49:20 -0700686 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 Device* device = getDevice(deviceId);
688 if (device) {
689 return device->relativeAxes.indexOfKey(axis) >= 0;
690 }
691 return false;
692 }
693
Chris Yea52ade12020-08-27 16:49:20 -0700694 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695
Chris Yef59a2f42020-10-16 12:55:26 -0700696 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
697 Device* device = getDevice(deviceId);
698 if (device) {
699 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
700 }
701 return false;
702 }
703
Chris Yea52ade12020-08-27 16:49:20 -0700704 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
705 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 Device* device = getDevice(deviceId);
707 if (device) {
708 const KeyInfo* key = getKey(device, scanCode, usageCode);
709 if (key) {
710 if (outKeycode) {
711 *outKeycode = key->keyCode;
712 }
713 if (outFlags) {
714 *outFlags = key->flags;
715 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700716 if (outMetaState) {
717 *outMetaState = metaState;
718 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 return OK;
720 }
721 }
722 return NAME_NOT_FOUND;
723 }
724
725 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
726 if (usageCode) {
727 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
728 if (index >= 0) {
729 return &device->keysByUsageCode.valueAt(index);
730 }
731 }
732 if (scanCode) {
733 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
734 if (index >= 0) {
735 return &device->keysByScanCode.valueAt(index);
736 }
737 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700738 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 }
740
Chris Yea52ade12020-08-27 16:49:20 -0700741 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742
Chris Yef59a2f42020-10-16 12:55:26 -0700743 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
744 int32_t absCode) {
745 Device* device = getDevice(deviceId);
746 if (!device) {
747 return Errorf("Sensor device not found.");
748 }
749 auto it = device->sensorsByAbsCode.find(absCode);
750 if (it == device->sensorsByAbsCode.end()) {
751 return Errorf("Sensor map not found.");
752 }
753 const SensorInfo& info = it->second;
754 return std::make_pair(info.sensorType, info.sensorDataIndex);
755 }
756
Chris Yea52ade12020-08-27 16:49:20 -0700757 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 mExcludedDevices = devices;
759 }
760
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000761 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
762 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000764 const size_t filledSize = std::min(mEvents.size(), bufferSize);
765 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
766
767 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700768 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000769 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600773 auto it = mVideoFrames.find(deviceId);
774 if (it != mVideoFrames.end()) {
775 std::vector<TouchVideoFrame> frames = std::move(it->second);
776 mVideoFrames.erase(deviceId);
777 return frames;
778 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800779 return {};
780 }
781
Chris Yea52ade12020-08-27 16:49:20 -0700782 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 Device* device = getDevice(deviceId);
784 if (device) {
785 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
786 if (index >= 0) {
787 return device->scanCodeStates.valueAt(index);
788 }
789 }
790 return AKEY_STATE_UNKNOWN;
791 }
792
Chris Yea52ade12020-08-27 16:49:20 -0700793 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 Device* device = getDevice(deviceId);
795 if (device) {
796 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
797 if (index >= 0) {
798 return device->keyCodeStates.valueAt(index);
799 }
800 }
801 return AKEY_STATE_UNKNOWN;
802 }
803
Chris Yea52ade12020-08-27 16:49:20 -0700804 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 Device* device = getDevice(deviceId);
806 if (device) {
807 ssize_t index = device->switchStates.indexOfKey(sw);
808 if (index >= 0) {
809 return device->switchStates.valueAt(index);
810 }
811 }
812 return AKEY_STATE_UNKNOWN;
813 }
814
Chris Yea52ade12020-08-27 16:49:20 -0700815 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
816 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 Device* device = getDevice(deviceId);
818 if (device) {
819 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
820 if (index >= 0) {
821 *outValue = device->absoluteAxisValue.valueAt(index);
822 return OK;
823 }
824 }
825 *outValue = 0;
826 return -1;
827 }
828
Chris Yea52ade12020-08-27 16:49:20 -0700829 // Return true if the device has non-empty key layout.
830 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
831 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 bool result = false;
833 Device* device = getDevice(deviceId);
834 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700835 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 for (size_t i = 0; i < numCodes; i++) {
837 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
838 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
839 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840 }
841 }
842 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
843 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
844 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 }
846 }
847 }
848 }
849 return result;
850 }
851
Chris Yea52ade12020-08-27 16:49:20 -0700852 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853 Device* device = getDevice(deviceId);
854 if (device) {
855 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
856 return index >= 0;
857 }
858 return false;
859 }
860
Chris Yea52ade12020-08-27 16:49:20 -0700861 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800862 Device* device = getDevice(deviceId);
863 return device && device->leds.indexOfKey(led) >= 0;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867 Device* device = getDevice(deviceId);
868 if (device) {
869 ssize_t index = device->leds.indexOfKey(led);
870 if (index >= 0) {
871 device->leds.replaceValueAt(led, on);
872 } else {
873 ADD_FAILURE()
874 << "Attempted to set the state of an LED that the EventHub declared "
875 "was not present. led=" << led;
876 }
877 }
878 }
879
Chris Yea52ade12020-08-27 16:49:20 -0700880 void getVirtualKeyDefinitions(
881 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 outVirtualKeys.clear();
883
884 Device* device = getDevice(deviceId);
885 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800886 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887 }
888 }
889
Chris Yea52ade12020-08-27 16:49:20 -0700890 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700891 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 }
893
Chris Yea52ade12020-08-27 16:49:20 -0700894 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 return false;
896 }
897
Chris Yea52ade12020-08-27 16:49:20 -0700898 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899
Chris Yea52ade12020-08-27 16:49:20 -0700900 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Chris Ye87143712020-11-10 05:05:58 +0000902 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
903
Chris Yee2b1e5c2021-03-10 22:45:12 -0800904 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
905 return BATTERY_CAPACITY;
906 }
Kim Low03ea0352020-11-06 12:45:07 -0800907
Chris Yee2b1e5c2021-03-10 22:45:12 -0800908 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
909 return BATTERY_STATUS;
910 }
911
912 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
913
914 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
915 return std::nullopt;
916 }
Kim Low03ea0352020-11-06 12:45:07 -0800917
Chris Ye3fdbfef2021-01-06 18:45:18 -0800918 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
919 std::vector<int32_t> ids;
920 for (const auto& [rawId, info] : mRawLightInfos) {
921 ids.push_back(rawId);
922 }
923 return ids;
924 }
925
926 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
927 auto it = mRawLightInfos.find(lightId);
928 if (it == mRawLightInfos.end()) {
929 return std::nullopt;
930 }
931 return it->second;
932 }
933
934 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
935 mLightBrightness.emplace(lightId, brightness);
936 }
937
938 void setLightIntensities(int32_t deviceId, int32_t lightId,
939 std::unordered_map<LightColor, int32_t> intensities) override {
940 mLightIntensities.emplace(lightId, intensities);
941 };
942
943 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
944 auto lightIt = mLightBrightness.find(lightId);
945 if (lightIt == mLightBrightness.end()) {
946 return std::nullopt;
947 }
948 return lightIt->second;
949 }
950
951 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
952 int32_t deviceId, int32_t lightId) override {
953 auto lightIt = mLightIntensities.find(lightId);
954 if (lightIt == mLightIntensities.end()) {
955 return std::nullopt;
956 }
957 return lightIt->second;
958 };
959
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100960 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 return false;
962 }
963
Chris Yea52ade12020-08-27 16:49:20 -0700964 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Chris Yea52ade12020-08-27 16:49:20 -0700966 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Chris Yea52ade12020-08-27 16:49:20 -0700968 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969
Chris Yea52ade12020-08-27 16:49:20 -0700970 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971};
972
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973// --- FakeInputMapper ---
974
975class FakeInputMapper : public InputMapper {
976 uint32_t mSources;
977 int32_t mKeyboardType;
978 int32_t mMetaState;
979 KeyedVector<int32_t, int32_t> mKeyCodeStates;
980 KeyedVector<int32_t, int32_t> mScanCodeStates;
981 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800982 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700984 std::mutex mLock;
985 std::condition_variable mStateChangedCondition;
986 bool mConfigureWasCalled GUARDED_BY(mLock);
987 bool mResetWasCalled GUARDED_BY(mLock);
988 bool mProcessWasCalled GUARDED_BY(mLock);
989 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990
Arthur Hungc23540e2018-11-29 20:42:11 +0800991 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800993 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
994 : InputMapper(deviceContext),
995 mSources(sources),
996 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800998 mConfigureWasCalled(false),
999 mResetWasCalled(false),
1000 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
Chris Yea52ade12020-08-27 16:49:20 -07001002 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
1004 void setKeyboardType(int32_t keyboardType) {
1005 mKeyboardType = keyboardType;
1006 }
1007
1008 void setMetaState(int32_t metaState) {
1009 mMetaState = metaState;
1010 }
1011
1012 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 std::unique_lock<std::mutex> lock(mLock);
1014 base::ScopedLockAssertion assumeLocked(mLock);
1015 const bool configureCalled =
1016 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1017 return mConfigureWasCalled;
1018 });
1019 if (!configureCalled) {
1020 FAIL() << "Expected configure() to have been called.";
1021 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022 mConfigureWasCalled = false;
1023 }
1024
1025 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001026 std::unique_lock<std::mutex> lock(mLock);
1027 base::ScopedLockAssertion assumeLocked(mLock);
1028 const bool resetCalled =
1029 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1030 return mResetWasCalled;
1031 });
1032 if (!resetCalled) {
1033 FAIL() << "Expected reset() to have been called.";
1034 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035 mResetWasCalled = false;
1036 }
1037
Yi Kong9b14ac62018-07-17 13:48:38 -07001038 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001039 std::unique_lock<std::mutex> lock(mLock);
1040 base::ScopedLockAssertion assumeLocked(mLock);
1041 const bool processCalled =
1042 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1043 return mProcessWasCalled;
1044 });
1045 if (!processCalled) {
1046 FAIL() << "Expected process() to have been called.";
1047 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048 if (outLastEvent) {
1049 *outLastEvent = mLastEvent;
1050 }
1051 mProcessWasCalled = false;
1052 }
1053
1054 void setKeyCodeState(int32_t keyCode, int32_t state) {
1055 mKeyCodeStates.replaceValueFor(keyCode, state);
1056 }
1057
1058 void setScanCodeState(int32_t scanCode, int32_t state) {
1059 mScanCodeStates.replaceValueFor(scanCode, state);
1060 }
1061
1062 void setSwitchState(int32_t switchCode, int32_t state) {
1063 mSwitchStates.replaceValueFor(switchCode, state);
1064 }
1065
1066 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001067 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068 }
1069
1070private:
Chris Yea52ade12020-08-27 16:49:20 -07001071 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Chris Yea52ade12020-08-27 16:49:20 -07001073 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074 InputMapper::populateDeviceInfo(deviceInfo);
1075
1076 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1077 deviceInfo->setKeyboardType(mKeyboardType);
1078 }
1079 }
1080
Chris Yea52ade12020-08-27 16:49:20 -07001081 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001082 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001084
1085 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001086 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001087 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1088 mViewport = config->getDisplayViewportByPort(*displayPort);
1089 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001090
1091 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 }
1093
Chris Yea52ade12020-08-27 16:49:20 -07001094 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001095 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001097 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098 }
1099
Chris Yea52ade12020-08-27 16:49:20 -07001100 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001101 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 mLastEvent = *rawEvent;
1103 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001104 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 }
1106
Chris Yea52ade12020-08-27 16:49:20 -07001107 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1109 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1110 }
1111
Chris Yea52ade12020-08-27 16:49:20 -07001112 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1114 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1115 }
1116
Chris Yea52ade12020-08-27 16:49:20 -07001117 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1119 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1120 }
1121
Chris Yea52ade12020-08-27 16:49:20 -07001122 // Return true if the device has non-empty key layout.
1123 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1124 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 for (size_t i = 0; i < numCodes; i++) {
1126 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1127 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1128 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 }
1130 }
1131 }
Chris Yea52ade12020-08-27 16:49:20 -07001132 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133 return result;
1134 }
1135
1136 virtual int32_t getMetaState() {
1137 return mMetaState;
1138 }
1139
1140 virtual void fadePointer() {
1141 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001142
1143 virtual std::optional<int32_t> getAssociatedDisplay() {
1144 if (mViewport) {
1145 return std::make_optional(mViewport->displayId);
1146 }
1147 return std::nullopt;
1148 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149};
1150
1151
1152// --- InstrumentedInputReader ---
1153
1154class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001155 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156
1157public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001158 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1159 const sp<InputReaderPolicyInterface>& policy,
1160 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001161 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001163 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001165 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001166
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001167 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001168 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001169 InputDeviceIdentifier identifier;
1170 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001171 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001173 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 }
1175
Prabir Pradhan28efc192019-11-05 01:10:04 +00001176 // Make the protected loopOnce method accessible to tests.
1177 using InputReader::loopOnce;
1178
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001180 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1181 const InputDeviceIdentifier& identifier)
1182 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001183 if (!mNextDevices.empty()) {
1184 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1185 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 return device;
1187 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001188 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 }
1190
arthurhungdcef2dc2020-08-11 14:47:50 +08001191 // --- FakeInputReaderContext ---
1192 class FakeInputReaderContext : public ContextImpl {
1193 int32_t mGlobalMetaState;
1194 bool mUpdateGlobalMetaStateWasCalled;
1195 int32_t mGeneration;
1196
1197 public:
1198 FakeInputReaderContext(InputReader* reader)
1199 : ContextImpl(reader),
1200 mGlobalMetaState(0),
1201 mUpdateGlobalMetaStateWasCalled(false),
1202 mGeneration(1) {}
1203
1204 virtual ~FakeInputReaderContext() {}
1205
1206 void assertUpdateGlobalMetaStateWasCalled() {
1207 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1208 << "Expected updateGlobalMetaState() to have been called.";
1209 mUpdateGlobalMetaStateWasCalled = false;
1210 }
1211
1212 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1213
1214 uint32_t getGeneration() { return mGeneration; }
1215
1216 void updateGlobalMetaState() override {
1217 mUpdateGlobalMetaStateWasCalled = true;
1218 ContextImpl::updateGlobalMetaState();
1219 }
1220
1221 int32_t getGlobalMetaState() override {
1222 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1223 }
1224
1225 int32_t bumpGeneration() override {
1226 mGeneration = ContextImpl::bumpGeneration();
1227 return mGeneration;
1228 }
1229 } mFakeContext;
1230
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001232
1233public:
1234 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235};
1236
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237// --- InputReaderPolicyTest ---
1238class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001239protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 sp<FakeInputReaderPolicy> mFakePolicy;
1241
Chris Yea52ade12020-08-27 16:49:20 -07001242 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1243 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001244};
1245
1246/**
1247 * Check that empty set of viewports is an acceptable configuration.
1248 * Also try to get internal viewport two different ways - by type and by uniqueId.
1249 *
1250 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1251 * Such configuration is not currently allowed.
1252 */
1253TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001254 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001255
1256 // We didn't add any viewports yet, so there shouldn't be any.
1257 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001258 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001259 ASSERT_FALSE(internalViewport);
1260
1261 // Add an internal viewport, then clear it
1262 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001263 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001264 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001265
1266 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001267 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001269 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270
1271 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001272 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001273 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001274 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275
1276 mFakePolicy->clearViewports();
1277 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001278 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001279 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001280 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001281 ASSERT_FALSE(internalViewport);
1282}
1283
1284TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1285 const std::string internalUniqueId = "local:0";
1286 const std::string externalUniqueId = "local:1";
1287 const std::string virtualUniqueId1 = "virtual:2";
1288 const std::string virtualUniqueId2 = "virtual:3";
1289 constexpr int32_t virtualDisplayId1 = 2;
1290 constexpr int32_t virtualDisplayId2 = 3;
1291
1292 // Add an internal viewport
1293 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001294 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1295 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001296 // Add an external viewport
1297 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001298 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1299 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001300 // Add an virtual viewport
1301 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001302 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1303 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001304 // Add another virtual viewport
1305 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001306 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1307 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001308
1309 // Check matching by type for internal
1310 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001311 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001312 ASSERT_TRUE(internalViewport);
1313 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1314
1315 // Check matching by type for external
1316 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001317 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001318 ASSERT_TRUE(externalViewport);
1319 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1320
1321 // Check matching by uniqueId for virtual viewport #1
1322 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001323 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001324 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001325 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1327 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1328
1329 // Check matching by uniqueId for virtual viewport #2
1330 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001331 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001332 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001333 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1335 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1336}
1337
1338
1339/**
1340 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1341 * that lookup works by checking display id.
1342 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1343 */
1344TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1345 const std::string uniqueId1 = "uniqueId1";
1346 const std::string uniqueId2 = "uniqueId2";
1347 constexpr int32_t displayId1 = 2;
1348 constexpr int32_t displayId2 = 3;
1349
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001350 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1351 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 for (const ViewportType& type : types) {
1353 mFakePolicy->clearViewports();
1354 // Add a viewport
1355 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001356 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1357 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358 // Add another viewport
1359 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001360 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1361 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362
1363 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001364 std::optional<DisplayViewport> viewport1 =
1365 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366 ASSERT_TRUE(viewport1);
1367 ASSERT_EQ(displayId1, viewport1->displayId);
1368 ASSERT_EQ(type, viewport1->type);
1369
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001370 std::optional<DisplayViewport> viewport2 =
1371 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 ASSERT_TRUE(viewport2);
1373 ASSERT_EQ(displayId2, viewport2->displayId);
1374 ASSERT_EQ(type, viewport2->type);
1375
1376 // When there are multiple viewports of the same kind, and uniqueId is not specified
1377 // in the call to getDisplayViewport, then that situation is not supported.
1378 // The viewports can be stored in any order, so we cannot rely on the order, since that
1379 // is just implementation detail.
1380 // However, we can check that it still returns *a* viewport, we just cannot assert
1381 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001382 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383 ASSERT_TRUE(someViewport);
1384 }
1385}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001387/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001388 * When we have multiple internal displays make sure we always return the default display when
1389 * querying by type.
1390 */
1391TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1392 const std::string uniqueId1 = "uniqueId1";
1393 const std::string uniqueId2 = "uniqueId2";
1394 constexpr int32_t nonDefaultDisplayId = 2;
1395 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1396 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1397
1398 // Add the default display first and ensure it gets returned.
1399 mFakePolicy->clearViewports();
1400 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001401 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001402 ViewportType::INTERNAL);
1403 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001404 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001405 ViewportType::INTERNAL);
1406
1407 std::optional<DisplayViewport> viewport =
1408 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1409 ASSERT_TRUE(viewport);
1410 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1411 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1412
1413 // Add the default display second to make sure order doesn't matter.
1414 mFakePolicy->clearViewports();
1415 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001416 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001417 ViewportType::INTERNAL);
1418 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001419 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001420 ViewportType::INTERNAL);
1421
1422 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1423 ASSERT_TRUE(viewport);
1424 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1425 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1426}
1427
1428/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001429 * Check getDisplayViewportByPort
1430 */
1431TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001432 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001433 const std::string uniqueId1 = "uniqueId1";
1434 const std::string uniqueId2 = "uniqueId2";
1435 constexpr int32_t displayId1 = 1;
1436 constexpr int32_t displayId2 = 2;
1437 const uint8_t hdmi1 = 0;
1438 const uint8_t hdmi2 = 1;
1439 const uint8_t hdmi3 = 2;
1440
1441 mFakePolicy->clearViewports();
1442 // Add a viewport that's associated with some display port that's not of interest.
1443 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001444 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1445 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001446 // Add another viewport, connected to HDMI1 port
1447 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001448 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1449 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001450
1451 // Check that correct display viewport was returned by comparing the display ports.
1452 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1453 ASSERT_TRUE(hdmi1Viewport);
1454 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1455 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1456
1457 // Check that we can still get the same viewport using the uniqueId
1458 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1459 ASSERT_TRUE(hdmi1Viewport);
1460 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1461 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1462 ASSERT_EQ(type, hdmi1Viewport->type);
1463
1464 // Check that we cannot find a port with "HDMI2", because we never added one
1465 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1466 ASSERT_FALSE(hdmi2Viewport);
1467}
1468
Michael Wrightd02c5b62014-02-10 15:10:22 -08001469// --- InputReaderTest ---
1470
1471class InputReaderTest : public testing::Test {
1472protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001473 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001474 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001475 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001476 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477
Chris Yea52ade12020-08-27 16:49:20 -07001478 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001479 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001480 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001481 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001482
Prabir Pradhan28efc192019-11-05 01:10:04 +00001483 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1484 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001485 }
1486
Chris Yea52ade12020-08-27 16:49:20 -07001487 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001488 mFakeListener.clear();
1489 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001490 }
1491
Chris Ye1b0c7342020-07-28 21:57:03 -07001492 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001493 const PropertyMap* configuration) {
1494 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495
1496 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001497 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001498 }
1499 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001500 mReader->loopOnce();
1501 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001502 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1503 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504 }
1505
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001506 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001507 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001508 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001509 }
1510
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001511 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001512 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001513 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001514 }
1515
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001516 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001517 const std::string& name,
1518 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001519 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001520 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1521 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001522 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001523 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524 return mapper;
1525 }
1526};
1527
Chris Ye98d3f532020-10-01 21:48:59 -07001528TEST_F(InputReaderTest, PolicyGetInputDevices) {
1529 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1530 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1531 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001532
1533 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001534 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001536 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001537 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001538 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1539 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001540 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541}
1542
Chris Yee7310032020-09-22 15:36:28 -07001543TEST_F(InputReaderTest, GetMergedInputDevices) {
1544 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1545 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1546 // Add two subdevices to device
1547 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1548 // Must add at least one mapper or the device will be ignored!
1549 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1550 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1551
1552 // Push same device instance for next device to be added, so they'll have same identifier.
1553 mReader->pushNextDevice(device);
1554 mReader->pushNextDevice(device);
1555 ASSERT_NO_FATAL_FAILURE(
1556 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1557 ASSERT_NO_FATAL_FAILURE(
1558 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1559
1560 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001561 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001562}
1563
Chris Yee14523a2020-12-19 13:46:00 -08001564TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1565 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1566 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1567 // Add two subdevices to device
1568 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1569 // Must add at least one mapper or the device will be ignored!
1570 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1571 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1572
1573 // Push same device instance for next device to be added, so they'll have same identifier.
1574 mReader->pushNextDevice(device);
1575 mReader->pushNextDevice(device);
1576 // Sensor device is initially disabled
1577 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1578 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1579 nullptr));
1580 // Device is disabled because the only sub device is a sensor device and disabled initially.
1581 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1582 ASSERT_FALSE(device->isEnabled());
1583 ASSERT_NO_FATAL_FAILURE(
1584 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1585 // The merged device is enabled if any sub device is enabled
1586 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1587 ASSERT_TRUE(device->isEnabled());
1588}
1589
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001590TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001592 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001593 constexpr int32_t eventHubId = 1;
1594 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001595 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001596 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001597 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001598 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001599
Yi Kong9b14ac62018-07-17 13:48:38 -07001600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001601
1602 NotifyDeviceResetArgs resetArgs;
1603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001604 ASSERT_EQ(deviceId, resetArgs.deviceId);
1605
1606 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001607 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001608 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001609
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001611 ASSERT_EQ(deviceId, resetArgs.deviceId);
1612 ASSERT_EQ(device->isEnabled(), false);
1613
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001614 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001615 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618 ASSERT_EQ(device->isEnabled(), false);
1619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001620 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001621 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 ASSERT_EQ(deviceId, resetArgs.deviceId);
1624 ASSERT_EQ(device->isEnabled(), true);
1625}
1626
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001628 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001629 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001631 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001633 AINPUT_SOURCE_KEYBOARD, nullptr);
1634 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635
1636 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1637 AINPUT_SOURCE_ANY, AKEYCODE_A))
1638 << "Should return unknown when the device id is >= 0 but unknown.";
1639
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001640 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1641 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1642 << "Should return unknown when the device id is valid but the sources are not "
1643 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 ASSERT_EQ(AKEY_STATE_DOWN,
1646 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1647 AKEYCODE_A))
1648 << "Should return value provided by mapper when device id is valid and the device "
1649 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650
1651 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1652 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1653 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1654
1655 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1656 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1657 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1658}
1659
1660TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001661 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001662 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001664 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001665 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001666 AINPUT_SOURCE_KEYBOARD, nullptr);
1667 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001668
1669 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1670 AINPUT_SOURCE_ANY, KEY_A))
1671 << "Should return unknown when the device id is >= 0 but unknown.";
1672
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001673 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1674 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1675 << "Should return unknown when the device id is valid but the sources are not "
1676 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001677
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 ASSERT_EQ(AKEY_STATE_DOWN,
1679 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1680 KEY_A))
1681 << "Should return value provided by mapper when device id is valid and the device "
1682 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683
1684 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1685 AINPUT_SOURCE_TRACKBALL, KEY_A))
1686 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1687
1688 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1689 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1690 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1691}
1692
1693TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001694 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001695 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001697 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001698 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001699 AINPUT_SOURCE_KEYBOARD, nullptr);
1700 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001701
1702 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1703 AINPUT_SOURCE_ANY, SW_LID))
1704 << "Should return unknown when the device id is >= 0 but unknown.";
1705
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1707 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1708 << "Should return unknown when the device id is valid but the sources are not "
1709 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001710
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 ASSERT_EQ(AKEY_STATE_DOWN,
1712 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1713 SW_LID))
1714 << "Should return value provided by mapper when device id is valid and the device "
1715 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001716
1717 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1718 AINPUT_SOURCE_TRACKBALL, SW_LID))
1719 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1720
1721 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1722 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1723 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1724}
1725
1726TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001728 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001729 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001732 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001734 mapper.addSupportedKeyCode(AKEYCODE_A);
1735 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001736
1737 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1738 uint8_t flags[4] = { 0, 0, 0, 1 };
1739
1740 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1741 << "Should return false when device id is >= 0 but unknown.";
1742 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1743
1744 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001745 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1746 << "Should return false when device id is valid but the sources are not supported by "
1747 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001748 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1749
1750 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001751 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1752 keyCodes, flags))
1753 << "Should return value provided by mapper when device id is valid and the device "
1754 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001755 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1756
1757 flags[3] = 1;
1758 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1759 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1760 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1761
1762 flags[3] = 1;
1763 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1764 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1765 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1766}
1767
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001768TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001769 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001770 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001771
1772 NotifyConfigurationChangedArgs args;
1773
1774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1775 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1776}
1777
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001778TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001780 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001781 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001783 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001784 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001785 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001786 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001787
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001788 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001789 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1791
1792 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001793 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001794 ASSERT_EQ(when, event.when);
1795 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001796 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001797 ASSERT_EQ(EV_KEY, event.type);
1798 ASSERT_EQ(KEY_A, event.code);
1799 ASSERT_EQ(1, event.value);
1800}
1801
Garfield Tan1c7bc862020-01-28 13:24:04 -08001802TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001804 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001805 constexpr int32_t eventHubId = 1;
1806 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001807 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001809 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001811
1812 NotifyDeviceResetArgs resetArgs;
1813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001814 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001816 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001817 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001819 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001820 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001821
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001822 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001823 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001825 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001826 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001828 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001829 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001831 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001832 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001833}
1834
Garfield Tan1c7bc862020-01-28 13:24:04 -08001835TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1836 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001837 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001838 constexpr int32_t eventHubId = 1;
1839 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1840 // Must add at least one mapper or the device will be ignored!
1841 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001842 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001843 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1844
1845 NotifyDeviceResetArgs resetArgs;
1846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1847 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1848}
1849
Arthur Hungc23540e2018-11-29 20:42:11 +08001850TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001852 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001853 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001854 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001855 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1856 FakeInputMapper& mapper =
1857 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001858 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001859
1860 const uint8_t hdmi1 = 1;
1861
1862 // Associated touch screen with second display.
1863 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1864
1865 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001866 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001867 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001868 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001869 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001870 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001871 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001872 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001873 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001874 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001875
1876 // Add the device, and make sure all of the callbacks are triggered.
1877 // The device is added after the input port associations are processed since
1878 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001879 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001882 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001883
Arthur Hung2c9a3342019-07-23 14:18:59 +08001884 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001885 ASSERT_EQ(deviceId, device->getId());
1886 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1887 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001888
1889 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001890 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001891 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001892 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001893}
1894
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001895TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1896 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1897 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1898 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1899 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1900 // Must add at least one mapper or the device will be ignored!
1901 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1902 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1903 mReader->pushNextDevice(device);
1904 mReader->pushNextDevice(device);
1905 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1906 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1907
1908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1909
1910 NotifyDeviceResetArgs resetArgs;
1911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1912 ASSERT_EQ(deviceId, resetArgs.deviceId);
1913 ASSERT_TRUE(device->isEnabled());
1914 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1915 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1916
1917 disableDevice(deviceId);
1918 mReader->loopOnce();
1919
1920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1921 ASSERT_EQ(deviceId, resetArgs.deviceId);
1922 ASSERT_FALSE(device->isEnabled());
1923 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1924 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1925
1926 enableDevice(deviceId);
1927 mReader->loopOnce();
1928
1929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1930 ASSERT_EQ(deviceId, resetArgs.deviceId);
1931 ASSERT_TRUE(device->isEnabled());
1932 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1933 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1934}
1935
1936TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1937 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1938 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1939 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1940 // Add two subdevices to device
1941 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1942 FakeInputMapper& mapperDevice1 =
1943 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1944 FakeInputMapper& mapperDevice2 =
1945 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1946 mReader->pushNextDevice(device);
1947 mReader->pushNextDevice(device);
1948 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1949 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1950
1951 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1952 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1953
1954 ASSERT_EQ(AKEY_STATE_DOWN,
1955 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1956 ASSERT_EQ(AKEY_STATE_DOWN,
1957 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1958 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1959 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1960}
1961
Prabir Pradhan7e186182020-11-10 13:56:45 -08001962TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1963 NotifyPointerCaptureChangedArgs args;
1964
1965 mFakePolicy->setPointerCapture(true);
1966 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1967 mReader->loopOnce();
1968 mFakeListener->assertNotifyCaptureWasCalled(&args);
1969 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1970
1971 mFakePolicy->setPointerCapture(false);
1972 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1973 mReader->loopOnce();
1974 mFakeListener->assertNotifyCaptureWasCalled(&args);
1975 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1976
1977 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1978 // does not change.
1979 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1980 mReader->loopOnce();
1981 mFakeListener->assertNotifyCaptureWasCalled(&args);
1982 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1983}
1984
Chris Ye87143712020-11-10 05:05:58 +00001985class FakeVibratorInputMapper : public FakeInputMapper {
1986public:
1987 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1988 : FakeInputMapper(deviceContext, sources) {}
1989
1990 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1991};
1992
1993TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1994 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1995 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1996 constexpr int32_t eventHubId = 1;
1997 const char* DEVICE_LOCATION = "BLUETOOTH";
1998 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1999 FakeVibratorInputMapper& mapper =
2000 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2001 mReader->pushNextDevice(device);
2002
2003 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2004 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2005
2006 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2007 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2008}
2009
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002010// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002011
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002012class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002013public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002014 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002015
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002016 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002017
2018 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2019
2020 void dump(std::string& dump) override {}
2021
2022 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2023 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002024 }
2025
Chris Yee2b1e5c2021-03-10 22:45:12 -08002026 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2027 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002028 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002029
2030 bool setLightColor(int32_t lightId, int32_t color) override {
2031 getDeviceContext().setLightBrightness(lightId, color >> 24);
2032 return true;
2033 }
2034
2035 std::optional<int32_t> getLightColor(int32_t lightId) override {
2036 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2037 if (!result.has_value()) {
2038 return std::nullopt;
2039 }
2040 return result.value() << 24;
2041 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002042
2043 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2044
2045 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2046
2047private:
2048 InputDeviceContext& mDeviceContext;
2049 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2050 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002051};
2052
Chris Yee2b1e5c2021-03-10 22:45:12 -08002053TEST_F(InputReaderTest, BatteryGetCapacity) {
2054 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2055 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2056 constexpr int32_t eventHubId = 1;
2057 const char* DEVICE_LOCATION = "BLUETOOTH";
2058 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002059 FakePeripheralController& controller =
2060 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002061 mReader->pushNextDevice(device);
2062
2063 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2064
2065 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2066 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2067}
2068
2069TEST_F(InputReaderTest, BatteryGetStatus) {
2070 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2071 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2072 constexpr int32_t eventHubId = 1;
2073 const char* DEVICE_LOCATION = "BLUETOOTH";
2074 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002075 FakePeripheralController& controller =
2076 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002077 mReader->pushNextDevice(device);
2078
2079 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2080
2081 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2082 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2083}
2084
Chris Ye3fdbfef2021-01-06 18:45:18 -08002085TEST_F(InputReaderTest, LightGetColor) {
2086 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2087 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2088 constexpr int32_t eventHubId = 1;
2089 const char* DEVICE_LOCATION = "BLUETOOTH";
2090 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002091 FakePeripheralController& controller =
2092 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002093 mReader->pushNextDevice(device);
2094 RawLightInfo info = {.id = 1,
2095 .name = "Mono",
2096 .maxBrightness = 255,
2097 .flags = InputLightClass::BRIGHTNESS,
2098 .path = ""};
2099 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2100 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2101
2102 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002103
Chris Yee2b1e5c2021-03-10 22:45:12 -08002104 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2105 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002106 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2107 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2108}
2109
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002110// --- InputReaderIntegrationTest ---
2111
2112// These tests create and interact with the InputReader only through its interface.
2113// The InputReader is started during SetUp(), which starts its processing in its own
2114// thread. The tests use linux uinput to emulate input devices.
2115// NOTE: Interacting with the physical device while these tests are running may cause
2116// the tests to fail.
2117class InputReaderIntegrationTest : public testing::Test {
2118protected:
2119 sp<TestInputListener> mTestListener;
2120 sp<FakeInputReaderPolicy> mFakePolicy;
2121 sp<InputReaderInterface> mReader;
2122
Chris Yea52ade12020-08-27 16:49:20 -07002123 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002124 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002125 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2126 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002127
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002128 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002129 ASSERT_EQ(mReader->start(), OK);
2130
2131 // Since this test is run on a real device, all the input devices connected
2132 // to the test device will show up in mReader. We wait for those input devices to
2133 // show up before beginning the tests.
2134 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2136 }
2137
Chris Yea52ade12020-08-27 16:49:20 -07002138 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002139 ASSERT_EQ(mReader->stop(), OK);
2140 mTestListener.clear();
2141 mFakePolicy.clear();
2142 }
2143};
2144
2145TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2146 // An invalid input device that is only used for this test.
2147 class InvalidUinputDevice : public UinputDevice {
2148 public:
2149 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2150
2151 private:
2152 void configureDevice(int fd, uinput_user_dev* device) override {}
2153 };
2154
2155 const size_t numDevices = mFakePolicy->getInputDevices().size();
2156
2157 // UinputDevice does not set any event or key bits, so InputReader should not
2158 // consider it as a valid device.
2159 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2160 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2161 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2162 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2163
2164 invalidDevice.reset();
2165 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2166 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2167 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2168}
2169
2170TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2171 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2172
2173 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2174 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2175 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2176 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2177
2178 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002179 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002180 const auto& it =
2181 std::find_if(inputDevices.begin(), inputDevices.end(),
2182 [&keyboard](const InputDeviceInfo& info) {
2183 return info.getIdentifier().name == keyboard->getName();
2184 });
2185
2186 ASSERT_NE(it, inputDevices.end());
2187 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2188 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2189 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002190
2191 keyboard.reset();
2192 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2193 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2194 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2195}
2196
2197TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2198 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2199 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2200
2201 NotifyConfigurationChangedArgs configChangedArgs;
2202 ASSERT_NO_FATAL_FAILURE(
2203 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002204 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002205 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2206
2207 NotifyKeyArgs keyArgs;
2208 keyboard->pressAndReleaseHomeKey();
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2210 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002211 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002212 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002213 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002214 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002215 prevTimestamp = keyArgs.eventTime;
2216
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2218 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002219 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002220 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002221 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002222}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002223
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002224/**
2225 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2226 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2227 * are passed to the listener.
2228 */
2229static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2230TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2231 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2232 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2233 NotifyKeyArgs keyArgs;
2234
2235 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2236 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2237 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2238 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2239
2240 controller->pressAndReleaseKey(BTN_GEAR_UP);
2241 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2242 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2243 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2244}
2245
Arthur Hungaab25622020-01-16 11:22:11 +08002246// --- TouchProcessTest ---
2247class TouchIntegrationTest : public InputReaderIntegrationTest {
2248protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002249 const std::string UNIQUE_ID = "local:0";
2250
Chris Yea52ade12020-08-27 16:49:20 -07002251 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002252 InputReaderIntegrationTest::SetUp();
2253 // At least add an internal display.
2254 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2255 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002256 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002257
2258 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2259 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2260 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2261 }
2262
2263 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2264 int32_t orientation, const std::string& uniqueId,
2265 std::optional<uint8_t> physicalPort,
2266 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002267 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2268 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002269 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2270 }
2271
2272 std::unique_ptr<UinputTouchScreen> mDevice;
2273};
2274
2275TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2276 NotifyMotionArgs args;
2277 const Point centerPoint = mDevice->getCenterPoint();
2278
2279 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002280 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002281 mDevice->sendDown(centerPoint);
2282 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2283 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2284
2285 // ACTION_MOVE
2286 mDevice->sendMove(centerPoint + Point(1, 1));
2287 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2289
2290 // ACTION_UP
2291 mDevice->sendUp();
2292 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2293 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2294}
2295
2296TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2297 NotifyMotionArgs args;
2298 const Point centerPoint = mDevice->getCenterPoint();
2299
2300 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002301 mDevice->sendSlot(FIRST_SLOT);
2302 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002303 mDevice->sendDown(centerPoint);
2304 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2305 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2306
2307 // ACTION_POINTER_DOWN (Second slot)
2308 const Point secondPoint = centerPoint + Point(100, 100);
2309 mDevice->sendSlot(SECOND_SLOT);
2310 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2311 mDevice->sendDown(secondPoint + Point(1, 1));
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2313 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2314 args.action);
2315
2316 // ACTION_MOVE (Second slot)
2317 mDevice->sendMove(secondPoint);
2318 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2320
2321 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002322 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002323 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002324 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002325 args.action);
2326
2327 // ACTION_UP
2328 mDevice->sendSlot(FIRST_SLOT);
2329 mDevice->sendUp();
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2331 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2332}
2333
2334TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2335 NotifyMotionArgs args;
2336 const Point centerPoint = mDevice->getCenterPoint();
2337
2338 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002339 mDevice->sendSlot(FIRST_SLOT);
2340 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002341 mDevice->sendDown(centerPoint);
2342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2344
arthurhungcc7f9802020-04-30 17:55:40 +08002345 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002346 const Point secondPoint = centerPoint + Point(100, 100);
2347 mDevice->sendSlot(SECOND_SLOT);
2348 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2349 mDevice->sendDown(secondPoint);
2350 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2351 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2352 args.action);
2353
arthurhungcc7f9802020-04-30 17:55:40 +08002354 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002355 mDevice->sendMove(secondPoint + Point(1, 1));
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2358
arthurhungcc7f9802020-04-30 17:55:40 +08002359 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2360 // a palm event.
2361 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002362 mDevice->sendToolType(MT_TOOL_PALM);
2363 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002364 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2365 args.action);
2366 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002367
arthurhungcc7f9802020-04-30 17:55:40 +08002368 // Send up to second slot, expect first slot send moving.
2369 mDevice->sendPointerUp();
2370 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002372
arthurhungcc7f9802020-04-30 17:55:40 +08002373 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002374 mDevice->sendSlot(FIRST_SLOT);
2375 mDevice->sendUp();
2376
arthurhungcc7f9802020-04-30 17:55:40 +08002377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2378 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002379}
2380
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382class InputDeviceTest : public testing::Test {
2383protected:
2384 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002385 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386 static const int32_t DEVICE_ID;
2387 static const int32_t DEVICE_GENERATION;
2388 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002389 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002390 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002392 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002394 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002395 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002396 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397
Chris Yea52ade12020-08-27 16:49:20 -07002398 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002399 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002401 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002402 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2403 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404 InputDeviceIdentifier identifier;
2405 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002406 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002407 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002408 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002409 mReader->pushNextDevice(mDevice);
2410 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2411 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412 }
2413
Chris Yea52ade12020-08-27 16:49:20 -07002414 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415 mFakeListener.clear();
2416 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417 }
2418};
2419
2420const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002421const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002422const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2424const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002425const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2426 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002427const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428
2429TEST_F(InputDeviceTest, ImmutableProperties) {
2430 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002431 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002432 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433}
2434
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002435TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2436 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002437}
2438
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2440 // Configuration.
2441 InputReaderConfiguration config;
2442 mDevice->configure(ARBITRARY_TIME, &config, 0);
2443
2444 // Reset.
2445 mDevice->reset(ARBITRARY_TIME);
2446
2447 NotifyDeviceResetArgs resetArgs;
2448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2449 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2450 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2451
2452 // Metadata.
2453 ASSERT_TRUE(mDevice->isIgnored());
2454 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2455
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002456 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002458 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2460 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2461
2462 // State queries.
2463 ASSERT_EQ(0, mDevice->getMetaState());
2464
2465 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2466 << "Ignored device should return unknown key code state.";
2467 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2468 << "Ignored device should return unknown scan code state.";
2469 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2470 << "Ignored device should return unknown switch state.";
2471
2472 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2473 uint8_t flags[2] = { 0, 1 };
2474 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2475 << "Ignored device should never mark any key codes.";
2476 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2477 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2478}
2479
2480TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2481 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002482 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002484 FakeInputMapper& mapper1 =
2485 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002486 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2487 mapper1.setMetaState(AMETA_ALT_ON);
2488 mapper1.addSupportedKeyCode(AKEYCODE_A);
2489 mapper1.addSupportedKeyCode(AKEYCODE_B);
2490 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2491 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2492 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2493 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2494 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002496 FakeInputMapper& mapper2 =
2497 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002498 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499
2500 InputReaderConfiguration config;
2501 mDevice->configure(ARBITRARY_TIME, &config, 0);
2502
2503 String8 propertyValue;
2504 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2505 << "Device should have read configuration during configuration phase.";
2506 ASSERT_STREQ("value", propertyValue.string());
2507
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002508 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2509 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002510
2511 // Reset
2512 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002513 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2514 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515
2516 NotifyDeviceResetArgs resetArgs;
2517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2518 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2519 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2520
2521 // Metadata.
2522 ASSERT_FALSE(mDevice->isIgnored());
2523 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2524
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002525 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002527 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2529 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2530
2531 // State queries.
2532 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2533 << "Should query mappers and combine meta states.";
2534
2535 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2536 << "Should return unknown key code state when source not supported.";
2537 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2538 << "Should return unknown scan code state when source not supported.";
2539 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2540 << "Should return unknown switch state when source not supported.";
2541
2542 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2543 << "Should query mapper when source is supported.";
2544 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2545 << "Should query mapper when source is supported.";
2546 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2547 << "Should query mapper when source is supported.";
2548
2549 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2550 uint8_t flags[4] = { 0, 0, 0, 1 };
2551 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2552 << "Should do nothing when source is unsupported.";
2553 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2554 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2555 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2556 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2557
2558 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2559 << "Should query mapper when source is supported.";
2560 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2561 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2562 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2563 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2564
2565 // Event handling.
2566 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002567 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568 mDevice->process(&event, 1);
2569
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002570 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2571 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572}
2573
Arthur Hung2c9a3342019-07-23 14:18:59 +08002574// A single input device is associated with a specific display. Check that:
2575// 1. Device is disabled if the viewport corresponding to the associated display is not found
2576// 2. Device is disabled when setEnabled API is called
2577TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002578 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002579
2580 // First Configuration.
2581 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2582
2583 // Device should be enabled by default.
2584 ASSERT_TRUE(mDevice->isEnabled());
2585
2586 // Prepare associated info.
2587 constexpr uint8_t hdmi = 1;
2588 const std::string UNIQUE_ID = "local:1";
2589
2590 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2591 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2592 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2593 // Device should be disabled because it is associated with a specific display via
2594 // input port <-> display port association, but the corresponding display is not found
2595 ASSERT_FALSE(mDevice->isEnabled());
2596
2597 // Prepare displays.
2598 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002599 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2600 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002601 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2602 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2603 ASSERT_TRUE(mDevice->isEnabled());
2604
2605 // Device should be disabled after set disable.
2606 mFakePolicy->addDisabledDevice(mDevice->getId());
2607 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2608 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2609 ASSERT_FALSE(mDevice->isEnabled());
2610
2611 // Device should still be disabled even found the associated display.
2612 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2613 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2614 ASSERT_FALSE(mDevice->isEnabled());
2615}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616
Christine Franks1ba71cc2021-04-07 14:37:42 -07002617TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2618 // Device should be enabled by default.
2619 mFakePolicy->clearViewports();
2620 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2621 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2622 ASSERT_TRUE(mDevice->isEnabled());
2623
2624 // Device should be disabled because it is associated with a specific display, but the
2625 // corresponding display is not found.
2626 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2627 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2628 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2629 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2630 ASSERT_FALSE(mDevice->isEnabled());
2631
2632 // Device should be enabled when a display is found.
2633 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2634 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2635 NO_PORT, ViewportType::INTERNAL);
2636 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2637 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2638 ASSERT_TRUE(mDevice->isEnabled());
2639
2640 // Device should be disabled after set disable.
2641 mFakePolicy->addDisabledDevice(mDevice->getId());
2642 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2643 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2644 ASSERT_FALSE(mDevice->isEnabled());
2645
2646 // Device should still be disabled even found the associated display.
2647 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2648 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2649 ASSERT_FALSE(mDevice->isEnabled());
2650}
2651
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652// --- InputMapperTest ---
2653
2654class InputMapperTest : public testing::Test {
2655protected:
2656 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002657 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658 static const int32_t DEVICE_ID;
2659 static const int32_t DEVICE_GENERATION;
2660 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002661 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002662 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002664 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002666 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 std::unique_ptr<InstrumentedInputReader> mReader;
2668 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669
Chris Ye1b0c7342020-07-28 21:57:03 -07002670 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002671 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002673 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002674 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2675 mFakeListener);
2676 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677 }
2678
Chris Yea52ade12020-08-27 16:49:20 -07002679 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002680
Chris Yea52ade12020-08-27 16:49:20 -07002681 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682 mFakeListener.clear();
2683 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 }
2685
2686 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002687 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688 }
2689
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002690 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002691 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002692 mReader->requestRefreshConfiguration(changes);
2693 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002694 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002695 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2696 }
2697
arthurhungdcef2dc2020-08-11 14:47:50 +08002698 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2699 const std::string& location, int32_t eventHubId,
2700 Flags<InputDeviceClass> classes) {
2701 InputDeviceIdentifier identifier;
2702 identifier.name = name;
2703 identifier.location = location;
2704 std::shared_ptr<InputDevice> device =
2705 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2706 identifier);
2707 mReader->pushNextDevice(device);
2708 mFakeEventHub->addDevice(eventHubId, name, classes);
2709 mReader->loopOnce();
2710 return device;
2711 }
2712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002713 template <class T, typename... Args>
2714 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002715 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002716 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002718 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002719 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720 }
2721
2722 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002723 int32_t orientation, const std::string& uniqueId,
2724 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002725 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2726 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002727 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2728 }
2729
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002730 void clearViewports() {
2731 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732 }
2733
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002734 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2735 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002736 RawEvent event;
2737 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002738 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002739 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002740 event.type = type;
2741 event.code = code;
2742 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002743 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002744 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002745 }
2746
2747 static void assertMotionRange(const InputDeviceInfo& info,
2748 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2749 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002750 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2752 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2753 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2754 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2755 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2756 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2757 }
2758
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002759 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2760 float size, float touchMajor, float touchMinor, float toolMajor,
2761 float toolMinor, float orientation, float distance,
2762 float scaledAxisEpsilon = 1.f) {
2763 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2764 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2766 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002767 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2768 scaledAxisEpsilon);
2769 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2770 scaledAxisEpsilon);
2771 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2772 scaledAxisEpsilon);
2773 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2774 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2776 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2777 }
2778
Michael Wright17db18e2020-06-26 20:51:44 +01002779 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002781 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_NEAR(x, actualX, 1);
2783 ASSERT_NEAR(y, actualY, 1);
2784 }
2785};
2786
2787const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002788const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002789const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2791const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002792const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2793 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002794const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795
2796// --- SwitchInputMapperTest ---
2797
2798class SwitchInputMapperTest : public InputMapperTest {
2799protected:
2800};
2801
2802TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002803 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806}
2807
2808TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002809 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002811 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002812 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002814 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002815 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816}
2817
2818TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825
2826 NotifySwitchArgs args;
2827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2828 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002829 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2830 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831 args.switchMask);
2832 ASSERT_EQ(uint32_t(0), args.policyFlags);
2833}
2834
Chris Ye87143712020-11-10 05:05:58 +00002835// --- VibratorInputMapperTest ---
2836class VibratorInputMapperTest : public InputMapperTest {
2837protected:
2838 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2839};
2840
2841TEST_F(VibratorInputMapperTest, GetSources) {
2842 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2843
2844 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2845}
2846
2847TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2848 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2849
2850 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2851}
2852
2853TEST_F(VibratorInputMapperTest, Vibrate) {
2854 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002855 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002856 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2857
2858 VibrationElement pattern(2);
2859 VibrationSequence sequence(2);
2860 pattern.duration = std::chrono::milliseconds(200);
2861 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2862 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2863 sequence.addElement(pattern);
2864 pattern.duration = std::chrono::milliseconds(500);
2865 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2866 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2867 sequence.addElement(pattern);
2868
2869 std::vector<int64_t> timings = {0, 1};
2870 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2871
2872 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002873 // Start vibrating
2874 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002875 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002876 // Verify vibrator state listener was notified.
2877 mReader->loopOnce();
2878 NotifyVibratorStateArgs args;
2879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2880 ASSERT_EQ(DEVICE_ID, args.deviceId);
2881 ASSERT_TRUE(args.isOn);
2882 // Stop vibrating
2883 mapper.cancelVibrate(VIBRATION_TOKEN);
2884 ASSERT_FALSE(mapper.isVibrating());
2885 // Verify vibrator state listener was notified.
2886 mReader->loopOnce();
2887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2888 ASSERT_EQ(DEVICE_ID, args.deviceId);
2889 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002890}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891
Chris Yef59a2f42020-10-16 12:55:26 -07002892// --- SensorInputMapperTest ---
2893
2894class SensorInputMapperTest : public InputMapperTest {
2895protected:
2896 static const int32_t ACCEL_RAW_MIN;
2897 static const int32_t ACCEL_RAW_MAX;
2898 static const int32_t ACCEL_RAW_FUZZ;
2899 static const int32_t ACCEL_RAW_FLAT;
2900 static const int32_t ACCEL_RAW_RESOLUTION;
2901
2902 static const int32_t GYRO_RAW_MIN;
2903 static const int32_t GYRO_RAW_MAX;
2904 static const int32_t GYRO_RAW_FUZZ;
2905 static const int32_t GYRO_RAW_FLAT;
2906 static const int32_t GYRO_RAW_RESOLUTION;
2907
2908 static const float GRAVITY_MS2_UNIT;
2909 static const float DEGREE_RADIAN_UNIT;
2910
2911 void prepareAccelAxes();
2912 void prepareGyroAxes();
2913 void setAccelProperties();
2914 void setGyroProperties();
2915 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2916};
2917
2918const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2919const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2920const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2921const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2922const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2923
2924const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2925const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2926const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2927const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2928const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2929
2930const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2931const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2932
2933void SensorInputMapperTest::prepareAccelAxes() {
2934 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2935 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2936 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2937 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2939 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2940}
2941
2942void SensorInputMapperTest::prepareGyroAxes() {
2943 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2944 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2945 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2946 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2947 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2948 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2949}
2950
2951void SensorInputMapperTest::setAccelProperties() {
2952 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2953 /* sensorDataIndex */ 0);
2954 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2955 /* sensorDataIndex */ 1);
2956 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2957 /* sensorDataIndex */ 2);
2958 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2959 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2960 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2961 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2962 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2963}
2964
2965void SensorInputMapperTest::setGyroProperties() {
2966 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2967 /* sensorDataIndex */ 0);
2968 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2969 /* sensorDataIndex */ 1);
2970 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2971 /* sensorDataIndex */ 2);
2972 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2973 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2974 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2975 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2976 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2977}
2978
2979TEST_F(SensorInputMapperTest, GetSources) {
2980 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2981
2982 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2983}
2984
2985TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2986 setAccelProperties();
2987 prepareAccelAxes();
2988 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2989
2990 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2991 std::chrono::microseconds(10000),
2992 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002993 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002999
3000 NotifySensorArgs args;
3001 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3002 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3003 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3004
3005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3006 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3007 ASSERT_EQ(args.deviceId, DEVICE_ID);
3008 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3009 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3010 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3011 ASSERT_EQ(args.values, values);
3012 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3013}
3014
3015TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3016 setGyroProperties();
3017 prepareGyroAxes();
3018 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3019
3020 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3021 std::chrono::microseconds(10000),
3022 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003023 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003029
3030 NotifySensorArgs args;
3031 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3032 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3033 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3034
3035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3036 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3037 ASSERT_EQ(args.deviceId, DEVICE_ID);
3038 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3039 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3040 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3041 ASSERT_EQ(args.values, values);
3042 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3043}
3044
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045// --- KeyboardInputMapperTest ---
3046
3047class KeyboardInputMapperTest : public InputMapperTest {
3048protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003049 const std::string UNIQUE_ID = "local:0";
3050
3051 void prepareDisplay(int32_t orientation);
3052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003053 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003054 int32_t originalKeyCode, int32_t rotatedKeyCode,
3055 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056};
3057
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003058/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3059 * orientation.
3060 */
3061void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003062 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3063 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003064}
3065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003066void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003067 int32_t originalScanCode, int32_t originalKeyCode,
3068 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 NotifyKeyArgs args;
3070
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3073 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3074 ASSERT_EQ(originalScanCode, args.scanCode);
3075 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003076 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3080 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3081 ASSERT_EQ(originalScanCode, args.scanCode);
3082 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003083 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084}
3085
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 KeyboardInputMapper& mapper =
3088 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3089 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
3094TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3095 const int32_t USAGE_A = 0x070004;
3096 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3098 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003099 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3100 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3101 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003103 KeyboardInputMapper& mapper =
3104 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3105 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003106 // Initial metastate to AMETA_NONE.
3107 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3108 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109
3110 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 NotifyKeyArgs args;
3113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3114 ASSERT_EQ(DEVICE_ID, args.deviceId);
3115 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3116 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3117 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3118 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3119 ASSERT_EQ(KEY_HOME, args.scanCode);
3120 ASSERT_EQ(AMETA_NONE, args.metaState);
3121 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3122 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3123 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3124
3125 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003126 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3128 ASSERT_EQ(DEVICE_ID, args.deviceId);
3129 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3130 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3131 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3132 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3133 ASSERT_EQ(KEY_HOME, args.scanCode);
3134 ASSERT_EQ(AMETA_NONE, args.metaState);
3135 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3136 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3137 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3138
3139 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3143 ASSERT_EQ(DEVICE_ID, args.deviceId);
3144 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3145 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3146 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3147 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3148 ASSERT_EQ(0, args.scanCode);
3149 ASSERT_EQ(AMETA_NONE, args.metaState);
3150 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3151 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3152 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3153
3154 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3156 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3158 ASSERT_EQ(DEVICE_ID, args.deviceId);
3159 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3160 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3161 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3162 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3163 ASSERT_EQ(0, args.scanCode);
3164 ASSERT_EQ(AMETA_NONE, args.metaState);
3165 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3166 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3167 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3168
3169 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3173 ASSERT_EQ(DEVICE_ID, args.deviceId);
3174 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3175 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3177 ASSERT_EQ(0, args.keyCode);
3178 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3179 ASSERT_EQ(AMETA_NONE, args.metaState);
3180 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3181 ASSERT_EQ(0U, args.policyFlags);
3182 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3183
3184 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3186 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3188 ASSERT_EQ(DEVICE_ID, args.deviceId);
3189 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3190 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3191 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3192 ASSERT_EQ(0, args.keyCode);
3193 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3194 ASSERT_EQ(AMETA_NONE, args.metaState);
3195 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3196 ASSERT_EQ(0U, args.policyFlags);
3197 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3198}
3199
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003200/**
3201 * Ensure that the readTime is set to the time when the EV_KEY is received.
3202 */
3203TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3204 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3205
3206 KeyboardInputMapper& mapper =
3207 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3208 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3209 NotifyKeyArgs args;
3210
3211 // Key down
3212 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3214 ASSERT_EQ(12, args.readTime);
3215
3216 // Key up
3217 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3219 ASSERT_EQ(15, args.readTime);
3220}
3221
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003223 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3224 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003225 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3226 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3227 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003229 KeyboardInputMapper& mapper =
3230 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3231 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232
arthurhungc903df12020-08-11 15:08:42 +08003233 // Initial metastate to AMETA_NONE.
3234 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3235 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236
3237 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239 NotifyKeyArgs args;
3240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3241 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003242 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003243 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244
3245 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003246 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250
3251 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003252 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003255 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256
3257 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003258 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3260 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003261 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003262 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263}
3264
3265TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003266 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3267 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3269 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003271 KeyboardInputMapper& mapper =
3272 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3273 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003275 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3277 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3279 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3280 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3281 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3282 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3283 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3284}
3285
3286TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3288 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 KeyboardInputMapper& mapper =
3294 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3295 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003297 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003298 ASSERT_NO_FATAL_FAILURE(
3299 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3300 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3301 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3302 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3303 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3305 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003307 clearViewports();
3308 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003309 ASSERT_NO_FATAL_FAILURE(
3310 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3311 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3312 AKEYCODE_DPAD_UP, DISPLAY_ID));
3313 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3314 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3315 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3316 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003318 clearViewports();
3319 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003320 ASSERT_NO_FATAL_FAILURE(
3321 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3322 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3323 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3324 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3325 AKEYCODE_DPAD_UP, DISPLAY_ID));
3326 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3327 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003329 clearViewports();
3330 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003331 ASSERT_NO_FATAL_FAILURE(
3332 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3333 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3334 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3335 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3336 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3337 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3338 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339
3340 // Special case: if orientation changes while key is down, we still emit the same keycode
3341 // in the key up as we did in the key down.
3342 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003343 clearViewports();
3344 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3348 ASSERT_EQ(KEY_UP, args.scanCode);
3349 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3350
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003351 clearViewports();
3352 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3355 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3356 ASSERT_EQ(KEY_UP, args.scanCode);
3357 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3358}
3359
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003360TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3361 // If the keyboard is not orientation aware,
3362 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003363 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003365 KeyboardInputMapper& mapper =
3366 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3367 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003368 NotifyKeyArgs args;
3369
3370 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3375 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3376
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003377 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3382 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3383}
3384
3385TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3386 // If the keyboard is orientation aware,
3387 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003388 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003389
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003390 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003391 KeyboardInputMapper& mapper =
3392 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3393 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003394 NotifyKeyArgs args;
3395
3396 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3397 // ^--- already checked by the previous test
3398
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003399 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003400 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3405 ASSERT_EQ(DISPLAY_ID, args.displayId);
3406
3407 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003408 clearViewports();
3409 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003410 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3415 ASSERT_EQ(newDisplayId, args.displayId);
3416}
3417
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 KeyboardInputMapper& mapper =
3420 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3421 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003423 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003424 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003426 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003427 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428}
3429
3430TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003431 KeyboardInputMapper& mapper =
3432 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3433 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003435 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003438 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003439 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440}
3441
3442TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003443 KeyboardInputMapper& mapper =
3444 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3445 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003447 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
3449 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3450 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003451 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452 ASSERT_TRUE(flags[0]);
3453 ASSERT_FALSE(flags[1]);
3454}
3455
3456TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003457 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3458 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3459 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3460 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3461 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3462 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003464 KeyboardInputMapper& mapper =
3465 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3466 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003467 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003468 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3469 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470
3471 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003472 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3473 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3474 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475
3476 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003479 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3480 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3481 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
3484 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003487 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3488 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3489 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491
3492 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003495 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3496 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
3500 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003503 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3504 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3505 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003506 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
3508 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003511 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3512 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3513 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003514 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515
3516 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003519 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3520 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523}
3524
Chris Yea52ade12020-08-27 16:49:20 -07003525TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3526 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3527 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3528 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3529 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3530
3531 KeyboardInputMapper& mapper =
3532 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3533 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3534
3535 // Initial metastate should be AMETA_NONE as no meta keys added.
3536 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3537 // Meta state should be AMETA_NONE after reset
3538 mapper.reset(ARBITRARY_TIME);
3539 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3540 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3541 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3542 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3543
3544 NotifyKeyArgs args;
3545 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3548 ASSERT_EQ(AMETA_NONE, args.metaState);
3549 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3550 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3551 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3552
3553 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003554 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3556 ASSERT_EQ(AMETA_NONE, args.metaState);
3557 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3558 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3559 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3560}
3561
Arthur Hung2c9a3342019-07-23 14:18:59 +08003562TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3563 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3565 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3566 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3567 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003568
3569 // keyboard 2.
3570 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003571 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003572 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003573 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003574 std::shared_ptr<InputDevice> device2 =
3575 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3576 Flags<InputDeviceClass>(0));
3577
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003578 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3579 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3580 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3581 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 KeyboardInputMapper& mapper =
3584 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3585 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003587 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003588 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003589 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3591 device2->reset(ARBITRARY_TIME);
3592
3593 // Prepared displays and associated info.
3594 constexpr uint8_t hdmi1 = 0;
3595 constexpr uint8_t hdmi2 = 1;
3596 const std::string SECONDARY_UNIQUE_ID = "local:1";
3597
3598 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3599 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3600
3601 // No associated display viewport found, should disable the device.
3602 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3603 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3604 ASSERT_FALSE(device2->isEnabled());
3605
3606 // Prepare second display.
3607 constexpr int32_t newDisplayId = 2;
3608 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003609 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003610 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003611 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003612 // Default device will reconfigure above, need additional reconfiguration for another device.
3613 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3614 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3615
3616 // Device should be enabled after the associated display is found.
3617 ASSERT_TRUE(mDevice->isEnabled());
3618 ASSERT_TRUE(device2->isEnabled());
3619
3620 // Test pad key events
3621 ASSERT_NO_FATAL_FAILURE(
3622 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3624 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3626 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3627 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3628 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3629
3630 ASSERT_NO_FATAL_FAILURE(
3631 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3633 AKEYCODE_DPAD_RIGHT, newDisplayId));
3634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3635 AKEYCODE_DPAD_DOWN, newDisplayId));
3636 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3637 AKEYCODE_DPAD_LEFT, newDisplayId));
3638}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639
arthurhungc903df12020-08-11 15:08:42 +08003640TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3641 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3642 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3643 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3644 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3645 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3646 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3647
3648 KeyboardInputMapper& mapper =
3649 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3650 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3651 // Initial metastate to AMETA_NONE.
3652 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3653 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3654
3655 // Initialization should have turned all of the lights off.
3656 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3657 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3658 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3659
3660 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003663 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3664 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3665
3666 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003669 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3670 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3671
3672 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003675 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3676 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3677
3678 mFakeEventHub->removeDevice(EVENTHUB_ID);
3679 mReader->loopOnce();
3680
3681 // keyboard 2 should default toggle keys.
3682 const std::string USB2 = "USB2";
3683 const std::string DEVICE_NAME2 = "KEYBOARD2";
3684 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3685 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3686 std::shared_ptr<InputDevice> device2 =
3687 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3688 Flags<InputDeviceClass>(0));
3689 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3690 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3691 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3692 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3693 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3694 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3695
arthurhung6fe95782020-10-05 22:41:16 +08003696 KeyboardInputMapper& mapper2 =
3697 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3698 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003699 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3700 device2->reset(ARBITRARY_TIME);
3701
3702 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3703 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3704 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003705 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3706 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003707}
3708
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003709// --- KeyboardInputMapperTest_ExternalDevice ---
3710
3711class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3712protected:
Chris Yea52ade12020-08-27 16:49:20 -07003713 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003714};
3715
3716TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003717 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3718 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003719
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003720 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3722 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3723 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003724
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 KeyboardInputMapper& mapper =
3726 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3727 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003728
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003730 NotifyKeyArgs args;
3731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3732 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3733
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003734 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3736 ASSERT_EQ(uint32_t(0), args.policyFlags);
3737
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3740 ASSERT_EQ(uint32_t(0), args.policyFlags);
3741
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003742 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3744 ASSERT_EQ(uint32_t(0), args.policyFlags);
3745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3748 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3749
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003750 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3752 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3753}
3754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003755TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003756 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003761
Powei Fengd041c5d2019-05-03 17:11:33 -07003762 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003763 KeyboardInputMapper& mapper =
3764 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3765 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003766
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003768 NotifyKeyArgs args;
3769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3770 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3771
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3774 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3775
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3778 ASSERT_EQ(uint32_t(0), args.policyFlags);
3779
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3782 ASSERT_EQ(uint32_t(0), args.policyFlags);
3783
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3786 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3787
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003788 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3790 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3791}
3792
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793// --- CursorInputMapperTest ---
3794
3795class CursorInputMapperTest : public InputMapperTest {
3796protected:
3797 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3798
Michael Wright17db18e2020-06-26 20:51:44 +01003799 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800
Chris Yea52ade12020-08-27 16:49:20 -07003801 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802 InputMapperTest::SetUp();
3803
Michael Wright17db18e2020-06-26 20:51:44 +01003804 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003805 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 }
3807
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003808 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3809 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003810
3811 void prepareDisplay(int32_t orientation) {
3812 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003813 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003814 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3815 orientation, uniqueId, NO_PORT, viewportType);
3816 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003817
3818 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3819 float pressure) {
3820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3821 0.0f, 0.0f, 0.0f, EPSILON));
3822 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823};
3824
3825const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003827void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3828 int32_t originalY, int32_t rotatedX,
3829 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830 NotifyMotionArgs args;
3831
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003837 ASSERT_NO_FATAL_FAILURE(
3838 assertCursorPointerCoords(args.pointerCoords[0],
3839 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3840 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841}
3842
3843TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003844 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003845 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003847 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848}
3849
3850TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003852 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003854 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855}
3856
3857TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003859 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860
3861 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003862 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863
3864 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003865 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3866 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3868 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3869
3870 // When the bounds are set, then there should be a valid motion range.
3871 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3872
3873 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003874 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875
3876 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3877 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3878 1, 800 - 1, 0.0f, 0.0f));
3879 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3880 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3881 2, 480 - 1, 0.0f, 0.0f));
3882 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3883 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3884 0.0f, 1.0f, 0.0f, 0.0f));
3885}
3886
3887TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003889 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890
3891 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003892 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893
3894 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3895 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3896 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3897 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3898 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3899 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3900 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3901 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3902 0.0f, 1.0f, 0.0f, 0.0f));
3903}
3904
3905TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908
arthurhungdcef2dc2020-08-11 14:47:50 +08003909 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910
3911 NotifyMotionArgs args;
3912
3913 // Button press.
3914 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3918 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3919 ASSERT_EQ(DEVICE_ID, args.deviceId);
3920 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3921 ASSERT_EQ(uint32_t(0), args.policyFlags);
3922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3923 ASSERT_EQ(0, args.flags);
3924 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3925 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3926 ASSERT_EQ(0, args.edgeFlags);
3927 ASSERT_EQ(uint32_t(1), args.pointerCount);
3928 ASSERT_EQ(0, args.pointerProperties[0].id);
3929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003930 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3932 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3933 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3934
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3936 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3937 ASSERT_EQ(DEVICE_ID, args.deviceId);
3938 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3939 ASSERT_EQ(uint32_t(0), args.policyFlags);
3940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3941 ASSERT_EQ(0, args.flags);
3942 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3943 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3944 ASSERT_EQ(0, args.edgeFlags);
3945 ASSERT_EQ(uint32_t(1), args.pointerCount);
3946 ASSERT_EQ(0, args.pointerProperties[0].id);
3947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003948 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003949 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3950 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3951 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3952
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003954 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3955 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3957 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3958 ASSERT_EQ(DEVICE_ID, args.deviceId);
3959 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3960 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3962 ASSERT_EQ(0, args.flags);
3963 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3964 ASSERT_EQ(0, args.buttonState);
3965 ASSERT_EQ(0, args.edgeFlags);
3966 ASSERT_EQ(uint32_t(1), args.pointerCount);
3967 ASSERT_EQ(0, args.pointerProperties[0].id);
3968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003969 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003970 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3971 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3972 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3973
3974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3975 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3976 ASSERT_EQ(DEVICE_ID, args.deviceId);
3977 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3978 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3980 ASSERT_EQ(0, args.flags);
3981 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3982 ASSERT_EQ(0, args.buttonState);
3983 ASSERT_EQ(0, args.edgeFlags);
3984 ASSERT_EQ(uint32_t(1), args.pointerCount);
3985 ASSERT_EQ(0, args.pointerProperties[0].id);
3986 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003987 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3989 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3990 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3991}
3992
3993TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003994 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996
3997 NotifyMotionArgs args;
3998
3999 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004004 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4005 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4006 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007
4008 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4014 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015}
4016
4017TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004019 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020
4021 NotifyMotionArgs args;
4022
4023 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004028 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4031 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004032 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004033
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004039 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004040
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004043 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044}
4045
4046TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004048 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049
4050 NotifyMotionArgs args;
4051
4052 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004059 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4060 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4061 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004065 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4066 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4067 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004068
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004075 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4076 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4077 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078
4079 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004084 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004085
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004088 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089}
4090
4091TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004093 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004095 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4104}
4105
4106TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 addConfigurationProperty("cursor.mode", "navigation");
4108 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004109 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004111 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4118 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4119 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4120
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004121 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4130
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004131 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4140
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004141 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4150}
4151
4152TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004154 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155
4156 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4157 mFakePointerController->setPosition(100, 200);
4158 mFakePointerController->setButtonState(0);
4159
4160 NotifyMotionArgs motionArgs;
4161 NotifyKeyArgs keyArgs;
4162
4163 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4168 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4169 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004170 ASSERT_NO_FATAL_FAILURE(
4171 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4175 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4176 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004177 ASSERT_NO_FATAL_FAILURE(
4178 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004179
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_EQ(0, motionArgs.buttonState);
4185 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004186 ASSERT_NO_FATAL_FAILURE(
4187 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 ASSERT_EQ(0, motionArgs.buttonState);
4192 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004193 ASSERT_NO_FATAL_FAILURE(
4194 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195
4196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004198 ASSERT_EQ(0, motionArgs.buttonState);
4199 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004200 ASSERT_NO_FATAL_FAILURE(
4201 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
4203 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4209 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4210 motionArgs.buttonState);
4211 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4212 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004213 ASSERT_NO_FATAL_FAILURE(
4214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4218 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4219 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4220 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004221 ASSERT_NO_FATAL_FAILURE(
4222 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004223
4224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4226 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4227 motionArgs.buttonState);
4228 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4229 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004230 ASSERT_NO_FATAL_FAILURE(
4231 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004232
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4238 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(
4240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241
4242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004244 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4245 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004246 ASSERT_NO_FATAL_FAILURE(
4247 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4253 ASSERT_EQ(0, motionArgs.buttonState);
4254 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004255 ASSERT_NO_FATAL_FAILURE(
4256 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_EQ(0, motionArgs.buttonState);
4262 ASSERT_EQ(0, mFakePointerController->getButtonState());
4263 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004264 ASSERT_NO_FATAL_FAILURE(
4265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4268 ASSERT_EQ(0, motionArgs.buttonState);
4269 ASSERT_EQ(0, mFakePointerController->getButtonState());
4270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004271 ASSERT_NO_FATAL_FAILURE(
4272 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273
4274 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4278 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4279 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4284 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004285 ASSERT_NO_FATAL_FAILURE(
4286 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004287
4288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004292 ASSERT_NO_FATAL_FAILURE(
4293 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_EQ(0, motionArgs.buttonState);
4300 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(
4302 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303
4304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004306 ASSERT_EQ(0, motionArgs.buttonState);
4307 ASSERT_EQ(0, mFakePointerController->getButtonState());
4308
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004309 ASSERT_NO_FATAL_FAILURE(
4310 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4312 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4313 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4314
4315 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4319 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4320 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4325 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(
4327 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4332 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(
4334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_EQ(0, motionArgs.buttonState);
4341 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004342 ASSERT_NO_FATAL_FAILURE(
4343 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
4345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4347 ASSERT_EQ(0, motionArgs.buttonState);
4348 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004349 ASSERT_NO_FATAL_FAILURE(
4350 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4354 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4355
4356 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4360 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4361 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4366 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004367 ASSERT_NO_FATAL_FAILURE(
4368 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4373 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004374 ASSERT_NO_FATAL_FAILURE(
4375 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
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_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_EQ(0, motionArgs.buttonState);
4382 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004383 ASSERT_NO_FATAL_FAILURE(
4384 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004385
4386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4388 ASSERT_EQ(0, motionArgs.buttonState);
4389 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(
4391 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4395 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4396
4397 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4402 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4407 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(
4409 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4414 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(
4416 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_EQ(0, motionArgs.buttonState);
4423 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004424 ASSERT_NO_FATAL_FAILURE(
4425 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426
4427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4429 ASSERT_EQ(0, motionArgs.buttonState);
4430 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004431 ASSERT_NO_FATAL_FAILURE(
4432 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4435 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4436 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4437}
4438
4439TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004441 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442
4443 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4444 mFakePointerController->setPosition(100, 200);
4445 mFakePointerController->setButtonState(0);
4446
4447 NotifyMotionArgs args;
4448
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004453 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4454 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4456 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 +01004457 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004458}
4459
4460TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004461 addConfigurationProperty("cursor.mode", "pointer");
4462 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004463 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004464
4465 NotifyDeviceResetArgs resetArgs;
4466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4467 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4468 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4469
4470 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4471 mFakePointerController->setPosition(100, 200);
4472 mFakePointerController->setButtonState(0);
4473
4474 NotifyMotionArgs args;
4475
4476 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4481 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4484 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 +01004485 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004486
4487 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4491 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4494 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4496 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4499 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4500
4501 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004502 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4503 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4505 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4508 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4510 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4511 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4513 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4514
4515 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4520 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4523 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 +01004524 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004525
4526 // Disable pointer capture and check that the device generation got bumped
4527 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004528 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004529 mFakePolicy->setPointerCapture(false);
4530 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004531 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004532
4533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4534 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4535 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4536
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4541 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4544 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 +01004545 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546}
4547
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004548TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004549 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004550
Garfield Tan888a6a42020-01-09 11:39:16 -08004551 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004552 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004553 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4554 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004555 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4556 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004557 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4558 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4559
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004560 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4561 mFakePointerController->setPosition(100, 200);
4562 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004563
4564 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4569 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4572 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 +01004573 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004574 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4575}
4576
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577// --- TouchInputMapperTest ---
4578
4579class TouchInputMapperTest : public InputMapperTest {
4580protected:
4581 static const int32_t RAW_X_MIN;
4582 static const int32_t RAW_X_MAX;
4583 static const int32_t RAW_Y_MIN;
4584 static const int32_t RAW_Y_MAX;
4585 static const int32_t RAW_TOUCH_MIN;
4586 static const int32_t RAW_TOUCH_MAX;
4587 static const int32_t RAW_TOOL_MIN;
4588 static const int32_t RAW_TOOL_MAX;
4589 static const int32_t RAW_PRESSURE_MIN;
4590 static const int32_t RAW_PRESSURE_MAX;
4591 static const int32_t RAW_ORIENTATION_MIN;
4592 static const int32_t RAW_ORIENTATION_MAX;
4593 static const int32_t RAW_DISTANCE_MIN;
4594 static const int32_t RAW_DISTANCE_MAX;
4595 static const int32_t RAW_TILT_MIN;
4596 static const int32_t RAW_TILT_MAX;
4597 static const int32_t RAW_ID_MIN;
4598 static const int32_t RAW_ID_MAX;
4599 static const int32_t RAW_SLOT_MIN;
4600 static const int32_t RAW_SLOT_MAX;
4601 static const float X_PRECISION;
4602 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004603 static const float X_PRECISION_VIRTUAL;
4604 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004607 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608
4609 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4610
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004611 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004612 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004613
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 enum Axes {
4615 POSITION = 1 << 0,
4616 TOUCH = 1 << 1,
4617 TOOL = 1 << 2,
4618 PRESSURE = 1 << 3,
4619 ORIENTATION = 1 << 4,
4620 MINOR = 1 << 5,
4621 ID = 1 << 6,
4622 DISTANCE = 1 << 7,
4623 TILT = 1 << 8,
4624 SLOT = 1 << 9,
4625 TOOL_TYPE = 1 << 10,
4626 };
4627
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004628 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4629 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004630 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004632 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 int32_t toRawX(float displayX);
4634 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004635 float toCookedX(float rawX, float rawY);
4636 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004638 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004640 float toDisplayY(int32_t rawY, int32_t displayHeight);
4641
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642};
4643
4644const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4645const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4646const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4647const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4648const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4649const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4650const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4651const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004652const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4653const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4655const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4656const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4657const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4658const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4659const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4660const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4661const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4662const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4663const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4664const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4665const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004666const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4667 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4668const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4669 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004670const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4671 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672
4673const float TouchInputMapperTest::GEOMETRIC_SCALE =
4674 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4675 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4676
4677const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4678 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4679 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4680};
4681
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004682void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004683 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4684 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004685}
4686
4687void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4688 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4689 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690}
4691
Santos Cordonfa5cf462017-04-05 10:37:00 -07004692void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004693 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4694 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4695 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004696}
4697
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004699 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4700 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4701 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703}
4704
Jason Gerecke489fda82012-09-07 17:19:40 -07004705void TouchInputMapperTest::prepareLocationCalibration() {
4706 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4707}
4708
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709int32_t TouchInputMapperTest::toRawX(float displayX) {
4710 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4711}
4712
4713int32_t TouchInputMapperTest::toRawY(float displayY) {
4714 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4715}
4716
Jason Gerecke489fda82012-09-07 17:19:40 -07004717float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4718 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4719 return rawX;
4720}
4721
4722float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4723 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4724 return rawY;
4725}
4726
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004728 return toDisplayX(rawX, DISPLAY_WIDTH);
4729}
4730
4731float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4732 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733}
4734
4735float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004736 return toDisplayY(rawY, DISPLAY_HEIGHT);
4737}
4738
4739float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4740 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741}
4742
4743
4744// --- SingleTouchInputMapperTest ---
4745
4746class SingleTouchInputMapperTest : public TouchInputMapperTest {
4747protected:
4748 void prepareButtons();
4749 void prepareAxes(int axes);
4750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004751 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4752 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4753 void processUp(SingleTouchInputMapper& mappery);
4754 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4755 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4756 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4757 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4758 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4759 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760};
4761
4762void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004763 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764}
4765
4766void SingleTouchInputMapperTest::prepareAxes(int axes) {
4767 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004768 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4769 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 }
4771 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004772 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4773 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 }
4775 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004776 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4777 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778 }
4779 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004780 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4781 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 }
4783 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004784 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4785 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 }
4787}
4788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004789void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793}
4794
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004795void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798}
4799
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004800void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802}
4803
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004804void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806}
4807
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004808void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4809 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811}
4812
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004813void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815}
4816
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004817void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4818 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821}
4822
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004823void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4824 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826}
4827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004828void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830}
4831
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 prepareButtons();
4834 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004835 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004837 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838}
4839
4840TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004841 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4842 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 prepareButtons();
4844 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004845 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004847 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848}
4849
4850TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 prepareButtons();
4852 prepareAxes(POSITION);
4853 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004854 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004856 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857}
4858
4859TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860 prepareButtons();
4861 prepareAxes(POSITION);
4862 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004863 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004865 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866}
4867
4868TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 addConfigurationProperty("touch.deviceType", "touchScreen");
4870 prepareDisplay(DISPLAY_ORIENTATION_0);
4871 prepareButtons();
4872 prepareAxes(POSITION);
4873 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004874 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875
4876 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878
4879 // Virtual key is down.
4880 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4881 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4882 processDown(mapper, x, y);
4883 processSync(mapper);
4884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4885
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004886 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887
4888 // Virtual key is up.
4889 processUp(mapper);
4890 processSync(mapper);
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004893 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894}
4895
4896TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897 addConfigurationProperty("touch.deviceType", "touchScreen");
4898 prepareDisplay(DISPLAY_ORIENTATION_0);
4899 prepareButtons();
4900 prepareAxes(POSITION);
4901 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903
4904 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004905 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906
4907 // Virtual key is down.
4908 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4909 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4910 processDown(mapper, x, y);
4911 processSync(mapper);
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915
4916 // Virtual key is up.
4917 processUp(mapper);
4918 processSync(mapper);
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4920
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004921 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922}
4923
4924TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 addConfigurationProperty("touch.deviceType", "touchScreen");
4926 prepareDisplay(DISPLAY_ORIENTATION_0);
4927 prepareButtons();
4928 prepareAxes(POSITION);
4929 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004930 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931
4932 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4933 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004934 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 ASSERT_TRUE(flags[0]);
4936 ASSERT_FALSE(flags[1]);
4937}
4938
4939TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 addConfigurationProperty("touch.deviceType", "touchScreen");
4941 prepareDisplay(DISPLAY_ORIENTATION_0);
4942 prepareButtons();
4943 prepareAxes(POSITION);
4944 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004945 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946
arthurhungdcef2dc2020-08-11 14:47:50 +08004947 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948
4949 NotifyKeyArgs args;
4950
4951 // Press virtual key.
4952 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4953 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4954 processDown(mapper, x, y);
4955 processSync(mapper);
4956
4957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4958 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4959 ASSERT_EQ(DEVICE_ID, args.deviceId);
4960 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4961 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4962 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4963 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4964 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4965 ASSERT_EQ(KEY_HOME, args.scanCode);
4966 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4967 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4968
4969 // Release virtual key.
4970 processUp(mapper);
4971 processSync(mapper);
4972
4973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4974 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4975 ASSERT_EQ(DEVICE_ID, args.deviceId);
4976 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4977 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4978 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4979 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4980 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4981 ASSERT_EQ(KEY_HOME, args.scanCode);
4982 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4983 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4984
4985 // Should not have sent any motions.
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4987}
4988
4989TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990 addConfigurationProperty("touch.deviceType", "touchScreen");
4991 prepareDisplay(DISPLAY_ORIENTATION_0);
4992 prepareButtons();
4993 prepareAxes(POSITION);
4994 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004995 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996
arthurhungdcef2dc2020-08-11 14:47:50 +08004997 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998
4999 NotifyKeyArgs keyArgs;
5000
5001 // Press virtual key.
5002 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5003 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5004 processDown(mapper, x, y);
5005 processSync(mapper);
5006
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5008 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5009 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5010 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5011 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5012 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5013 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5014 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5015 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5016 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5017 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5018
5019 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5020 // into the display area.
5021 y -= 100;
5022 processMove(mapper, x, y);
5023 processSync(mapper);
5024
5025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5026 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5027 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5028 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5029 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5030 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5031 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5032 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5033 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5034 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5036 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5037
5038 NotifyMotionArgs motionArgs;
5039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5040 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5041 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5042 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5043 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5045 ASSERT_EQ(0, motionArgs.flags);
5046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5047 ASSERT_EQ(0, motionArgs.buttonState);
5048 ASSERT_EQ(0, motionArgs.edgeFlags);
5049 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5050 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5053 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5054 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5055 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5057
5058 // Keep moving out of bounds. Should generate a pointer move.
5059 y -= 50;
5060 processMove(mapper, x, y);
5061 processSync(mapper);
5062
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5064 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5065 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5066 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5067 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5069 ASSERT_EQ(0, motionArgs.flags);
5070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5071 ASSERT_EQ(0, motionArgs.buttonState);
5072 ASSERT_EQ(0, motionArgs.edgeFlags);
5073 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5074 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5077 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5078 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5079 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5081
5082 // Release out of bounds. Should generate a pointer up.
5083 processUp(mapper);
5084 processSync(mapper);
5085
5086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5087 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5088 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5089 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5090 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5091 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5092 ASSERT_EQ(0, motionArgs.flags);
5093 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5094 ASSERT_EQ(0, motionArgs.buttonState);
5095 ASSERT_EQ(0, motionArgs.edgeFlags);
5096 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5097 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5100 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5101 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5102 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5103 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5104
5105 // Should not have sent any more keys or motions.
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5108}
5109
5110TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 addConfigurationProperty("touch.deviceType", "touchScreen");
5112 prepareDisplay(DISPLAY_ORIENTATION_0);
5113 prepareButtons();
5114 prepareAxes(POSITION);
5115 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005116 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117
arthurhungdcef2dc2020-08-11 14:47:50 +08005118 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119
5120 NotifyMotionArgs motionArgs;
5121
5122 // Initially go down out of bounds.
5123 int32_t x = -10;
5124 int32_t y = -10;
5125 processDown(mapper, x, y);
5126 processSync(mapper);
5127
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5129
5130 // Move into the display area. Should generate a pointer down.
5131 x = 50;
5132 y = 75;
5133 processMove(mapper, x, y);
5134 processSync(mapper);
5135
5136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5138 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5139 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5140 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5142 ASSERT_EQ(0, motionArgs.flags);
5143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5144 ASSERT_EQ(0, motionArgs.buttonState);
5145 ASSERT_EQ(0, motionArgs.edgeFlags);
5146 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5147 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5150 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5151 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5152 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5153 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5154
5155 // Release. Should generate a pointer up.
5156 processUp(mapper);
5157 processSync(mapper);
5158
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5160 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5161 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5162 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5163 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5164 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5165 ASSERT_EQ(0, motionArgs.flags);
5166 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5167 ASSERT_EQ(0, motionArgs.buttonState);
5168 ASSERT_EQ(0, motionArgs.edgeFlags);
5169 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5170 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5173 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5174 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5175 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5176 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5177
5178 // Should not have sent any more keys or motions.
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5181}
5182
Santos Cordonfa5cf462017-04-05 10:37:00 -07005183TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005184 addConfigurationProperty("touch.deviceType", "touchScreen");
5185 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5186
5187 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5188 prepareButtons();
5189 prepareAxes(POSITION);
5190 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005191 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005192
arthurhungdcef2dc2020-08-11 14:47:50 +08005193 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005194
5195 NotifyMotionArgs motionArgs;
5196
5197 // Down.
5198 int32_t x = 100;
5199 int32_t y = 125;
5200 processDown(mapper, x, y);
5201 processSync(mapper);
5202
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5205 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5206 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5207 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5208 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5210 ASSERT_EQ(0, motionArgs.flags);
5211 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5212 ASSERT_EQ(0, motionArgs.buttonState);
5213 ASSERT_EQ(0, motionArgs.edgeFlags);
5214 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5215 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5218 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5219 1, 0, 0, 0, 0, 0, 0, 0));
5220 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5221 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5222 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5223
5224 // Move.
5225 x += 50;
5226 y += 75;
5227 processMove(mapper, x, y);
5228 processSync(mapper);
5229
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5232 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5233 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5234 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5235 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5237 ASSERT_EQ(0, motionArgs.flags);
5238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5239 ASSERT_EQ(0, motionArgs.buttonState);
5240 ASSERT_EQ(0, motionArgs.edgeFlags);
5241 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5242 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5245 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5246 1, 0, 0, 0, 0, 0, 0, 0));
5247 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5248 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5250
5251 // Up.
5252 processUp(mapper);
5253 processSync(mapper);
5254
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5257 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5258 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5259 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5260 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5261 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5262 ASSERT_EQ(0, motionArgs.flags);
5263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5264 ASSERT_EQ(0, motionArgs.buttonState);
5265 ASSERT_EQ(0, motionArgs.edgeFlags);
5266 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5267 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5270 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5271 1, 0, 0, 0, 0, 0, 0, 0));
5272 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5273 ASSERT_NEAR(Y_PRECISION_VIRTUAL, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282 addConfigurationProperty("touch.deviceType", "touchScreen");
5283 prepareDisplay(DISPLAY_ORIENTATION_0);
5284 prepareButtons();
5285 prepareAxes(POSITION);
5286 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005287 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288
arthurhungdcef2dc2020-08-11 14:47:50 +08005289 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
5291 NotifyMotionArgs motionArgs;
5292
5293 // Down.
5294 int32_t x = 100;
5295 int32_t y = 125;
5296 processDown(mapper, x, y);
5297 processSync(mapper);
5298
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5301 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5302 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5303 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5304 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5305 ASSERT_EQ(0, motionArgs.flags);
5306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5307 ASSERT_EQ(0, motionArgs.buttonState);
5308 ASSERT_EQ(0, motionArgs.edgeFlags);
5309 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5310 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5313 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5314 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5315 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5317
5318 // Move.
5319 x += 50;
5320 y += 75;
5321 processMove(mapper, x, y);
5322 processSync(mapper);
5323
5324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5326 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5327 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5328 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5330 ASSERT_EQ(0, motionArgs.flags);
5331 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5332 ASSERT_EQ(0, motionArgs.buttonState);
5333 ASSERT_EQ(0, motionArgs.edgeFlags);
5334 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5335 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5338 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5339 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5340 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5342
5343 // Up.
5344 processUp(mapper);
5345 processSync(mapper);
5346
5347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5349 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5350 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5351 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5352 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5353 ASSERT_EQ(0, motionArgs.flags);
5354 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5355 ASSERT_EQ(0, motionArgs.buttonState);
5356 ASSERT_EQ(0, motionArgs.edgeFlags);
5357 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5358 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5361 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5362 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5363 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5365
5366 // Should not have sent any more keys or motions.
5367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5369}
5370
5371TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372 addConfigurationProperty("touch.deviceType", "touchScreen");
5373 prepareButtons();
5374 prepareAxes(POSITION);
5375 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005376 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377
5378 NotifyMotionArgs args;
5379
5380 // Rotation 90.
5381 prepareDisplay(DISPLAY_ORIENTATION_90);
5382 processDown(mapper, toRawX(50), toRawY(75));
5383 processSync(mapper);
5384
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5386 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5387 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5388
5389 processUp(mapper);
5390 processSync(mapper);
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5392}
5393
5394TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395 addConfigurationProperty("touch.deviceType", "touchScreen");
5396 prepareButtons();
5397 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005398 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399
5400 NotifyMotionArgs args;
5401
5402 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005403 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 prepareDisplay(DISPLAY_ORIENTATION_0);
5405 processDown(mapper, toRawX(50), toRawY(75));
5406 processSync(mapper);
5407
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5409 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5410 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5411
5412 processUp(mapper);
5413 processSync(mapper);
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5415
5416 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005417 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 prepareDisplay(DISPLAY_ORIENTATION_90);
5419 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5420 processSync(mapper);
5421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5423 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5424 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5425
5426 processUp(mapper);
5427 processSync(mapper);
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5429
5430 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005431 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432 prepareDisplay(DISPLAY_ORIENTATION_180);
5433 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5434 processSync(mapper);
5435
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5437 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5438 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5439
5440 processUp(mapper);
5441 processSync(mapper);
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5443
5444 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005445 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446 prepareDisplay(DISPLAY_ORIENTATION_270);
5447 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5448 processSync(mapper);
5449
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5451 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5452 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5453
5454 processUp(mapper);
5455 processSync(mapper);
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5457}
5458
5459TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460 addConfigurationProperty("touch.deviceType", "touchScreen");
5461 prepareDisplay(DISPLAY_ORIENTATION_0);
5462 prepareButtons();
5463 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465
5466 // These calculations are based on the input device calibration documentation.
5467 int32_t rawX = 100;
5468 int32_t rawY = 200;
5469 int32_t rawPressure = 10;
5470 int32_t rawToolMajor = 12;
5471 int32_t rawDistance = 2;
5472 int32_t rawTiltX = 30;
5473 int32_t rawTiltY = 110;
5474
5475 float x = toDisplayX(rawX);
5476 float y = toDisplayY(rawY);
5477 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5478 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5479 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5480 float distance = float(rawDistance);
5481
5482 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5483 float tiltScale = M_PI / 180;
5484 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5485 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5486 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5487 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5488
5489 processDown(mapper, rawX, rawY);
5490 processPressure(mapper, rawPressure);
5491 processToolMajor(mapper, rawToolMajor);
5492 processDistance(mapper, rawDistance);
5493 processTilt(mapper, rawTiltX, rawTiltY);
5494 processSync(mapper);
5495
5496 NotifyMotionArgs args;
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5499 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5500 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5501}
5502
Jason Gerecke489fda82012-09-07 17:19:40 -07005503TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005504 addConfigurationProperty("touch.deviceType", "touchScreen");
5505 prepareDisplay(DISPLAY_ORIENTATION_0);
5506 prepareLocationCalibration();
5507 prepareButtons();
5508 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005509 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005510
5511 int32_t rawX = 100;
5512 int32_t rawY = 200;
5513
5514 float x = toDisplayX(toCookedX(rawX, rawY));
5515 float y = toDisplayY(toCookedY(rawX, rawY));
5516
5517 processDown(mapper, rawX, rawY);
5518 processSync(mapper);
5519
5520 NotifyMotionArgs args;
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5523 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5524}
5525
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 addConfigurationProperty("touch.deviceType", "touchScreen");
5528 prepareDisplay(DISPLAY_ORIENTATION_0);
5529 prepareButtons();
5530 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005531 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532
5533 NotifyMotionArgs motionArgs;
5534 NotifyKeyArgs keyArgs;
5535
5536 processDown(mapper, 100, 200);
5537 processSync(mapper);
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5539 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5540 ASSERT_EQ(0, motionArgs.buttonState);
5541
5542 // press BTN_LEFT, release BTN_LEFT
5543 processKey(mapper, BTN_LEFT, 1);
5544 processSync(mapper);
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5547 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5548
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5551 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5552
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 processKey(mapper, BTN_LEFT, 0);
5554 processSync(mapper);
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005557 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005558
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005561 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562
5563 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5564 processKey(mapper, BTN_RIGHT, 1);
5565 processKey(mapper, BTN_MIDDLE, 1);
5566 processSync(mapper);
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5569 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5570 motionArgs.buttonState);
5571
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5574 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5575
5576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5577 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5578 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5579 motionArgs.buttonState);
5580
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581 processKey(mapper, BTN_RIGHT, 0);
5582 processSync(mapper);
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005589 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590
5591 processKey(mapper, BTN_MIDDLE, 0);
5592 processSync(mapper);
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005596
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005599 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600
5601 // press BTN_BACK, release BTN_BACK
5602 processKey(mapper, BTN_BACK, 1);
5603 processSync(mapper);
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5605 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5606 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005607
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005610 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5614 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005615
5616 processKey(mapper, BTN_BACK, 0);
5617 processSync(mapper);
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005621
5622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005624 ASSERT_EQ(0, motionArgs.buttonState);
5625
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5627 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5628 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5629
5630 // press BTN_SIDE, release BTN_SIDE
5631 processKey(mapper, BTN_SIDE, 1);
5632 processSync(mapper);
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5634 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5635 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005636
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005639 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5642 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5643 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005644
5645 processKey(mapper, BTN_SIDE, 0);
5646 processSync(mapper);
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005650
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005653 ASSERT_EQ(0, motionArgs.buttonState);
5654
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5656 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5657 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5658
5659 // press BTN_FORWARD, release BTN_FORWARD
5660 processKey(mapper, BTN_FORWARD, 1);
5661 processSync(mapper);
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5663 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5664 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005665
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005668 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5669
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5672 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673
5674 processKey(mapper, BTN_FORWARD, 0);
5675 processSync(mapper);
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005677 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005679
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005682 ASSERT_EQ(0, motionArgs.buttonState);
5683
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5685 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5686 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5687
5688 // press BTN_EXTRA, release BTN_EXTRA
5689 processKey(mapper, BTN_EXTRA, 1);
5690 processSync(mapper);
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5692 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5693 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005694
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5698
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5700 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5701 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702
5703 processKey(mapper, BTN_EXTRA, 0);
5704 processSync(mapper);
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005711 ASSERT_EQ(0, motionArgs.buttonState);
5712
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5714 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5715 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5716
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5718
Michael Wrightd02c5b62014-02-10 15:10:22 -08005719 // press BTN_STYLUS, release BTN_STYLUS
5720 processKey(mapper, BTN_STYLUS, 1);
5721 processSync(mapper);
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005724 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5725
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5728 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729
5730 processKey(mapper, BTN_STYLUS, 0);
5731 processSync(mapper);
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005733 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005735
5736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005738 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005739
5740 // press BTN_STYLUS2, release BTN_STYLUS2
5741 processKey(mapper, BTN_STYLUS2, 1);
5742 processSync(mapper);
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005745 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5748 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5749 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750
5751 processKey(mapper, BTN_STYLUS2, 0);
5752 processSync(mapper);
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005754 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005756
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005759 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760
5761 // release touch
5762 processUp(mapper);
5763 processSync(mapper);
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5766 ASSERT_EQ(0, motionArgs.buttonState);
5767}
5768
5769TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 addConfigurationProperty("touch.deviceType", "touchScreen");
5771 prepareDisplay(DISPLAY_ORIENTATION_0);
5772 prepareButtons();
5773 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005774 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775
5776 NotifyMotionArgs motionArgs;
5777
5778 // default tool type is finger
5779 processDown(mapper, 100, 200);
5780 processSync(mapper);
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5784
5785 // eraser
5786 processKey(mapper, BTN_TOOL_RUBBER, 1);
5787 processSync(mapper);
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5791
5792 // stylus
5793 processKey(mapper, BTN_TOOL_RUBBER, 0);
5794 processKey(mapper, BTN_TOOL_PEN, 1);
5795 processSync(mapper);
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5799
5800 // brush
5801 processKey(mapper, BTN_TOOL_PEN, 0);
5802 processKey(mapper, BTN_TOOL_BRUSH, 1);
5803 processSync(mapper);
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5807
5808 // pencil
5809 processKey(mapper, BTN_TOOL_BRUSH, 0);
5810 processKey(mapper, BTN_TOOL_PENCIL, 1);
5811 processSync(mapper);
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5815
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005816 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817 processKey(mapper, BTN_TOOL_PENCIL, 0);
5818 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5819 processSync(mapper);
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5823
5824 // mouse
5825 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5826 processKey(mapper, BTN_TOOL_MOUSE, 1);
5827 processSync(mapper);
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5831
5832 // lens
5833 processKey(mapper, BTN_TOOL_MOUSE, 0);
5834 processKey(mapper, BTN_TOOL_LENS, 1);
5835 processSync(mapper);
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5839
5840 // double-tap
5841 processKey(mapper, BTN_TOOL_LENS, 0);
5842 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5843 processSync(mapper);
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5847
5848 // triple-tap
5849 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5850 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5851 processSync(mapper);
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5855
5856 // quad-tap
5857 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5858 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5859 processSync(mapper);
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5863
5864 // finger
5865 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5866 processKey(mapper, BTN_TOOL_FINGER, 1);
5867 processSync(mapper);
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5870 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5871
5872 // stylus trumps finger
5873 processKey(mapper, BTN_TOOL_PEN, 1);
5874 processSync(mapper);
5875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5878
5879 // eraser trumps stylus
5880 processKey(mapper, BTN_TOOL_RUBBER, 1);
5881 processSync(mapper);
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5885
5886 // mouse trumps eraser
5887 processKey(mapper, BTN_TOOL_MOUSE, 1);
5888 processSync(mapper);
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5892
5893 // back to default tool type
5894 processKey(mapper, BTN_TOOL_MOUSE, 0);
5895 processKey(mapper, BTN_TOOL_RUBBER, 0);
5896 processKey(mapper, BTN_TOOL_PEN, 0);
5897 processKey(mapper, BTN_TOOL_FINGER, 0);
5898 processSync(mapper);
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5902}
5903
5904TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 addConfigurationProperty("touch.deviceType", "touchScreen");
5906 prepareDisplay(DISPLAY_ORIENTATION_0);
5907 prepareButtons();
5908 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005909 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005910 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911
5912 NotifyMotionArgs motionArgs;
5913
5914 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5915 processKey(mapper, BTN_TOOL_FINGER, 1);
5916 processMove(mapper, 100, 200);
5917 processSync(mapper);
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5921 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5922
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5926 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5927
5928 // move a little
5929 processMove(mapper, 150, 250);
5930 processSync(mapper);
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5935
5936 // down when BTN_TOUCH is pressed, pressure defaults to 1
5937 processKey(mapper, BTN_TOUCH, 1);
5938 processSync(mapper);
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5940 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5942 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5943
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5948
5949 // up when BTN_TOUCH is released, hover restored
5950 processKey(mapper, BTN_TOUCH, 0);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5953 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5955 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5956
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5960 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5961
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5965 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5966
5967 // exit hover when pointer goes away
5968 processKey(mapper, BTN_TOOL_FINGER, 0);
5969 processSync(mapper);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5973 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5974}
5975
5976TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 addConfigurationProperty("touch.deviceType", "touchScreen");
5978 prepareDisplay(DISPLAY_ORIENTATION_0);
5979 prepareButtons();
5980 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005981 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982
5983 NotifyMotionArgs motionArgs;
5984
5985 // initially hovering because pressure is 0
5986 processDown(mapper, 100, 200);
5987 processPressure(mapper, 0);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5990 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5992 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5998
5999 // move a little
6000 processMove(mapper, 150, 250);
6001 processSync(mapper);
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6003 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6005 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6006
6007 // down when pressure is non-zero
6008 processPressure(mapper, RAW_PRESSURE_MAX);
6009 processSync(mapper);
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6013 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6014
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6016 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6018 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6019
6020 // up when pressure becomes 0, hover restored
6021 processPressure(mapper, 0);
6022 processSync(mapper);
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6024 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6026 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6027
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6031 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6032
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6034 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6036 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6037
6038 // exit hover when pointer goes away
6039 processUp(mapper);
6040 processSync(mapper);
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6044 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6045}
6046
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047// --- MultiTouchInputMapperTest ---
6048
6049class MultiTouchInputMapperTest : public TouchInputMapperTest {
6050protected:
6051 void prepareAxes(int axes);
6052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006053 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6054 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6055 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6056 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6057 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6058 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6059 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6060 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6061 void processId(MultiTouchInputMapper& mapper, int32_t id);
6062 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6063 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6064 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6065 void processMTSync(MultiTouchInputMapper& mapper);
6066 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067};
6068
6069void MultiTouchInputMapperTest::prepareAxes(int axes) {
6070 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6072 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 }
6074 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6076 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006078 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6079 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 }
6081 }
6082 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6084 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6087 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 }
6089 }
6090 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6092 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 }
6094 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6096 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 }
6098 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6100 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 }
6102 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6104 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 }
6106 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6108 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 }
6110 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 }
6113}
6114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006115void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6116 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006119}
6120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006121void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6122 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124}
6125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006126void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6127 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129}
6130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006131void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133}
6134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006135void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137}
6138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006139void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6140 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142}
6143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006144void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146}
6147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006148void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150}
6151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006152void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154}
6155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006156void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158}
6159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006160void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162}
6163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006164void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6165 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167}
6168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006169void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171}
6172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006173void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175}
6176
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 addConfigurationProperty("touch.deviceType", "touchScreen");
6179 prepareDisplay(DISPLAY_ORIENTATION_0);
6180 prepareAxes(POSITION);
6181 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183
arthurhungdcef2dc2020-08-11 14:47:50 +08006184 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185
6186 NotifyMotionArgs motionArgs;
6187
6188 // Two fingers down at once.
6189 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6190 processPosition(mapper, x1, y1);
6191 processMTSync(mapper);
6192 processPosition(mapper, x2, y2);
6193 processMTSync(mapper);
6194 processSync(mapper);
6195
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6198 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6199 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6200 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6201 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6202 ASSERT_EQ(0, motionArgs.flags);
6203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6204 ASSERT_EQ(0, motionArgs.buttonState);
6205 ASSERT_EQ(0, motionArgs.edgeFlags);
6206 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6210 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6211 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6212 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6214
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6221 motionArgs.action);
6222 ASSERT_EQ(0, motionArgs.flags);
6223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6224 ASSERT_EQ(0, motionArgs.buttonState);
6225 ASSERT_EQ(0, motionArgs.edgeFlags);
6226 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6232 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6238
6239 // Move.
6240 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6241 processPosition(mapper, x1, y1);
6242 processMTSync(mapper);
6243 processPosition(mapper, x2, y2);
6244 processMTSync(mapper);
6245 processSync(mapper);
6246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6249 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6250 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6251 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6253 ASSERT_EQ(0, motionArgs.flags);
6254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6255 ASSERT_EQ(0, motionArgs.buttonState);
6256 ASSERT_EQ(0, motionArgs.edgeFlags);
6257 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6258 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6260 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6263 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6265 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6266 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6267 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6269
6270 // First finger up.
6271 x2 += 15; y2 -= 20;
6272 processPosition(mapper, x2, y2);
6273 processMTSync(mapper);
6274 processSync(mapper);
6275
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6278 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6279 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6280 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6282 motionArgs.action);
6283 ASSERT_EQ(0, motionArgs.flags);
6284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6285 ASSERT_EQ(0, motionArgs.buttonState);
6286 ASSERT_EQ(0, motionArgs.edgeFlags);
6287 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6288 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6290 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6293 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6295 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6296 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6297 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6298 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6302 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6303 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6304 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6306 ASSERT_EQ(0, motionArgs.flags);
6307 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6308 ASSERT_EQ(0, motionArgs.buttonState);
6309 ASSERT_EQ(0, motionArgs.edgeFlags);
6310 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6311 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6314 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6315 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6316 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6317 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6318
6319 // Move.
6320 x2 += 20; y2 -= 25;
6321 processPosition(mapper, x2, y2);
6322 processMTSync(mapper);
6323 processSync(mapper);
6324
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6327 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6328 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6329 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6331 ASSERT_EQ(0, motionArgs.flags);
6332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6333 ASSERT_EQ(0, motionArgs.buttonState);
6334 ASSERT_EQ(0, motionArgs.edgeFlags);
6335 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6336 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6339 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6340 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6341 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6343
6344 // New finger down.
6345 int32_t x3 = 700, y3 = 300;
6346 processPosition(mapper, x2, y2);
6347 processMTSync(mapper);
6348 processPosition(mapper, x3, y3);
6349 processMTSync(mapper);
6350 processSync(mapper);
6351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6354 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6355 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6356 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6358 motionArgs.action);
6359 ASSERT_EQ(0, motionArgs.flags);
6360 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6361 ASSERT_EQ(0, motionArgs.buttonState);
6362 ASSERT_EQ(0, motionArgs.edgeFlags);
6363 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6364 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6366 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6369 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6371 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6372 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6373 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6375
6376 // Second finger up.
6377 x3 += 30; y3 -= 20;
6378 processPosition(mapper, x3, y3);
6379 processMTSync(mapper);
6380 processSync(mapper);
6381
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6384 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6385 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6386 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6388 motionArgs.action);
6389 ASSERT_EQ(0, motionArgs.flags);
6390 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6391 ASSERT_EQ(0, motionArgs.buttonState);
6392 ASSERT_EQ(0, motionArgs.edgeFlags);
6393 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6394 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6396 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6399 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6401 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6402 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6403 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6405
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6408 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6409 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6410 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6412 ASSERT_EQ(0, motionArgs.flags);
6413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6414 ASSERT_EQ(0, motionArgs.buttonState);
6415 ASSERT_EQ(0, motionArgs.edgeFlags);
6416 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6420 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6421 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6422 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6424
6425 // Last finger up.
6426 processMTSync(mapper);
6427 processSync(mapper);
6428
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6431 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6432 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6433 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6435 ASSERT_EQ(0, motionArgs.flags);
6436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6437 ASSERT_EQ(0, motionArgs.buttonState);
6438 ASSERT_EQ(0, motionArgs.edgeFlags);
6439 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6443 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6444 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6445 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6447
6448 // Should not have sent any more keys or motions.
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6451}
6452
6453TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 addConfigurationProperty("touch.deviceType", "touchScreen");
6455 prepareDisplay(DISPLAY_ORIENTATION_0);
6456 prepareAxes(POSITION | ID);
6457 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006458 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006459
arthurhungdcef2dc2020-08-11 14:47:50 +08006460 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461
6462 NotifyMotionArgs motionArgs;
6463
6464 // Two fingers down at once.
6465 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6466 processPosition(mapper, x1, y1);
6467 processId(mapper, 1);
6468 processMTSync(mapper);
6469 processPosition(mapper, x2, y2);
6470 processId(mapper, 2);
6471 processMTSync(mapper);
6472 processSync(mapper);
6473
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6476 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6477 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6480 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6481
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6483 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6484 motionArgs.action);
6485 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6488 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6491 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6493 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6494
6495 // Move.
6496 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6497 processPosition(mapper, x1, y1);
6498 processId(mapper, 1);
6499 processMTSync(mapper);
6500 processPosition(mapper, x2, y2);
6501 processId(mapper, 2);
6502 processMTSync(mapper);
6503 processSync(mapper);
6504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6507 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6510 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6513 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6515 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6516
6517 // First finger up.
6518 x2 += 15; y2 -= 20;
6519 processPosition(mapper, x2, y2);
6520 processId(mapper, 2);
6521 processMTSync(mapper);
6522 processSync(mapper);
6523
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6526 motionArgs.action);
6527 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6530 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6533 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6535 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6536
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6539 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6540 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6543 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6544
6545 // Move.
6546 x2 += 20; y2 -= 25;
6547 processPosition(mapper, x2, y2);
6548 processId(mapper, 2);
6549 processMTSync(mapper);
6550 processSync(mapper);
6551
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6554 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6555 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6558 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6559
6560 // New finger down.
6561 int32_t x3 = 700, y3 = 300;
6562 processPosition(mapper, x2, y2);
6563 processId(mapper, 2);
6564 processMTSync(mapper);
6565 processPosition(mapper, x3, y3);
6566 processId(mapper, 3);
6567 processMTSync(mapper);
6568 processSync(mapper);
6569
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6572 motionArgs.action);
6573 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6574 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6576 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6579 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6581 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6582
6583 // Second finger up.
6584 x3 += 30; y3 -= 20;
6585 processPosition(mapper, x3, y3);
6586 processId(mapper, 3);
6587 processMTSync(mapper);
6588 processSync(mapper);
6589
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6592 motionArgs.action);
6593 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6596 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6599 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6601 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6602
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6605 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6609 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6610
6611 // Last finger up.
6612 processMTSync(mapper);
6613 processSync(mapper);
6614
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6616 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6617 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6618 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6621 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6622
6623 // Should not have sent any more keys or motions.
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6626}
6627
6628TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006629 addConfigurationProperty("touch.deviceType", "touchScreen");
6630 prepareDisplay(DISPLAY_ORIENTATION_0);
6631 prepareAxes(POSITION | ID | SLOT);
6632 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006633 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634
arthurhungdcef2dc2020-08-11 14:47:50 +08006635 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006636
6637 NotifyMotionArgs motionArgs;
6638
6639 // Two fingers down at once.
6640 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6641 processPosition(mapper, x1, y1);
6642 processId(mapper, 1);
6643 processSlot(mapper, 1);
6644 processPosition(mapper, x2, y2);
6645 processId(mapper, 2);
6646 processSync(mapper);
6647
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6649 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6650 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6654 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6655
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6658 motionArgs.action);
6659 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6660 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6662 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6667 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6668
6669 // Move.
6670 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6671 processSlot(mapper, 0);
6672 processPosition(mapper, x1, y1);
6673 processSlot(mapper, 1);
6674 processPosition(mapper, x2, y2);
6675 processSync(mapper);
6676
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6679 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6680 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6682 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6683 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6685 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6687 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6688
6689 // First finger up.
6690 x2 += 15; y2 -= 20;
6691 processSlot(mapper, 0);
6692 processId(mapper, -1);
6693 processSlot(mapper, 1);
6694 processPosition(mapper, x2, y2);
6695 processSync(mapper);
6696
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6698 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6699 motionArgs.action);
6700 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6701 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6703 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6706 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6708 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6709
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6712 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6713 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6716 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6717
6718 // Move.
6719 x2 += 20; y2 -= 25;
6720 processPosition(mapper, x2, y2);
6721 processSync(mapper);
6722
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6725 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6726 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6729 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6730
6731 // New finger down.
6732 int32_t x3 = 700, y3 = 300;
6733 processPosition(mapper, x2, y2);
6734 processSlot(mapper, 0);
6735 processId(mapper, 3);
6736 processPosition(mapper, x3, y3);
6737 processSync(mapper);
6738
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6741 motionArgs.action);
6742 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6743 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6745 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6748 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6750 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6751
6752 // Second finger up.
6753 x3 += 30; y3 -= 20;
6754 processSlot(mapper, 1);
6755 processId(mapper, -1);
6756 processSlot(mapper, 0);
6757 processPosition(mapper, x3, y3);
6758 processSync(mapper);
6759
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6762 motionArgs.action);
6763 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6764 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6766 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6769 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6771 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6772
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6775 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6776 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6779 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6780
6781 // Last finger up.
6782 processId(mapper, -1);
6783 processSync(mapper);
6784
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6786 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6787 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6791 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6792
6793 // Should not have sent any more keys or motions.
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6796}
6797
6798TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006799 addConfigurationProperty("touch.deviceType", "touchScreen");
6800 prepareDisplay(DISPLAY_ORIENTATION_0);
6801 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006802 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006803
6804 // These calculations are based on the input device calibration documentation.
6805 int32_t rawX = 100;
6806 int32_t rawY = 200;
6807 int32_t rawTouchMajor = 7;
6808 int32_t rawTouchMinor = 6;
6809 int32_t rawToolMajor = 9;
6810 int32_t rawToolMinor = 8;
6811 int32_t rawPressure = 11;
6812 int32_t rawDistance = 0;
6813 int32_t rawOrientation = 3;
6814 int32_t id = 5;
6815
6816 float x = toDisplayX(rawX);
6817 float y = toDisplayY(rawY);
6818 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6819 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6820 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6821 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6822 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6823 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6824 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6825 float distance = float(rawDistance);
6826
6827 processPosition(mapper, rawX, rawY);
6828 processTouchMajor(mapper, rawTouchMajor);
6829 processTouchMinor(mapper, rawTouchMinor);
6830 processToolMajor(mapper, rawToolMajor);
6831 processToolMinor(mapper, rawToolMinor);
6832 processPressure(mapper, rawPressure);
6833 processOrientation(mapper, rawOrientation);
6834 processDistance(mapper, rawDistance);
6835 processId(mapper, id);
6836 processMTSync(mapper);
6837 processSync(mapper);
6838
6839 NotifyMotionArgs args;
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6841 ASSERT_EQ(0, args.pointerProperties[0].id);
6842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6843 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6844 orientation, distance));
6845}
6846
6847TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006848 addConfigurationProperty("touch.deviceType", "touchScreen");
6849 prepareDisplay(DISPLAY_ORIENTATION_0);
6850 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6851 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006852 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006853
6854 // These calculations are based on the input device calibration documentation.
6855 int32_t rawX = 100;
6856 int32_t rawY = 200;
6857 int32_t rawTouchMajor = 140;
6858 int32_t rawTouchMinor = 120;
6859 int32_t rawToolMajor = 180;
6860 int32_t rawToolMinor = 160;
6861
6862 float x = toDisplayX(rawX);
6863 float y = toDisplayY(rawY);
6864 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6865 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6866 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6867 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6868 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6869
6870 processPosition(mapper, rawX, rawY);
6871 processTouchMajor(mapper, rawTouchMajor);
6872 processTouchMinor(mapper, rawTouchMinor);
6873 processToolMajor(mapper, rawToolMajor);
6874 processToolMinor(mapper, rawToolMinor);
6875 processMTSync(mapper);
6876 processSync(mapper);
6877
6878 NotifyMotionArgs args;
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6881 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6882}
6883
6884TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006885 addConfigurationProperty("touch.deviceType", "touchScreen");
6886 prepareDisplay(DISPLAY_ORIENTATION_0);
6887 prepareAxes(POSITION | TOUCH | TOOL);
6888 addConfigurationProperty("touch.size.calibration", "diameter");
6889 addConfigurationProperty("touch.size.scale", "10");
6890 addConfigurationProperty("touch.size.bias", "160");
6891 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006892 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893
6894 // These calculations are based on the input device calibration documentation.
6895 // Note: We only provide a single common touch/tool value because the device is assumed
6896 // not to emit separate values for each pointer (isSummed = 1).
6897 int32_t rawX = 100;
6898 int32_t rawY = 200;
6899 int32_t rawX2 = 150;
6900 int32_t rawY2 = 250;
6901 int32_t rawTouchMajor = 5;
6902 int32_t rawToolMajor = 8;
6903
6904 float x = toDisplayX(rawX);
6905 float y = toDisplayY(rawY);
6906 float x2 = toDisplayX(rawX2);
6907 float y2 = toDisplayY(rawY2);
6908 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6909 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6910 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6911
6912 processPosition(mapper, rawX, rawY);
6913 processTouchMajor(mapper, rawTouchMajor);
6914 processToolMajor(mapper, rawToolMajor);
6915 processMTSync(mapper);
6916 processPosition(mapper, rawX2, rawY2);
6917 processTouchMajor(mapper, rawTouchMajor);
6918 processToolMajor(mapper, rawToolMajor);
6919 processMTSync(mapper);
6920 processSync(mapper);
6921
6922 NotifyMotionArgs args;
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6925
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6927 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6928 args.action);
6929 ASSERT_EQ(size_t(2), args.pointerCount);
6930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6931 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6933 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6934}
6935
6936TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006937 addConfigurationProperty("touch.deviceType", "touchScreen");
6938 prepareDisplay(DISPLAY_ORIENTATION_0);
6939 prepareAxes(POSITION | TOUCH | TOOL);
6940 addConfigurationProperty("touch.size.calibration", "area");
6941 addConfigurationProperty("touch.size.scale", "43");
6942 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006943 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006944
6945 // These calculations are based on the input device calibration documentation.
6946 int32_t rawX = 100;
6947 int32_t rawY = 200;
6948 int32_t rawTouchMajor = 5;
6949 int32_t rawToolMajor = 8;
6950
6951 float x = toDisplayX(rawX);
6952 float y = toDisplayY(rawY);
6953 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6954 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6955 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6956
6957 processPosition(mapper, rawX, rawY);
6958 processTouchMajor(mapper, rawTouchMajor);
6959 processToolMajor(mapper, rawToolMajor);
6960 processMTSync(mapper);
6961 processSync(mapper);
6962
6963 NotifyMotionArgs args;
6964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6966 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6967}
6968
6969TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970 addConfigurationProperty("touch.deviceType", "touchScreen");
6971 prepareDisplay(DISPLAY_ORIENTATION_0);
6972 prepareAxes(POSITION | PRESSURE);
6973 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6974 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006975 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976
Michael Wrightaa449c92017-12-13 21:21:43 +00006977 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006978 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006979 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6980 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6981 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6982
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983 // These calculations are based on the input device calibration documentation.
6984 int32_t rawX = 100;
6985 int32_t rawY = 200;
6986 int32_t rawPressure = 60;
6987
6988 float x = toDisplayX(rawX);
6989 float y = toDisplayY(rawY);
6990 float pressure = float(rawPressure) * 0.01f;
6991
6992 processPosition(mapper, rawX, rawY);
6993 processPressure(mapper, rawPressure);
6994 processMTSync(mapper);
6995 processSync(mapper);
6996
6997 NotifyMotionArgs args;
6998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7000 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7001}
7002
7003TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004 addConfigurationProperty("touch.deviceType", "touchScreen");
7005 prepareDisplay(DISPLAY_ORIENTATION_0);
7006 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008
7009 NotifyMotionArgs motionArgs;
7010 NotifyKeyArgs keyArgs;
7011
7012 processId(mapper, 1);
7013 processPosition(mapper, 100, 200);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7016 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7017 ASSERT_EQ(0, motionArgs.buttonState);
7018
7019 // press BTN_LEFT, release BTN_LEFT
7020 processKey(mapper, BTN_LEFT, 1);
7021 processSync(mapper);
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7024 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7025
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7028 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7029
Michael Wrightd02c5b62014-02-10 15:10:22 -08007030 processKey(mapper, BTN_LEFT, 0);
7031 processSync(mapper);
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007034 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007035
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007038 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039
7040 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7041 processKey(mapper, BTN_RIGHT, 1);
7042 processKey(mapper, BTN_MIDDLE, 1);
7043 processSync(mapper);
7044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7046 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7047 motionArgs.buttonState);
7048
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7051 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7052
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7055 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7056 motionArgs.buttonState);
7057
Michael Wrightd02c5b62014-02-10 15:10:22 -08007058 processKey(mapper, BTN_RIGHT, 0);
7059 processSync(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007062 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007063
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007066 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067
7068 processKey(mapper, BTN_MIDDLE, 0);
7069 processSync(mapper);
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007072 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007073
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007076 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077
7078 // press BTN_BACK, release BTN_BACK
7079 processKey(mapper, BTN_BACK, 1);
7080 processSync(mapper);
7081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7083 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007084
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007087 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7088
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7091 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092
7093 processKey(mapper, BTN_BACK, 0);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007098
7099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007101 ASSERT_EQ(0, motionArgs.buttonState);
7102
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7104 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7105 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7106
7107 // press BTN_SIDE, release BTN_SIDE
7108 processKey(mapper, BTN_SIDE, 1);
7109 processSync(mapper);
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7111 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7112 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007113
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007116 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7117
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7120 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007121
7122 processKey(mapper, BTN_SIDE, 0);
7123 processSync(mapper);
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007130 ASSERT_EQ(0, motionArgs.buttonState);
7131
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7133 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7134 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7135
7136 // press BTN_FORWARD, release BTN_FORWARD
7137 processKey(mapper, BTN_FORWARD, 1);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7140 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7141 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007142
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007145 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7146
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7149 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150
7151 processKey(mapper, BTN_FORWARD, 0);
7152 processSync(mapper);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007156
7157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007159 ASSERT_EQ(0, motionArgs.buttonState);
7160
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7162 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7163 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7164
7165 // press BTN_EXTRA, release BTN_EXTRA
7166 processKey(mapper, BTN_EXTRA, 1);
7167 processSync(mapper);
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7169 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7170 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007171
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7175
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7177 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7178 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179
7180 processKey(mapper, BTN_EXTRA, 0);
7181 processSync(mapper);
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007185
7186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007188 ASSERT_EQ(0, motionArgs.buttonState);
7189
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7191 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7192 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7193
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7195
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196 // press BTN_STYLUS, release BTN_STYLUS
7197 processKey(mapper, BTN_STYLUS, 1);
7198 processSync(mapper);
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007201 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7202
7203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7205 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206
7207 processKey(mapper, BTN_STYLUS, 0);
7208 processSync(mapper);
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007212
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007215 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216
7217 // press BTN_STYLUS2, release BTN_STYLUS2
7218 processKey(mapper, BTN_STYLUS2, 1);
7219 processSync(mapper);
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007222 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7223
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7226 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007227
7228 processKey(mapper, BTN_STYLUS2, 0);
7229 processSync(mapper);
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007233
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007236 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237
7238 // release touch
7239 processId(mapper, -1);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7243 ASSERT_EQ(0, motionArgs.buttonState);
7244}
7245
7246TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 addConfigurationProperty("touch.deviceType", "touchScreen");
7248 prepareDisplay(DISPLAY_ORIENTATION_0);
7249 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007251
7252 NotifyMotionArgs motionArgs;
7253
7254 // default tool type is finger
7255 processId(mapper, 1);
7256 processPosition(mapper, 100, 200);
7257 processSync(mapper);
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7261
7262 // eraser
7263 processKey(mapper, BTN_TOOL_RUBBER, 1);
7264 processSync(mapper);
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7268
7269 // stylus
7270 processKey(mapper, BTN_TOOL_RUBBER, 0);
7271 processKey(mapper, BTN_TOOL_PEN, 1);
7272 processSync(mapper);
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7276
7277 // brush
7278 processKey(mapper, BTN_TOOL_PEN, 0);
7279 processKey(mapper, BTN_TOOL_BRUSH, 1);
7280 processSync(mapper);
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7284
7285 // pencil
7286 processKey(mapper, BTN_TOOL_BRUSH, 0);
7287 processKey(mapper, BTN_TOOL_PENCIL, 1);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7292
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007293 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007294 processKey(mapper, BTN_TOOL_PENCIL, 0);
7295 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7300
7301 // mouse
7302 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7303 processKey(mapper, BTN_TOOL_MOUSE, 1);
7304 processSync(mapper);
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7308
7309 // lens
7310 processKey(mapper, BTN_TOOL_MOUSE, 0);
7311 processKey(mapper, BTN_TOOL_LENS, 1);
7312 processSync(mapper);
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7316
7317 // double-tap
7318 processKey(mapper, BTN_TOOL_LENS, 0);
7319 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7320 processSync(mapper);
7321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7324
7325 // triple-tap
7326 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7327 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7328 processSync(mapper);
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7332
7333 // quad-tap
7334 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7335 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7336 processSync(mapper);
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7340
7341 // finger
7342 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7343 processKey(mapper, BTN_TOOL_FINGER, 1);
7344 processSync(mapper);
7345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7348
7349 // stylus trumps finger
7350 processKey(mapper, BTN_TOOL_PEN, 1);
7351 processSync(mapper);
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7355
7356 // eraser trumps stylus
7357 processKey(mapper, BTN_TOOL_RUBBER, 1);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7362
7363 // mouse trumps eraser
7364 processKey(mapper, BTN_TOOL_MOUSE, 1);
7365 processSync(mapper);
7366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7369
7370 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7371 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7376
7377 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7378 processToolType(mapper, MT_TOOL_PEN);
7379 processSync(mapper);
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7383
7384 // back to default tool type
7385 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7386 processKey(mapper, BTN_TOOL_MOUSE, 0);
7387 processKey(mapper, BTN_TOOL_RUBBER, 0);
7388 processKey(mapper, BTN_TOOL_PEN, 0);
7389 processKey(mapper, BTN_TOOL_FINGER, 0);
7390 processSync(mapper);
7391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7394}
7395
7396TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007397 addConfigurationProperty("touch.deviceType", "touchScreen");
7398 prepareDisplay(DISPLAY_ORIENTATION_0);
7399 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007400 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007401 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007402
7403 NotifyMotionArgs motionArgs;
7404
7405 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7406 processId(mapper, 1);
7407 processPosition(mapper, 100, 200);
7408 processSync(mapper);
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7412 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7413
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7417 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7418
7419 // move a little
7420 processPosition(mapper, 150, 250);
7421 processSync(mapper);
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7425 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7426
7427 // down when BTN_TOUCH is pressed, pressure defaults to 1
7428 processKey(mapper, BTN_TOUCH, 1);
7429 processSync(mapper);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7433 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7434
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7436 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7438 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7439
7440 // up when BTN_TOUCH is released, hover restored
7441 processKey(mapper, BTN_TOUCH, 0);
7442 processSync(mapper);
7443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7446 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7447
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7451 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7452
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7454 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7456 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7457
7458 // exit hover when pointer goes away
7459 processId(mapper, -1);
7460 processSync(mapper);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7464 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7465}
7466
7467TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007468 addConfigurationProperty("touch.deviceType", "touchScreen");
7469 prepareDisplay(DISPLAY_ORIENTATION_0);
7470 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007471 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007472
7473 NotifyMotionArgs motionArgs;
7474
7475 // initially hovering because pressure is 0
7476 processId(mapper, 1);
7477 processPosition(mapper, 100, 200);
7478 processPressure(mapper, 0);
7479 processSync(mapper);
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7483 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7484
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7489
7490 // move a little
7491 processPosition(mapper, 150, 250);
7492 processSync(mapper);
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7496 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7497
7498 // down when pressure becomes non-zero
7499 processPressure(mapper, RAW_PRESSURE_MAX);
7500 processSync(mapper);
7501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7504 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7505
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7507 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7509 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7510
7511 // up when pressure becomes 0, hover restored
7512 processPressure(mapper, 0);
7513 processSync(mapper);
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7515 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7517 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7518
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7522 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7523
7524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7527 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7528
7529 // exit hover when pointer goes away
7530 processId(mapper, -1);
7531 processSync(mapper);
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7536}
7537
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007538/**
7539 * Set the input device port <--> display port associations, and check that the
7540 * events are routed to the display that matches the display port.
7541 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7542 */
7543TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007544 const std::string usb2 = "USB2";
7545 const uint8_t hdmi1 = 0;
7546 const uint8_t hdmi2 = 1;
7547 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007548 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007549
7550 addConfigurationProperty("touch.deviceType", "touchScreen");
7551 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007552 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007553
7554 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7555 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7556
7557 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7558 // for this input device is specified, and the matching viewport is not present,
7559 // the input device should be disabled (at the mapper level).
7560
7561 // Add viewport for display 2 on hdmi2
7562 prepareSecondaryDisplay(type, hdmi2);
7563 // Send a touch event
7564 processPosition(mapper, 100, 100);
7565 processSync(mapper);
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7567
7568 // Add viewport for display 1 on hdmi1
7569 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7570 // Send a touch event again
7571 processPosition(mapper, 100, 100);
7572 processSync(mapper);
7573
7574 NotifyMotionArgs args;
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7576 ASSERT_EQ(DISPLAY_ID, args.displayId);
7577}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007579TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007580 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007581 std::shared_ptr<FakePointerController> fakePointerController =
7582 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007583 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007584 fakePointerController->setPosition(100, 200);
7585 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007586 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7587
Garfield Tan888a6a42020-01-09 11:39:16 -08007588 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007589 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007590
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007591 prepareDisplay(DISPLAY_ORIENTATION_0);
7592 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007594
7595 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007596 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007597
7598 NotifyMotionArgs motionArgs;
7599 processPosition(mapper, 100, 100);
7600 processSync(mapper);
7601
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7604 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7605}
7606
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007607/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007608 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7609 */
7610TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7611 addConfigurationProperty("touch.deviceType", "touchScreen");
7612 prepareAxes(POSITION);
7613 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7614
7615 prepareDisplay(DISPLAY_ORIENTATION_0);
7616 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7617 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7618 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7619 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7620
7621 NotifyMotionArgs args;
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7623 ASSERT_EQ(26, args.readTime);
7624
7625 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7626 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7627 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7628
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7630 ASSERT_EQ(33, args.readTime);
7631}
7632
7633/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007634 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7635 * events should not be delivered to the listener.
7636 */
7637TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7638 addConfigurationProperty("touch.deviceType", "touchScreen");
7639 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7640 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7641 ViewportType::INTERNAL);
7642 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7643 prepareAxes(POSITION);
7644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7645
7646 NotifyMotionArgs motionArgs;
7647 processPosition(mapper, 100, 100);
7648 processSync(mapper);
7649
7650 mFakeListener->assertNotifyMotionWasNotCalled();
7651}
7652
Garfield Tanc734e4f2021-01-15 20:01:39 -08007653TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7654 addConfigurationProperty("touch.deviceType", "touchScreen");
7655 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7656 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7657 ViewportType::INTERNAL);
7658 std::optional<DisplayViewport> optionalDisplayViewport =
7659 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7660 ASSERT_TRUE(optionalDisplayViewport.has_value());
7661 DisplayViewport displayViewport = *optionalDisplayViewport;
7662
7663 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7664 prepareAxes(POSITION);
7665 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7666
7667 // Finger down
7668 int32_t x = 100, y = 100;
7669 processPosition(mapper, x, y);
7670 processSync(mapper);
7671
7672 NotifyMotionArgs motionArgs;
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7674 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7675
7676 // Deactivate display viewport
7677 displayViewport.isActive = false;
7678 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7679 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7680
7681 // Finger move
7682 x += 10, y += 10;
7683 processPosition(mapper, x, y);
7684 processSync(mapper);
7685
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7687 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7688
7689 // Reactivate display viewport
7690 displayViewport.isActive = true;
7691 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7692 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7693
7694 // Finger move again
7695 x += 10, y += 10;
7696 processPosition(mapper, x, y);
7697 processSync(mapper);
7698
7699 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7700 // no pointer on the touch device.
7701 mFakeListener->assertNotifyMotionWasNotCalled();
7702}
7703
Arthur Hung7c645402019-01-25 17:45:42 +08007704TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7705 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007706 prepareAxes(POSITION | ID | SLOT);
7707 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007708 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007709
7710 // Create the second touch screen device, and enable multi fingers.
7711 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007712 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007713 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007714 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007715 std::shared_ptr<InputDevice> device2 =
7716 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7717 Flags<InputDeviceClass>(0));
7718
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007719 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7720 0 /*flat*/, 0 /*fuzz*/);
7721 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7722 0 /*flat*/, 0 /*fuzz*/);
7723 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7724 0 /*flat*/, 0 /*fuzz*/);
7725 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7726 0 /*flat*/, 0 /*fuzz*/);
7727 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7728 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7729 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007730
7731 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007732 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007733 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7734 device2->reset(ARBITRARY_TIME);
7735
7736 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007737 std::shared_ptr<FakePointerController> fakePointerController =
7738 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007739 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7740 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7741
7742 // Setup policy for associated displays and show touches.
7743 const uint8_t hdmi1 = 0;
7744 const uint8_t hdmi2 = 1;
7745 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7746 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7747 mFakePolicy->setShowTouches(true);
7748
7749 // Create displays.
7750 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007751 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007752
7753 // Default device will reconfigure above, need additional reconfiguration for another device.
7754 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007755 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007756
7757 // Two fingers down at default display.
7758 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7759 processPosition(mapper, x1, y1);
7760 processId(mapper, 1);
7761 processSlot(mapper, 1);
7762 processPosition(mapper, x2, y2);
7763 processId(mapper, 2);
7764 processSync(mapper);
7765
7766 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7767 fakePointerController->getSpots().find(DISPLAY_ID);
7768 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7769 ASSERT_EQ(size_t(2), iter->second.size());
7770
7771 // Two fingers down at second display.
7772 processPosition(mapper2, x1, y1);
7773 processId(mapper2, 1);
7774 processSlot(mapper2, 1);
7775 processPosition(mapper2, x2, y2);
7776 processId(mapper2, 2);
7777 processSync(mapper2);
7778
7779 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7780 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7781 ASSERT_EQ(size_t(2), iter->second.size());
7782}
7783
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007784TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007785 prepareAxes(POSITION);
7786 addConfigurationProperty("touch.deviceType", "touchScreen");
7787 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007788 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007789
7790 NotifyMotionArgs motionArgs;
7791 // Unrotated video frame
7792 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7793 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007794 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007795 processPosition(mapper, 100, 200);
7796 processSync(mapper);
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7798 ASSERT_EQ(frames, motionArgs.videoFrames);
7799
7800 // Subsequent touch events should not have any videoframes
7801 // This is implemented separately in FakeEventHub,
7802 // but that should match the behaviour of TouchVideoDevice.
7803 processPosition(mapper, 200, 200);
7804 processSync(mapper);
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7806 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7807}
7808
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007809TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007810 prepareAxes(POSITION);
7811 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007812 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007813 // Unrotated video frame
7814 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7815 NotifyMotionArgs motionArgs;
7816
7817 // Test all 4 orientations
7818 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7819 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7820 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7821 clearViewports();
7822 prepareDisplay(orientation);
7823 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007824 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007825 processPosition(mapper, 100, 200);
7826 processSync(mapper);
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7828 frames[0].rotate(orientation);
7829 ASSERT_EQ(frames, motionArgs.videoFrames);
7830 }
7831}
7832
7833TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007834 prepareAxes(POSITION);
7835 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007836 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007837 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7838 // so mix these.
7839 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7840 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7841 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7842 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7843 NotifyMotionArgs motionArgs;
7844
7845 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007846 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007847 processPosition(mapper, 100, 200);
7848 processSync(mapper);
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7850 std::for_each(frames.begin(), frames.end(),
7851 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7852 ASSERT_EQ(frames, motionArgs.videoFrames);
7853}
7854
Arthur Hung9da14732019-09-02 16:16:58 +08007855/**
7856 * If we had defined port associations, but the viewport is not ready, the touch device would be
7857 * expected to be disabled, and it should be enabled after the viewport has found.
7858 */
7859TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007860 constexpr uint8_t hdmi2 = 1;
7861 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007862 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007863
7864 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7865
7866 addConfigurationProperty("touch.deviceType", "touchScreen");
7867 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007868 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007869
7870 ASSERT_EQ(mDevice->isEnabled(), false);
7871
7872 // Add display on hdmi2, the device should be enabled and can receive touch event.
7873 prepareSecondaryDisplay(type, hdmi2);
7874 ASSERT_EQ(mDevice->isEnabled(), true);
7875
7876 // Send a touch event.
7877 processPosition(mapper, 100, 100);
7878 processSync(mapper);
7879
7880 NotifyMotionArgs args;
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7882 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7883}
7884
Arthur Hung421eb1c2020-01-16 00:09:42 +08007885TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007886 addConfigurationProperty("touch.deviceType", "touchScreen");
7887 prepareDisplay(DISPLAY_ORIENTATION_0);
7888 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007889 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007890
7891 NotifyMotionArgs motionArgs;
7892
7893 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7894 // finger down
7895 processId(mapper, 1);
7896 processPosition(mapper, x1, y1);
7897 processSync(mapper);
7898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7899 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7901
7902 // finger move
7903 processId(mapper, 1);
7904 processPosition(mapper, x2, y2);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7909
7910 // finger up.
7911 processId(mapper, -1);
7912 processSync(mapper);
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7914 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7916
7917 // new finger down
7918 processId(mapper, 1);
7919 processPosition(mapper, x3, y3);
7920 processSync(mapper);
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7924}
7925
7926/**
arthurhungcc7f9802020-04-30 17:55:40 +08007927 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7928 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007929 */
arthurhungcc7f9802020-04-30 17:55:40 +08007930TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007931 addConfigurationProperty("touch.deviceType", "touchScreen");
7932 prepareDisplay(DISPLAY_ORIENTATION_0);
7933 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007934 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007935
7936 NotifyMotionArgs motionArgs;
7937
7938 // default tool type is finger
7939 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007940 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007941 processPosition(mapper, x1, y1);
7942 processSync(mapper);
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7946
7947 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7948 processToolType(mapper, MT_TOOL_PALM);
7949 processSync(mapper);
7950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7951 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7952
7953 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007954 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007955 processPosition(mapper, x2, y2);
7956 processSync(mapper);
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7958
7959 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007960 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7963
7964 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007965 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007966 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007967 processPosition(mapper, x3, y3);
7968 processSync(mapper);
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7970 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7972}
7973
arthurhungbf89a482020-04-17 17:37:55 +08007974/**
arthurhungcc7f9802020-04-30 17:55:40 +08007975 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7976 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007977 */
arthurhungcc7f9802020-04-30 17:55:40 +08007978TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007979 addConfigurationProperty("touch.deviceType", "touchScreen");
7980 prepareDisplay(DISPLAY_ORIENTATION_0);
7981 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7982 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7983
7984 NotifyMotionArgs motionArgs;
7985
7986 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007987 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7988 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007989 processPosition(mapper, x1, y1);
7990 processSync(mapper);
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7994
7995 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007996 processSlot(mapper, SECOND_SLOT);
7997 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007998 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007999 processSync(mapper);
8000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8001 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8002 motionArgs.action);
8003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8004
8005 // If the tool type of the first finger changes to MT_TOOL_PALM,
8006 // we expect to receive ACTION_POINTER_UP with cancel flag.
8007 processSlot(mapper, FIRST_SLOT);
8008 processId(mapper, FIRST_TRACKING_ID);
8009 processToolType(mapper, MT_TOOL_PALM);
8010 processSync(mapper);
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8012 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8013 motionArgs.action);
8014 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8015
8016 // The following MOVE events of second finger should be processed.
8017 processSlot(mapper, SECOND_SLOT);
8018 processId(mapper, SECOND_TRACKING_ID);
8019 processPosition(mapper, x2 + 1, y2 + 1);
8020 processSync(mapper);
8021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8023 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8024
8025 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8026 // it. Second finger receive move.
8027 processSlot(mapper, FIRST_SLOT);
8028 processId(mapper, INVALID_TRACKING_ID);
8029 processSync(mapper);
8030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8032 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8033
8034 // Second finger keeps moving.
8035 processSlot(mapper, SECOND_SLOT);
8036 processId(mapper, SECOND_TRACKING_ID);
8037 processPosition(mapper, x2 + 2, y2 + 2);
8038 processSync(mapper);
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8041 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8042
8043 // Second finger up.
8044 processId(mapper, INVALID_TRACKING_ID);
8045 processSync(mapper);
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8047 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8048 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8049}
8050
8051/**
8052 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8053 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8054 */
8055TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8056 addConfigurationProperty("touch.deviceType", "touchScreen");
8057 prepareDisplay(DISPLAY_ORIENTATION_0);
8058 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8059 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8060
8061 NotifyMotionArgs motionArgs;
8062
8063 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8064 // First finger down.
8065 processId(mapper, FIRST_TRACKING_ID);
8066 processPosition(mapper, x1, y1);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8071
8072 // Second finger down.
8073 processSlot(mapper, SECOND_SLOT);
8074 processId(mapper, SECOND_TRACKING_ID);
8075 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008076 processSync(mapper);
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8078 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8079 motionArgs.action);
8080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8081
arthurhungcc7f9802020-04-30 17:55:40 +08008082 // If the tool type of the first finger changes to MT_TOOL_PALM,
8083 // we expect to receive ACTION_POINTER_UP with cancel flag.
8084 processSlot(mapper, FIRST_SLOT);
8085 processId(mapper, FIRST_TRACKING_ID);
8086 processToolType(mapper, MT_TOOL_PALM);
8087 processSync(mapper);
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8090 motionArgs.action);
8091 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8092
8093 // Second finger keeps moving.
8094 processSlot(mapper, SECOND_SLOT);
8095 processId(mapper, SECOND_TRACKING_ID);
8096 processPosition(mapper, x2 + 1, y2 + 1);
8097 processSync(mapper);
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8100
8101 // second finger becomes palm, receive cancel due to only 1 finger is active.
8102 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008103 processToolType(mapper, MT_TOOL_PALM);
8104 processSync(mapper);
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8106 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8107
arthurhungcc7f9802020-04-30 17:55:40 +08008108 // third finger down.
8109 processSlot(mapper, THIRD_SLOT);
8110 processId(mapper, THIRD_TRACKING_ID);
8111 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008112 processPosition(mapper, x3, y3);
8113 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8115 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008117 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8118
8119 // third finger move
8120 processId(mapper, THIRD_TRACKING_ID);
8121 processPosition(mapper, x3 + 1, y3 + 1);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8125
8126 // first finger up, third 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 up, third finger receive move.
8135 processSlot(mapper, SECOND_SLOT);
8136 processId(mapper, INVALID_TRACKING_ID);
8137 processSync(mapper);
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8140 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8141
8142 // third finger up.
8143 processSlot(mapper, THIRD_SLOT);
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 from some finger,
8153 * and the active finger could still be allowed to receive the events
8154 */
8155TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
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 // default tool type is finger
8164 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
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);
8176 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
8182 // If the tool type of the second finger changes to MT_TOOL_PALM,
8183 // we expect to receive ACTION_POINTER_UP with cancel flag.
8184 processId(mapper, SECOND_TRACKING_ID);
8185 processToolType(mapper, MT_TOOL_PALM);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8189 motionArgs.action);
8190 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8191
8192 // The following MOVE event should be processed.
8193 processSlot(mapper, FIRST_SLOT);
8194 processId(mapper, FIRST_TRACKING_ID);
8195 processPosition(mapper, x1 + 1, y1 + 1);
8196 processSync(mapper);
8197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8199 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8200
8201 // second finger up.
8202 processSlot(mapper, SECOND_SLOT);
8203 processId(mapper, INVALID_TRACKING_ID);
8204 processSync(mapper);
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8207
8208 // first finger keep moving
8209 processSlot(mapper, FIRST_SLOT);
8210 processId(mapper, FIRST_TRACKING_ID);
8211 processPosition(mapper, x1 + 2, y1 + 2);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8215
8216 // first finger up.
8217 processId(mapper, INVALID_TRACKING_ID);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8221 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008222}
8223
Arthur Hung9ad18942021-06-19 02:04:46 +00008224/**
8225 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8226 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8227 * cause slot be valid again.
8228 */
8229TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8230 addConfigurationProperty("touch.deviceType", "touchScreen");
8231 prepareDisplay(DISPLAY_ORIENTATION_0);
8232 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8233 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8234
8235 NotifyMotionArgs motionArgs;
8236
8237 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8238 // First finger down.
8239 processId(mapper, FIRST_TRACKING_ID);
8240 processPosition(mapper, x1, y1);
8241 processPressure(mapper, RAW_PRESSURE_MAX);
8242 processSync(mapper);
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8245 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8246
8247 // First finger move.
8248 processId(mapper, FIRST_TRACKING_ID);
8249 processPosition(mapper, x1 + 1, y1 + 1);
8250 processPressure(mapper, RAW_PRESSURE_MAX);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8254 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8255
8256 // Second finger down.
8257 processSlot(mapper, SECOND_SLOT);
8258 processId(mapper, SECOND_TRACKING_ID);
8259 processPosition(mapper, x2, y2);
8260 processPressure(mapper, RAW_PRESSURE_MAX);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8263 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8264 motionArgs.action);
8265 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8266
8267 // second finger up with some unexpected data.
8268 processSlot(mapper, SECOND_SLOT);
8269 processId(mapper, INVALID_TRACKING_ID);
8270 processPosition(mapper, x2, y2);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8273 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8274 motionArgs.action);
8275 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8276
8277 // first finger up with some unexpected data.
8278 processSlot(mapper, FIRST_SLOT);
8279 processId(mapper, INVALID_TRACKING_ID);
8280 processPosition(mapper, x2, y2);
8281 processPressure(mapper, RAW_PRESSURE_MAX);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8284 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8285 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8286}
8287
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008288// --- MultiTouchInputMapperTest_ExternalDevice ---
8289
8290class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8291protected:
Chris Yea52ade12020-08-27 16:49:20 -07008292 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008293};
8294
8295/**
8296 * Expect fallback to internal viewport if device is external and external viewport is not present.
8297 */
8298TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8299 prepareAxes(POSITION);
8300 addConfigurationProperty("touch.deviceType", "touchScreen");
8301 prepareDisplay(DISPLAY_ORIENTATION_0);
8302 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8303
8304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8305
8306 NotifyMotionArgs motionArgs;
8307
8308 // Expect the event to be sent to the internal viewport,
8309 // because an external viewport is not present.
8310 processPosition(mapper, 100, 100);
8311 processSync(mapper);
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8314
8315 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008316 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008317 processPosition(mapper, 100, 100);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8321}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008322
8323/**
8324 * Test touch should not work if outside of surface.
8325 */
8326class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8327protected:
8328 void halfDisplayToCenterHorizontal(int32_t orientation) {
8329 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008330 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008331
8332 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8333 internalViewport->orientation = orientation;
8334 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8335 internalViewport->logicalLeft = 0;
8336 internalViewport->logicalTop = 0;
8337 internalViewport->logicalRight = DISPLAY_HEIGHT;
8338 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8339
8340 internalViewport->physicalLeft = 0;
8341 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8342 internalViewport->physicalRight = DISPLAY_HEIGHT;
8343 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8344
8345 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8346 internalViewport->deviceHeight = DISPLAY_WIDTH;
8347 } else {
8348 internalViewport->logicalLeft = 0;
8349 internalViewport->logicalTop = 0;
8350 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8351 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8352
8353 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8354 internalViewport->physicalTop = 0;
8355 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8356 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8357
8358 internalViewport->deviceWidth = DISPLAY_WIDTH;
8359 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8360 }
8361
8362 mFakePolicy->updateViewport(internalViewport.value());
8363 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8364 }
8365
arthurhung5d547942020-12-14 17:04:45 +08008366 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8367 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008368 int32_t yExpected) {
8369 // touch on outside area should not work.
8370 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8371 processSync(mapper);
8372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8373
8374 // touch on inside area should receive the event.
8375 NotifyMotionArgs args;
8376 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8377 processSync(mapper);
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8379 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8380 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8381
8382 // Reset.
8383 mapper.reset(ARBITRARY_TIME);
8384 }
8385};
8386
arthurhung5d547942020-12-14 17:04:45 +08008387TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008388 addConfigurationProperty("touch.deviceType", "touchScreen");
8389 prepareDisplay(DISPLAY_ORIENTATION_0);
8390 prepareAxes(POSITION);
8391 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8392
8393 // Touch on center of normal display should work.
8394 const int32_t x = DISPLAY_WIDTH / 4;
8395 const int32_t y = DISPLAY_HEIGHT / 2;
8396 processPosition(mapper, toRawX(x), toRawY(y));
8397 processSync(mapper);
8398 NotifyMotionArgs args;
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8401 0.0f, 0.0f, 0.0f, 0.0f));
8402 // Reset.
8403 mapper.reset(ARBITRARY_TIME);
8404
8405 // Let physical display be different to device, and make surface and physical could be 1:1.
8406 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8407
8408 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8409 const int32_t yExpected = y;
8410 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8411}
8412
arthurhung5d547942020-12-14 17:04:45 +08008413TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008414 addConfigurationProperty("touch.deviceType", "touchScreen");
8415 prepareDisplay(DISPLAY_ORIENTATION_0);
8416 prepareAxes(POSITION);
8417 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8418
8419 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8420 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8421
8422 const int32_t x = DISPLAY_WIDTH / 4;
8423 const int32_t y = DISPLAY_HEIGHT / 2;
8424
8425 // expect x/y = swap x/y then reverse y.
8426 const int32_t xExpected = y;
8427 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8428 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8429}
8430
arthurhung5d547942020-12-14 17:04:45 +08008431TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008432 addConfigurationProperty("touch.deviceType", "touchScreen");
8433 prepareDisplay(DISPLAY_ORIENTATION_0);
8434 prepareAxes(POSITION);
8435 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8436
8437 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8438 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8439
8440 const int32_t x = DISPLAY_WIDTH / 4;
8441 const int32_t y = DISPLAY_HEIGHT / 2;
8442
8443 // expect x/y = swap x/y then reverse x.
8444 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8445 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8446 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8447}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008448
arthurhunga36b28e2020-12-29 20:28:15 +08008449TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8450 addConfigurationProperty("touch.deviceType", "touchScreen");
8451 prepareDisplay(DISPLAY_ORIENTATION_0);
8452 prepareAxes(POSITION);
8453 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8454
8455 const int32_t x = 0;
8456 const int32_t y = 0;
8457
8458 const int32_t xExpected = x;
8459 const int32_t yExpected = y;
8460 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8461
8462 clearViewports();
8463 prepareDisplay(DISPLAY_ORIENTATION_90);
8464 // expect x/y = swap x/y then reverse y.
8465 const int32_t xExpected90 = y;
8466 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8467 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8468
8469 clearViewports();
8470 prepareDisplay(DISPLAY_ORIENTATION_270);
8471 // expect x/y = swap x/y then reverse x.
8472 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8473 const int32_t yExpected270 = x;
8474 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8475}
8476
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008477TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8478 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8479 std::shared_ptr<FakePointerController> fakePointerController =
8480 std::make_shared<FakePointerController>();
8481 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8482 fakePointerController->setPosition(0, 0);
8483 fakePointerController->setButtonState(0);
8484
8485 // prepare device and capture
8486 prepareDisplay(DISPLAY_ORIENTATION_0);
8487 prepareAxes(POSITION | ID | SLOT);
8488 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8489 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8490 mFakePolicy->setPointerCapture(true);
8491 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8492 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8493
8494 // captured touchpad should be a touchpad source
8495 NotifyDeviceResetArgs resetArgs;
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8497 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8498
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008499 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008500
8501 const InputDeviceInfo::MotionRange* relRangeX =
8502 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8503 ASSERT_NE(relRangeX, nullptr);
8504 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8505 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8506 const InputDeviceInfo::MotionRange* relRangeY =
8507 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8508 ASSERT_NE(relRangeY, nullptr);
8509 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8510 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8511
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008512 // run captured pointer tests - note that this is unscaled, so input listener events should be
8513 // identical to what the hardware sends (accounting for any
8514 // calibration).
8515 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008516 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008517 processId(mapper, 1);
8518 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8519 processKey(mapper, BTN_TOUCH, 1);
8520 processSync(mapper);
8521
8522 // expect coord[0] to contain initial location of touch 0
8523 NotifyMotionArgs args;
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8526 ASSERT_EQ(1U, args.pointerCount);
8527 ASSERT_EQ(0, args.pointerProperties[0].id);
8528 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8529 ASSERT_NO_FATAL_FAILURE(
8530 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8531
8532 // FINGER 1 DOWN
8533 processSlot(mapper, 1);
8534 processId(mapper, 2);
8535 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8536 processSync(mapper);
8537
8538 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008540 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8541 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008542 ASSERT_EQ(2U, args.pointerCount);
8543 ASSERT_EQ(0, args.pointerProperties[0].id);
8544 ASSERT_EQ(1, args.pointerProperties[1].id);
8545 ASSERT_NO_FATAL_FAILURE(
8546 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8547 ASSERT_NO_FATAL_FAILURE(
8548 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8549
8550 // FINGER 1 MOVE
8551 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8552 processSync(mapper);
8553
8554 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8555 // from move
8556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8558 ASSERT_NO_FATAL_FAILURE(
8559 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8560 ASSERT_NO_FATAL_FAILURE(
8561 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8562
8563 // FINGER 0 MOVE
8564 processSlot(mapper, 0);
8565 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8566 processSync(mapper);
8567
8568 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8571 ASSERT_NO_FATAL_FAILURE(
8572 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8573 ASSERT_NO_FATAL_FAILURE(
8574 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8575
8576 // BUTTON DOWN
8577 processKey(mapper, BTN_LEFT, 1);
8578 processSync(mapper);
8579
8580 // touchinputmapper design sends a move before button press
8581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8585
8586 // BUTTON UP
8587 processKey(mapper, BTN_LEFT, 0);
8588 processSync(mapper);
8589
8590 // touchinputmapper design sends a move after button release
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8592 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8595
8596 // FINGER 0 UP
8597 processId(mapper, -1);
8598 processSync(mapper);
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8601
8602 // FINGER 1 MOVE
8603 processSlot(mapper, 1);
8604 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8605 processSync(mapper);
8606
8607 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8610 ASSERT_EQ(1U, args.pointerCount);
8611 ASSERT_EQ(1, args.pointerProperties[0].id);
8612 ASSERT_NO_FATAL_FAILURE(
8613 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8614
8615 // FINGER 1 UP
8616 processId(mapper, -1);
8617 processKey(mapper, BTN_TOUCH, 0);
8618 processSync(mapper);
8619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8620 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8621
8622 // non captured touchpad should be a mouse source
8623 mFakePolicy->setPointerCapture(false);
8624 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8626 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8627}
8628
8629TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8630 std::shared_ptr<FakePointerController> fakePointerController =
8631 std::make_shared<FakePointerController>();
8632 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8633 fakePointerController->setPosition(0, 0);
8634 fakePointerController->setButtonState(0);
8635
8636 // prepare device and capture
8637 prepareDisplay(DISPLAY_ORIENTATION_0);
8638 prepareAxes(POSITION | ID | SLOT);
8639 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8640 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8641 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8643 // run uncaptured pointer tests - pushes out generic events
8644 // FINGER 0 DOWN
8645 processId(mapper, 3);
8646 processPosition(mapper, 100, 100);
8647 processKey(mapper, BTN_TOUCH, 1);
8648 processSync(mapper);
8649
8650 // start at (100,100), cursor should be at (0,0) * scale
8651 NotifyMotionArgs args;
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8654 ASSERT_NO_FATAL_FAILURE(
8655 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8656
8657 // FINGER 0 MOVE
8658 processPosition(mapper, 200, 200);
8659 processSync(mapper);
8660
8661 // compute scaling to help with touch position checking
8662 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8663 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8664 float scale =
8665 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8666
8667 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8671 0, 0, 0, 0, 0, 0, 0));
8672}
8673
8674TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8675 std::shared_ptr<FakePointerController> fakePointerController =
8676 std::make_shared<FakePointerController>();
8677
8678 prepareDisplay(DISPLAY_ORIENTATION_0);
8679 prepareAxes(POSITION | ID | SLOT);
8680 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8681 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8682 mFakePolicy->setPointerCapture(false);
8683 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8684
8685 // uncaptured touchpad should be a pointer device
8686 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8687
8688 // captured touchpad should be a touchpad device
8689 mFakePolicy->setPointerCapture(true);
8690 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8691 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8692}
8693
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008694// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008695
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008696class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008697protected:
8698 static const char* DEVICE_NAME;
8699 static const char* DEVICE_LOCATION;
8700 static const int32_t DEVICE_ID;
8701 static const int32_t DEVICE_GENERATION;
8702 static const int32_t DEVICE_CONTROLLER_NUMBER;
8703 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8704 static const int32_t EVENTHUB_ID;
8705
8706 std::shared_ptr<FakeEventHub> mFakeEventHub;
8707 sp<FakeInputReaderPolicy> mFakePolicy;
8708 sp<TestInputListener> mFakeListener;
8709 std::unique_ptr<InstrumentedInputReader> mReader;
8710 std::shared_ptr<InputDevice> mDevice;
8711
8712 virtual void SetUp(Flags<InputDeviceClass> classes) {
8713 mFakeEventHub = std::make_unique<FakeEventHub>();
8714 mFakePolicy = new FakeInputReaderPolicy();
8715 mFakeListener = new TestInputListener();
8716 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8717 mFakeListener);
8718 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8719 }
8720
8721 void SetUp() override { SetUp(DEVICE_CLASSES); }
8722
8723 void TearDown() override {
8724 mFakeListener.clear();
8725 mFakePolicy.clear();
8726 }
8727
8728 void configureDevice(uint32_t changes) {
8729 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8730 mReader->requestRefreshConfiguration(changes);
8731 mReader->loopOnce();
8732 }
8733 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8734 }
8735
8736 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8737 const std::string& location, int32_t eventHubId,
8738 Flags<InputDeviceClass> classes) {
8739 InputDeviceIdentifier identifier;
8740 identifier.name = name;
8741 identifier.location = location;
8742 std::shared_ptr<InputDevice> device =
8743 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8744 identifier);
8745 mReader->pushNextDevice(device);
8746 mFakeEventHub->addDevice(eventHubId, name, classes);
8747 mReader->loopOnce();
8748 return device;
8749 }
8750
8751 template <class T, typename... Args>
8752 T& addControllerAndConfigure(Args... args) {
8753 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8754
8755 return controller;
8756 }
8757};
8758
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008759const char* PeripheralControllerTest::DEVICE_NAME = "device";
8760const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8761const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8762const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8763const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8764const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008765 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008766const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008767
8768// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008769class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008770protected:
8771 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008772 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008773 }
8774};
8775
8776TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008777 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008778
8779 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8780 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8781}
8782
8783TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008784 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008785
8786 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8787 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8788}
8789
8790// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008791class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008792protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008793 void SetUp() override {
8794 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8795 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008796};
8797
Chris Ye85758332021-05-16 23:05:17 -07008798TEST_F(LightControllerTest, MonoLight) {
8799 RawLightInfo infoMono = {.id = 1,
8800 .name = "Mono",
8801 .maxBrightness = 255,
8802 .flags = InputLightClass::BRIGHTNESS,
8803 .path = ""};
8804 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008805
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008806 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008807 InputDeviceInfo info;
8808 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008809 std::vector<InputDeviceLightInfo> lights = info.getLights();
8810 ASSERT_EQ(1U, lights.size());
8811 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008812
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008813 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8814 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008815}
8816
8817TEST_F(LightControllerTest, RGBLight) {
8818 RawLightInfo infoRed = {.id = 1,
8819 .name = "red",
8820 .maxBrightness = 255,
8821 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8822 .path = ""};
8823 RawLightInfo infoGreen = {.id = 2,
8824 .name = "green",
8825 .maxBrightness = 255,
8826 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8827 .path = ""};
8828 RawLightInfo infoBlue = {.id = 3,
8829 .name = "blue",
8830 .maxBrightness = 255,
8831 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8832 .path = ""};
8833 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8834 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8835 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8836
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008837 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008838 InputDeviceInfo info;
8839 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008840 std::vector<InputDeviceLightInfo> lights = info.getLights();
8841 ASSERT_EQ(1U, lights.size());
8842 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008843
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008844 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8845 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008846}
8847
8848TEST_F(LightControllerTest, MultiColorRGBLight) {
8849 RawLightInfo infoColor = {.id = 1,
8850 .name = "red",
8851 .maxBrightness = 255,
8852 .flags = InputLightClass::BRIGHTNESS |
8853 InputLightClass::MULTI_INTENSITY |
8854 InputLightClass::MULTI_INDEX,
8855 .path = ""};
8856
8857 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8858
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008859 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008860 InputDeviceInfo info;
8861 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008862 std::vector<InputDeviceLightInfo> lights = info.getLights();
8863 ASSERT_EQ(1U, lights.size());
8864 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008865
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008866 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8867 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008868}
8869
8870TEST_F(LightControllerTest, PlayerIdLight) {
8871 RawLightInfo info1 = {.id = 1,
8872 .name = "player1",
8873 .maxBrightness = 255,
8874 .flags = InputLightClass::BRIGHTNESS,
8875 .path = ""};
8876 RawLightInfo info2 = {.id = 2,
8877 .name = "player2",
8878 .maxBrightness = 255,
8879 .flags = InputLightClass::BRIGHTNESS,
8880 .path = ""};
8881 RawLightInfo info3 = {.id = 3,
8882 .name = "player3",
8883 .maxBrightness = 255,
8884 .flags = InputLightClass::BRIGHTNESS,
8885 .path = ""};
8886 RawLightInfo info4 = {.id = 4,
8887 .name = "player4",
8888 .maxBrightness = 255,
8889 .flags = InputLightClass::BRIGHTNESS,
8890 .path = ""};
8891 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8892 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8893 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8894 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8895
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008896 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008897 InputDeviceInfo info;
8898 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008899 std::vector<InputDeviceLightInfo> lights = info.getLights();
8900 ASSERT_EQ(1U, lights.size());
8901 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008902
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008903 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8904 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
8905 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008906}
8907
Michael Wrightd02c5b62014-02-10 15:10:22 -08008908} // namespace android