blob: 02423c42d54f6ed17cd8c87a1ccf94b823f3aff5 [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
2280 mDevice->sendDown(centerPoint);
2281 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2283
2284 // ACTION_MOVE
2285 mDevice->sendMove(centerPoint + Point(1, 1));
2286 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2288
2289 // ACTION_UP
2290 mDevice->sendUp();
2291 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2292 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2293}
2294
2295TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2296 NotifyMotionArgs args;
2297 const Point centerPoint = mDevice->getCenterPoint();
2298
2299 // ACTION_DOWN
2300 mDevice->sendDown(centerPoint);
2301 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2302 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2303
2304 // ACTION_POINTER_DOWN (Second slot)
2305 const Point secondPoint = centerPoint + Point(100, 100);
2306 mDevice->sendSlot(SECOND_SLOT);
2307 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2308 mDevice->sendDown(secondPoint + Point(1, 1));
2309 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2310 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2311 args.action);
2312
2313 // ACTION_MOVE (Second slot)
2314 mDevice->sendMove(secondPoint);
2315 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2317
2318 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002319 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002320 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002321 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002322 args.action);
2323
2324 // ACTION_UP
2325 mDevice->sendSlot(FIRST_SLOT);
2326 mDevice->sendUp();
2327 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2328 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2329}
2330
2331TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2332 NotifyMotionArgs args;
2333 const Point centerPoint = mDevice->getCenterPoint();
2334
2335 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002336 mDevice->sendSlot(FIRST_SLOT);
2337 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002338 mDevice->sendDown(centerPoint);
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2340 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2341
arthurhungcc7f9802020-04-30 17:55:40 +08002342 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002343 const Point secondPoint = centerPoint + Point(100, 100);
2344 mDevice->sendSlot(SECOND_SLOT);
2345 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2346 mDevice->sendDown(secondPoint);
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2348 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2349 args.action);
2350
arthurhungcc7f9802020-04-30 17:55:40 +08002351 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002352 mDevice->sendMove(secondPoint + Point(1, 1));
2353 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2355
arthurhungcc7f9802020-04-30 17:55:40 +08002356 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2357 // a palm event.
2358 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002359 mDevice->sendToolType(MT_TOOL_PALM);
2360 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002361 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2362 args.action);
2363 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002364
arthurhungcc7f9802020-04-30 17:55:40 +08002365 // Send up to second slot, expect first slot send moving.
2366 mDevice->sendPointerUp();
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002369
arthurhungcc7f9802020-04-30 17:55:40 +08002370 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002371 mDevice->sendSlot(FIRST_SLOT);
2372 mDevice->sendUp();
2373
arthurhungcc7f9802020-04-30 17:55:40 +08002374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2375 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002376}
2377
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379class InputDeviceTest : public testing::Test {
2380protected:
2381 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002382 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383 static const int32_t DEVICE_ID;
2384 static const int32_t DEVICE_GENERATION;
2385 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002386 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002387 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002388
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002389 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002391 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002392 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002393 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394
Chris Yea52ade12020-08-27 16:49:20 -07002395 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002396 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002398 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002399 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2400 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 InputDeviceIdentifier identifier;
2402 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002403 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002404 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002405 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002406 mReader->pushNextDevice(mDevice);
2407 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2408 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409 }
2410
Chris Yea52ade12020-08-27 16:49:20 -07002411 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412 mFakeListener.clear();
2413 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414 }
2415};
2416
2417const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002418const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002419const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2421const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002422const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2423 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002424const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425
2426TEST_F(InputDeviceTest, ImmutableProperties) {
2427 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002428 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002429 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430}
2431
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002432TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2433 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002434}
2435
Michael Wrightd02c5b62014-02-10 15:10:22 -08002436TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2437 // Configuration.
2438 InputReaderConfiguration config;
2439 mDevice->configure(ARBITRARY_TIME, &config, 0);
2440
2441 // Reset.
2442 mDevice->reset(ARBITRARY_TIME);
2443
2444 NotifyDeviceResetArgs resetArgs;
2445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2446 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2447 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2448
2449 // Metadata.
2450 ASSERT_TRUE(mDevice->isIgnored());
2451 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2452
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002453 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002455 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2457 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2458
2459 // State queries.
2460 ASSERT_EQ(0, mDevice->getMetaState());
2461
2462 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2463 << "Ignored device should return unknown key code state.";
2464 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2465 << "Ignored device should return unknown scan code state.";
2466 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2467 << "Ignored device should return unknown switch state.";
2468
2469 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2470 uint8_t flags[2] = { 0, 1 };
2471 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2472 << "Ignored device should never mark any key codes.";
2473 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2474 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2475}
2476
2477TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2478 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002479 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002481 FakeInputMapper& mapper1 =
2482 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002483 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2484 mapper1.setMetaState(AMETA_ALT_ON);
2485 mapper1.addSupportedKeyCode(AKEYCODE_A);
2486 mapper1.addSupportedKeyCode(AKEYCODE_B);
2487 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2488 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2489 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2490 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2491 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002492
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002493 FakeInputMapper& mapper2 =
2494 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002495 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496
2497 InputReaderConfiguration config;
2498 mDevice->configure(ARBITRARY_TIME, &config, 0);
2499
2500 String8 propertyValue;
2501 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2502 << "Device should have read configuration during configuration phase.";
2503 ASSERT_STREQ("value", propertyValue.string());
2504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002505 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2506 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507
2508 // Reset
2509 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002510 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2511 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002512
2513 NotifyDeviceResetArgs resetArgs;
2514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2515 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2516 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2517
2518 // Metadata.
2519 ASSERT_FALSE(mDevice->isIgnored());
2520 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2521
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002522 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002524 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2526 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2527
2528 // State queries.
2529 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2530 << "Should query mappers and combine meta states.";
2531
2532 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2533 << "Should return unknown key code state when source not supported.";
2534 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2535 << "Should return unknown scan code state when source not supported.";
2536 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2537 << "Should return unknown switch state when source not supported.";
2538
2539 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2540 << "Should query mapper when source is supported.";
2541 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2542 << "Should query mapper when source is supported.";
2543 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2544 << "Should query mapper when source is supported.";
2545
2546 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2547 uint8_t flags[4] = { 0, 0, 0, 1 };
2548 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2549 << "Should do nothing when source is unsupported.";
2550 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2551 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2552 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2553 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2554
2555 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2556 << "Should query mapper when source is supported.";
2557 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2558 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2559 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2560 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2561
2562 // Event handling.
2563 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002564 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002565 mDevice->process(&event, 1);
2566
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002567 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2568 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002569}
2570
Arthur Hung2c9a3342019-07-23 14:18:59 +08002571// A single input device is associated with a specific display. Check that:
2572// 1. Device is disabled if the viewport corresponding to the associated display is not found
2573// 2. Device is disabled when setEnabled API is called
2574TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002575 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002576
2577 // First Configuration.
2578 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2579
2580 // Device should be enabled by default.
2581 ASSERT_TRUE(mDevice->isEnabled());
2582
2583 // Prepare associated info.
2584 constexpr uint8_t hdmi = 1;
2585 const std::string UNIQUE_ID = "local:1";
2586
2587 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2588 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2589 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2590 // Device should be disabled because it is associated with a specific display via
2591 // input port <-> display port association, but the corresponding display is not found
2592 ASSERT_FALSE(mDevice->isEnabled());
2593
2594 // Prepare displays.
2595 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002596 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2597 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002598 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2599 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2600 ASSERT_TRUE(mDevice->isEnabled());
2601
2602 // Device should be disabled after set disable.
2603 mFakePolicy->addDisabledDevice(mDevice->getId());
2604 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2605 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2606 ASSERT_FALSE(mDevice->isEnabled());
2607
2608 // Device should still be disabled even found the associated display.
2609 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2610 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2611 ASSERT_FALSE(mDevice->isEnabled());
2612}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Christine Franks1ba71cc2021-04-07 14:37:42 -07002614TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2615 // Device should be enabled by default.
2616 mFakePolicy->clearViewports();
2617 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2618 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2619 ASSERT_TRUE(mDevice->isEnabled());
2620
2621 // Device should be disabled because it is associated with a specific display, but the
2622 // corresponding display is not found.
2623 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2624 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2625 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2626 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2627 ASSERT_FALSE(mDevice->isEnabled());
2628
2629 // Device should be enabled when a display is found.
2630 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2631 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2632 NO_PORT, ViewportType::INTERNAL);
2633 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2634 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2635 ASSERT_TRUE(mDevice->isEnabled());
2636
2637 // Device should be disabled after set disable.
2638 mFakePolicy->addDisabledDevice(mDevice->getId());
2639 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2640 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2641 ASSERT_FALSE(mDevice->isEnabled());
2642
2643 // Device should still be disabled even found the associated display.
2644 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2645 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2646 ASSERT_FALSE(mDevice->isEnabled());
2647}
2648
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649// --- InputMapperTest ---
2650
2651class InputMapperTest : public testing::Test {
2652protected:
2653 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002654 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 static const int32_t DEVICE_ID;
2656 static const int32_t DEVICE_GENERATION;
2657 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002658 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002659 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002661 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002663 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002664 std::unique_ptr<InstrumentedInputReader> mReader;
2665 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666
Chris Ye1b0c7342020-07-28 21:57:03 -07002667 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002668 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002670 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002671 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2672 mFakeListener);
2673 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 }
2675
Chris Yea52ade12020-08-27 16:49:20 -07002676 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002677
Chris Yea52ade12020-08-27 16:49:20 -07002678 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679 mFakeListener.clear();
2680 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681 }
2682
2683 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002684 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 }
2686
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002687 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002688 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002689 mReader->requestRefreshConfiguration(changes);
2690 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002691 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002692 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2693 }
2694
arthurhungdcef2dc2020-08-11 14:47:50 +08002695 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2696 const std::string& location, int32_t eventHubId,
2697 Flags<InputDeviceClass> classes) {
2698 InputDeviceIdentifier identifier;
2699 identifier.name = name;
2700 identifier.location = location;
2701 std::shared_ptr<InputDevice> device =
2702 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2703 identifier);
2704 mReader->pushNextDevice(device);
2705 mFakeEventHub->addDevice(eventHubId, name, classes);
2706 mReader->loopOnce();
2707 return device;
2708 }
2709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002710 template <class T, typename... Args>
2711 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002712 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002713 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002715 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002716 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717 }
2718
2719 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002720 int32_t orientation, const std::string& uniqueId,
2721 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002722 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2723 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002724 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2725 }
2726
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002727 void clearViewports() {
2728 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 }
2730
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002731 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2732 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733 RawEvent event;
2734 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002735 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737 event.type = type;
2738 event.code = code;
2739 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002740 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002741 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742 }
2743
2744 static void assertMotionRange(const InputDeviceInfo& info,
2745 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2746 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002747 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2749 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2750 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2751 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2752 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2753 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2754 }
2755
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002756 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2757 float size, float touchMajor, float touchMinor, float toolMajor,
2758 float toolMinor, float orientation, float distance,
2759 float scaledAxisEpsilon = 1.f) {
2760 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2761 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002762 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2763 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002764 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2765 scaledAxisEpsilon);
2766 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2767 scaledAxisEpsilon);
2768 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2769 scaledAxisEpsilon);
2770 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2771 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2773 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2774 }
2775
Michael Wright17db18e2020-06-26 20:51:44 +01002776 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002778 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 ASSERT_NEAR(x, actualX, 1);
2780 ASSERT_NEAR(y, actualY, 1);
2781 }
2782};
2783
2784const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002785const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002786const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2788const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002789const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2790 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002791const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792
2793// --- SwitchInputMapperTest ---
2794
2795class SwitchInputMapperTest : public InputMapperTest {
2796protected:
2797};
2798
2799TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002800 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002802 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803}
2804
2805TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002806 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002808 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002809 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002811 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002812 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813}
2814
2815TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002816 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822
2823 NotifySwitchArgs args;
2824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2825 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002826 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2827 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828 args.switchMask);
2829 ASSERT_EQ(uint32_t(0), args.policyFlags);
2830}
2831
Chris Ye87143712020-11-10 05:05:58 +00002832// --- VibratorInputMapperTest ---
2833class VibratorInputMapperTest : public InputMapperTest {
2834protected:
2835 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2836};
2837
2838TEST_F(VibratorInputMapperTest, GetSources) {
2839 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2840
2841 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2842}
2843
2844TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2845 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2846
2847 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2848}
2849
2850TEST_F(VibratorInputMapperTest, Vibrate) {
2851 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002852 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002853 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2854
2855 VibrationElement pattern(2);
2856 VibrationSequence sequence(2);
2857 pattern.duration = std::chrono::milliseconds(200);
2858 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2859 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2860 sequence.addElement(pattern);
2861 pattern.duration = std::chrono::milliseconds(500);
2862 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2863 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2864 sequence.addElement(pattern);
2865
2866 std::vector<int64_t> timings = {0, 1};
2867 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2868
2869 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002870 // Start vibrating
2871 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002872 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002873 // Verify vibrator state listener was notified.
2874 mReader->loopOnce();
2875 NotifyVibratorStateArgs args;
2876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2877 ASSERT_EQ(DEVICE_ID, args.deviceId);
2878 ASSERT_TRUE(args.isOn);
2879 // Stop vibrating
2880 mapper.cancelVibrate(VIBRATION_TOKEN);
2881 ASSERT_FALSE(mapper.isVibrating());
2882 // Verify vibrator state listener was notified.
2883 mReader->loopOnce();
2884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2885 ASSERT_EQ(DEVICE_ID, args.deviceId);
2886 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002887}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888
Chris Yef59a2f42020-10-16 12:55:26 -07002889// --- SensorInputMapperTest ---
2890
2891class SensorInputMapperTest : public InputMapperTest {
2892protected:
2893 static const int32_t ACCEL_RAW_MIN;
2894 static const int32_t ACCEL_RAW_MAX;
2895 static const int32_t ACCEL_RAW_FUZZ;
2896 static const int32_t ACCEL_RAW_FLAT;
2897 static const int32_t ACCEL_RAW_RESOLUTION;
2898
2899 static const int32_t GYRO_RAW_MIN;
2900 static const int32_t GYRO_RAW_MAX;
2901 static const int32_t GYRO_RAW_FUZZ;
2902 static const int32_t GYRO_RAW_FLAT;
2903 static const int32_t GYRO_RAW_RESOLUTION;
2904
2905 static const float GRAVITY_MS2_UNIT;
2906 static const float DEGREE_RADIAN_UNIT;
2907
2908 void prepareAccelAxes();
2909 void prepareGyroAxes();
2910 void setAccelProperties();
2911 void setGyroProperties();
2912 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2913};
2914
2915const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2916const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2917const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2918const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2919const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2920
2921const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2922const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2923const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2924const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2925const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2926
2927const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2928const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2929
2930void SensorInputMapperTest::prepareAccelAxes() {
2931 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2932 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2933 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2934 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2935 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2936 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2937}
2938
2939void SensorInputMapperTest::prepareGyroAxes() {
2940 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2941 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2942 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2943 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2944 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2945 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2946}
2947
2948void SensorInputMapperTest::setAccelProperties() {
2949 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2950 /* sensorDataIndex */ 0);
2951 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2952 /* sensorDataIndex */ 1);
2953 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2954 /* sensorDataIndex */ 2);
2955 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2956 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2957 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2958 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2959 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2960}
2961
2962void SensorInputMapperTest::setGyroProperties() {
2963 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2964 /* sensorDataIndex */ 0);
2965 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2966 /* sensorDataIndex */ 1);
2967 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2968 /* sensorDataIndex */ 2);
2969 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2970 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2971 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2972 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2973 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2974}
2975
2976TEST_F(SensorInputMapperTest, GetSources) {
2977 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2978
2979 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2980}
2981
2982TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2983 setAccelProperties();
2984 prepareAccelAxes();
2985 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2986
2987 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2988 std::chrono::microseconds(10000),
2989 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002990 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002996
2997 NotifySensorArgs args;
2998 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2999 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3000 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3001
3002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3003 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3004 ASSERT_EQ(args.deviceId, DEVICE_ID);
3005 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3006 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3007 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3008 ASSERT_EQ(args.values, values);
3009 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3010}
3011
3012TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3013 setGyroProperties();
3014 prepareGyroAxes();
3015 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3016
3017 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3018 std::chrono::microseconds(10000),
3019 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003020 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003026
3027 NotifySensorArgs args;
3028 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3029 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3030 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3031
3032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3033 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3034 ASSERT_EQ(args.deviceId, DEVICE_ID);
3035 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3036 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3037 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3038 ASSERT_EQ(args.values, values);
3039 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3040}
3041
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042// --- KeyboardInputMapperTest ---
3043
3044class KeyboardInputMapperTest : public InputMapperTest {
3045protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003046 const std::string UNIQUE_ID = "local:0";
3047
3048 void prepareDisplay(int32_t orientation);
3049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003050 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003051 int32_t originalKeyCode, int32_t rotatedKeyCode,
3052 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053};
3054
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003055/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3056 * orientation.
3057 */
3058void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003059 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3060 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003061}
3062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003063void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003064 int32_t originalScanCode, int32_t originalKeyCode,
3065 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066 NotifyKeyArgs args;
3067
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3070 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3071 ASSERT_EQ(originalScanCode, args.scanCode);
3072 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003073 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3078 ASSERT_EQ(originalScanCode, args.scanCode);
3079 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003080 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081}
3082
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 KeyboardInputMapper& mapper =
3085 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3086 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089}
3090
3091TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3092 const int32_t USAGE_A = 0x070004;
3093 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3095 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003096 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3097 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3098 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 KeyboardInputMapper& mapper =
3101 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3102 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003103 // Initial metastate to AMETA_NONE.
3104 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3105 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003106
3107 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109 NotifyKeyArgs args;
3110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3111 ASSERT_EQ(DEVICE_ID, args.deviceId);
3112 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3113 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3115 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3116 ASSERT_EQ(KEY_HOME, args.scanCode);
3117 ASSERT_EQ(AMETA_NONE, args.metaState);
3118 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3119 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3120 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3121
3122 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003123 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3125 ASSERT_EQ(DEVICE_ID, args.deviceId);
3126 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3127 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3128 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3129 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3130 ASSERT_EQ(KEY_HOME, args.scanCode);
3131 ASSERT_EQ(AMETA_NONE, args.metaState);
3132 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3133 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3134 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3135
3136 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(DEVICE_ID, args.deviceId);
3141 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3142 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3143 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3144 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3145 ASSERT_EQ(0, args.scanCode);
3146 ASSERT_EQ(AMETA_NONE, args.metaState);
3147 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3148 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3149 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3150
3151 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3153 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3155 ASSERT_EQ(DEVICE_ID, args.deviceId);
3156 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3157 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3158 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3159 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3160 ASSERT_EQ(0, args.scanCode);
3161 ASSERT_EQ(AMETA_NONE, args.metaState);
3162 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3164 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3165
3166 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3170 ASSERT_EQ(DEVICE_ID, args.deviceId);
3171 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3172 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3173 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3174 ASSERT_EQ(0, args.keyCode);
3175 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3176 ASSERT_EQ(AMETA_NONE, args.metaState);
3177 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3178 ASSERT_EQ(0U, args.policyFlags);
3179 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3180
3181 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3183 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3185 ASSERT_EQ(DEVICE_ID, args.deviceId);
3186 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3187 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3188 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3189 ASSERT_EQ(0, args.keyCode);
3190 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3191 ASSERT_EQ(AMETA_NONE, args.metaState);
3192 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3193 ASSERT_EQ(0U, args.policyFlags);
3194 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3195}
3196
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197/**
3198 * Ensure that the readTime is set to the time when the EV_KEY is received.
3199 */
3200TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3201 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3202
3203 KeyboardInputMapper& mapper =
3204 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3205 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3206 NotifyKeyArgs args;
3207
3208 // Key down
3209 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3211 ASSERT_EQ(12, args.readTime);
3212
3213 // Key up
3214 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3216 ASSERT_EQ(15, args.readTime);
3217}
3218
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003220 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3221 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003222 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3223 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3224 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003226 KeyboardInputMapper& mapper =
3227 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3228 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229
arthurhungc903df12020-08-11 15:08:42 +08003230 // Initial metastate to AMETA_NONE.
3231 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3232 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233
3234 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 NotifyKeyArgs args;
3237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003239 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003240 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241
3242 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003243 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3245 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003246 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247
3248 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003249 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3257 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003258 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003259 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260}
3261
3262TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003263 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3264 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3265 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3266 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 KeyboardInputMapper& mapper =
3269 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3270 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003272 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3274 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3275 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3276 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3277 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3278 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3279 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3280 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3281}
3282
3283TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003284 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3285 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 KeyboardInputMapper& mapper =
3291 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3292 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003294 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003295 ASSERT_NO_FATAL_FAILURE(
3296 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3297 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3298 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3299 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3300 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3302 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003304 clearViewports();
3305 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003306 ASSERT_NO_FATAL_FAILURE(
3307 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3309 AKEYCODE_DPAD_UP, DISPLAY_ID));
3310 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3311 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3312 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3313 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003315 clearViewports();
3316 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003317 ASSERT_NO_FATAL_FAILURE(
3318 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3320 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3321 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3322 AKEYCODE_DPAD_UP, DISPLAY_ID));
3323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3324 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003326 clearViewports();
3327 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003328 ASSERT_NO_FATAL_FAILURE(
3329 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3331 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3332 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3333 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3335 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
3337 // Special case: if orientation changes while key is down, we still emit the same keycode
3338 // in the key up as we did in the key down.
3339 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003340 clearViewports();
3341 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3345 ASSERT_EQ(KEY_UP, args.scanCode);
3346 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3347
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003348 clearViewports();
3349 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3353 ASSERT_EQ(KEY_UP, args.scanCode);
3354 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3355}
3356
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003357TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3358 // If the keyboard is not orientation aware,
3359 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003360 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003362 KeyboardInputMapper& mapper =
3363 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3364 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003365 NotifyKeyArgs args;
3366
3367 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3372 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3373
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003374 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3379 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3380}
3381
3382TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3383 // If the keyboard is orientation aware,
3384 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003385 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003386
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003387 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 KeyboardInputMapper& mapper =
3389 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003391 NotifyKeyArgs args;
3392
3393 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3394 // ^--- already checked by the previous test
3395
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003396 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003397 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3402 ASSERT_EQ(DISPLAY_ID, args.displayId);
3403
3404 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003405 clearViewports();
3406 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003407 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3412 ASSERT_EQ(newDisplayId, args.displayId);
3413}
3414
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003416 KeyboardInputMapper& mapper =
3417 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3418 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003420 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003421 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003423 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003424 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425}
3426
3427TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003428 KeyboardInputMapper& mapper =
3429 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3430 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003432 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003433 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003435 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437}
3438
3439TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003440 KeyboardInputMapper& mapper =
3441 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3442 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003444 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445
3446 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3447 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003448 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449 ASSERT_TRUE(flags[0]);
3450 ASSERT_FALSE(flags[1]);
3451}
3452
3453TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003454 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3455 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3456 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3457 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3458 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003461 KeyboardInputMapper& mapper =
3462 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3463 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003464 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003465 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3466 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467
3468 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003469 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3470 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3471 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
3473 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003476 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3477 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3478 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003479 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
3481 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3485 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3486 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003487 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488
3489 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003492 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3493 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3494 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
3497 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003500 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3501 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3502 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003503 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504
3505 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003508 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3509 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3510 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512
3513 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003516 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3517 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003519 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520}
3521
Chris Yea52ade12020-08-27 16:49:20 -07003522TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3523 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3524 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3525 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3526 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3527
3528 KeyboardInputMapper& mapper =
3529 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3530 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3531
3532 // Initial metastate should be AMETA_NONE as no meta keys added.
3533 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3534 // Meta state should be AMETA_NONE after reset
3535 mapper.reset(ARBITRARY_TIME);
3536 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3537 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3538 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3539 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3540
3541 NotifyKeyArgs args;
3542 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3545 ASSERT_EQ(AMETA_NONE, args.metaState);
3546 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3547 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3548 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3549
3550 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003551 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3553 ASSERT_EQ(AMETA_NONE, args.metaState);
3554 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3555 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3556 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3557}
3558
Arthur Hung2c9a3342019-07-23 14:18:59 +08003559TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3560 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003565
3566 // keyboard 2.
3567 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003568 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003569 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003570 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003571 std::shared_ptr<InputDevice> device2 =
3572 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3573 Flags<InputDeviceClass>(0));
3574
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003575 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3576 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3577 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3578 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003580 KeyboardInputMapper& mapper =
3581 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3582 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003584 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003586 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003587 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3588 device2->reset(ARBITRARY_TIME);
3589
3590 // Prepared displays and associated info.
3591 constexpr uint8_t hdmi1 = 0;
3592 constexpr uint8_t hdmi2 = 1;
3593 const std::string SECONDARY_UNIQUE_ID = "local:1";
3594
3595 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3596 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3597
3598 // No associated display viewport found, should disable the device.
3599 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3600 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3601 ASSERT_FALSE(device2->isEnabled());
3602
3603 // Prepare second display.
3604 constexpr int32_t newDisplayId = 2;
3605 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003606 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003607 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003608 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609 // Default device will reconfigure above, need additional reconfiguration for another device.
3610 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3611 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3612
3613 // Device should be enabled after the associated display is found.
3614 ASSERT_TRUE(mDevice->isEnabled());
3615 ASSERT_TRUE(device2->isEnabled());
3616
3617 // Test pad key events
3618 ASSERT_NO_FATAL_FAILURE(
3619 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3620 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3621 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3622 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3623 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3624 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3625 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3626
3627 ASSERT_NO_FATAL_FAILURE(
3628 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3629 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3630 AKEYCODE_DPAD_RIGHT, newDisplayId));
3631 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3632 AKEYCODE_DPAD_DOWN, newDisplayId));
3633 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3634 AKEYCODE_DPAD_LEFT, newDisplayId));
3635}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636
arthurhungc903df12020-08-11 15:08:42 +08003637TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3638 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3639 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3640 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3641 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3642 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3643 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3644
3645 KeyboardInputMapper& mapper =
3646 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3647 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3648 // Initial metastate to AMETA_NONE.
3649 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3650 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3651
3652 // Initialization should have turned all of the lights off.
3653 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3654 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3655 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3656
3657 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003660 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3661 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3662
3663 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003666 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3667 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3668
3669 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003672 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3673 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3674
3675 mFakeEventHub->removeDevice(EVENTHUB_ID);
3676 mReader->loopOnce();
3677
3678 // keyboard 2 should default toggle keys.
3679 const std::string USB2 = "USB2";
3680 const std::string DEVICE_NAME2 = "KEYBOARD2";
3681 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3682 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3683 std::shared_ptr<InputDevice> device2 =
3684 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3685 Flags<InputDeviceClass>(0));
3686 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3687 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3688 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3689 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3690 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3691 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3692
arthurhung6fe95782020-10-05 22:41:16 +08003693 KeyboardInputMapper& mapper2 =
3694 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3695 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003696 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3697 device2->reset(ARBITRARY_TIME);
3698
3699 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3700 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3701 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003702 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3703 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003704}
3705
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003706// --- KeyboardInputMapperTest_ExternalDevice ---
3707
3708class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3709protected:
Chris Yea52ade12020-08-27 16:49:20 -07003710 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003711};
3712
3713TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003714 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3715 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003716
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003717 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3718 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3719 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3720 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003721
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003722 KeyboardInputMapper& mapper =
3723 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3724 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003725
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003727 NotifyKeyArgs args;
3728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3729 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3730
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003731 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3733 ASSERT_EQ(uint32_t(0), args.policyFlags);
3734
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3737 ASSERT_EQ(uint32_t(0), args.policyFlags);
3738
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003739 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3741 ASSERT_EQ(uint32_t(0), args.policyFlags);
3742
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3745 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3746
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003747 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3749 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3750}
3751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003752TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003753 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003758
Powei Fengd041c5d2019-05-03 17:11:33 -07003759 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003760 KeyboardInputMapper& mapper =
3761 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3762 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003763
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003765 NotifyKeyArgs args;
3766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3767 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3768
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3771 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(uint32_t(0), args.policyFlags);
3776
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3779 ASSERT_EQ(uint32_t(0), args.policyFlags);
3780
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3783 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3784
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3787 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3788}
3789
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790// --- CursorInputMapperTest ---
3791
3792class CursorInputMapperTest : public InputMapperTest {
3793protected:
3794 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3795
Michael Wright17db18e2020-06-26 20:51:44 +01003796 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
Chris Yea52ade12020-08-27 16:49:20 -07003798 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799 InputMapperTest::SetUp();
3800
Michael Wright17db18e2020-06-26 20:51:44 +01003801 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003802 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803 }
3804
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003805 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3806 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003807
3808 void prepareDisplay(int32_t orientation) {
3809 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003810 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003811 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3812 orientation, uniqueId, NO_PORT, viewportType);
3813 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003814
3815 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3816 float pressure) {
3817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3818 0.0f, 0.0f, 0.0f, EPSILON));
3819 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003820};
3821
3822const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003824void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3825 int32_t originalY, int32_t rotatedX,
3826 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827 NotifyMotionArgs args;
3828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003834 ASSERT_NO_FATAL_FAILURE(
3835 assertCursorPointerCoords(args.pointerCoords[0],
3836 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3837 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838}
3839
3840TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003842 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003844 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845}
3846
3847TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003849 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003851 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852}
3853
3854TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857
3858 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003859 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860
3861 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003862 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3863 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3865 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3866
3867 // When the bounds are set, then there should be a valid motion range.
3868 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3869
3870 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872
3873 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3874 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3875 1, 800 - 1, 0.0f, 0.0f));
3876 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3877 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3878 2, 480 - 1, 0.0f, 0.0f));
3879 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3880 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3881 0.0f, 1.0f, 0.0f, 0.0f));
3882}
3883
3884TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003886 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887
3888 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003889 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890
3891 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3892 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3893 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3894 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3895 AINPUT_MOTION_RANGE_Y, 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_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3899 0.0f, 1.0f, 0.0f, 0.0f));
3900}
3901
3902TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003904 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
arthurhungdcef2dc2020-08-11 14:47:50 +08003906 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003907
3908 NotifyMotionArgs args;
3909
3910 // Button press.
3911 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3915 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3916 ASSERT_EQ(DEVICE_ID, args.deviceId);
3917 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3918 ASSERT_EQ(uint32_t(0), args.policyFlags);
3919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3920 ASSERT_EQ(0, args.flags);
3921 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3922 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3923 ASSERT_EQ(0, args.edgeFlags);
3924 ASSERT_EQ(uint32_t(1), args.pointerCount);
3925 ASSERT_EQ(0, args.pointerProperties[0].id);
3926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003927 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3929 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3930 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3931
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3933 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3934 ASSERT_EQ(DEVICE_ID, args.deviceId);
3935 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3936 ASSERT_EQ(uint32_t(0), args.policyFlags);
3937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3938 ASSERT_EQ(0, args.flags);
3939 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3940 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3941 ASSERT_EQ(0, args.edgeFlags);
3942 ASSERT_EQ(uint32_t(1), args.pointerCount);
3943 ASSERT_EQ(0, args.pointerProperties[0].id);
3944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003945 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003946 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3947 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3948 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3949
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003951 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3952 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3954 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3955 ASSERT_EQ(DEVICE_ID, args.deviceId);
3956 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3957 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003958 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3959 ASSERT_EQ(0, args.flags);
3960 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3961 ASSERT_EQ(0, args.buttonState);
3962 ASSERT_EQ(0, args.edgeFlags);
3963 ASSERT_EQ(uint32_t(1), args.pointerCount);
3964 ASSERT_EQ(0, args.pointerProperties[0].id);
3965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003966 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003967 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3968 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3969 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3970
3971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3972 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3973 ASSERT_EQ(DEVICE_ID, args.deviceId);
3974 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3975 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3977 ASSERT_EQ(0, args.flags);
3978 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3979 ASSERT_EQ(0, args.buttonState);
3980 ASSERT_EQ(0, args.edgeFlags);
3981 ASSERT_EQ(uint32_t(1), args.pointerCount);
3982 ASSERT_EQ(0, args.pointerProperties[0].id);
3983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003984 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3986 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3987 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3988}
3989
3990TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003992 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993
3994 NotifyMotionArgs args;
3995
3996 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4000 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004001 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4002 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4003 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004
4005 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004010 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4011 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012}
4013
4014TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004016 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017
4018 NotifyMotionArgs args;
4019
4020 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4024 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004025 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004029 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004030
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004036 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004037
4038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004040 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041}
4042
4043TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046
4047 NotifyMotionArgs args;
4048
4049 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004056 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4057 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4058 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004062 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4063 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4064 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004065
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004072 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4073 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4074 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075
4076 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004080 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004081 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004082
4083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004085 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086}
4087
4088TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004090 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004092 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4101}
4102
4103TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104 addConfigurationProperty("cursor.mode", "navigation");
4105 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004106 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004108 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4117
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004118 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4120 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4121 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4127
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004128 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4137
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004138 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4147}
4148
4149TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152
4153 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4154 mFakePointerController->setPosition(100, 200);
4155 mFakePointerController->setButtonState(0);
4156
4157 NotifyMotionArgs motionArgs;
4158 NotifyKeyArgs keyArgs;
4159
4160 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4164 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4165 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4166 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004167 ASSERT_NO_FATAL_FAILURE(
4168 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4171 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4172 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4173 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004174 ASSERT_NO_FATAL_FAILURE(
4175 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004176
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 ASSERT_EQ(0, motionArgs.buttonState);
4182 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004183 ASSERT_NO_FATAL_FAILURE(
4184 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185
4186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004187 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 ASSERT_EQ(0, motionArgs.buttonState);
4189 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004190 ASSERT_NO_FATAL_FAILURE(
4191 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004192
4193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195 ASSERT_EQ(0, motionArgs.buttonState);
4196 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(
4198 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
4200 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4205 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4206 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4207 motionArgs.buttonState);
4208 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4209 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004210 ASSERT_NO_FATAL_FAILURE(
4211 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4215 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4216 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4217 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004218 ASSERT_NO_FATAL_FAILURE(
4219 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004220
4221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4223 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4224 motionArgs.buttonState);
4225 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4226 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004227 ASSERT_NO_FATAL_FAILURE(
4228 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4235 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004236 ASSERT_NO_FATAL_FAILURE(
4237 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238
4239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4242 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4250 ASSERT_EQ(0, motionArgs.buttonState);
4251 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004252 ASSERT_NO_FATAL_FAILURE(
4253 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_EQ(0, motionArgs.buttonState);
4259 ASSERT_EQ(0, mFakePointerController->getButtonState());
4260 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004261 ASSERT_NO_FATAL_FAILURE(
4262 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004263
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4265 ASSERT_EQ(0, motionArgs.buttonState);
4266 ASSERT_EQ(0, mFakePointerController->getButtonState());
4267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004268 ASSERT_NO_FATAL_FAILURE(
4269 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
4271 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4275 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4276 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004277
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4281 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(
4283 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284
4285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4287 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4288 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004289 ASSERT_NO_FATAL_FAILURE(
4290 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_EQ(0, motionArgs.buttonState);
4297 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303 ASSERT_EQ(0, motionArgs.buttonState);
4304 ASSERT_EQ(0, mFakePointerController->getButtonState());
4305
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004306 ASSERT_NO_FATAL_FAILURE(
4307 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4309 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4310 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4311
4312 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4316 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4317 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4322 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004323 ASSERT_NO_FATAL_FAILURE(
4324 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325
4326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4328 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4329 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004330 ASSERT_NO_FATAL_FAILURE(
4331 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_EQ(0, motionArgs.buttonState);
4338 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004339 ASSERT_NO_FATAL_FAILURE(
4340 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4344 ASSERT_EQ(0, motionArgs.buttonState);
4345 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004346 ASSERT_NO_FATAL_FAILURE(
4347 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4350 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4351 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4352
4353 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4357 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4358 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004359
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4363 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(
4365 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004366
4367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4369 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4370 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004371 ASSERT_NO_FATAL_FAILURE(
4372 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004377 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_EQ(0, motionArgs.buttonState);
4379 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004380 ASSERT_NO_FATAL_FAILURE(
4381 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004382
4383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4385 ASSERT_EQ(0, motionArgs.buttonState);
4386 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004387 ASSERT_NO_FATAL_FAILURE(
4388 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4391 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4392 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4393
4394 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4398 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4399 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004400
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4404 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004405 ASSERT_NO_FATAL_FAILURE(
4406 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407
4408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4410 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4411 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004412 ASSERT_NO_FATAL_FAILURE(
4413 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_EQ(0, motionArgs.buttonState);
4420 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004421 ASSERT_NO_FATAL_FAILURE(
4422 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004423
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4426 ASSERT_EQ(0, motionArgs.buttonState);
4427 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004428 ASSERT_NO_FATAL_FAILURE(
4429 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004430
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4432 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4433 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4434}
4435
4436TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004438 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
4440 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4441 mFakePointerController->setPosition(100, 200);
4442 mFakePointerController->setButtonState(0);
4443
4444 NotifyMotionArgs args;
4445
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004450 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4453 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 +01004454 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004455}
4456
4457TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004458 addConfigurationProperty("cursor.mode", "pointer");
4459 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004460 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004461
4462 NotifyDeviceResetArgs resetArgs;
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4464 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4465 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4466
4467 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4468 mFakePointerController->setPosition(100, 200);
4469 mFakePointerController->setButtonState(0);
4470
4471 NotifyMotionArgs args;
4472
4473 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4478 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4481 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 +01004482 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004483
4484 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4488 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4489 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4491 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4493 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4496 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4497
4498 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004499 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4500 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4502 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4505 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4507 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4508 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4510 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4511
4512 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4517 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4520 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 +01004521 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004522
4523 // Disable pointer capture and check that the device generation got bumped
4524 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004525 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004526 mFakePolicy->setPointerCapture(false);
4527 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004528 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004529
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4531 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4532 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4533
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4538 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4541 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 +01004542 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543}
4544
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004545TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004546 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004547
Garfield Tan888a6a42020-01-09 11:39:16 -08004548 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004549 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004550 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4551 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004552 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4553 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004554 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4555 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4556
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004557 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4558 mFakePointerController->setPosition(100, 200);
4559 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004560
4561 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4566 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4569 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 +01004570 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004571 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4572}
4573
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574// --- TouchInputMapperTest ---
4575
4576class TouchInputMapperTest : public InputMapperTest {
4577protected:
4578 static const int32_t RAW_X_MIN;
4579 static const int32_t RAW_X_MAX;
4580 static const int32_t RAW_Y_MIN;
4581 static const int32_t RAW_Y_MAX;
4582 static const int32_t RAW_TOUCH_MIN;
4583 static const int32_t RAW_TOUCH_MAX;
4584 static const int32_t RAW_TOOL_MIN;
4585 static const int32_t RAW_TOOL_MAX;
4586 static const int32_t RAW_PRESSURE_MIN;
4587 static const int32_t RAW_PRESSURE_MAX;
4588 static const int32_t RAW_ORIENTATION_MIN;
4589 static const int32_t RAW_ORIENTATION_MAX;
4590 static const int32_t RAW_DISTANCE_MIN;
4591 static const int32_t RAW_DISTANCE_MAX;
4592 static const int32_t RAW_TILT_MIN;
4593 static const int32_t RAW_TILT_MAX;
4594 static const int32_t RAW_ID_MIN;
4595 static const int32_t RAW_ID_MAX;
4596 static const int32_t RAW_SLOT_MIN;
4597 static const int32_t RAW_SLOT_MAX;
4598 static const float X_PRECISION;
4599 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004600 static const float X_PRECISION_VIRTUAL;
4601 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602
4603 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004604 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4607
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004608 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004609 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004610
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 enum Axes {
4612 POSITION = 1 << 0,
4613 TOUCH = 1 << 1,
4614 TOOL = 1 << 2,
4615 PRESSURE = 1 << 3,
4616 ORIENTATION = 1 << 4,
4617 MINOR = 1 << 5,
4618 ID = 1 << 6,
4619 DISTANCE = 1 << 7,
4620 TILT = 1 << 8,
4621 SLOT = 1 << 9,
4622 TOOL_TYPE = 1 << 10,
4623 };
4624
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004625 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4626 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004627 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004629 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 int32_t toRawX(float displayX);
4631 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004632 float toCookedX(float rawX, float rawY);
4633 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004635 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004637 float toDisplayY(int32_t rawY, int32_t displayHeight);
4638
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639};
4640
4641const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4642const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4643const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4644const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4645const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4646const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4647const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4648const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004649const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4650const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4652const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4653const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4654const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4655const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4656const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4657const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4658const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4659const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4660const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4661const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4662const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004663const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4664 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4665const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4666 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004667const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4668 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669
4670const float TouchInputMapperTest::GEOMETRIC_SCALE =
4671 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4672 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4673
4674const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4675 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4676 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4677};
4678
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004679void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004680 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4681 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004682}
4683
4684void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4685 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4686 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687}
4688
Santos Cordonfa5cf462017-04-05 10:37:00 -07004689void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004690 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4691 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4692 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004693}
4694
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004696 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4697 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700}
4701
Jason Gerecke489fda82012-09-07 17:19:40 -07004702void TouchInputMapperTest::prepareLocationCalibration() {
4703 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4704}
4705
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706int32_t TouchInputMapperTest::toRawX(float displayX) {
4707 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4708}
4709
4710int32_t TouchInputMapperTest::toRawY(float displayY) {
4711 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4712}
4713
Jason Gerecke489fda82012-09-07 17:19:40 -07004714float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4715 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4716 return rawX;
4717}
4718
4719float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4720 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4721 return rawY;
4722}
4723
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004725 return toDisplayX(rawX, DISPLAY_WIDTH);
4726}
4727
4728float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4729 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730}
4731
4732float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004733 return toDisplayY(rawY, DISPLAY_HEIGHT);
4734}
4735
4736float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4737 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738}
4739
4740
4741// --- SingleTouchInputMapperTest ---
4742
4743class SingleTouchInputMapperTest : public TouchInputMapperTest {
4744protected:
4745 void prepareButtons();
4746 void prepareAxes(int axes);
4747
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004748 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4749 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4750 void processUp(SingleTouchInputMapper& mappery);
4751 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4752 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4753 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4754 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4755 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4756 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757};
4758
4759void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004760 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761}
4762
4763void SingleTouchInputMapperTest::prepareAxes(int axes) {
4764 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004765 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4766 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767 }
4768 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004769 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4770 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 }
4772 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004773 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4774 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775 }
4776 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004777 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4778 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 }
4780 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004781 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4782 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 }
4784}
4785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004786void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790}
4791
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004792void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795}
4796
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004797void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799}
4800
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004801void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803}
4804
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004805void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4806 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808}
4809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004810void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812}
4813
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004814void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4815 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818}
4819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004820void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4821 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823}
4824
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004825void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827}
4828
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 prepareButtons();
4831 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004832 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004834 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835}
4836
4837TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004838 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4839 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 prepareButtons();
4841 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004842 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845}
4846
4847TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 prepareButtons();
4849 prepareAxes(POSITION);
4850 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004851 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004853 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854}
4855
4856TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 prepareButtons();
4858 prepareAxes(POSITION);
4859 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004860 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004862 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863}
4864
4865TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 addConfigurationProperty("touch.deviceType", "touchScreen");
4867 prepareDisplay(DISPLAY_ORIENTATION_0);
4868 prepareButtons();
4869 prepareAxes(POSITION);
4870 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004871 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872
4873 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004874 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875
4876 // Virtual key is down.
4877 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4878 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4879 processDown(mapper, x, y);
4880 processSync(mapper);
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004883 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884
4885 // Virtual key is up.
4886 processUp(mapper);
4887 processSync(mapper);
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004890 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891}
4892
4893TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 addConfigurationProperty("touch.deviceType", "touchScreen");
4895 prepareDisplay(DISPLAY_ORIENTATION_0);
4896 prepareButtons();
4897 prepareAxes(POSITION);
4898 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004899 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900
4901 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903
4904 // Virtual key is down.
4905 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4906 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4907 processDown(mapper, x, y);
4908 processSync(mapper);
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004911 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912
4913 // Virtual key is up.
4914 processUp(mapper);
4915 processSync(mapper);
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4917
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004918 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919}
4920
4921TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 addConfigurationProperty("touch.deviceType", "touchScreen");
4923 prepareDisplay(DISPLAY_ORIENTATION_0);
4924 prepareButtons();
4925 prepareAxes(POSITION);
4926 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004927 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928
4929 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4930 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004931 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 ASSERT_TRUE(flags[0]);
4933 ASSERT_FALSE(flags[1]);
4934}
4935
4936TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 addConfigurationProperty("touch.deviceType", "touchScreen");
4938 prepareDisplay(DISPLAY_ORIENTATION_0);
4939 prepareButtons();
4940 prepareAxes(POSITION);
4941 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
arthurhungdcef2dc2020-08-11 14:47:50 +08004944 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945
4946 NotifyKeyArgs args;
4947
4948 // Press virtual key.
4949 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4950 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4951 processDown(mapper, x, y);
4952 processSync(mapper);
4953
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4955 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4956 ASSERT_EQ(DEVICE_ID, args.deviceId);
4957 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4958 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4959 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4960 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4961 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4962 ASSERT_EQ(KEY_HOME, args.scanCode);
4963 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4964 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4965
4966 // Release virtual key.
4967 processUp(mapper);
4968 processSync(mapper);
4969
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4971 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4972 ASSERT_EQ(DEVICE_ID, args.deviceId);
4973 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4974 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4975 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4976 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4977 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4978 ASSERT_EQ(KEY_HOME, args.scanCode);
4979 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4980 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4981
4982 // Should not have sent any motions.
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4984}
4985
4986TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 addConfigurationProperty("touch.deviceType", "touchScreen");
4988 prepareDisplay(DISPLAY_ORIENTATION_0);
4989 prepareButtons();
4990 prepareAxes(POSITION);
4991 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004992 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
arthurhungdcef2dc2020-08-11 14:47:50 +08004994 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995
4996 NotifyKeyArgs keyArgs;
4997
4998 // Press virtual key.
4999 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5000 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5001 processDown(mapper, x, y);
5002 processSync(mapper);
5003
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5005 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5006 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5007 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5008 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5009 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5010 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5011 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5012 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5014 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5015
5016 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5017 // into the display area.
5018 y -= 100;
5019 processMove(mapper, x, y);
5020 processSync(mapper);
5021
5022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5023 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5024 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5025 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5026 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5027 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5028 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5029 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5030 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5031 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5032 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5033 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5034
5035 NotifyMotionArgs motionArgs;
5036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5037 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5038 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5039 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5040 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5042 ASSERT_EQ(0, motionArgs.flags);
5043 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5044 ASSERT_EQ(0, motionArgs.buttonState);
5045 ASSERT_EQ(0, motionArgs.edgeFlags);
5046 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5047 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5048 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5050 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5051 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5052 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5053 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5054
5055 // Keep moving out of bounds. Should generate a pointer move.
5056 y -= 50;
5057 processMove(mapper, x, y);
5058 processSync(mapper);
5059
5060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5061 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5062 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5063 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5064 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5066 ASSERT_EQ(0, motionArgs.flags);
5067 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5068 ASSERT_EQ(0, motionArgs.buttonState);
5069 ASSERT_EQ(0, motionArgs.edgeFlags);
5070 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5071 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5074 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5075 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5076 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5077 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5078
5079 // Release out of bounds. Should generate a pointer up.
5080 processUp(mapper);
5081 processSync(mapper);
5082
5083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5084 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5085 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5086 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5087 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5088 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5089 ASSERT_EQ(0, motionArgs.flags);
5090 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5091 ASSERT_EQ(0, motionArgs.buttonState);
5092 ASSERT_EQ(0, motionArgs.edgeFlags);
5093 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5094 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5097 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5098 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5099 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5100 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5101
5102 // Should not have sent any more keys or motions.
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5105}
5106
5107TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 addConfigurationProperty("touch.deviceType", "touchScreen");
5109 prepareDisplay(DISPLAY_ORIENTATION_0);
5110 prepareButtons();
5111 prepareAxes(POSITION);
5112 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005113 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114
arthurhungdcef2dc2020-08-11 14:47:50 +08005115 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116
5117 NotifyMotionArgs motionArgs;
5118
5119 // Initially go down out of bounds.
5120 int32_t x = -10;
5121 int32_t y = -10;
5122 processDown(mapper, x, y);
5123 processSync(mapper);
5124
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5126
5127 // Move into the display area. Should generate a pointer down.
5128 x = 50;
5129 y = 75;
5130 processMove(mapper, x, y);
5131 processSync(mapper);
5132
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5134 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5135 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5136 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5137 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5138 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5139 ASSERT_EQ(0, motionArgs.flags);
5140 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5141 ASSERT_EQ(0, motionArgs.buttonState);
5142 ASSERT_EQ(0, motionArgs.edgeFlags);
5143 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5144 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5147 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5148 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5149 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5150 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5151
5152 // Release. Should generate a pointer up.
5153 processUp(mapper);
5154 processSync(mapper);
5155
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5158 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5159 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5160 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5161 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5162 ASSERT_EQ(0, motionArgs.flags);
5163 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5164 ASSERT_EQ(0, motionArgs.buttonState);
5165 ASSERT_EQ(0, motionArgs.edgeFlags);
5166 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5167 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5170 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5171 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5172 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5173 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5174
5175 // Should not have sent any more keys or motions.
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5178}
5179
Santos Cordonfa5cf462017-04-05 10:37:00 -07005180TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005181 addConfigurationProperty("touch.deviceType", "touchScreen");
5182 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5183
5184 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5185 prepareButtons();
5186 prepareAxes(POSITION);
5187 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005188 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005189
arthurhungdcef2dc2020-08-11 14:47:50 +08005190 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005191
5192 NotifyMotionArgs motionArgs;
5193
5194 // Down.
5195 int32_t x = 100;
5196 int32_t y = 125;
5197 processDown(mapper, x, y);
5198 processSync(mapper);
5199
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5201 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5202 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5203 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5204 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5205 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5207 ASSERT_EQ(0, motionArgs.flags);
5208 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5209 ASSERT_EQ(0, motionArgs.buttonState);
5210 ASSERT_EQ(0, motionArgs.edgeFlags);
5211 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5212 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5215 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5216 1, 0, 0, 0, 0, 0, 0, 0));
5217 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5218 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5220
5221 // Move.
5222 x += 50;
5223 y += 75;
5224 processMove(mapper, x, y);
5225 processSync(mapper);
5226
5227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5229 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5230 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5231 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5232 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5234 ASSERT_EQ(0, motionArgs.flags);
5235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5236 ASSERT_EQ(0, motionArgs.buttonState);
5237 ASSERT_EQ(0, motionArgs.edgeFlags);
5238 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5242 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5243 1, 0, 0, 0, 0, 0, 0, 0));
5244 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5245 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5247
5248 // Up.
5249 processUp(mapper);
5250 processSync(mapper);
5251
5252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5253 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5254 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5255 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5256 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5257 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5258 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5259 ASSERT_EQ(0, motionArgs.flags);
5260 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5261 ASSERT_EQ(0, motionArgs.buttonState);
5262 ASSERT_EQ(0, motionArgs.edgeFlags);
5263 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5264 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5265 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5267 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5268 1, 0, 0, 0, 0, 0, 0, 0));
5269 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5270 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5272
5273 // Should not have sent any more keys or motions.
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5276}
5277
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279 addConfigurationProperty("touch.deviceType", "touchScreen");
5280 prepareDisplay(DISPLAY_ORIENTATION_0);
5281 prepareButtons();
5282 prepareAxes(POSITION);
5283 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005284 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285
arthurhungdcef2dc2020-08-11 14:47:50 +08005286 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287
5288 NotifyMotionArgs motionArgs;
5289
5290 // Down.
5291 int32_t x = 100;
5292 int32_t y = 125;
5293 processDown(mapper, x, y);
5294 processSync(mapper);
5295
5296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5298 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5300 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5301 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5302 ASSERT_EQ(0, motionArgs.flags);
5303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5304 ASSERT_EQ(0, motionArgs.buttonState);
5305 ASSERT_EQ(0, motionArgs.edgeFlags);
5306 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5307 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5310 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5314
5315 // Move.
5316 x += 50;
5317 y += 75;
5318 processMove(mapper, x, y);
5319 processSync(mapper);
5320
5321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5323 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5324 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5325 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5327 ASSERT_EQ(0, motionArgs.flags);
5328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5329 ASSERT_EQ(0, motionArgs.buttonState);
5330 ASSERT_EQ(0, motionArgs.edgeFlags);
5331 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5335 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5336 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5337 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5339
5340 // Up.
5341 processUp(mapper);
5342 processSync(mapper);
5343
5344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5345 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5346 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5348 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5349 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5350 ASSERT_EQ(0, motionArgs.flags);
5351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5352 ASSERT_EQ(0, motionArgs.buttonState);
5353 ASSERT_EQ(0, motionArgs.edgeFlags);
5354 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5355 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5358 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5359 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5360 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5362
5363 // Should not have sent any more keys or motions.
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5366}
5367
5368TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369 addConfigurationProperty("touch.deviceType", "touchScreen");
5370 prepareButtons();
5371 prepareAxes(POSITION);
5372 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005373 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
5375 NotifyMotionArgs args;
5376
5377 // Rotation 90.
5378 prepareDisplay(DISPLAY_ORIENTATION_90);
5379 processDown(mapper, toRawX(50), toRawY(75));
5380 processSync(mapper);
5381
5382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5383 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5384 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5385
5386 processUp(mapper);
5387 processSync(mapper);
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5389}
5390
5391TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392 addConfigurationProperty("touch.deviceType", "touchScreen");
5393 prepareButtons();
5394 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396
5397 NotifyMotionArgs args;
5398
5399 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005400 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 prepareDisplay(DISPLAY_ORIENTATION_0);
5402 processDown(mapper, toRawX(50), toRawY(75));
5403 processSync(mapper);
5404
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5406 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5407 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5408
5409 processUp(mapper);
5410 processSync(mapper);
5411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5412
5413 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005414 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415 prepareDisplay(DISPLAY_ORIENTATION_90);
5416 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5417 processSync(mapper);
5418
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5420 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5421 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5422
5423 processUp(mapper);
5424 processSync(mapper);
5425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5426
5427 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005428 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 prepareDisplay(DISPLAY_ORIENTATION_180);
5430 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5431 processSync(mapper);
5432
5433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5434 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5435 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5436
5437 processUp(mapper);
5438 processSync(mapper);
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5440
5441 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005442 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443 prepareDisplay(DISPLAY_ORIENTATION_270);
5444 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5445 processSync(mapper);
5446
5447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5448 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5449 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5450
5451 processUp(mapper);
5452 processSync(mapper);
5453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5454}
5455
5456TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457 addConfigurationProperty("touch.deviceType", "touchScreen");
5458 prepareDisplay(DISPLAY_ORIENTATION_0);
5459 prepareButtons();
5460 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005461 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462
5463 // These calculations are based on the input device calibration documentation.
5464 int32_t rawX = 100;
5465 int32_t rawY = 200;
5466 int32_t rawPressure = 10;
5467 int32_t rawToolMajor = 12;
5468 int32_t rawDistance = 2;
5469 int32_t rawTiltX = 30;
5470 int32_t rawTiltY = 110;
5471
5472 float x = toDisplayX(rawX);
5473 float y = toDisplayY(rawY);
5474 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5475 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5476 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5477 float distance = float(rawDistance);
5478
5479 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5480 float tiltScale = M_PI / 180;
5481 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5482 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5483 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5484 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5485
5486 processDown(mapper, rawX, rawY);
5487 processPressure(mapper, rawPressure);
5488 processToolMajor(mapper, rawToolMajor);
5489 processDistance(mapper, rawDistance);
5490 processTilt(mapper, rawTiltX, rawTiltY);
5491 processSync(mapper);
5492
5493 NotifyMotionArgs args;
5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5496 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5497 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5498}
5499
Jason Gerecke489fda82012-09-07 17:19:40 -07005500TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005501 addConfigurationProperty("touch.deviceType", "touchScreen");
5502 prepareDisplay(DISPLAY_ORIENTATION_0);
5503 prepareLocationCalibration();
5504 prepareButtons();
5505 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005506 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005507
5508 int32_t rawX = 100;
5509 int32_t rawY = 200;
5510
5511 float x = toDisplayX(toCookedX(rawX, rawY));
5512 float y = toDisplayY(toCookedY(rawX, rawY));
5513
5514 processDown(mapper, rawX, rawY);
5515 processSync(mapper);
5516
5517 NotifyMotionArgs args;
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5520 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5521}
5522
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524 addConfigurationProperty("touch.deviceType", "touchScreen");
5525 prepareDisplay(DISPLAY_ORIENTATION_0);
5526 prepareButtons();
5527 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005528 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529
5530 NotifyMotionArgs motionArgs;
5531 NotifyKeyArgs keyArgs;
5532
5533 processDown(mapper, 100, 200);
5534 processSync(mapper);
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5537 ASSERT_EQ(0, motionArgs.buttonState);
5538
5539 // press BTN_LEFT, release BTN_LEFT
5540 processKey(mapper, BTN_LEFT, 1);
5541 processSync(mapper);
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5544 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5545
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5548 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5549
Michael Wrightd02c5b62014-02-10 15:10:22 -08005550 processKey(mapper, BTN_LEFT, 0);
5551 processSync(mapper);
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005555
5556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005558 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559
5560 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5561 processKey(mapper, BTN_RIGHT, 1);
5562 processKey(mapper, BTN_MIDDLE, 1);
5563 processSync(mapper);
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5566 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5567 motionArgs.buttonState);
5568
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5571 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5572
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5575 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5576 motionArgs.buttonState);
5577
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578 processKey(mapper, BTN_RIGHT, 0);
5579 processSync(mapper);
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005583
5584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005586 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587
5588 processKey(mapper, BTN_MIDDLE, 0);
5589 processSync(mapper);
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005593
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005596 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597
5598 // press BTN_BACK, release BTN_BACK
5599 processKey(mapper, BTN_BACK, 1);
5600 processSync(mapper);
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5602 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5603 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005604
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005607 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5611 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612
5613 processKey(mapper, BTN_BACK, 0);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005618
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005621 ASSERT_EQ(0, motionArgs.buttonState);
5622
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5624 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5625 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5626
5627 // press BTN_SIDE, release BTN_SIDE
5628 processKey(mapper, BTN_SIDE, 1);
5629 processSync(mapper);
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5631 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5632 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005633
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005636 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5637
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5640 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641
5642 processKey(mapper, BTN_SIDE, 0);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005645 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005647
5648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005650 ASSERT_EQ(0, motionArgs.buttonState);
5651
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5653 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5654 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5655
5656 // press BTN_FORWARD, release BTN_FORWARD
5657 processKey(mapper, BTN_FORWARD, 1);
5658 processSync(mapper);
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5660 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5661 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005662
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005665 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5666
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5669 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670
5671 processKey(mapper, BTN_FORWARD, 0);
5672 processSync(mapper);
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005676
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005679 ASSERT_EQ(0, motionArgs.buttonState);
5680
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5682 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5683 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5684
5685 // press BTN_EXTRA, release BTN_EXTRA
5686 processKey(mapper, BTN_EXTRA, 1);
5687 processSync(mapper);
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5689 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5690 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005691
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005694 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5695
5696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5697 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5698 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699
5700 processKey(mapper, BTN_EXTRA, 0);
5701 processSync(mapper);
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005705
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708 ASSERT_EQ(0, motionArgs.buttonState);
5709
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5711 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5712 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5713
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5715
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716 // press BTN_STYLUS, release BTN_STYLUS
5717 processKey(mapper, BTN_STYLUS, 1);
5718 processSync(mapper);
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005721 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5722
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5725 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726
5727 processKey(mapper, BTN_STYLUS, 0);
5728 processSync(mapper);
5729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005735 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736
5737 // press BTN_STYLUS2, release BTN_STYLUS2
5738 processKey(mapper, BTN_STYLUS2, 1);
5739 processSync(mapper);
5740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005742 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5743
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5745 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5746 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747
5748 processKey(mapper, BTN_STYLUS2, 0);
5749 processSync(mapper);
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005751 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005753
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005756 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757
5758 // release touch
5759 processUp(mapper);
5760 processSync(mapper);
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5762 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5763 ASSERT_EQ(0, motionArgs.buttonState);
5764}
5765
5766TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 addConfigurationProperty("touch.deviceType", "touchScreen");
5768 prepareDisplay(DISPLAY_ORIENTATION_0);
5769 prepareButtons();
5770 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005771 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 NotifyMotionArgs motionArgs;
5774
5775 // default tool type is finger
5776 processDown(mapper, 100, 200);
5777 processSync(mapper);
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5779 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5781
5782 // eraser
5783 processKey(mapper, BTN_TOOL_RUBBER, 1);
5784 processSync(mapper);
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5788
5789 // stylus
5790 processKey(mapper, BTN_TOOL_RUBBER, 0);
5791 processKey(mapper, BTN_TOOL_PEN, 1);
5792 processSync(mapper);
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5796
5797 // brush
5798 processKey(mapper, BTN_TOOL_PEN, 0);
5799 processKey(mapper, BTN_TOOL_BRUSH, 1);
5800 processSync(mapper);
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5804
5805 // pencil
5806 processKey(mapper, BTN_TOOL_BRUSH, 0);
5807 processKey(mapper, BTN_TOOL_PENCIL, 1);
5808 processSync(mapper);
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5812
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005813 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814 processKey(mapper, BTN_TOOL_PENCIL, 0);
5815 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5816 processSync(mapper);
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5820
5821 // mouse
5822 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5823 processKey(mapper, BTN_TOOL_MOUSE, 1);
5824 processSync(mapper);
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5828
5829 // lens
5830 processKey(mapper, BTN_TOOL_MOUSE, 0);
5831 processKey(mapper, BTN_TOOL_LENS, 1);
5832 processSync(mapper);
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5836
5837 // double-tap
5838 processKey(mapper, BTN_TOOL_LENS, 0);
5839 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5840 processSync(mapper);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5844
5845 // triple-tap
5846 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5847 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5848 processSync(mapper);
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5852
5853 // quad-tap
5854 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5855 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5860
5861 // finger
5862 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5863 processKey(mapper, BTN_TOOL_FINGER, 1);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5868
5869 // stylus trumps finger
5870 processKey(mapper, BTN_TOOL_PEN, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5875
5876 // eraser trumps stylus
5877 processKey(mapper, BTN_TOOL_RUBBER, 1);
5878 processSync(mapper);
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5882
5883 // mouse trumps eraser
5884 processKey(mapper, BTN_TOOL_MOUSE, 1);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5889
5890 // back to default tool type
5891 processKey(mapper, BTN_TOOL_MOUSE, 0);
5892 processKey(mapper, BTN_TOOL_RUBBER, 0);
5893 processKey(mapper, BTN_TOOL_PEN, 0);
5894 processKey(mapper, BTN_TOOL_FINGER, 0);
5895 processSync(mapper);
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5899}
5900
5901TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 addConfigurationProperty("touch.deviceType", "touchScreen");
5903 prepareDisplay(DISPLAY_ORIENTATION_0);
5904 prepareButtons();
5905 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005906 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005907 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908
5909 NotifyMotionArgs motionArgs;
5910
5911 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5912 processKey(mapper, BTN_TOOL_FINGER, 1);
5913 processMove(mapper, 100, 200);
5914 processSync(mapper);
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5916 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5918 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5919
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5923 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5924
5925 // move a little
5926 processMove(mapper, 150, 250);
5927 processSync(mapper);
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5931 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5932
5933 // down when BTN_TOUCH is pressed, pressure defaults to 1
5934 processKey(mapper, BTN_TOUCH, 1);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5939 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5940
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5944 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5945
5946 // up when BTN_TOUCH is released, hover restored
5947 processKey(mapper, BTN_TOUCH, 0);
5948 processSync(mapper);
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5950 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5952 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5957 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5958
5959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5962 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5963
5964 // exit hover when pointer goes away
5965 processKey(mapper, BTN_TOOL_FINGER, 0);
5966 processSync(mapper);
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5970 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5971}
5972
5973TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974 addConfigurationProperty("touch.deviceType", "touchScreen");
5975 prepareDisplay(DISPLAY_ORIENTATION_0);
5976 prepareButtons();
5977 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005978 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979
5980 NotifyMotionArgs motionArgs;
5981
5982 // initially hovering because pressure is 0
5983 processDown(mapper, 100, 200);
5984 processPressure(mapper, 0);
5985 processSync(mapper);
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5989 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5990
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5994 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5995
5996 // move a little
5997 processMove(mapper, 150, 250);
5998 processSync(mapper);
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6002 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6003
6004 // down when pressure is non-zero
6005 processPressure(mapper, RAW_PRESSURE_MAX);
6006 processSync(mapper);
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6010 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6011
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6015 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6016
6017 // up when pressure becomes 0, hover restored
6018 processPressure(mapper, 0);
6019 processSync(mapper);
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6023 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6028 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6033 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6034
6035 // exit hover when pointer goes away
6036 processUp(mapper);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6041 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6042}
6043
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044// --- MultiTouchInputMapperTest ---
6045
6046class MultiTouchInputMapperTest : public TouchInputMapperTest {
6047protected:
6048 void prepareAxes(int axes);
6049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006050 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6051 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6052 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6053 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6054 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6055 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6056 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6057 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6058 void processId(MultiTouchInputMapper& mapper, int32_t id);
6059 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6060 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6061 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6062 void processMTSync(MultiTouchInputMapper& mapper);
6063 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064};
6065
6066void MultiTouchInputMapperTest::prepareAxes(int axes) {
6067 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006068 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 }
6071 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006072 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6073 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6076 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077 }
6078 }
6079 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6081 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6084 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 }
6086 }
6087 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6089 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 }
6091 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6093 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 }
6095 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6097 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098 }
6099 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6101 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 }
6103 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006104 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6105 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106 }
6107 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 }
6110}
6111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006112void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6113 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116}
6117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006118void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6119 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121}
6122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006123void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6124 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126}
6127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006128void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130}
6131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006132void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134}
6135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006136void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6137 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139}
6140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006141void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143}
6144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006145void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147}
6148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006149void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151}
6152
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006153void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155}
6156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006157void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159}
6160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006161void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6162 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164}
6165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006166void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168}
6169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006170void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172}
6173
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 addConfigurationProperty("touch.deviceType", "touchScreen");
6176 prepareDisplay(DISPLAY_ORIENTATION_0);
6177 prepareAxes(POSITION);
6178 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006179 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180
arthurhungdcef2dc2020-08-11 14:47:50 +08006181 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182
6183 NotifyMotionArgs motionArgs;
6184
6185 // Two fingers down at once.
6186 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6187 processPosition(mapper, x1, y1);
6188 processMTSync(mapper);
6189 processPosition(mapper, x2, y2);
6190 processMTSync(mapper);
6191 processSync(mapper);
6192
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6195 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6196 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6197 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6199 ASSERT_EQ(0, motionArgs.flags);
6200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6201 ASSERT_EQ(0, motionArgs.buttonState);
6202 ASSERT_EQ(0, motionArgs.edgeFlags);
6203 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6204 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6207 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6208 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6209 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6210 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6211
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6214 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6215 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6216 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6217 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6218 motionArgs.action);
6219 ASSERT_EQ(0, motionArgs.flags);
6220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6221 ASSERT_EQ(0, motionArgs.buttonState);
6222 ASSERT_EQ(0, motionArgs.edgeFlags);
6223 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6224 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6226 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6229 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6231 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6232 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6233 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6235
6236 // Move.
6237 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6238 processPosition(mapper, x1, y1);
6239 processMTSync(mapper);
6240 processPosition(mapper, x2, y2);
6241 processMTSync(mapper);
6242 processSync(mapper);
6243
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6246 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6247 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6248 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6250 ASSERT_EQ(0, motionArgs.flags);
6251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6252 ASSERT_EQ(0, motionArgs.buttonState);
6253 ASSERT_EQ(0, motionArgs.edgeFlags);
6254 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6257 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6260 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6262 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6263 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6264 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6266
6267 // First finger up.
6268 x2 += 15; y2 -= 20;
6269 processPosition(mapper, x2, y2);
6270 processMTSync(mapper);
6271 processSync(mapper);
6272
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6275 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6276 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6277 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6279 motionArgs.action);
6280 ASSERT_EQ(0, motionArgs.flags);
6281 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6282 ASSERT_EQ(0, motionArgs.buttonState);
6283 ASSERT_EQ(0, motionArgs.edgeFlags);
6284 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6285 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6287 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6290 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6292 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6293 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6294 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6295 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6299 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6300 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6301 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6303 ASSERT_EQ(0, motionArgs.flags);
6304 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6305 ASSERT_EQ(0, motionArgs.buttonState);
6306 ASSERT_EQ(0, motionArgs.edgeFlags);
6307 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6308 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6311 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6312 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6313 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6315
6316 // Move.
6317 x2 += 20; y2 -= 25;
6318 processPosition(mapper, x2, y2);
6319 processMTSync(mapper);
6320 processSync(mapper);
6321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6324 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6325 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6326 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6328 ASSERT_EQ(0, motionArgs.flags);
6329 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6330 ASSERT_EQ(0, motionArgs.buttonState);
6331 ASSERT_EQ(0, motionArgs.edgeFlags);
6332 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6333 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6336 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6337 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6338 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6340
6341 // New finger down.
6342 int32_t x3 = 700, y3 = 300;
6343 processPosition(mapper, x2, y2);
6344 processMTSync(mapper);
6345 processPosition(mapper, x3, y3);
6346 processMTSync(mapper);
6347 processSync(mapper);
6348
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6355 motionArgs.action);
6356 ASSERT_EQ(0, motionArgs.flags);
6357 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6358 ASSERT_EQ(0, motionArgs.buttonState);
6359 ASSERT_EQ(0, motionArgs.edgeFlags);
6360 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6361 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6363 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6366 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6368 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6369 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6370 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6372
6373 // Second finger up.
6374 x3 += 30; y3 -= 20;
6375 processPosition(mapper, x3, y3);
6376 processMTSync(mapper);
6377 processSync(mapper);
6378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6381 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6382 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6383 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6385 motionArgs.action);
6386 ASSERT_EQ(0, motionArgs.flags);
6387 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6388 ASSERT_EQ(0, motionArgs.buttonState);
6389 ASSERT_EQ(0, motionArgs.edgeFlags);
6390 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6391 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6393 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6396 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6398 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6399 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6400 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6405 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6406 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6407 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6409 ASSERT_EQ(0, motionArgs.flags);
6410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6411 ASSERT_EQ(0, motionArgs.buttonState);
6412 ASSERT_EQ(0, motionArgs.edgeFlags);
6413 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6414 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6417 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6418 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6419 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6421
6422 // Last finger up.
6423 processMTSync(mapper);
6424 processSync(mapper);
6425
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6428 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6429 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6430 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6432 ASSERT_EQ(0, motionArgs.flags);
6433 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6434 ASSERT_EQ(0, motionArgs.buttonState);
6435 ASSERT_EQ(0, motionArgs.edgeFlags);
6436 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6440 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6441 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6442 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6444
6445 // Should not have sent any more keys or motions.
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6448}
6449
6450TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451 addConfigurationProperty("touch.deviceType", "touchScreen");
6452 prepareDisplay(DISPLAY_ORIENTATION_0);
6453 prepareAxes(POSITION | ID);
6454 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006455 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456
arthurhungdcef2dc2020-08-11 14:47:50 +08006457 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458
6459 NotifyMotionArgs motionArgs;
6460
6461 // Two fingers down at once.
6462 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6463 processPosition(mapper, x1, y1);
6464 processId(mapper, 1);
6465 processMTSync(mapper);
6466 processPosition(mapper, x2, y2);
6467 processId(mapper, 2);
6468 processMTSync(mapper);
6469 processSync(mapper);
6470
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6473 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6474 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6477 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6478
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6481 motionArgs.action);
6482 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6485 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6488 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6490 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6491
6492 // Move.
6493 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6494 processPosition(mapper, x1, y1);
6495 processId(mapper, 1);
6496 processMTSync(mapper);
6497 processPosition(mapper, x2, y2);
6498 processId(mapper, 2);
6499 processMTSync(mapper);
6500 processSync(mapper);
6501
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6504 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6505 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6507 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6510 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6512 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6513
6514 // First finger up.
6515 x2 += 15; y2 -= 20;
6516 processPosition(mapper, x2, y2);
6517 processId(mapper, 2);
6518 processMTSync(mapper);
6519 processSync(mapper);
6520
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6522 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6523 motionArgs.action);
6524 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6525 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6527 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6530 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6532 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6533
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6536 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6537 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6540 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6541
6542 // Move.
6543 x2 += 20; y2 -= 25;
6544 processPosition(mapper, x2, y2);
6545 processId(mapper, 2);
6546 processMTSync(mapper);
6547 processSync(mapper);
6548
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6551 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6552 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6555 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6556
6557 // New finger down.
6558 int32_t x3 = 700, y3 = 300;
6559 processPosition(mapper, x2, y2);
6560 processId(mapper, 2);
6561 processMTSync(mapper);
6562 processPosition(mapper, x3, y3);
6563 processId(mapper, 3);
6564 processMTSync(mapper);
6565 processSync(mapper);
6566
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6569 motionArgs.action);
6570 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6571 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6573 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6576 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6578 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6579
6580 // Second finger up.
6581 x3 += 30; y3 -= 20;
6582 processPosition(mapper, x3, y3);
6583 processId(mapper, 3);
6584 processMTSync(mapper);
6585 processSync(mapper);
6586
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6589 motionArgs.action);
6590 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6591 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6593 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6598 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6599
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6602 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6603 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6607
6608 // Last finger up.
6609 processMTSync(mapper);
6610 processSync(mapper);
6611
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6614 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6615 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6618 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6619
6620 // Should not have sent any more keys or motions.
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6623}
6624
6625TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006626 addConfigurationProperty("touch.deviceType", "touchScreen");
6627 prepareDisplay(DISPLAY_ORIENTATION_0);
6628 prepareAxes(POSITION | ID | SLOT);
6629 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006630 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631
arthurhungdcef2dc2020-08-11 14:47:50 +08006632 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006633
6634 NotifyMotionArgs motionArgs;
6635
6636 // Two fingers down at once.
6637 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6638 processPosition(mapper, x1, y1);
6639 processId(mapper, 1);
6640 processSlot(mapper, 1);
6641 processPosition(mapper, x2, y2);
6642 processId(mapper, 2);
6643 processSync(mapper);
6644
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6651 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6652
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6655 motionArgs.action);
6656 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6659 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6664 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6665
6666 // Move.
6667 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6668 processSlot(mapper, 0);
6669 processPosition(mapper, x1, y1);
6670 processSlot(mapper, 1);
6671 processPosition(mapper, x2, y2);
6672 processSync(mapper);
6673
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6676 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6677 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6679 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6682 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6684 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6685
6686 // First finger up.
6687 x2 += 15; y2 -= 20;
6688 processSlot(mapper, 0);
6689 processId(mapper, -1);
6690 processSlot(mapper, 1);
6691 processPosition(mapper, x2, y2);
6692 processSync(mapper);
6693
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6696 motionArgs.action);
6697 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6700 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6703 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6705 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6706
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6709 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6710 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6713 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6714
6715 // Move.
6716 x2 += 20; y2 -= 25;
6717 processPosition(mapper, x2, y2);
6718 processSync(mapper);
6719
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6722 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6723 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6726 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6727
6728 // New finger down.
6729 int32_t x3 = 700, y3 = 300;
6730 processPosition(mapper, x2, y2);
6731 processSlot(mapper, 0);
6732 processId(mapper, 3);
6733 processPosition(mapper, x3, y3);
6734 processSync(mapper);
6735
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6737 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6738 motionArgs.action);
6739 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6740 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6742 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6745 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6747 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6748
6749 // Second finger up.
6750 x3 += 30; y3 -= 20;
6751 processSlot(mapper, 1);
6752 processId(mapper, -1);
6753 processSlot(mapper, 0);
6754 processPosition(mapper, x3, y3);
6755 processSync(mapper);
6756
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6758 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6759 motionArgs.action);
6760 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6761 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6763 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6766 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6768 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6769
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6772 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6776 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6777
6778 // Last finger up.
6779 processId(mapper, -1);
6780 processSync(mapper);
6781
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6784 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6785 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6788 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6789
6790 // Should not have sent any more keys or motions.
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6793}
6794
6795TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006796 addConfigurationProperty("touch.deviceType", "touchScreen");
6797 prepareDisplay(DISPLAY_ORIENTATION_0);
6798 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800
6801 // These calculations are based on the input device calibration documentation.
6802 int32_t rawX = 100;
6803 int32_t rawY = 200;
6804 int32_t rawTouchMajor = 7;
6805 int32_t rawTouchMinor = 6;
6806 int32_t rawToolMajor = 9;
6807 int32_t rawToolMinor = 8;
6808 int32_t rawPressure = 11;
6809 int32_t rawDistance = 0;
6810 int32_t rawOrientation = 3;
6811 int32_t id = 5;
6812
6813 float x = toDisplayX(rawX);
6814 float y = toDisplayY(rawY);
6815 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6816 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6817 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6818 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6819 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6820 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6821 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6822 float distance = float(rawDistance);
6823
6824 processPosition(mapper, rawX, rawY);
6825 processTouchMajor(mapper, rawTouchMajor);
6826 processTouchMinor(mapper, rawTouchMinor);
6827 processToolMajor(mapper, rawToolMajor);
6828 processToolMinor(mapper, rawToolMinor);
6829 processPressure(mapper, rawPressure);
6830 processOrientation(mapper, rawOrientation);
6831 processDistance(mapper, rawDistance);
6832 processId(mapper, id);
6833 processMTSync(mapper);
6834 processSync(mapper);
6835
6836 NotifyMotionArgs args;
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6838 ASSERT_EQ(0, args.pointerProperties[0].id);
6839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6840 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6841 orientation, distance));
6842}
6843
6844TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006845 addConfigurationProperty("touch.deviceType", "touchScreen");
6846 prepareDisplay(DISPLAY_ORIENTATION_0);
6847 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6848 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006849 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006850
6851 // These calculations are based on the input device calibration documentation.
6852 int32_t rawX = 100;
6853 int32_t rawY = 200;
6854 int32_t rawTouchMajor = 140;
6855 int32_t rawTouchMinor = 120;
6856 int32_t rawToolMajor = 180;
6857 int32_t rawToolMinor = 160;
6858
6859 float x = toDisplayX(rawX);
6860 float y = toDisplayY(rawY);
6861 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6862 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6863 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6864 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6865 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6866
6867 processPosition(mapper, rawX, rawY);
6868 processTouchMajor(mapper, rawTouchMajor);
6869 processTouchMinor(mapper, rawTouchMinor);
6870 processToolMajor(mapper, rawToolMajor);
6871 processToolMinor(mapper, rawToolMinor);
6872 processMTSync(mapper);
6873 processSync(mapper);
6874
6875 NotifyMotionArgs args;
6876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6878 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6879}
6880
6881TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882 addConfigurationProperty("touch.deviceType", "touchScreen");
6883 prepareDisplay(DISPLAY_ORIENTATION_0);
6884 prepareAxes(POSITION | TOUCH | TOOL);
6885 addConfigurationProperty("touch.size.calibration", "diameter");
6886 addConfigurationProperty("touch.size.scale", "10");
6887 addConfigurationProperty("touch.size.bias", "160");
6888 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006889 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890
6891 // These calculations are based on the input device calibration documentation.
6892 // Note: We only provide a single common touch/tool value because the device is assumed
6893 // not to emit separate values for each pointer (isSummed = 1).
6894 int32_t rawX = 100;
6895 int32_t rawY = 200;
6896 int32_t rawX2 = 150;
6897 int32_t rawY2 = 250;
6898 int32_t rawTouchMajor = 5;
6899 int32_t rawToolMajor = 8;
6900
6901 float x = toDisplayX(rawX);
6902 float y = toDisplayY(rawY);
6903 float x2 = toDisplayX(rawX2);
6904 float y2 = toDisplayY(rawY2);
6905 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6906 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6907 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6908
6909 processPosition(mapper, rawX, rawY);
6910 processTouchMajor(mapper, rawTouchMajor);
6911 processToolMajor(mapper, rawToolMajor);
6912 processMTSync(mapper);
6913 processPosition(mapper, rawX2, rawY2);
6914 processTouchMajor(mapper, rawTouchMajor);
6915 processToolMajor(mapper, rawToolMajor);
6916 processMTSync(mapper);
6917 processSync(mapper);
6918
6919 NotifyMotionArgs args;
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6921 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6922
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6924 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6925 args.action);
6926 ASSERT_EQ(size_t(2), args.pointerCount);
6927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6928 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6930 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6931}
6932
6933TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934 addConfigurationProperty("touch.deviceType", "touchScreen");
6935 prepareDisplay(DISPLAY_ORIENTATION_0);
6936 prepareAxes(POSITION | TOUCH | TOOL);
6937 addConfigurationProperty("touch.size.calibration", "area");
6938 addConfigurationProperty("touch.size.scale", "43");
6939 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006940 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006941
6942 // These calculations are based on the input device calibration documentation.
6943 int32_t rawX = 100;
6944 int32_t rawY = 200;
6945 int32_t rawTouchMajor = 5;
6946 int32_t rawToolMajor = 8;
6947
6948 float x = toDisplayX(rawX);
6949 float y = toDisplayY(rawY);
6950 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6951 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6952 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6953
6954 processPosition(mapper, rawX, rawY);
6955 processTouchMajor(mapper, rawTouchMajor);
6956 processToolMajor(mapper, rawToolMajor);
6957 processMTSync(mapper);
6958 processSync(mapper);
6959
6960 NotifyMotionArgs args;
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6963 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6964}
6965
6966TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006967 addConfigurationProperty("touch.deviceType", "touchScreen");
6968 prepareDisplay(DISPLAY_ORIENTATION_0);
6969 prepareAxes(POSITION | PRESSURE);
6970 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6971 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006972 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006973
Michael Wrightaa449c92017-12-13 21:21:43 +00006974 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006975 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006976 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6977 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6978 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6979
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980 // These calculations are based on the input device calibration documentation.
6981 int32_t rawX = 100;
6982 int32_t rawY = 200;
6983 int32_t rawPressure = 60;
6984
6985 float x = toDisplayX(rawX);
6986 float y = toDisplayY(rawY);
6987 float pressure = float(rawPressure) * 0.01f;
6988
6989 processPosition(mapper, rawX, rawY);
6990 processPressure(mapper, rawPressure);
6991 processMTSync(mapper);
6992 processSync(mapper);
6993
6994 NotifyMotionArgs args;
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6997 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6998}
6999
7000TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001 addConfigurationProperty("touch.deviceType", "touchScreen");
7002 prepareDisplay(DISPLAY_ORIENTATION_0);
7003 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007004 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005
7006 NotifyMotionArgs motionArgs;
7007 NotifyKeyArgs keyArgs;
7008
7009 processId(mapper, 1);
7010 processPosition(mapper, 100, 200);
7011 processSync(mapper);
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7013 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7014 ASSERT_EQ(0, motionArgs.buttonState);
7015
7016 // press BTN_LEFT, release BTN_LEFT
7017 processKey(mapper, BTN_LEFT, 1);
7018 processSync(mapper);
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7021 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7022
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7025 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7026
Michael Wrightd02c5b62014-02-10 15:10:22 -08007027 processKey(mapper, BTN_LEFT, 0);
7028 processSync(mapper);
7029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007032
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007035 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036
7037 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7038 processKey(mapper, BTN_RIGHT, 1);
7039 processKey(mapper, BTN_MIDDLE, 1);
7040 processSync(mapper);
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7043 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7044 motionArgs.buttonState);
7045
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7048 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7049
7050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7051 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7052 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7053 motionArgs.buttonState);
7054
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055 processKey(mapper, BTN_RIGHT, 0);
7056 processSync(mapper);
7057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007060
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007063 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064
7065 processKey(mapper, BTN_MIDDLE, 0);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007070
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007073 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074
7075 // press BTN_BACK, release BTN_BACK
7076 processKey(mapper, BTN_BACK, 1);
7077 processSync(mapper);
7078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7079 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7080 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007081
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007084 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7085
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7088 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007089
7090 processKey(mapper, BTN_BACK, 0);
7091 processSync(mapper);
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007095
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007098 ASSERT_EQ(0, motionArgs.buttonState);
7099
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7101 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7102 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7103
7104 // press BTN_SIDE, release BTN_SIDE
7105 processKey(mapper, BTN_SIDE, 1);
7106 processSync(mapper);
7107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7108 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7109 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007110
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007113 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7114
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7117 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118
7119 processKey(mapper, BTN_SIDE, 0);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007124
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127 ASSERT_EQ(0, motionArgs.buttonState);
7128
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7130 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7131 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7132
7133 // press BTN_FORWARD, release BTN_FORWARD
7134 processKey(mapper, BTN_FORWARD, 1);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7137 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7138 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007139
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007142 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7143
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7146 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147
7148 processKey(mapper, BTN_FORWARD, 0);
7149 processSync(mapper);
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007151 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007153
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007156 ASSERT_EQ(0, motionArgs.buttonState);
7157
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7159 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7160 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7161
7162 // press BTN_EXTRA, release BTN_EXTRA
7163 processKey(mapper, BTN_EXTRA, 1);
7164 processSync(mapper);
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7166 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7167 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007168
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007171 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7172
7173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7175 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176
7177 processKey(mapper, BTN_EXTRA, 0);
7178 processSync(mapper);
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007182
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007185 ASSERT_EQ(0, motionArgs.buttonState);
7186
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7188 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7189 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7190
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7192
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193 // press BTN_STYLUS, release BTN_STYLUS
7194 processKey(mapper, BTN_STYLUS, 1);
7195 processSync(mapper);
7196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007198 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7199
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7202 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203
7204 processKey(mapper, BTN_STYLUS, 0);
7205 processSync(mapper);
7206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007209
7210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007212 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007213
7214 // press BTN_STYLUS2, release BTN_STYLUS2
7215 processKey(mapper, BTN_STYLUS2, 1);
7216 processSync(mapper);
7217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007219 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7220
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7223 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007224
7225 processKey(mapper, BTN_STYLUS2, 0);
7226 processSync(mapper);
7227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007230
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007233 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234
7235 // release touch
7236 processId(mapper, -1);
7237 processSync(mapper);
7238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7239 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7240 ASSERT_EQ(0, motionArgs.buttonState);
7241}
7242
7243TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007244 addConfigurationProperty("touch.deviceType", "touchScreen");
7245 prepareDisplay(DISPLAY_ORIENTATION_0);
7246 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007247 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007248
7249 NotifyMotionArgs motionArgs;
7250
7251 // default tool type is finger
7252 processId(mapper, 1);
7253 processPosition(mapper, 100, 200);
7254 processSync(mapper);
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7256 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7258
7259 // eraser
7260 processKey(mapper, BTN_TOOL_RUBBER, 1);
7261 processSync(mapper);
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7265
7266 // stylus
7267 processKey(mapper, BTN_TOOL_RUBBER, 0);
7268 processKey(mapper, BTN_TOOL_PEN, 1);
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7273
7274 // brush
7275 processKey(mapper, BTN_TOOL_PEN, 0);
7276 processKey(mapper, BTN_TOOL_BRUSH, 1);
7277 processSync(mapper);
7278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7281
7282 // pencil
7283 processKey(mapper, BTN_TOOL_BRUSH, 0);
7284 processKey(mapper, BTN_TOOL_PENCIL, 1);
7285 processSync(mapper);
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7289
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007290 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007291 processKey(mapper, BTN_TOOL_PENCIL, 0);
7292 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7297
7298 // mouse
7299 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7300 processKey(mapper, BTN_TOOL_MOUSE, 1);
7301 processSync(mapper);
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7305
7306 // lens
7307 processKey(mapper, BTN_TOOL_MOUSE, 0);
7308 processKey(mapper, BTN_TOOL_LENS, 1);
7309 processSync(mapper);
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7313
7314 // double-tap
7315 processKey(mapper, BTN_TOOL_LENS, 0);
7316 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7317 processSync(mapper);
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7321
7322 // triple-tap
7323 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7324 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7325 processSync(mapper);
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7329
7330 // quad-tap
7331 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7332 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7333 processSync(mapper);
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7337
7338 // finger
7339 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7340 processKey(mapper, BTN_TOOL_FINGER, 1);
7341 processSync(mapper);
7342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7345
7346 // stylus trumps finger
7347 processKey(mapper, BTN_TOOL_PEN, 1);
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7352
7353 // eraser trumps stylus
7354 processKey(mapper, BTN_TOOL_RUBBER, 1);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7359
7360 // mouse trumps eraser
7361 processKey(mapper, BTN_TOOL_MOUSE, 1);
7362 processSync(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7366
7367 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7368 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7369 processSync(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7373
7374 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7375 processToolType(mapper, MT_TOOL_PEN);
7376 processSync(mapper);
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7380
7381 // back to default tool type
7382 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7383 processKey(mapper, BTN_TOOL_MOUSE, 0);
7384 processKey(mapper, BTN_TOOL_RUBBER, 0);
7385 processKey(mapper, BTN_TOOL_PEN, 0);
7386 processKey(mapper, BTN_TOOL_FINGER, 0);
7387 processSync(mapper);
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7391}
7392
7393TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007394 addConfigurationProperty("touch.deviceType", "touchScreen");
7395 prepareDisplay(DISPLAY_ORIENTATION_0);
7396 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007397 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007398 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007399
7400 NotifyMotionArgs motionArgs;
7401
7402 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7403 processId(mapper, 1);
7404 processPosition(mapper, 100, 200);
7405 processSync(mapper);
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7409 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7410
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7414 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7415
7416 // move a little
7417 processPosition(mapper, 150, 250);
7418 processSync(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7422 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7423
7424 // down when BTN_TOUCH is pressed, pressure defaults to 1
7425 processKey(mapper, BTN_TOUCH, 1);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7430 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7431
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7433 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7435 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7436
7437 // up when BTN_TOUCH is released, hover restored
7438 processKey(mapper, BTN_TOUCH, 0);
7439 processSync(mapper);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7443 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7444
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7448 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7449
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7453 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7454
7455 // exit hover when pointer goes away
7456 processId(mapper, -1);
7457 processSync(mapper);
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7461 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7462}
7463
7464TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007465 addConfigurationProperty("touch.deviceType", "touchScreen");
7466 prepareDisplay(DISPLAY_ORIENTATION_0);
7467 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007469
7470 NotifyMotionArgs motionArgs;
7471
7472 // initially hovering because pressure is 0
7473 processId(mapper, 1);
7474 processPosition(mapper, 100, 200);
7475 processPressure(mapper, 0);
7476 processSync(mapper);
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7480 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7481
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7486
7487 // move a little
7488 processPosition(mapper, 150, 250);
7489 processSync(mapper);
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7493 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7494
7495 // down when pressure becomes non-zero
7496 processPressure(mapper, RAW_PRESSURE_MAX);
7497 processSync(mapper);
7498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7501 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7502
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7506 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7507
7508 // up when pressure becomes 0, hover restored
7509 processPressure(mapper, 0);
7510 processSync(mapper);
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7512 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7514 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7515
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7524 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7525
7526 // exit hover when pointer goes away
7527 processId(mapper, -1);
7528 processSync(mapper);
7529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7532 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7533}
7534
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007535/**
7536 * Set the input device port <--> display port associations, and check that the
7537 * events are routed to the display that matches the display port.
7538 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7539 */
7540TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007541 const std::string usb2 = "USB2";
7542 const uint8_t hdmi1 = 0;
7543 const uint8_t hdmi2 = 1;
7544 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007545 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007546
7547 addConfigurationProperty("touch.deviceType", "touchScreen");
7548 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007549 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007550
7551 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7552 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7553
7554 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7555 // for this input device is specified, and the matching viewport is not present,
7556 // the input device should be disabled (at the mapper level).
7557
7558 // Add viewport for display 2 on hdmi2
7559 prepareSecondaryDisplay(type, hdmi2);
7560 // Send a touch event
7561 processPosition(mapper, 100, 100);
7562 processSync(mapper);
7563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7564
7565 // Add viewport for display 1 on hdmi1
7566 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7567 // Send a touch event again
7568 processPosition(mapper, 100, 100);
7569 processSync(mapper);
7570
7571 NotifyMotionArgs args;
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7573 ASSERT_EQ(DISPLAY_ID, args.displayId);
7574}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007575
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007576TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007577 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007578 std::shared_ptr<FakePointerController> fakePointerController =
7579 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007580 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007581 fakePointerController->setPosition(100, 200);
7582 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007583 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7584
Garfield Tan888a6a42020-01-09 11:39:16 -08007585 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007586 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007587
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007588 prepareDisplay(DISPLAY_ORIENTATION_0);
7589 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007591
7592 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007593 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007594
7595 NotifyMotionArgs motionArgs;
7596 processPosition(mapper, 100, 100);
7597 processSync(mapper);
7598
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7601 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7602}
7603
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007604/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007605 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7606 */
7607TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7608 addConfigurationProperty("touch.deviceType", "touchScreen");
7609 prepareAxes(POSITION);
7610 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7611
7612 prepareDisplay(DISPLAY_ORIENTATION_0);
7613 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7614 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7615 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7616 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7617
7618 NotifyMotionArgs args;
7619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7620 ASSERT_EQ(26, args.readTime);
7621
7622 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7623 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7624 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7625
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7627 ASSERT_EQ(33, args.readTime);
7628}
7629
7630/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007631 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7632 * events should not be delivered to the listener.
7633 */
7634TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7635 addConfigurationProperty("touch.deviceType", "touchScreen");
7636 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7637 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7638 ViewportType::INTERNAL);
7639 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7640 prepareAxes(POSITION);
7641 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7642
7643 NotifyMotionArgs motionArgs;
7644 processPosition(mapper, 100, 100);
7645 processSync(mapper);
7646
7647 mFakeListener->assertNotifyMotionWasNotCalled();
7648}
7649
Garfield Tanc734e4f2021-01-15 20:01:39 -08007650TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7651 addConfigurationProperty("touch.deviceType", "touchScreen");
7652 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7653 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7654 ViewportType::INTERNAL);
7655 std::optional<DisplayViewport> optionalDisplayViewport =
7656 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7657 ASSERT_TRUE(optionalDisplayViewport.has_value());
7658 DisplayViewport displayViewport = *optionalDisplayViewport;
7659
7660 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7661 prepareAxes(POSITION);
7662 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7663
7664 // Finger down
7665 int32_t x = 100, y = 100;
7666 processPosition(mapper, x, y);
7667 processSync(mapper);
7668
7669 NotifyMotionArgs motionArgs;
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7672
7673 // Deactivate display viewport
7674 displayViewport.isActive = false;
7675 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7676 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7677
7678 // Finger move
7679 x += 10, y += 10;
7680 processPosition(mapper, x, y);
7681 processSync(mapper);
7682
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7684 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7685
7686 // Reactivate display viewport
7687 displayViewport.isActive = true;
7688 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7689 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7690
7691 // Finger move again
7692 x += 10, y += 10;
7693 processPosition(mapper, x, y);
7694 processSync(mapper);
7695
7696 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7697 // no pointer on the touch device.
7698 mFakeListener->assertNotifyMotionWasNotCalled();
7699}
7700
Arthur Hung7c645402019-01-25 17:45:42 +08007701TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7702 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007703 prepareAxes(POSITION | ID | SLOT);
7704 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007705 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007706
7707 // Create the second touch screen device, and enable multi fingers.
7708 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007709 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007710 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007711 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007712 std::shared_ptr<InputDevice> device2 =
7713 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7714 Flags<InputDeviceClass>(0));
7715
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007716 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7717 0 /*flat*/, 0 /*fuzz*/);
7718 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7719 0 /*flat*/, 0 /*fuzz*/);
7720 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7721 0 /*flat*/, 0 /*fuzz*/);
7722 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7723 0 /*flat*/, 0 /*fuzz*/);
7724 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7725 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7726 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007727
7728 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007729 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007730 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7731 device2->reset(ARBITRARY_TIME);
7732
7733 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007734 std::shared_ptr<FakePointerController> fakePointerController =
7735 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007736 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7737 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7738
7739 // Setup policy for associated displays and show touches.
7740 const uint8_t hdmi1 = 0;
7741 const uint8_t hdmi2 = 1;
7742 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7743 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7744 mFakePolicy->setShowTouches(true);
7745
7746 // Create displays.
7747 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007748 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007749
7750 // Default device will reconfigure above, need additional reconfiguration for another device.
7751 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007752 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007753
7754 // Two fingers down at default display.
7755 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7756 processPosition(mapper, x1, y1);
7757 processId(mapper, 1);
7758 processSlot(mapper, 1);
7759 processPosition(mapper, x2, y2);
7760 processId(mapper, 2);
7761 processSync(mapper);
7762
7763 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7764 fakePointerController->getSpots().find(DISPLAY_ID);
7765 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7766 ASSERT_EQ(size_t(2), iter->second.size());
7767
7768 // Two fingers down at second display.
7769 processPosition(mapper2, x1, y1);
7770 processId(mapper2, 1);
7771 processSlot(mapper2, 1);
7772 processPosition(mapper2, x2, y2);
7773 processId(mapper2, 2);
7774 processSync(mapper2);
7775
7776 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7777 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7778 ASSERT_EQ(size_t(2), iter->second.size());
7779}
7780
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007781TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007782 prepareAxes(POSITION);
7783 addConfigurationProperty("touch.deviceType", "touchScreen");
7784 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007786
7787 NotifyMotionArgs motionArgs;
7788 // Unrotated video frame
7789 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7790 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007791 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007792 processPosition(mapper, 100, 200);
7793 processSync(mapper);
7794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7795 ASSERT_EQ(frames, motionArgs.videoFrames);
7796
7797 // Subsequent touch events should not have any videoframes
7798 // This is implemented separately in FakeEventHub,
7799 // but that should match the behaviour of TouchVideoDevice.
7800 processPosition(mapper, 200, 200);
7801 processSync(mapper);
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7803 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7804}
7805
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007806TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007807 prepareAxes(POSITION);
7808 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007809 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007810 // Unrotated video frame
7811 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7812 NotifyMotionArgs motionArgs;
7813
7814 // Test all 4 orientations
7815 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7816 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7817 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7818 clearViewports();
7819 prepareDisplay(orientation);
7820 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007821 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007822 processPosition(mapper, 100, 200);
7823 processSync(mapper);
7824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7825 frames[0].rotate(orientation);
7826 ASSERT_EQ(frames, motionArgs.videoFrames);
7827 }
7828}
7829
7830TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007831 prepareAxes(POSITION);
7832 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007833 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007834 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7835 // so mix these.
7836 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7837 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7838 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7839 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7840 NotifyMotionArgs motionArgs;
7841
7842 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007843 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007844 processPosition(mapper, 100, 200);
7845 processSync(mapper);
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7847 std::for_each(frames.begin(), frames.end(),
7848 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7849 ASSERT_EQ(frames, motionArgs.videoFrames);
7850}
7851
Arthur Hung9da14732019-09-02 16:16:58 +08007852/**
7853 * If we had defined port associations, but the viewport is not ready, the touch device would be
7854 * expected to be disabled, and it should be enabled after the viewport has found.
7855 */
7856TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007857 constexpr uint8_t hdmi2 = 1;
7858 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007859 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007860
7861 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7862
7863 addConfigurationProperty("touch.deviceType", "touchScreen");
7864 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007865 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007866
7867 ASSERT_EQ(mDevice->isEnabled(), false);
7868
7869 // Add display on hdmi2, the device should be enabled and can receive touch event.
7870 prepareSecondaryDisplay(type, hdmi2);
7871 ASSERT_EQ(mDevice->isEnabled(), true);
7872
7873 // Send a touch event.
7874 processPosition(mapper, 100, 100);
7875 processSync(mapper);
7876
7877 NotifyMotionArgs args;
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7879 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7880}
7881
Arthur Hung421eb1c2020-01-16 00:09:42 +08007882TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007883 addConfigurationProperty("touch.deviceType", "touchScreen");
7884 prepareDisplay(DISPLAY_ORIENTATION_0);
7885 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007887
7888 NotifyMotionArgs motionArgs;
7889
7890 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7891 // finger down
7892 processId(mapper, 1);
7893 processPosition(mapper, x1, y1);
7894 processSync(mapper);
7895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7896 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7898
7899 // finger move
7900 processId(mapper, 1);
7901 processPosition(mapper, x2, y2);
7902 processSync(mapper);
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7906
7907 // finger up.
7908 processId(mapper, -1);
7909 processSync(mapper);
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7911 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7913
7914 // new finger down
7915 processId(mapper, 1);
7916 processPosition(mapper, x3, y3);
7917 processSync(mapper);
7918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7921}
7922
7923/**
arthurhungcc7f9802020-04-30 17:55:40 +08007924 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7925 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007926 */
arthurhungcc7f9802020-04-30 17:55:40 +08007927TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007928 addConfigurationProperty("touch.deviceType", "touchScreen");
7929 prepareDisplay(DISPLAY_ORIENTATION_0);
7930 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007931 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007932
7933 NotifyMotionArgs motionArgs;
7934
7935 // default tool type is finger
7936 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007937 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007938 processPosition(mapper, x1, y1);
7939 processSync(mapper);
7940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7941 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7943
7944 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7945 processToolType(mapper, MT_TOOL_PALM);
7946 processSync(mapper);
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7948 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7949
7950 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007951 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007952 processPosition(mapper, x2, y2);
7953 processSync(mapper);
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7955
7956 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007957 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007958 processSync(mapper);
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7960
7961 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007962 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007963 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007964 processPosition(mapper, x3, y3);
7965 processSync(mapper);
7966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7969}
7970
arthurhungbf89a482020-04-17 17:37:55 +08007971/**
arthurhungcc7f9802020-04-30 17:55:40 +08007972 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7973 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007974 */
arthurhungcc7f9802020-04-30 17:55:40 +08007975TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007976 addConfigurationProperty("touch.deviceType", "touchScreen");
7977 prepareDisplay(DISPLAY_ORIENTATION_0);
7978 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7979 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7980
7981 NotifyMotionArgs motionArgs;
7982
7983 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007984 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7985 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007986 processPosition(mapper, x1, y1);
7987 processSync(mapper);
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7991
7992 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007993 processSlot(mapper, SECOND_SLOT);
7994 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007995 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007996 processSync(mapper);
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7998 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7999 motionArgs.action);
8000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8001
8002 // If the tool type of the first finger changes to MT_TOOL_PALM,
8003 // we expect to receive ACTION_POINTER_UP with cancel flag.
8004 processSlot(mapper, FIRST_SLOT);
8005 processId(mapper, FIRST_TRACKING_ID);
8006 processToolType(mapper, MT_TOOL_PALM);
8007 processSync(mapper);
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8009 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8010 motionArgs.action);
8011 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8012
8013 // The following MOVE events of second finger should be processed.
8014 processSlot(mapper, SECOND_SLOT);
8015 processId(mapper, SECOND_TRACKING_ID);
8016 processPosition(mapper, x2 + 1, y2 + 1);
8017 processSync(mapper);
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8020 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8021
8022 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8023 // it. Second finger receive move.
8024 processSlot(mapper, FIRST_SLOT);
8025 processId(mapper, INVALID_TRACKING_ID);
8026 processSync(mapper);
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8029 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8030
8031 // Second finger keeps moving.
8032 processSlot(mapper, SECOND_SLOT);
8033 processId(mapper, SECOND_TRACKING_ID);
8034 processPosition(mapper, x2 + 2, y2 + 2);
8035 processSync(mapper);
8036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8038 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8039
8040 // Second finger up.
8041 processId(mapper, INVALID_TRACKING_ID);
8042 processSync(mapper);
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8045 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8046}
8047
8048/**
8049 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8050 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8051 */
8052TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8053 addConfigurationProperty("touch.deviceType", "touchScreen");
8054 prepareDisplay(DISPLAY_ORIENTATION_0);
8055 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8056 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8057
8058 NotifyMotionArgs motionArgs;
8059
8060 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8061 // First finger down.
8062 processId(mapper, FIRST_TRACKING_ID);
8063 processPosition(mapper, x1, y1);
8064 processSync(mapper);
8065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8066 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8068
8069 // Second finger down.
8070 processSlot(mapper, SECOND_SLOT);
8071 processId(mapper, SECOND_TRACKING_ID);
8072 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008073 processSync(mapper);
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8075 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8076 motionArgs.action);
8077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8078
arthurhungcc7f9802020-04-30 17:55:40 +08008079 // If the tool type of the first finger changes to MT_TOOL_PALM,
8080 // we expect to receive ACTION_POINTER_UP with cancel flag.
8081 processSlot(mapper, FIRST_SLOT);
8082 processId(mapper, FIRST_TRACKING_ID);
8083 processToolType(mapper, MT_TOOL_PALM);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8086 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8087 motionArgs.action);
8088 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8089
8090 // Second finger keeps moving.
8091 processSlot(mapper, SECOND_SLOT);
8092 processId(mapper, SECOND_TRACKING_ID);
8093 processPosition(mapper, x2 + 1, y2 + 1);
8094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8097
8098 // second finger becomes palm, receive cancel due to only 1 finger is active.
8099 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008100 processToolType(mapper, MT_TOOL_PALM);
8101 processSync(mapper);
8102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8103 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8104
arthurhungcc7f9802020-04-30 17:55:40 +08008105 // third finger down.
8106 processSlot(mapper, THIRD_SLOT);
8107 processId(mapper, THIRD_TRACKING_ID);
8108 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008109 processPosition(mapper, x3, y3);
8110 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008114 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8115
8116 // third finger move
8117 processId(mapper, THIRD_TRACKING_ID);
8118 processPosition(mapper, x3 + 1, y3 + 1);
8119 processSync(mapper);
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8122
8123 // first finger up, third finger receive move.
8124 processSlot(mapper, FIRST_SLOT);
8125 processId(mapper, INVALID_TRACKING_ID);
8126 processSync(mapper);
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8129 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8130
8131 // second finger up, third finger receive move.
8132 processSlot(mapper, SECOND_SLOT);
8133 processId(mapper, INVALID_TRACKING_ID);
8134 processSync(mapper);
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8137 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8138
8139 // third finger up.
8140 processSlot(mapper, THIRD_SLOT);
8141 processId(mapper, INVALID_TRACKING_ID);
8142 processSync(mapper);
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8145 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8146}
8147
8148/**
8149 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8150 * and the active finger could still be allowed to receive the events
8151 */
8152TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8153 addConfigurationProperty("touch.deviceType", "touchScreen");
8154 prepareDisplay(DISPLAY_ORIENTATION_0);
8155 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8156 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8157
8158 NotifyMotionArgs motionArgs;
8159
8160 // default tool type is finger
8161 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8162 processId(mapper, FIRST_TRACKING_ID);
8163 processPosition(mapper, x1, y1);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8168
8169 // Second finger down.
8170 processSlot(mapper, SECOND_SLOT);
8171 processId(mapper, SECOND_TRACKING_ID);
8172 processPosition(mapper, x2, y2);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8176 motionArgs.action);
8177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8178
8179 // If the tool type of the second finger changes to MT_TOOL_PALM,
8180 // we expect to receive ACTION_POINTER_UP with cancel flag.
8181 processId(mapper, SECOND_TRACKING_ID);
8182 processToolType(mapper, MT_TOOL_PALM);
8183 processSync(mapper);
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8185 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8186 motionArgs.action);
8187 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8188
8189 // The following MOVE event should be processed.
8190 processSlot(mapper, FIRST_SLOT);
8191 processId(mapper, FIRST_TRACKING_ID);
8192 processPosition(mapper, x1 + 1, y1 + 1);
8193 processSync(mapper);
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8196 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8197
8198 // second finger up.
8199 processSlot(mapper, SECOND_SLOT);
8200 processId(mapper, INVALID_TRACKING_ID);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8204
8205 // first finger keep moving
8206 processSlot(mapper, FIRST_SLOT);
8207 processId(mapper, FIRST_TRACKING_ID);
8208 processPosition(mapper, x1 + 2, y1 + 2);
8209 processSync(mapper);
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8212
8213 // first finger up.
8214 processId(mapper, INVALID_TRACKING_ID);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8217 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8218 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008219}
8220
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008221// --- MultiTouchInputMapperTest_ExternalDevice ---
8222
8223class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8224protected:
Chris Yea52ade12020-08-27 16:49:20 -07008225 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008226};
8227
8228/**
8229 * Expect fallback to internal viewport if device is external and external viewport is not present.
8230 */
8231TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8232 prepareAxes(POSITION);
8233 addConfigurationProperty("touch.deviceType", "touchScreen");
8234 prepareDisplay(DISPLAY_ORIENTATION_0);
8235 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8236
8237 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8238
8239 NotifyMotionArgs motionArgs;
8240
8241 // Expect the event to be sent to the internal viewport,
8242 // because an external viewport is not present.
8243 processPosition(mapper, 100, 100);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8246 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8247
8248 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008249 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008250 processPosition(mapper, 100, 100);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8254}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008255
8256/**
8257 * Test touch should not work if outside of surface.
8258 */
8259class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8260protected:
8261 void halfDisplayToCenterHorizontal(int32_t orientation) {
8262 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008263 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008264
8265 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8266 internalViewport->orientation = orientation;
8267 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8268 internalViewport->logicalLeft = 0;
8269 internalViewport->logicalTop = 0;
8270 internalViewport->logicalRight = DISPLAY_HEIGHT;
8271 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8272
8273 internalViewport->physicalLeft = 0;
8274 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8275 internalViewport->physicalRight = DISPLAY_HEIGHT;
8276 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8277
8278 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8279 internalViewport->deviceHeight = DISPLAY_WIDTH;
8280 } else {
8281 internalViewport->logicalLeft = 0;
8282 internalViewport->logicalTop = 0;
8283 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8284 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8285
8286 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8287 internalViewport->physicalTop = 0;
8288 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8289 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8290
8291 internalViewport->deviceWidth = DISPLAY_WIDTH;
8292 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8293 }
8294
8295 mFakePolicy->updateViewport(internalViewport.value());
8296 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8297 }
8298
arthurhung5d547942020-12-14 17:04:45 +08008299 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8300 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008301 int32_t yExpected) {
8302 // touch on outside area should not work.
8303 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8304 processSync(mapper);
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8306
8307 // touch on inside area should receive the event.
8308 NotifyMotionArgs args;
8309 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8312 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8313 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8314
8315 // Reset.
8316 mapper.reset(ARBITRARY_TIME);
8317 }
8318};
8319
arthurhung5d547942020-12-14 17:04:45 +08008320TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008321 addConfigurationProperty("touch.deviceType", "touchScreen");
8322 prepareDisplay(DISPLAY_ORIENTATION_0);
8323 prepareAxes(POSITION);
8324 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8325
8326 // Touch on center of normal display should work.
8327 const int32_t x = DISPLAY_WIDTH / 4;
8328 const int32_t y = DISPLAY_HEIGHT / 2;
8329 processPosition(mapper, toRawX(x), toRawY(y));
8330 processSync(mapper);
8331 NotifyMotionArgs args;
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8334 0.0f, 0.0f, 0.0f, 0.0f));
8335 // Reset.
8336 mapper.reset(ARBITRARY_TIME);
8337
8338 // Let physical display be different to device, and make surface and physical could be 1:1.
8339 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8340
8341 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8342 const int32_t yExpected = y;
8343 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8344}
8345
arthurhung5d547942020-12-14 17:04:45 +08008346TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008347 addConfigurationProperty("touch.deviceType", "touchScreen");
8348 prepareDisplay(DISPLAY_ORIENTATION_0);
8349 prepareAxes(POSITION);
8350 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8351
8352 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8353 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8354
8355 const int32_t x = DISPLAY_WIDTH / 4;
8356 const int32_t y = DISPLAY_HEIGHT / 2;
8357
8358 // expect x/y = swap x/y then reverse y.
8359 const int32_t xExpected = y;
8360 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8361 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8362}
8363
arthurhung5d547942020-12-14 17:04:45 +08008364TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008365 addConfigurationProperty("touch.deviceType", "touchScreen");
8366 prepareDisplay(DISPLAY_ORIENTATION_0);
8367 prepareAxes(POSITION);
8368 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8369
8370 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8371 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8372
8373 const int32_t x = DISPLAY_WIDTH / 4;
8374 const int32_t y = DISPLAY_HEIGHT / 2;
8375
8376 // expect x/y = swap x/y then reverse x.
8377 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8378 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8379 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8380}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008381
arthurhunga36b28e2020-12-29 20:28:15 +08008382TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8383 addConfigurationProperty("touch.deviceType", "touchScreen");
8384 prepareDisplay(DISPLAY_ORIENTATION_0);
8385 prepareAxes(POSITION);
8386 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8387
8388 const int32_t x = 0;
8389 const int32_t y = 0;
8390
8391 const int32_t xExpected = x;
8392 const int32_t yExpected = y;
8393 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8394
8395 clearViewports();
8396 prepareDisplay(DISPLAY_ORIENTATION_90);
8397 // expect x/y = swap x/y then reverse y.
8398 const int32_t xExpected90 = y;
8399 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8400 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8401
8402 clearViewports();
8403 prepareDisplay(DISPLAY_ORIENTATION_270);
8404 // expect x/y = swap x/y then reverse x.
8405 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8406 const int32_t yExpected270 = x;
8407 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8408}
8409
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008410TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8411 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8412 std::shared_ptr<FakePointerController> fakePointerController =
8413 std::make_shared<FakePointerController>();
8414 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8415 fakePointerController->setPosition(0, 0);
8416 fakePointerController->setButtonState(0);
8417
8418 // prepare device and capture
8419 prepareDisplay(DISPLAY_ORIENTATION_0);
8420 prepareAxes(POSITION | ID | SLOT);
8421 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8422 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8423 mFakePolicy->setPointerCapture(true);
8424 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8426
8427 // captured touchpad should be a touchpad source
8428 NotifyDeviceResetArgs resetArgs;
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8430 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8431
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008432 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008433
8434 const InputDeviceInfo::MotionRange* relRangeX =
8435 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8436 ASSERT_NE(relRangeX, nullptr);
8437 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8438 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8439 const InputDeviceInfo::MotionRange* relRangeY =
8440 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8441 ASSERT_NE(relRangeY, nullptr);
8442 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8443 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8444
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008445 // run captured pointer tests - note that this is unscaled, so input listener events should be
8446 // identical to what the hardware sends (accounting for any
8447 // calibration).
8448 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008449 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008450 processId(mapper, 1);
8451 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8452 processKey(mapper, BTN_TOUCH, 1);
8453 processSync(mapper);
8454
8455 // expect coord[0] to contain initial location of touch 0
8456 NotifyMotionArgs args;
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8458 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8459 ASSERT_EQ(1U, args.pointerCount);
8460 ASSERT_EQ(0, args.pointerProperties[0].id);
8461 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8462 ASSERT_NO_FATAL_FAILURE(
8463 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8464
8465 // FINGER 1 DOWN
8466 processSlot(mapper, 1);
8467 processId(mapper, 2);
8468 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8469 processSync(mapper);
8470
8471 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008473 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8474 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008475 ASSERT_EQ(2U, args.pointerCount);
8476 ASSERT_EQ(0, args.pointerProperties[0].id);
8477 ASSERT_EQ(1, args.pointerProperties[1].id);
8478 ASSERT_NO_FATAL_FAILURE(
8479 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8480 ASSERT_NO_FATAL_FAILURE(
8481 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8482
8483 // FINGER 1 MOVE
8484 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8485 processSync(mapper);
8486
8487 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8488 // from move
8489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8491 ASSERT_NO_FATAL_FAILURE(
8492 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8493 ASSERT_NO_FATAL_FAILURE(
8494 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8495
8496 // FINGER 0 MOVE
8497 processSlot(mapper, 0);
8498 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8499 processSync(mapper);
8500
8501 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8504 ASSERT_NO_FATAL_FAILURE(
8505 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8506 ASSERT_NO_FATAL_FAILURE(
8507 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8508
8509 // BUTTON DOWN
8510 processKey(mapper, BTN_LEFT, 1);
8511 processSync(mapper);
8512
8513 // touchinputmapper design sends a move before button press
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8518
8519 // BUTTON UP
8520 processKey(mapper, BTN_LEFT, 0);
8521 processSync(mapper);
8522
8523 // touchinputmapper design sends a move after button release
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8528
8529 // FINGER 0 UP
8530 processId(mapper, -1);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8534
8535 // FINGER 1 MOVE
8536 processSlot(mapper, 1);
8537 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8538 processSync(mapper);
8539
8540 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8543 ASSERT_EQ(1U, args.pointerCount);
8544 ASSERT_EQ(1, args.pointerProperties[0].id);
8545 ASSERT_NO_FATAL_FAILURE(
8546 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8547
8548 // FINGER 1 UP
8549 processId(mapper, -1);
8550 processKey(mapper, BTN_TOUCH, 0);
8551 processSync(mapper);
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8553 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8554
8555 // non captured touchpad should be a mouse source
8556 mFakePolicy->setPointerCapture(false);
8557 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8559 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8560}
8561
8562TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8563 std::shared_ptr<FakePointerController> fakePointerController =
8564 std::make_shared<FakePointerController>();
8565 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8566 fakePointerController->setPosition(0, 0);
8567 fakePointerController->setButtonState(0);
8568
8569 // prepare device and capture
8570 prepareDisplay(DISPLAY_ORIENTATION_0);
8571 prepareAxes(POSITION | ID | SLOT);
8572 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8573 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8574 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8575 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8576 // run uncaptured pointer tests - pushes out generic events
8577 // FINGER 0 DOWN
8578 processId(mapper, 3);
8579 processPosition(mapper, 100, 100);
8580 processKey(mapper, BTN_TOUCH, 1);
8581 processSync(mapper);
8582
8583 // start at (100,100), cursor should be at (0,0) * scale
8584 NotifyMotionArgs args;
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8587 ASSERT_NO_FATAL_FAILURE(
8588 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8589
8590 // FINGER 0 MOVE
8591 processPosition(mapper, 200, 200);
8592 processSync(mapper);
8593
8594 // compute scaling to help with touch position checking
8595 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8596 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8597 float scale =
8598 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8599
8600 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8604 0, 0, 0, 0, 0, 0, 0));
8605}
8606
8607TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8608 std::shared_ptr<FakePointerController> fakePointerController =
8609 std::make_shared<FakePointerController>();
8610
8611 prepareDisplay(DISPLAY_ORIENTATION_0);
8612 prepareAxes(POSITION | ID | SLOT);
8613 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8614 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8615 mFakePolicy->setPointerCapture(false);
8616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8617
8618 // uncaptured touchpad should be a pointer device
8619 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8620
8621 // captured touchpad should be a touchpad device
8622 mFakePolicy->setPointerCapture(true);
8623 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8624 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8625}
8626
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008627// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008628
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008629class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008630protected:
8631 static const char* DEVICE_NAME;
8632 static const char* DEVICE_LOCATION;
8633 static const int32_t DEVICE_ID;
8634 static const int32_t DEVICE_GENERATION;
8635 static const int32_t DEVICE_CONTROLLER_NUMBER;
8636 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8637 static const int32_t EVENTHUB_ID;
8638
8639 std::shared_ptr<FakeEventHub> mFakeEventHub;
8640 sp<FakeInputReaderPolicy> mFakePolicy;
8641 sp<TestInputListener> mFakeListener;
8642 std::unique_ptr<InstrumentedInputReader> mReader;
8643 std::shared_ptr<InputDevice> mDevice;
8644
8645 virtual void SetUp(Flags<InputDeviceClass> classes) {
8646 mFakeEventHub = std::make_unique<FakeEventHub>();
8647 mFakePolicy = new FakeInputReaderPolicy();
8648 mFakeListener = new TestInputListener();
8649 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8650 mFakeListener);
8651 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8652 }
8653
8654 void SetUp() override { SetUp(DEVICE_CLASSES); }
8655
8656 void TearDown() override {
8657 mFakeListener.clear();
8658 mFakePolicy.clear();
8659 }
8660
8661 void configureDevice(uint32_t changes) {
8662 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8663 mReader->requestRefreshConfiguration(changes);
8664 mReader->loopOnce();
8665 }
8666 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8667 }
8668
8669 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8670 const std::string& location, int32_t eventHubId,
8671 Flags<InputDeviceClass> classes) {
8672 InputDeviceIdentifier identifier;
8673 identifier.name = name;
8674 identifier.location = location;
8675 std::shared_ptr<InputDevice> device =
8676 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8677 identifier);
8678 mReader->pushNextDevice(device);
8679 mFakeEventHub->addDevice(eventHubId, name, classes);
8680 mReader->loopOnce();
8681 return device;
8682 }
8683
8684 template <class T, typename... Args>
8685 T& addControllerAndConfigure(Args... args) {
8686 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8687
8688 return controller;
8689 }
8690};
8691
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008692const char* PeripheralControllerTest::DEVICE_NAME = "device";
8693const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8694const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8695const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8696const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8697const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008698 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008699const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008700
8701// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008702class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008703protected:
8704 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008705 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008706 }
8707};
8708
8709TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008710 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008711
8712 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8713 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8714}
8715
8716TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008717 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008718
8719 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8720 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8721}
8722
8723// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008724class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008725protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008726 void SetUp() override {
8727 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8728 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008729};
8730
Chris Ye85758332021-05-16 23:05:17 -07008731TEST_F(LightControllerTest, MonoLight) {
8732 RawLightInfo infoMono = {.id = 1,
8733 .name = "Mono",
8734 .maxBrightness = 255,
8735 .flags = InputLightClass::BRIGHTNESS,
8736 .path = ""};
8737 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008738
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008739 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008740 InputDeviceInfo info;
8741 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008742 std::vector<InputDeviceLightInfo> lights = info.getLights();
8743 ASSERT_EQ(1U, lights.size());
8744 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008745
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008746 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8747 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008748}
8749
8750TEST_F(LightControllerTest, RGBLight) {
8751 RawLightInfo infoRed = {.id = 1,
8752 .name = "red",
8753 .maxBrightness = 255,
8754 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8755 .path = ""};
8756 RawLightInfo infoGreen = {.id = 2,
8757 .name = "green",
8758 .maxBrightness = 255,
8759 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8760 .path = ""};
8761 RawLightInfo infoBlue = {.id = 3,
8762 .name = "blue",
8763 .maxBrightness = 255,
8764 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8765 .path = ""};
8766 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8767 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8768 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8769
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008770 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008771 InputDeviceInfo info;
8772 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008773 std::vector<InputDeviceLightInfo> lights = info.getLights();
8774 ASSERT_EQ(1U, lights.size());
8775 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008776
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008777 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8778 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008779}
8780
8781TEST_F(LightControllerTest, MultiColorRGBLight) {
8782 RawLightInfo infoColor = {.id = 1,
8783 .name = "red",
8784 .maxBrightness = 255,
8785 .flags = InputLightClass::BRIGHTNESS |
8786 InputLightClass::MULTI_INTENSITY |
8787 InputLightClass::MULTI_INDEX,
8788 .path = ""};
8789
8790 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8791
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008792 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008793 InputDeviceInfo info;
8794 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008795 std::vector<InputDeviceLightInfo> lights = info.getLights();
8796 ASSERT_EQ(1U, lights.size());
8797 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008798
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008799 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8800 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008801}
8802
8803TEST_F(LightControllerTest, PlayerIdLight) {
8804 RawLightInfo info1 = {.id = 1,
8805 .name = "player1",
8806 .maxBrightness = 255,
8807 .flags = InputLightClass::BRIGHTNESS,
8808 .path = ""};
8809 RawLightInfo info2 = {.id = 2,
8810 .name = "player2",
8811 .maxBrightness = 255,
8812 .flags = InputLightClass::BRIGHTNESS,
8813 .path = ""};
8814 RawLightInfo info3 = {.id = 3,
8815 .name = "player3",
8816 .maxBrightness = 255,
8817 .flags = InputLightClass::BRIGHTNESS,
8818 .path = ""};
8819 RawLightInfo info4 = {.id = 4,
8820 .name = "player4",
8821 .maxBrightness = 255,
8822 .flags = InputLightClass::BRIGHTNESS,
8823 .path = ""};
8824 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8825 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8826 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8827 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8828
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008829 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008830 InputDeviceInfo info;
8831 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008832 std::vector<InputDeviceLightInfo> lights = info.getLights();
8833 ASSERT_EQ(1U, lights.size());
8834 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008835
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008836 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8837 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
8838 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008839}
8840
Michael Wrightd02c5b62014-02-10 15:10:22 -08008841} // namespace android