blob: 23f30260bc392c392d0e58228adb5fb46bfc0d5c [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 Yef59a2f42020-10-16 12:55:26 -070025#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070026#include <SingleTouchInputMapper.h>
27#include <SwitchInputMapper.h>
28#include <TestInputListener.h>
29#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080030#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000031#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070032#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080034#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <math.h>
36
Michael Wright17db18e2020-06-26 20:51:44 +010037#include <memory>
Michael Wrightdde67b82020-10-27 16:09:22 +000038#include "input/DisplayViewport.h"
39#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041namespace android {
42
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070043using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070044using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070045
46// Timeout for waiting for an expected event
47static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049// An arbitrary time value.
50static const nsecs_t ARBITRARY_TIME = 1234;
51
52// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080053static constexpr int32_t DISPLAY_ID = 0;
54static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
55static constexpr int32_t DISPLAY_WIDTH = 480;
56static constexpr int32_t DISPLAY_HEIGHT = 800;
57static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
58static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
59static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070060static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070061static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t FIRST_SLOT = 0;
64static constexpr int32_t SECOND_SLOT = 1;
65static constexpr int32_t THIRD_SLOT = 2;
66static constexpr int32_t INVALID_TRACKING_ID = -1;
67static constexpr int32_t FIRST_TRACKING_ID = 0;
68static constexpr int32_t SECOND_TRACKING_ID = 1;
69static constexpr int32_t THIRD_TRACKING_ID = 2;
70
Michael Wrightd02c5b62014-02-10 15:10:22 -080071// Error tolerance for floating point assertions.
72static const float EPSILON = 0.001f;
73
74template<typename T>
75static inline T min(T a, T b) {
76 return a < b ? a : b;
77}
78
79static inline float avg(float x, float y) {
80 return (x + y) / 2;
81}
82
83
84// --- FakePointerController ---
85
86class FakePointerController : public PointerControllerInterface {
87 bool mHaveBounds;
88 float mMinX, mMinY, mMaxX, mMaxY;
89 float mX, mY;
90 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080091 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080092
Michael Wrightd02c5b62014-02-10 15:10:22 -080093public:
94 FakePointerController() :
95 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080096 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 }
98
Michael Wright17db18e2020-06-26 20:51:44 +010099 virtual ~FakePointerController() {}
100
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101 void setBounds(float minX, float minY, float maxX, float maxY) {
102 mHaveBounds = true;
103 mMinX = minX;
104 mMinY = minY;
105 mMaxX = maxX;
106 mMaxY = maxY;
107 }
108
Chris Yea52ade12020-08-27 16:49:20 -0700109 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110 mX = x;
111 mY = y;
112 }
113
Chris Yea52ade12020-08-27 16:49:20 -0700114 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Chris Yea52ade12020-08-27 16:49:20 -0700116 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Chris Yea52ade12020-08-27 16:49:20 -0700118 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 *outX = mX;
120 *outY = mY;
121 }
122
Chris Yea52ade12020-08-27 16:49:20 -0700123 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800124
Chris Yea52ade12020-08-27 16:49:20 -0700125 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800126 mDisplayId = viewport.displayId;
127 }
128
Arthur Hung7c645402019-01-25 17:45:42 +0800129 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
130 return mSpotsByDisplay;
131 }
132
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133private:
Chris Yea52ade12020-08-27 16:49:20 -0700134 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 *outMinX = mMinX;
136 *outMinY = mMinY;
137 *outMaxX = mMaxX;
138 *outMaxY = mMaxY;
139 return mHaveBounds;
140 }
141
Chris Yea52ade12020-08-27 16:49:20 -0700142 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 mX += deltaX;
144 if (mX < mMinX) mX = mMinX;
145 if (mX > mMaxX) mX = mMaxX;
146 mY += deltaY;
147 if (mY < mMinY) mY = mMinY;
148 if (mY > mMaxY) mY = mMaxY;
149 }
150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Chris Yea52ade12020-08-27 16:49:20 -0700153 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154
Chris Yea52ade12020-08-27 16:49:20 -0700155 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156
Chris Yea52ade12020-08-27 16:49:20 -0700157 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
158 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800159 std::vector<int32_t> newSpots;
160 // Add spots for fingers that are down.
161 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
162 uint32_t id = idBits.clearFirstMarkedBit();
163 newSpots.push_back(id);
164 }
165
166 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 }
168
Chris Yea52ade12020-08-27 16:49:20 -0700169 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800170
171 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172};
173
174
175// --- FakeInputReaderPolicy ---
176
177class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700178 std::mutex mLock;
179 std::condition_variable mDevicesChangedCondition;
180
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100182 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700183 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
184 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100185 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700186 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
188protected:
Chris Yea52ade12020-08-27 16:49:20 -0700189 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191public:
192 FakeInputReaderPolicy() {
193 }
194
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800196 waitForInputDevices([](bool devicesChanged) {
197 if (!devicesChanged) {
198 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
199 }
200 });
201 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700202
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800203 void assertInputDevicesNotChanged() {
204 waitForInputDevices([](bool devicesChanged) {
205 if (devicesChanged) {
206 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
207 }
208 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 }
210
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700211 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100212 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100213 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700214 }
215
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700216 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
217 return mConfig.getDisplayViewportByUniqueId(uniqueId);
218 }
219 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
220 return mConfig.getDisplayViewportByType(type);
221 }
222
223 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
224 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700225 }
226
227 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000228 bool isActive, const std::string& uniqueId,
229 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
230 const DisplayViewport viewport =
231 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
232 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700233 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100234 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235 }
236
Arthur Hung6cd19a42019-08-30 19:04:12 +0800237 bool updateViewport(const DisplayViewport& viewport) {
238 size_t count = mViewports.size();
239 for (size_t i = 0; i < count; i++) {
240 const DisplayViewport& currentViewport = mViewports[i];
241 if (currentViewport.displayId == viewport.displayId) {
242 mViewports[i] = viewport;
243 mConfig.setDisplayViewports(mViewports);
244 return true;
245 }
246 }
247 // no viewport found.
248 return false;
249 }
250
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100251 void addExcludedDeviceName(const std::string& deviceName) {
252 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253 }
254
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700255 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
256 mConfig.portAssociations.insert({inputPort, displayPort});
257 }
258
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000259 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700260
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000261 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700262
Michael Wright17db18e2020-06-26 20:51:44 +0100263 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
264 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 }
266
267 const InputReaderConfiguration* getReaderConfiguration() const {
268 return &mConfig;
269 }
270
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800271 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 return mInputDevices;
273 }
274
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100275 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700276 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700277 return transform;
278 }
279
280 void setTouchAffineTransformation(const TouchAffineTransformation t) {
281 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800282 }
283
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800284 void setPointerCapture(bool enabled) {
285 mConfig.pointerCapture = enabled;
286 }
287
Arthur Hung7c645402019-01-25 17:45:42 +0800288 void setShowTouches(bool enabled) {
289 mConfig.showTouches = enabled;
290 }
291
Garfield Tan888a6a42020-01-09 11:39:16 -0800292 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
293 mConfig.defaultPointerDisplayId = pointerDisplayId;
294 }
295
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800296 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
297
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700299 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000300 int32_t orientation, bool isActive,
301 const std::string& uniqueId,
302 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700303 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
304 || orientation == DISPLAY_ORIENTATION_270);
305 DisplayViewport v;
306 v.displayId = displayId;
307 v.orientation = orientation;
308 v.logicalLeft = 0;
309 v.logicalTop = 0;
310 v.logicalRight = isRotated ? height : width;
311 v.logicalBottom = isRotated ? width : height;
312 v.physicalLeft = 0;
313 v.physicalTop = 0;
314 v.physicalRight = isRotated ? height : width;
315 v.physicalBottom = isRotated ? width : height;
316 v.deviceWidth = isRotated ? height : width;
317 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000318 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700319 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700320 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100321 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700322 return v;
323 }
324
Chris Yea52ade12020-08-27 16:49:20 -0700325 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 *outConfig = mConfig;
327 }
328
Chris Yea52ade12020-08-27 16:49:20 -0700329 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100330 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 }
332
Chris Yea52ade12020-08-27 16:49:20 -0700333 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700334 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700336 mInputDevicesChanged = true;
337 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
341 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700342 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 }
344
Chris Yea52ade12020-08-27 16:49:20 -0700345 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800346
347 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
348 std::unique_lock<std::mutex> lock(mLock);
349 base::ScopedLockAssertion assumeLocked(mLock);
350
351 const bool devicesChanged =
352 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
353 return mInputDevicesChanged;
354 });
355 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
356 mInputDevicesChanged = false;
357 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358};
359
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360// --- FakeEventHub ---
361
362class FakeEventHub : public EventHubInterface {
363 struct KeyInfo {
364 int32_t keyCode;
365 uint32_t flags;
366 };
367
Chris Yef59a2f42020-10-16 12:55:26 -0700368 struct SensorInfo {
369 InputDeviceSensorType sensorType;
370 int32_t sensorDataIndex;
371 };
372
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 struct Device {
374 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700375 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 PropertyMap configuration;
377 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
378 KeyedVector<int, bool> relativeAxes;
379 KeyedVector<int32_t, int32_t> keyCodeStates;
380 KeyedVector<int32_t, int32_t> scanCodeStates;
381 KeyedVector<int32_t, int32_t> switchStates;
382 KeyedVector<int32_t, int32_t> absoluteAxisValue;
383 KeyedVector<int32_t, KeyInfo> keysByScanCode;
384 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
385 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700386 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
387 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800388 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700389 bool enabled;
390
391 status_t enable() {
392 enabled = true;
393 return OK;
394 }
395
396 status_t disable() {
397 enabled = false;
398 return OK;
399 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400
Chris Ye1b0c7342020-07-28 21:57:03 -0700401 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800402 };
403
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700404 std::mutex mLock;
405 std::condition_variable mEventsCondition;
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100408 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700409 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600410 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000411 std::vector<int32_t> mVibrators = {0, 1};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700413public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414 virtual ~FakeEventHub() {
415 for (size_t i = 0; i < mDevices.size(); i++) {
416 delete mDevices.valueAt(i);
417 }
418 }
419
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 FakeEventHub() { }
421
Chris Ye1b0c7342020-07-28 21:57:03 -0700422 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 Device* device = new Device(classes);
424 device->identifier.name = name;
425 mDevices.add(deviceId, device);
426
427 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
428 }
429
430 void removeDevice(int32_t deviceId) {
431 delete mDevices.valueFor(deviceId);
432 mDevices.removeItem(deviceId);
433
434 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
435 }
436
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 bool isDeviceEnabled(int32_t deviceId) {
438 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700439 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700440 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
441 return false;
442 }
443 return device->enabled;
444 }
445
446 status_t enableDevice(int32_t deviceId) {
447 status_t result;
448 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700449 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700450 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
451 return BAD_VALUE;
452 }
453 if (device->enabled) {
454 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
455 return OK;
456 }
457 result = device->enable();
458 return result;
459 }
460
461 status_t disableDevice(int32_t deviceId) {
462 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700463 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700464 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
465 return BAD_VALUE;
466 }
467 if (!device->enabled) {
468 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
469 return OK;
470 }
471 return device->disable();
472 }
473
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474 void finishDeviceScan() {
475 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
476 }
477
478 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
479 Device* device = getDevice(deviceId);
480 device->configuration.addProperty(key, value);
481 }
482
483 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
484 Device* device = getDevice(deviceId);
485 device->configuration.addAll(configuration);
486 }
487
488 void addAbsoluteAxis(int32_t deviceId, int axis,
489 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
490 Device* device = getDevice(deviceId);
491
492 RawAbsoluteAxisInfo info;
493 info.valid = true;
494 info.minValue = minValue;
495 info.maxValue = maxValue;
496 info.flat = flat;
497 info.fuzz = fuzz;
498 info.resolution = resolution;
499 device->absoluteAxes.add(axis, info);
500 }
501
502 void addRelativeAxis(int32_t deviceId, int32_t axis) {
503 Device* device = getDevice(deviceId);
504 device->relativeAxes.add(axis, true);
505 }
506
507 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
508 Device* device = getDevice(deviceId);
509 device->keyCodeStates.replaceValueFor(keyCode, state);
510 }
511
512 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
513 Device* device = getDevice(deviceId);
514 device->scanCodeStates.replaceValueFor(scanCode, state);
515 }
516
517 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
518 Device* device = getDevice(deviceId);
519 device->switchStates.replaceValueFor(switchCode, state);
520 }
521
522 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
523 Device* device = getDevice(deviceId);
524 device->absoluteAxisValue.replaceValueFor(axis, value);
525 }
526
527 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
528 int32_t keyCode, uint32_t flags) {
529 Device* device = getDevice(deviceId);
530 KeyInfo info;
531 info.keyCode = keyCode;
532 info.flags = flags;
533 if (scanCode) {
534 device->keysByScanCode.add(scanCode, info);
535 }
536 if (usageCode) {
537 device->keysByUsageCode.add(usageCode, info);
538 }
539 }
540
541 void addLed(int32_t deviceId, int32_t led, bool initialState) {
542 Device* device = getDevice(deviceId);
543 device->leds.add(led, initialState);
544 }
545
Chris Yef59a2f42020-10-16 12:55:26 -0700546 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
547 int32_t sensorDataIndex) {
548 Device* device = getDevice(deviceId);
549 SensorInfo info;
550 info.sensorType = sensorType;
551 info.sensorDataIndex = sensorDataIndex;
552 device->sensorsByAbsCode.emplace(absCode, info);
553 }
554
555 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
556 Device* device = getDevice(deviceId);
557 typename BitArray<MSC_MAX>::Buffer buffer;
558 buffer[mscEvent / 32] = 1 << mscEvent % 32;
559 device->mscBitmask.loadFromBuffer(buffer);
560 }
561
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 bool getLedState(int32_t deviceId, int32_t led) {
563 Device* device = getDevice(deviceId);
564 return device->leds.valueFor(led);
565 }
566
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100567 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 return mExcludedDevices;
569 }
570
571 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
572 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800573 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
576 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
577 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700578 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 RawEvent event;
580 event.when = when;
581 event.deviceId = deviceId;
582 event.type = type;
583 event.code = code;
584 event.value = value;
585 mEvents.push_back(event);
586
587 if (type == EV_ABS) {
588 setAbsoluteAxisValue(deviceId, code, value);
589 }
590 }
591
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600592 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
593 std::vector<TouchVideoFrame>> videoFrames) {
594 mVideoFrames = std::move(videoFrames);
595 }
596
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700598 std::unique_lock<std::mutex> lock(mLock);
599 base::ScopedLockAssertion assumeLocked(mLock);
600 const bool queueIsEmpty =
601 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
602 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
603 if (!queueIsEmpty) {
604 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
605 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 }
607
608private:
609 Device* getDevice(int32_t deviceId) const {
610 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100611 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Chris Yea52ade12020-08-27 16:49:20 -0700614 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700616 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Chris Yea52ade12020-08-27 16:49:20 -0700619 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 Device* device = getDevice(deviceId);
621 return device ? device->identifier : InputDeviceIdentifier();
622 }
623
Chris Yea52ade12020-08-27 16:49:20 -0700624 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625
Chris Yea52ade12020-08-27 16:49:20 -0700626 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 Device* device = getDevice(deviceId);
628 if (device) {
629 *outConfiguration = device->configuration;
630 }
631 }
632
Chris Yea52ade12020-08-27 16:49:20 -0700633 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
634 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800636 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 ssize_t index = device->absoluteAxes.indexOfKey(axis);
638 if (index >= 0) {
639 *outAxisInfo = device->absoluteAxes.valueAt(index);
640 return OK;
641 }
642 }
643 outAxisInfo->clear();
644 return -1;
645 }
646
Chris Yea52ade12020-08-27 16:49:20 -0700647 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 Device* device = getDevice(deviceId);
649 if (device) {
650 return device->relativeAxes.indexOfKey(axis) >= 0;
651 }
652 return false;
653 }
654
Chris Yea52ade12020-08-27 16:49:20 -0700655 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656
Chris Yef59a2f42020-10-16 12:55:26 -0700657 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
658 Device* device = getDevice(deviceId);
659 if (device) {
660 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
661 }
662 return false;
663 }
664
Chris Yea52ade12020-08-27 16:49:20 -0700665 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
666 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 Device* device = getDevice(deviceId);
668 if (device) {
669 const KeyInfo* key = getKey(device, scanCode, usageCode);
670 if (key) {
671 if (outKeycode) {
672 *outKeycode = key->keyCode;
673 }
674 if (outFlags) {
675 *outFlags = key->flags;
676 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700677 if (outMetaState) {
678 *outMetaState = metaState;
679 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 return OK;
681 }
682 }
683 return NAME_NOT_FOUND;
684 }
685
686 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
687 if (usageCode) {
688 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
689 if (index >= 0) {
690 return &device->keysByUsageCode.valueAt(index);
691 }
692 }
693 if (scanCode) {
694 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
695 if (index >= 0) {
696 return &device->keysByScanCode.valueAt(index);
697 }
698 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700699 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 }
701
Chris Yea52ade12020-08-27 16:49:20 -0700702 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703
Chris Yef59a2f42020-10-16 12:55:26 -0700704 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
705 int32_t absCode) {
706 Device* device = getDevice(deviceId);
707 if (!device) {
708 return Errorf("Sensor device not found.");
709 }
710 auto it = device->sensorsByAbsCode.find(absCode);
711 if (it == device->sensorsByAbsCode.end()) {
712 return Errorf("Sensor map not found.");
713 }
714 const SensorInfo& info = it->second;
715 return std::make_pair(info.sensorType, info.sensorDataIndex);
716 }
717
Chris Yea52ade12020-08-27 16:49:20 -0700718 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 mExcludedDevices = devices;
720 }
721
Chris Yea52ade12020-08-27 16:49:20 -0700722 size_t getEvents(int, RawEvent* buffer, size_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700723 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 if (mEvents.empty()) {
725 return 0;
726 }
727
728 *buffer = *mEvents.begin();
729 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700730 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 return 1;
732 }
733
Chris Yea52ade12020-08-27 16:49:20 -0700734 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600735 auto it = mVideoFrames.find(deviceId);
736 if (it != mVideoFrames.end()) {
737 std::vector<TouchVideoFrame> frames = std::move(it->second);
738 mVideoFrames.erase(deviceId);
739 return frames;
740 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800741 return {};
742 }
743
Chris Yea52ade12020-08-27 16:49:20 -0700744 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
746 if (device) {
747 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
748 if (index >= 0) {
749 return device->scanCodeStates.valueAt(index);
750 }
751 }
752 return AKEY_STATE_UNKNOWN;
753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 Device* device = getDevice(deviceId);
757 if (device) {
758 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
759 if (index >= 0) {
760 return device->keyCodeStates.valueAt(index);
761 }
762 }
763 return AKEY_STATE_UNKNOWN;
764 }
765
Chris Yea52ade12020-08-27 16:49:20 -0700766 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800767 Device* device = getDevice(deviceId);
768 if (device) {
769 ssize_t index = device->switchStates.indexOfKey(sw);
770 if (index >= 0) {
771 return device->switchStates.valueAt(index);
772 }
773 }
774 return AKEY_STATE_UNKNOWN;
775 }
776
Chris Yea52ade12020-08-27 16:49:20 -0700777 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
778 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779 Device* device = getDevice(deviceId);
780 if (device) {
781 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
782 if (index >= 0) {
783 *outValue = device->absoluteAxisValue.valueAt(index);
784 return OK;
785 }
786 }
787 *outValue = 0;
788 return -1;
789 }
790
Chris Yea52ade12020-08-27 16:49:20 -0700791 // Return true if the device has non-empty key layout.
792 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
793 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 bool result = false;
795 Device* device = getDevice(deviceId);
796 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700797 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 for (size_t i = 0; i < numCodes; i++) {
799 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
800 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
801 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 }
803 }
804 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
805 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
806 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 }
808 }
809 }
810 }
811 return result;
812 }
813
Chris Yea52ade12020-08-27 16:49:20 -0700814 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815 Device* device = getDevice(deviceId);
816 if (device) {
817 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
818 return index >= 0;
819 }
820 return false;
821 }
822
Chris Yea52ade12020-08-27 16:49:20 -0700823 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824 Device* device = getDevice(deviceId);
825 return device && device->leds.indexOfKey(led) >= 0;
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 Device* device = getDevice(deviceId);
830 if (device) {
831 ssize_t index = device->leds.indexOfKey(led);
832 if (index >= 0) {
833 device->leds.replaceValueAt(led, on);
834 } else {
835 ADD_FAILURE()
836 << "Attempted to set the state of an LED that the EventHub declared "
837 "was not present. led=" << led;
838 }
839 }
840 }
841
Chris Yea52ade12020-08-27 16:49:20 -0700842 void getVirtualKeyDefinitions(
843 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 outVirtualKeys.clear();
845
846 Device* device = getDevice(deviceId);
847 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800848 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 }
850 }
851
Chris Yea52ade12020-08-27 16:49:20 -0700852 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700853 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 }
855
Chris Yea52ade12020-08-27 16:49:20 -0700856 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 return false;
858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
Chris Yea52ade12020-08-27 16:49:20 -0700862 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863
Chris Ye87143712020-11-10 05:05:58 +0000864 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
865
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100866 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867 return false;
868 }
869
Chris Yea52ade12020-08-27 16:49:20 -0700870 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871
Chris Yea52ade12020-08-27 16:49:20 -0700872 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873
Chris Yea52ade12020-08-27 16:49:20 -0700874 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875
Chris Yea52ade12020-08-27 16:49:20 -0700876 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877};
878
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879// --- FakeInputMapper ---
880
881class FakeInputMapper : public InputMapper {
882 uint32_t mSources;
883 int32_t mKeyboardType;
884 int32_t mMetaState;
885 KeyedVector<int32_t, int32_t> mKeyCodeStates;
886 KeyedVector<int32_t, int32_t> mScanCodeStates;
887 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800888 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700890 std::mutex mLock;
891 std::condition_variable mStateChangedCondition;
892 bool mConfigureWasCalled GUARDED_BY(mLock);
893 bool mResetWasCalled GUARDED_BY(mLock);
894 bool mProcessWasCalled GUARDED_BY(mLock);
895 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896
Arthur Hungc23540e2018-11-29 20:42:11 +0800897 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800899 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
900 : InputMapper(deviceContext),
901 mSources(sources),
902 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800904 mConfigureWasCalled(false),
905 mResetWasCalled(false),
906 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
Chris Yea52ade12020-08-27 16:49:20 -0700908 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
910 void setKeyboardType(int32_t keyboardType) {
911 mKeyboardType = keyboardType;
912 }
913
914 void setMetaState(int32_t metaState) {
915 mMetaState = metaState;
916 }
917
918 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700919 std::unique_lock<std::mutex> lock(mLock);
920 base::ScopedLockAssertion assumeLocked(mLock);
921 const bool configureCalled =
922 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
923 return mConfigureWasCalled;
924 });
925 if (!configureCalled) {
926 FAIL() << "Expected configure() to have been called.";
927 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 mConfigureWasCalled = false;
929 }
930
931 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700932 std::unique_lock<std::mutex> lock(mLock);
933 base::ScopedLockAssertion assumeLocked(mLock);
934 const bool resetCalled =
935 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
936 return mResetWasCalled;
937 });
938 if (!resetCalled) {
939 FAIL() << "Expected reset() to have been called.";
940 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 mResetWasCalled = false;
942 }
943
Yi Kong9b14ac62018-07-17 13:48:38 -0700944 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700945 std::unique_lock<std::mutex> lock(mLock);
946 base::ScopedLockAssertion assumeLocked(mLock);
947 const bool processCalled =
948 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
949 return mProcessWasCalled;
950 });
951 if (!processCalled) {
952 FAIL() << "Expected process() to have been called.";
953 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954 if (outLastEvent) {
955 *outLastEvent = mLastEvent;
956 }
957 mProcessWasCalled = false;
958 }
959
960 void setKeyCodeState(int32_t keyCode, int32_t state) {
961 mKeyCodeStates.replaceValueFor(keyCode, state);
962 }
963
964 void setScanCodeState(int32_t scanCode, int32_t state) {
965 mScanCodeStates.replaceValueFor(scanCode, state);
966 }
967
968 void setSwitchState(int32_t switchCode, int32_t state) {
969 mSwitchStates.replaceValueFor(switchCode, state);
970 }
971
972 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800973 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975
976private:
Chris Yea52ade12020-08-27 16:49:20 -0700977 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978
Chris Yea52ade12020-08-27 16:49:20 -0700979 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 InputMapper::populateDeviceInfo(deviceInfo);
981
982 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
983 deviceInfo->setKeyboardType(mKeyboardType);
984 }
985 }
986
Chris Yea52ade12020-08-27 16:49:20 -0700987 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700988 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800990
991 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800992 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800993 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
994 mViewport = config->getDisplayViewportByPort(*displayPort);
995 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996
997 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 }
999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001001 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001003 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 }
1005
Chris Yea52ade12020-08-27 16:49:20 -07001006 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001007 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008 mLastEvent = *rawEvent;
1009 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001010 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 }
1012
Chris Yea52ade12020-08-27 16:49:20 -07001013 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1015 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1016 }
1017
Chris Yea52ade12020-08-27 16:49:20 -07001018 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1020 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1021 }
1022
Chris Yea52ade12020-08-27 16:49:20 -07001023 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1025 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1026 }
1027
Chris Yea52ade12020-08-27 16:49:20 -07001028 // Return true if the device has non-empty key layout.
1029 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1030 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 for (size_t i = 0; i < numCodes; i++) {
1032 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1033 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1034 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035 }
1036 }
1037 }
Chris Yea52ade12020-08-27 16:49:20 -07001038 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 return result;
1040 }
1041
1042 virtual int32_t getMetaState() {
1043 return mMetaState;
1044 }
1045
1046 virtual void fadePointer() {
1047 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001048
1049 virtual std::optional<int32_t> getAssociatedDisplay() {
1050 if (mViewport) {
1051 return std::make_optional(mViewport->displayId);
1052 }
1053 return std::nullopt;
1054 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055};
1056
1057
1058// --- InstrumentedInputReader ---
1059
1060class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001061 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062
1063public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001064 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1065 const sp<InputReaderPolicyInterface>& policy,
1066 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001067 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001069 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001073 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001074 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075 InputDeviceIdentifier identifier;
1076 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001077 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001079 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 }
1081
Prabir Pradhan28efc192019-11-05 01:10:04 +00001082 // Make the protected loopOnce method accessible to tests.
1083 using InputReader::loopOnce;
1084
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001086 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1087 const InputDeviceIdentifier& identifier)
1088 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001089 if (!mNextDevices.empty()) {
1090 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1091 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 return device;
1093 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001094 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 }
1096
arthurhungdcef2dc2020-08-11 14:47:50 +08001097 // --- FakeInputReaderContext ---
1098 class FakeInputReaderContext : public ContextImpl {
1099 int32_t mGlobalMetaState;
1100 bool mUpdateGlobalMetaStateWasCalled;
1101 int32_t mGeneration;
1102
1103 public:
1104 FakeInputReaderContext(InputReader* reader)
1105 : ContextImpl(reader),
1106 mGlobalMetaState(0),
1107 mUpdateGlobalMetaStateWasCalled(false),
1108 mGeneration(1) {}
1109
1110 virtual ~FakeInputReaderContext() {}
1111
1112 void assertUpdateGlobalMetaStateWasCalled() {
1113 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1114 << "Expected updateGlobalMetaState() to have been called.";
1115 mUpdateGlobalMetaStateWasCalled = false;
1116 }
1117
1118 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1119
1120 uint32_t getGeneration() { return mGeneration; }
1121
1122 void updateGlobalMetaState() override {
1123 mUpdateGlobalMetaStateWasCalled = true;
1124 ContextImpl::updateGlobalMetaState();
1125 }
1126
1127 int32_t getGlobalMetaState() override {
1128 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1129 }
1130
1131 int32_t bumpGeneration() override {
1132 mGeneration = ContextImpl::bumpGeneration();
1133 return mGeneration;
1134 }
1135 } mFakeContext;
1136
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001138
1139public:
1140 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141};
1142
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001143// --- InputReaderPolicyTest ---
1144class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001145protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001146 sp<FakeInputReaderPolicy> mFakePolicy;
1147
Chris Yea52ade12020-08-27 16:49:20 -07001148 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1149 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150};
1151
1152/**
1153 * Check that empty set of viewports is an acceptable configuration.
1154 * Also try to get internal viewport two different ways - by type and by uniqueId.
1155 *
1156 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1157 * Such configuration is not currently allowed.
1158 */
1159TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001160 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001161
1162 // We didn't add any viewports yet, so there shouldn't be any.
1163 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001164 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001165 ASSERT_FALSE(internalViewport);
1166
1167 // Add an internal viewport, then clear it
1168 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001169 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001170 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171
1172 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001173 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001174 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001175 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001176
1177 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001178 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181
1182 mFakePolicy->clearViewports();
1183 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001186 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187 ASSERT_FALSE(internalViewport);
1188}
1189
1190TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1191 const std::string internalUniqueId = "local:0";
1192 const std::string externalUniqueId = "local:1";
1193 const std::string virtualUniqueId1 = "virtual:2";
1194 const std::string virtualUniqueId2 = "virtual:3";
1195 constexpr int32_t virtualDisplayId1 = 2;
1196 constexpr int32_t virtualDisplayId2 = 3;
1197
1198 // Add an internal viewport
1199 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001200 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1201 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202 // Add an external viewport
1203 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001204 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1205 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001206 // Add an virtual viewport
1207 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001208 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1209 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 // Add another virtual viewport
1211 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001212 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1213 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214
1215 // Check matching by type for internal
1216 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001217 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001218 ASSERT_TRUE(internalViewport);
1219 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1220
1221 // Check matching by type for external
1222 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001223 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001224 ASSERT_TRUE(externalViewport);
1225 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1226
1227 // Check matching by uniqueId for virtual viewport #1
1228 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001229 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001230 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001231 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001232 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1233 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1234
1235 // Check matching by uniqueId for virtual viewport #2
1236 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001237 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001239 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1241 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1242}
1243
1244
1245/**
1246 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1247 * that lookup works by checking display id.
1248 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1249 */
1250TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1251 const std::string uniqueId1 = "uniqueId1";
1252 const std::string uniqueId2 = "uniqueId2";
1253 constexpr int32_t displayId1 = 2;
1254 constexpr int32_t displayId2 = 3;
1255
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001256 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1257 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001258 for (const ViewportType& type : types) {
1259 mFakePolicy->clearViewports();
1260 // Add a viewport
1261 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001262 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1263 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264 // Add another viewport
1265 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001266 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1267 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268
1269 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001270 std::optional<DisplayViewport> viewport1 =
1271 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272 ASSERT_TRUE(viewport1);
1273 ASSERT_EQ(displayId1, viewport1->displayId);
1274 ASSERT_EQ(type, viewport1->type);
1275
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001276 std::optional<DisplayViewport> viewport2 =
1277 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001278 ASSERT_TRUE(viewport2);
1279 ASSERT_EQ(displayId2, viewport2->displayId);
1280 ASSERT_EQ(type, viewport2->type);
1281
1282 // When there are multiple viewports of the same kind, and uniqueId is not specified
1283 // in the call to getDisplayViewport, then that situation is not supported.
1284 // The viewports can be stored in any order, so we cannot rely on the order, since that
1285 // is just implementation detail.
1286 // However, we can check that it still returns *a* viewport, we just cannot assert
1287 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001288 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001289 ASSERT_TRUE(someViewport);
1290 }
1291}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001292
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001293/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001294 * When we have multiple internal displays make sure we always return the default display when
1295 * querying by type.
1296 */
1297TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1298 const std::string uniqueId1 = "uniqueId1";
1299 const std::string uniqueId2 = "uniqueId2";
1300 constexpr int32_t nonDefaultDisplayId = 2;
1301 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1302 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1303
1304 // Add the default display first and ensure it gets returned.
1305 mFakePolicy->clearViewports();
1306 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001307 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001308 ViewportType::INTERNAL);
1309 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001310 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001311 ViewportType::INTERNAL);
1312
1313 std::optional<DisplayViewport> viewport =
1314 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1315 ASSERT_TRUE(viewport);
1316 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1317 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1318
1319 // Add the default display second to make sure order doesn't matter.
1320 mFakePolicy->clearViewports();
1321 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001322 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001323 ViewportType::INTERNAL);
1324 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001325 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001326 ViewportType::INTERNAL);
1327
1328 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1329 ASSERT_TRUE(viewport);
1330 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1331 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1332}
1333
1334/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001335 * Check getDisplayViewportByPort
1336 */
1337TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001338 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001339 const std::string uniqueId1 = "uniqueId1";
1340 const std::string uniqueId2 = "uniqueId2";
1341 constexpr int32_t displayId1 = 1;
1342 constexpr int32_t displayId2 = 2;
1343 const uint8_t hdmi1 = 0;
1344 const uint8_t hdmi2 = 1;
1345 const uint8_t hdmi3 = 2;
1346
1347 mFakePolicy->clearViewports();
1348 // Add a viewport that's associated with some display port that's not of interest.
1349 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001350 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1351 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001352 // Add another viewport, connected to HDMI1 port
1353 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001354 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1355 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001356
1357 // Check that correct display viewport was returned by comparing the display ports.
1358 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1359 ASSERT_TRUE(hdmi1Viewport);
1360 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1361 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1362
1363 // Check that we can still get the same viewport using the uniqueId
1364 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1365 ASSERT_TRUE(hdmi1Viewport);
1366 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1367 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1368 ASSERT_EQ(type, hdmi1Viewport->type);
1369
1370 // Check that we cannot find a port with "HDMI2", because we never added one
1371 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1372 ASSERT_FALSE(hdmi2Viewport);
1373}
1374
Michael Wrightd02c5b62014-02-10 15:10:22 -08001375// --- InputReaderTest ---
1376
1377class InputReaderTest : public testing::Test {
1378protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001379 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001381 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001382 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383
Chris Yea52ade12020-08-27 16:49:20 -07001384 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001385 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001387 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001388
Prabir Pradhan28efc192019-11-05 01:10:04 +00001389 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1390 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391 }
1392
Chris Yea52ade12020-08-27 16:49:20 -07001393 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394 mFakeListener.clear();
1395 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001396 }
1397
Chris Ye1b0c7342020-07-28 21:57:03 -07001398 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 const PropertyMap* configuration) {
1400 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401
1402 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001403 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001404 }
1405 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001406 mReader->loopOnce();
1407 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1409 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410 }
1411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001412 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001413 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001414 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001415 }
1416
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001417 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001418 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001419 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001420 }
1421
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001422 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001423 const std::string& name,
1424 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001425 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001426 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1427 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001428 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001430 return mapper;
1431 }
1432};
1433
Chris Ye98d3f532020-10-01 21:48:59 -07001434TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001435 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1436 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1437 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001438
Chris Ye98d3f532020-10-01 21:48:59 -07001439 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001440 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001441 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001442 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1444 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1445 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001446}
1447
1448TEST_F(InputReaderTest, PolicyGetInputDevices) {
1449 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1450 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1451 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001452
1453 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001454 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001455 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001456 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001457 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1459 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1460 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1461}
1462
Chris Yee7310032020-09-22 15:36:28 -07001463TEST_F(InputReaderTest, GetMergedInputDevices) {
1464 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1465 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1466 // Add two subdevices to device
1467 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1468 // Must add at least one mapper or the device will be ignored!
1469 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1470 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1471
1472 // Push same device instance for next device to be added, so they'll have same identifier.
1473 mReader->pushNextDevice(device);
1474 mReader->pushNextDevice(device);
1475 ASSERT_NO_FATAL_FAILURE(
1476 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1477 ASSERT_NO_FATAL_FAILURE(
1478 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1479
1480 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001481 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001482}
1483
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001484TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001485 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001486 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001487 constexpr int32_t eventHubId = 1;
1488 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001489 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001490 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001491 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001492 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001493
Yi Kong9b14ac62018-07-17 13:48:38 -07001494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001495
1496 NotifyDeviceResetArgs resetArgs;
1497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001498 ASSERT_EQ(deviceId, resetArgs.deviceId);
1499
1500 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001501 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001502 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001503
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001505 ASSERT_EQ(deviceId, resetArgs.deviceId);
1506 ASSERT_EQ(device->isEnabled(), false);
1507
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001508 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001509 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001512 ASSERT_EQ(device->isEnabled(), false);
1513
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001514 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001515 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001517 ASSERT_EQ(deviceId, resetArgs.deviceId);
1518 ASSERT_EQ(device->isEnabled(), true);
1519}
1520
Michael Wrightd02c5b62014-02-10 15:10:22 -08001521TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001522 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001523 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001525 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001526 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001527 AINPUT_SOURCE_KEYBOARD, nullptr);
1528 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529
1530 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1531 AINPUT_SOURCE_ANY, AKEYCODE_A))
1532 << "Should return unknown when the device id is >= 0 but unknown.";
1533
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001534 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1535 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1536 << "Should return unknown when the device id is valid but the sources are not "
1537 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001538
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001539 ASSERT_EQ(AKEY_STATE_DOWN,
1540 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1541 AKEYCODE_A))
1542 << "Should return value provided by mapper when device id is valid and the device "
1543 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544
1545 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1546 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1547 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1548
1549 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1550 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1551 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1552}
1553
1554TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001555 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001556 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001558 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001559 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001560 AINPUT_SOURCE_KEYBOARD, nullptr);
1561 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562
1563 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1564 AINPUT_SOURCE_ANY, KEY_A))
1565 << "Should return unknown when the device id is >= 0 but unknown.";
1566
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001567 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1568 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1569 << "Should return unknown when the device id is valid but the sources are not "
1570 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001572 ASSERT_EQ(AKEY_STATE_DOWN,
1573 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1574 KEY_A))
1575 << "Should return value provided by mapper when device id is valid and the device "
1576 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577
1578 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1579 AINPUT_SOURCE_TRACKBALL, KEY_A))
1580 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1581
1582 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1583 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1584 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1585}
1586
1587TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001588 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001589 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001591 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001592 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001593 AINPUT_SOURCE_KEYBOARD, nullptr);
1594 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595
1596 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1597 AINPUT_SOURCE_ANY, SW_LID))
1598 << "Should return unknown when the device id is >= 0 but unknown.";
1599
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001600 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1601 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1602 << "Should return unknown when the device id is valid but the sources are not "
1603 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 ASSERT_EQ(AKEY_STATE_DOWN,
1606 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1607 SW_LID))
1608 << "Should return value provided by mapper when device id is valid and the device "
1609 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001610
1611 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1612 AINPUT_SOURCE_TRACKBALL, SW_LID))
1613 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1614
1615 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1616 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1617 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1618}
1619
1620TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001621 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001622 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001623 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001624 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001626 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001628 mapper.addSupportedKeyCode(AKEYCODE_A);
1629 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630
1631 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1632 uint8_t flags[4] = { 0, 0, 0, 1 };
1633
1634 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1635 << "Should return false when device id is >= 0 but unknown.";
1636 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1637
1638 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1640 << "Should return false when device id is valid but the sources are not supported by "
1641 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1643
1644 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1646 keyCodes, flags))
1647 << "Should return value provided by mapper when device id is valid and the device "
1648 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001649 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1650
1651 flags[3] = 1;
1652 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1653 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1654 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1655
1656 flags[3] = 1;
1657 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1658 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1659 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1660}
1661
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001662TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001664 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665
1666 NotifyConfigurationChangedArgs args;
1667
1668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1669 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1670}
1671
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001672TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001673 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001674 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001675 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001676 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001677 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001678 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001680 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001681 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1683
1684 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001685 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001686 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001688 ASSERT_EQ(EV_KEY, event.type);
1689 ASSERT_EQ(KEY_A, event.code);
1690 ASSERT_EQ(1, event.value);
1691}
1692
Garfield Tan1c7bc862020-01-28 13:24:04 -08001693TEST_F(InputReaderTest, DeviceReset_RandomId) {
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;
1697 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001698 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001700 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001702
1703 NotifyDeviceResetArgs resetArgs;
1704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001705 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001707 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001708 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001710 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001711 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001713 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001714 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001716 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001717 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001718
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001719 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001720 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001722 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001723 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001724}
1725
Garfield Tan1c7bc862020-01-28 13:24:04 -08001726TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1727 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001728 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001729 constexpr int32_t eventHubId = 1;
1730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1731 // Must add at least one mapper or the device will be ignored!
1732 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001733 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001734 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1735
1736 NotifyDeviceResetArgs resetArgs;
1737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1738 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1739}
1740
Arthur Hungc23540e2018-11-29 20:42:11 +08001741TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001743 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001745 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001746 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1747 FakeInputMapper& mapper =
1748 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001749 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001750
1751 const uint8_t hdmi1 = 1;
1752
1753 // Associated touch screen with second display.
1754 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1755
1756 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001757 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001758 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001759 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001760 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001761 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001762 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001763 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001764 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001765 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001766
1767 // Add the device, and make sure all of the callbacks are triggered.
1768 // The device is added after the input port associations are processed since
1769 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001770 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001773 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001774
Arthur Hung2c9a3342019-07-23 14:18:59 +08001775 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001776 ASSERT_EQ(deviceId, device->getId());
1777 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1778 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001779
1780 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001781 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001782 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001783 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001784}
1785
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001786TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1787 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1788 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1789 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1790 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1791 // Must add at least one mapper or the device will be ignored!
1792 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1793 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1794 mReader->pushNextDevice(device);
1795 mReader->pushNextDevice(device);
1796 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1797 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1798
1799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1800
1801 NotifyDeviceResetArgs resetArgs;
1802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1803 ASSERT_EQ(deviceId, resetArgs.deviceId);
1804 ASSERT_TRUE(device->isEnabled());
1805 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1806 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1807
1808 disableDevice(deviceId);
1809 mReader->loopOnce();
1810
1811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1812 ASSERT_EQ(deviceId, resetArgs.deviceId);
1813 ASSERT_FALSE(device->isEnabled());
1814 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1815 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1816
1817 enableDevice(deviceId);
1818 mReader->loopOnce();
1819
1820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1821 ASSERT_EQ(deviceId, resetArgs.deviceId);
1822 ASSERT_TRUE(device->isEnabled());
1823 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1824 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1825}
1826
1827TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1828 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1829 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1830 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1831 // Add two subdevices to device
1832 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1833 FakeInputMapper& mapperDevice1 =
1834 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1835 FakeInputMapper& mapperDevice2 =
1836 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1837 mReader->pushNextDevice(device);
1838 mReader->pushNextDevice(device);
1839 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1840 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1841
1842 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1843 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1844
1845 ASSERT_EQ(AKEY_STATE_DOWN,
1846 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1847 ASSERT_EQ(AKEY_STATE_DOWN,
1848 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1849 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1850 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1851}
1852
Prabir Pradhan7e186182020-11-10 13:56:45 -08001853TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1854 NotifyPointerCaptureChangedArgs args;
1855
1856 mFakePolicy->setPointerCapture(true);
1857 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1858 mReader->loopOnce();
1859 mFakeListener->assertNotifyCaptureWasCalled(&args);
1860 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1861
1862 mFakePolicy->setPointerCapture(false);
1863 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1864 mReader->loopOnce();
1865 mFakeListener->assertNotifyCaptureWasCalled(&args);
1866 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1867
1868 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1869 // does not change.
1870 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1871 mReader->loopOnce();
1872 mFakeListener->assertNotifyCaptureWasCalled(&args);
1873 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1874}
1875
Chris Ye87143712020-11-10 05:05:58 +00001876class FakeVibratorInputMapper : public FakeInputMapper {
1877public:
1878 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1879 : FakeInputMapper(deviceContext, sources) {}
1880
1881 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1882};
1883
1884TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1885 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1886 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1887 constexpr int32_t eventHubId = 1;
1888 const char* DEVICE_LOCATION = "BLUETOOTH";
1889 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1890 FakeVibratorInputMapper& mapper =
1891 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1892 mReader->pushNextDevice(device);
1893
1894 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1895 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1896
1897 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1898 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1899}
1900
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001901// --- InputReaderIntegrationTest ---
1902
1903// These tests create and interact with the InputReader only through its interface.
1904// The InputReader is started during SetUp(), which starts its processing in its own
1905// thread. The tests use linux uinput to emulate input devices.
1906// NOTE: Interacting with the physical device while these tests are running may cause
1907// the tests to fail.
1908class InputReaderIntegrationTest : public testing::Test {
1909protected:
1910 sp<TestInputListener> mTestListener;
1911 sp<FakeInputReaderPolicy> mFakePolicy;
1912 sp<InputReaderInterface> mReader;
1913
Chris Yea52ade12020-08-27 16:49:20 -07001914 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001915 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001916 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1917 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001918
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001919 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001920 ASSERT_EQ(mReader->start(), OK);
1921
1922 // Since this test is run on a real device, all the input devices connected
1923 // to the test device will show up in mReader. We wait for those input devices to
1924 // show up before beginning the tests.
1925 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1926 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1927 }
1928
Chris Yea52ade12020-08-27 16:49:20 -07001929 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001930 ASSERT_EQ(mReader->stop(), OK);
1931 mTestListener.clear();
1932 mFakePolicy.clear();
1933 }
1934};
1935
1936TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1937 // An invalid input device that is only used for this test.
1938 class InvalidUinputDevice : public UinputDevice {
1939 public:
1940 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1941
1942 private:
1943 void configureDevice(int fd, uinput_user_dev* device) override {}
1944 };
1945
1946 const size_t numDevices = mFakePolicy->getInputDevices().size();
1947
1948 // UinputDevice does not set any event or key bits, so InputReader should not
1949 // consider it as a valid device.
1950 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1951 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1952 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1953 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1954
1955 invalidDevice.reset();
1956 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1957 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1958 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1959}
1960
1961TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1962 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1963
1964 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1965 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1966 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1967 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1968
1969 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07001970 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
1971 const auto& it =
1972 std::find_if(inputDevices.begin(), inputDevices.end(),
1973 [&keyboard](const InputDeviceInfo& info) {
1974 return info.getIdentifier().name == keyboard->getName();
1975 });
1976
1977 ASSERT_NE(it, inputDevices.end());
1978 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
1979 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
1980 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001981
1982 keyboard.reset();
1983 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1984 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1985 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1986}
1987
1988TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1989 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1990 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1991
1992 NotifyConfigurationChangedArgs configChangedArgs;
1993 ASSERT_NO_FATAL_FAILURE(
1994 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001995 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001996 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1997
1998 NotifyKeyArgs keyArgs;
1999 keyboard->pressAndReleaseHomeKey();
2000 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2001 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002002 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002003 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002004 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2005 prevTimestamp = keyArgs.eventTime;
2006
2007 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2008 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002009 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002010 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2011}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002012
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002013/**
2014 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2015 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2016 * are passed to the listener.
2017 */
2018static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2019TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2020 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2021 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2022 NotifyKeyArgs keyArgs;
2023
2024 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2025 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2026 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2027 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2028
2029 controller->pressAndReleaseKey(BTN_GEAR_UP);
2030 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2031 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2032 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2033}
2034
Arthur Hungaab25622020-01-16 11:22:11 +08002035// --- TouchProcessTest ---
2036class TouchIntegrationTest : public InputReaderIntegrationTest {
2037protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002038 const std::string UNIQUE_ID = "local:0";
2039
Chris Yea52ade12020-08-27 16:49:20 -07002040 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002041 InputReaderIntegrationTest::SetUp();
2042 // At least add an internal display.
2043 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2044 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002045 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002046
2047 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2048 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2049 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2050 }
2051
2052 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2053 int32_t orientation, const std::string& uniqueId,
2054 std::optional<uint8_t> physicalPort,
2055 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002056 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2057 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002058 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2059 }
2060
2061 std::unique_ptr<UinputTouchScreen> mDevice;
2062};
2063
2064TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2065 NotifyMotionArgs args;
2066 const Point centerPoint = mDevice->getCenterPoint();
2067
2068 // ACTION_DOWN
2069 mDevice->sendDown(centerPoint);
2070 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2071 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2072
2073 // ACTION_MOVE
2074 mDevice->sendMove(centerPoint + Point(1, 1));
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2077
2078 // ACTION_UP
2079 mDevice->sendUp();
2080 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2081 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2082}
2083
2084TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2085 NotifyMotionArgs args;
2086 const Point centerPoint = mDevice->getCenterPoint();
2087
2088 // ACTION_DOWN
2089 mDevice->sendDown(centerPoint);
2090 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2091 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2092
2093 // ACTION_POINTER_DOWN (Second slot)
2094 const Point secondPoint = centerPoint + Point(100, 100);
2095 mDevice->sendSlot(SECOND_SLOT);
2096 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2097 mDevice->sendDown(secondPoint + Point(1, 1));
2098 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2099 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2100 args.action);
2101
2102 // ACTION_MOVE (Second slot)
2103 mDevice->sendMove(secondPoint);
2104 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2106
2107 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002108 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002109 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002110 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002111 args.action);
2112
2113 // ACTION_UP
2114 mDevice->sendSlot(FIRST_SLOT);
2115 mDevice->sendUp();
2116 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2117 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2118}
2119
2120TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2121 NotifyMotionArgs args;
2122 const Point centerPoint = mDevice->getCenterPoint();
2123
2124 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002125 mDevice->sendSlot(FIRST_SLOT);
2126 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002127 mDevice->sendDown(centerPoint);
2128 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2129 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2130
arthurhungcc7f9802020-04-30 17:55:40 +08002131 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002132 const Point secondPoint = centerPoint + Point(100, 100);
2133 mDevice->sendSlot(SECOND_SLOT);
2134 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2135 mDevice->sendDown(secondPoint);
2136 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2137 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2138 args.action);
2139
arthurhungcc7f9802020-04-30 17:55:40 +08002140 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002141 mDevice->sendMove(secondPoint + Point(1, 1));
2142 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2144
arthurhungcc7f9802020-04-30 17:55:40 +08002145 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2146 // a palm event.
2147 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002148 mDevice->sendToolType(MT_TOOL_PALM);
2149 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002150 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2151 args.action);
2152 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002153
arthurhungcc7f9802020-04-30 17:55:40 +08002154 // Send up to second slot, expect first slot send moving.
2155 mDevice->sendPointerUp();
2156 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002158
arthurhungcc7f9802020-04-30 17:55:40 +08002159 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002160 mDevice->sendSlot(FIRST_SLOT);
2161 mDevice->sendUp();
2162
arthurhungcc7f9802020-04-30 17:55:40 +08002163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2164 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002165}
2166
Michael Wrightd02c5b62014-02-10 15:10:22 -08002167// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002168class InputDeviceTest : public testing::Test {
2169protected:
2170 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002171 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002172 static const int32_t DEVICE_ID;
2173 static const int32_t DEVICE_GENERATION;
2174 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002175 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002176 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002177
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002178 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002179 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002180 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002181 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002182 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002183
Chris Yea52ade12020-08-27 16:49:20 -07002184 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002185 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002186 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002187 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002188 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2189 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002190 InputDeviceIdentifier identifier;
2191 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002192 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002193 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002194 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002195 mReader->pushNextDevice(mDevice);
2196 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2197 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002198 }
2199
Chris Yea52ade12020-08-27 16:49:20 -07002200 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002201 mFakeListener.clear();
2202 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002203 }
2204};
2205
2206const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002207const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002208const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002209const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2210const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002211const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2212 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002213const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002214
2215TEST_F(InputDeviceTest, ImmutableProperties) {
2216 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002217 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002218 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219}
2220
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002221TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2222 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002223}
2224
Michael Wrightd02c5b62014-02-10 15:10:22 -08002225TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2226 // Configuration.
2227 InputReaderConfiguration config;
2228 mDevice->configure(ARBITRARY_TIME, &config, 0);
2229
2230 // Reset.
2231 mDevice->reset(ARBITRARY_TIME);
2232
2233 NotifyDeviceResetArgs resetArgs;
2234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2235 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2236 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2237
2238 // Metadata.
2239 ASSERT_TRUE(mDevice->isIgnored());
2240 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2241
2242 InputDeviceInfo info;
2243 mDevice->getDeviceInfo(&info);
2244 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002245 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2247 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2248
2249 // State queries.
2250 ASSERT_EQ(0, mDevice->getMetaState());
2251
2252 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2253 << "Ignored device should return unknown key code state.";
2254 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2255 << "Ignored device should return unknown scan code state.";
2256 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2257 << "Ignored device should return unknown switch state.";
2258
2259 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2260 uint8_t flags[2] = { 0, 1 };
2261 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2262 << "Ignored device should never mark any key codes.";
2263 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2264 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2265}
2266
2267TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2268 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002269 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002271 FakeInputMapper& mapper1 =
2272 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002273 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2274 mapper1.setMetaState(AMETA_ALT_ON);
2275 mapper1.addSupportedKeyCode(AKEYCODE_A);
2276 mapper1.addSupportedKeyCode(AKEYCODE_B);
2277 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2278 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2279 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2280 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2281 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002283 FakeInputMapper& mapper2 =
2284 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002285 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286
2287 InputReaderConfiguration config;
2288 mDevice->configure(ARBITRARY_TIME, &config, 0);
2289
2290 String8 propertyValue;
2291 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2292 << "Device should have read configuration during configuration phase.";
2293 ASSERT_STREQ("value", propertyValue.string());
2294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002295 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2296 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297
2298 // Reset
2299 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002300 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2301 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002302
2303 NotifyDeviceResetArgs resetArgs;
2304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2305 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2306 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2307
2308 // Metadata.
2309 ASSERT_FALSE(mDevice->isIgnored());
2310 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2311
2312 InputDeviceInfo info;
2313 mDevice->getDeviceInfo(&info);
2314 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002315 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002316 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2317 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2318
2319 // State queries.
2320 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2321 << "Should query mappers and combine meta states.";
2322
2323 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2324 << "Should return unknown key code state when source not supported.";
2325 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2326 << "Should return unknown scan code state when source not supported.";
2327 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2328 << "Should return unknown switch state when source not supported.";
2329
2330 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2331 << "Should query mapper when source is supported.";
2332 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2333 << "Should query mapper when source is supported.";
2334 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2335 << "Should query mapper when source is supported.";
2336
2337 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2338 uint8_t flags[4] = { 0, 0, 0, 1 };
2339 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2340 << "Should do nothing when source is unsupported.";
2341 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2342 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2343 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2344 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2345
2346 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2347 << "Should query mapper when source is supported.";
2348 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2349 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2350 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2351 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2352
2353 // Event handling.
2354 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002355 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002356 mDevice->process(&event, 1);
2357
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002358 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2359 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360}
2361
Arthur Hung2c9a3342019-07-23 14:18:59 +08002362// A single input device is associated with a specific display. Check that:
2363// 1. Device is disabled if the viewport corresponding to the associated display is not found
2364// 2. Device is disabled when setEnabled API is called
2365TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002366 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002367
2368 // First Configuration.
2369 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2370
2371 // Device should be enabled by default.
2372 ASSERT_TRUE(mDevice->isEnabled());
2373
2374 // Prepare associated info.
2375 constexpr uint8_t hdmi = 1;
2376 const std::string UNIQUE_ID = "local:1";
2377
2378 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2379 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2380 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2381 // Device should be disabled because it is associated with a specific display via
2382 // input port <-> display port association, but the corresponding display is not found
2383 ASSERT_FALSE(mDevice->isEnabled());
2384
2385 // Prepare displays.
2386 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002387 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2388 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002389 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2390 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2391 ASSERT_TRUE(mDevice->isEnabled());
2392
2393 // Device should be disabled after set disable.
2394 mFakePolicy->addDisabledDevice(mDevice->getId());
2395 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2396 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2397 ASSERT_FALSE(mDevice->isEnabled());
2398
2399 // Device should still be disabled even found the associated display.
2400 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2401 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2402 ASSERT_FALSE(mDevice->isEnabled());
2403}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404
2405// --- InputMapperTest ---
2406
2407class InputMapperTest : public testing::Test {
2408protected:
2409 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002410 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411 static const int32_t DEVICE_ID;
2412 static const int32_t DEVICE_GENERATION;
2413 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002414 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002415 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002417 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002419 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002420 std::unique_ptr<InstrumentedInputReader> mReader;
2421 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422
Chris Ye1b0c7342020-07-28 21:57:03 -07002423 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002424 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002426 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002427 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2428 mFakeListener);
2429 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 }
2431
Chris Yea52ade12020-08-27 16:49:20 -07002432 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002433
Chris Yea52ade12020-08-27 16:49:20 -07002434 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435 mFakeListener.clear();
2436 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 }
2438
2439 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002440 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441 }
2442
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002443 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002444 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002445 mReader->requestRefreshConfiguration(changes);
2446 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002447 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002448 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2449 }
2450
arthurhungdcef2dc2020-08-11 14:47:50 +08002451 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2452 const std::string& location, int32_t eventHubId,
2453 Flags<InputDeviceClass> classes) {
2454 InputDeviceIdentifier identifier;
2455 identifier.name = name;
2456 identifier.location = location;
2457 std::shared_ptr<InputDevice> device =
2458 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2459 identifier);
2460 mReader->pushNextDevice(device);
2461 mFakeEventHub->addDevice(eventHubId, name, classes);
2462 mReader->loopOnce();
2463 return device;
2464 }
2465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002466 template <class T, typename... Args>
2467 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002468 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002469 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002471 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002472 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473 }
2474
2475 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002476 int32_t orientation, const std::string& uniqueId,
2477 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002478 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2479 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002480 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2481 }
2482
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002483 void clearViewports() {
2484 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485 }
2486
arthurhungdcef2dc2020-08-11 14:47:50 +08002487 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002488 RawEvent event;
2489 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002490 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491 event.type = type;
2492 event.code = code;
2493 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002494 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002495 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496 }
2497
2498 static void assertMotionRange(const InputDeviceInfo& info,
2499 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2500 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002501 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2503 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2504 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2505 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2506 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2507 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2508 }
2509
2510 static void assertPointerCoords(const PointerCoords& coords,
2511 float x, float y, float pressure, float size,
2512 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2513 float orientation, float distance) {
2514 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2515 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2516 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2517 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2518 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2519 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2520 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2521 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2522 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2523 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2524 }
2525
Michael Wright17db18e2020-06-26 20:51:44 +01002526 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002528 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529 ASSERT_NEAR(x, actualX, 1);
2530 ASSERT_NEAR(y, actualY, 1);
2531 }
2532};
2533
2534const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002535const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002536const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2538const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002539const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2540 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002541const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002542
2543// --- SwitchInputMapperTest ---
2544
2545class SwitchInputMapperTest : public InputMapperTest {
2546protected:
2547};
2548
2549TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002550 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002552 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553}
2554
2555TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002556 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002557
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002558 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002559 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002561 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002562 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563}
2564
2565TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002566 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002568 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2569 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2570 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2571 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572
2573 NotifySwitchArgs args;
2574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2575 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002576 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2577 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002578 args.switchMask);
2579 ASSERT_EQ(uint32_t(0), args.policyFlags);
2580}
2581
Chris Ye87143712020-11-10 05:05:58 +00002582// --- VibratorInputMapperTest ---
2583class VibratorInputMapperTest : public InputMapperTest {
2584protected:
2585 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2586};
2587
2588TEST_F(VibratorInputMapperTest, GetSources) {
2589 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2590
2591 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2592}
2593
2594TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2595 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2596
2597 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2598}
2599
2600TEST_F(VibratorInputMapperTest, Vibrate) {
2601 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
2602 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2603
2604 VibrationElement pattern(2);
2605 VibrationSequence sequence(2);
2606 pattern.duration = std::chrono::milliseconds(200);
2607 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2608 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2609 sequence.addElement(pattern);
2610 pattern.duration = std::chrono::milliseconds(500);
2611 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2612 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2613 sequence.addElement(pattern);
2614
2615 std::vector<int64_t> timings = {0, 1};
2616 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2617
2618 ASSERT_FALSE(mapper.isVibrating());
2619 mapper.vibrate(sequence, -1 /* repeat */, 0 /* token */);
2620 ASSERT_TRUE(mapper.isVibrating());
2621}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622
Chris Yef59a2f42020-10-16 12:55:26 -07002623// --- SensorInputMapperTest ---
2624
2625class SensorInputMapperTest : public InputMapperTest {
2626protected:
2627 static const int32_t ACCEL_RAW_MIN;
2628 static const int32_t ACCEL_RAW_MAX;
2629 static const int32_t ACCEL_RAW_FUZZ;
2630 static const int32_t ACCEL_RAW_FLAT;
2631 static const int32_t ACCEL_RAW_RESOLUTION;
2632
2633 static const int32_t GYRO_RAW_MIN;
2634 static const int32_t GYRO_RAW_MAX;
2635 static const int32_t GYRO_RAW_FUZZ;
2636 static const int32_t GYRO_RAW_FLAT;
2637 static const int32_t GYRO_RAW_RESOLUTION;
2638
2639 static const float GRAVITY_MS2_UNIT;
2640 static const float DEGREE_RADIAN_UNIT;
2641
2642 void prepareAccelAxes();
2643 void prepareGyroAxes();
2644 void setAccelProperties();
2645 void setGyroProperties();
2646 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2647};
2648
2649const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2650const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2651const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2652const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2653const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2654
2655const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2656const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2657const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2658const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2659const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2660
2661const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2662const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2663
2664void SensorInputMapperTest::prepareAccelAxes() {
2665 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2666 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2667 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2668 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2669 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2670 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2671}
2672
2673void SensorInputMapperTest::prepareGyroAxes() {
2674 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2675 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2676 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2677 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2678 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2679 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2680}
2681
2682void SensorInputMapperTest::setAccelProperties() {
2683 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2684 /* sensorDataIndex */ 0);
2685 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2686 /* sensorDataIndex */ 1);
2687 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2688 /* sensorDataIndex */ 2);
2689 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2690 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2691 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2692 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2693 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2694}
2695
2696void SensorInputMapperTest::setGyroProperties() {
2697 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2698 /* sensorDataIndex */ 0);
2699 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2700 /* sensorDataIndex */ 1);
2701 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2702 /* sensorDataIndex */ 2);
2703 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2704 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2705 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2706 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2707 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2708}
2709
2710TEST_F(SensorInputMapperTest, GetSources) {
2711 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2712
2713 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2714}
2715
2716TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2717 setAccelProperties();
2718 prepareAccelAxes();
2719 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2720
2721 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2722 std::chrono::microseconds(10000),
2723 std::chrono::microseconds(0)));
2724 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, 20000);
2725 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, -20000);
2726 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Z, 40000);
2727 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2728 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2729
2730 NotifySensorArgs args;
2731 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2732 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2733 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2734
2735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2736 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2737 ASSERT_EQ(args.deviceId, DEVICE_ID);
2738 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2739 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2740 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2741 ASSERT_EQ(args.values, values);
2742 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2743}
2744
2745TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2746 setGyroProperties();
2747 prepareGyroAxes();
2748 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2749
2750 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2751 std::chrono::microseconds(10000),
2752 std::chrono::microseconds(0)));
2753 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RX, 20000);
2754 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RY, -20000);
2755 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RZ, 40000);
2756 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2757 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2758
2759 NotifySensorArgs args;
2760 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2761 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2762 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2763
2764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2765 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2766 ASSERT_EQ(args.deviceId, DEVICE_ID);
2767 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2768 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2769 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2770 ASSERT_EQ(args.values, values);
2771 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2772}
2773
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774// --- KeyboardInputMapperTest ---
2775
2776class KeyboardInputMapperTest : public InputMapperTest {
2777protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002778 const std::string UNIQUE_ID = "local:0";
2779
2780 void prepareDisplay(int32_t orientation);
2781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002783 int32_t originalKeyCode, int32_t rotatedKeyCode,
2784 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785};
2786
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002787/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2788 * orientation.
2789 */
2790void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002791 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2792 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002793}
2794
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002795void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002796 int32_t originalScanCode, int32_t originalKeyCode,
2797 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798 NotifyKeyArgs args;
2799
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002800 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2802 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2803 ASSERT_EQ(originalScanCode, args.scanCode);
2804 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002805 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002807 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2809 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2810 ASSERT_EQ(originalScanCode, args.scanCode);
2811 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002812 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813}
2814
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002816 KeyboardInputMapper& mapper =
2817 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2818 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002820 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821}
2822
2823TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2824 const int32_t USAGE_A = 0x070004;
2825 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002826 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2827 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002828 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2829 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2830 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002832 KeyboardInputMapper& mapper =
2833 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2834 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002835 // Initial metastate to AMETA_NONE.
2836 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2837 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838
2839 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002840 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841 NotifyKeyArgs args;
2842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2843 ASSERT_EQ(DEVICE_ID, args.deviceId);
2844 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2845 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2846 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2847 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2848 ASSERT_EQ(KEY_HOME, args.scanCode);
2849 ASSERT_EQ(AMETA_NONE, args.metaState);
2850 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2852 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2853
2854 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002855 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2857 ASSERT_EQ(DEVICE_ID, args.deviceId);
2858 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2859 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2860 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2861 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2862 ASSERT_EQ(KEY_HOME, args.scanCode);
2863 ASSERT_EQ(AMETA_NONE, args.metaState);
2864 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2865 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2866 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2867
2868 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002869 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2870 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2872 ASSERT_EQ(DEVICE_ID, args.deviceId);
2873 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2874 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2875 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2876 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2877 ASSERT_EQ(0, args.scanCode);
2878 ASSERT_EQ(AMETA_NONE, args.metaState);
2879 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2880 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2881 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2882
2883 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002884 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2885 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2887 ASSERT_EQ(DEVICE_ID, args.deviceId);
2888 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2889 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2890 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2891 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2892 ASSERT_EQ(0, args.scanCode);
2893 ASSERT_EQ(AMETA_NONE, args.metaState);
2894 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2895 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2896 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2897
2898 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002899 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2900 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2902 ASSERT_EQ(DEVICE_ID, args.deviceId);
2903 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2904 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2905 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2906 ASSERT_EQ(0, args.keyCode);
2907 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2908 ASSERT_EQ(AMETA_NONE, args.metaState);
2909 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2910 ASSERT_EQ(0U, args.policyFlags);
2911 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2912
2913 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002914 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2915 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2917 ASSERT_EQ(DEVICE_ID, args.deviceId);
2918 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2919 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2920 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2921 ASSERT_EQ(0, args.keyCode);
2922 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2923 ASSERT_EQ(AMETA_NONE, args.metaState);
2924 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2925 ASSERT_EQ(0U, args.policyFlags);
2926 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2927}
2928
2929TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002930 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2931 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002932 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2933 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2934 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002936 KeyboardInputMapper& mapper =
2937 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2938 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939
arthurhungc903df12020-08-11 15:08:42 +08002940 // Initial metastate to AMETA_NONE.
2941 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2942 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943
2944 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002945 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002946 NotifyKeyArgs args;
2947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2948 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002949 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002950 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951
2952 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002953 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2955 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002956 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957
2958 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002959 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2961 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002962 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963
2964 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002965 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2967 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002968 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002969 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002970}
2971
2972TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002973 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2974 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2975 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2976 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002978 KeyboardInputMapper& mapper =
2979 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2980 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002982 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002983 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2984 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2985 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2986 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2987 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2988 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2989 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2990 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2991}
2992
2993TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2995 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003000 KeyboardInputMapper& mapper =
3001 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3002 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003004 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003005 ASSERT_NO_FATAL_FAILURE(
3006 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3007 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3008 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3009 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3010 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3011 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3012 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003014 clearViewports();
3015 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003016 ASSERT_NO_FATAL_FAILURE(
3017 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3018 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3019 AKEYCODE_DPAD_UP, DISPLAY_ID));
3020 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3021 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3022 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3023 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003025 clearViewports();
3026 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003027 ASSERT_NO_FATAL_FAILURE(
3028 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3029 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3030 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3031 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3032 AKEYCODE_DPAD_UP, DISPLAY_ID));
3033 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3034 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003036 clearViewports();
3037 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003038 ASSERT_NO_FATAL_FAILURE(
3039 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3040 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3041 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3042 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3043 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3044 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3045 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046
3047 // Special case: if orientation changes while key is down, we still emit the same keycode
3048 // in the key up as we did in the key down.
3049 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003050 clearViewports();
3051 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003052 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3054 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3055 ASSERT_EQ(KEY_UP, args.scanCode);
3056 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3057
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003058 clearViewports();
3059 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003060 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3062 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3063 ASSERT_EQ(KEY_UP, args.scanCode);
3064 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3065}
3066
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003067TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3068 // If the keyboard is not orientation aware,
3069 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003070 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003072 KeyboardInputMapper& mapper =
3073 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3074 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003075 NotifyKeyArgs args;
3076
3077 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003078 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003080 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3082 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3083
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003084 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003085 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003087 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3089 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3090}
3091
3092TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3093 // If the keyboard is orientation aware,
3094 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003096
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003097 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003098 KeyboardInputMapper& mapper =
3099 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3100 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003101 NotifyKeyArgs args;
3102
3103 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3104 // ^--- already checked by the previous test
3105
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003106 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003107 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003108 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003110 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3112 ASSERT_EQ(DISPLAY_ID, args.displayId);
3113
3114 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003115 clearViewports();
3116 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003117 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003118 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003120 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3122 ASSERT_EQ(newDisplayId, args.displayId);
3123}
3124
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 KeyboardInputMapper& mapper =
3127 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3128 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003130 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003131 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003133 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003134 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135}
3136
3137TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003138 KeyboardInputMapper& mapper =
3139 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3140 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003142 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003143 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003145 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003146 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147}
3148
3149TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003150 KeyboardInputMapper& mapper =
3151 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3152 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003154 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155
3156 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3157 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003158 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_TRUE(flags[0]);
3160 ASSERT_FALSE(flags[1]);
3161}
3162
3163TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003164 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3165 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3166 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3167 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3168 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003171 KeyboardInputMapper& mapper =
3172 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3173 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003174 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003175 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3176 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177
3178 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003179 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3180 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3181 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182
3183 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003184 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3185 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003186 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3187 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3188 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003189 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190
3191 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003192 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3193 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003194 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3195 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3196 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003197 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198
3199 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003200 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3201 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003202 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3203 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3204 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003205 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206
3207 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003208 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3209 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003210 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3211 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3212 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003213 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214
3215 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003216 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3217 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003218 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3219 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3220 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003221 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222
3223 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003224 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3225 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003226 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3227 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3228 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003229 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230}
3231
Chris Yea52ade12020-08-27 16:49:20 -07003232TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3233 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3234 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3235 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3236 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3237
3238 KeyboardInputMapper& mapper =
3239 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3240 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3241
3242 // Initial metastate should be AMETA_NONE as no meta keys added.
3243 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3244 // Meta state should be AMETA_NONE after reset
3245 mapper.reset(ARBITRARY_TIME);
3246 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3247 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3248 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3249 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3250
3251 NotifyKeyArgs args;
3252 // Press button "A"
3253 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
3254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3255 ASSERT_EQ(AMETA_NONE, args.metaState);
3256 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3258 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3259
3260 // Button up.
3261 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
3262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3263 ASSERT_EQ(AMETA_NONE, args.metaState);
3264 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3265 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3266 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3267}
3268
Arthur Hung2c9a3342019-07-23 14:18:59 +08003269TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3270 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003271 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3272 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3273 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3274 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003275
3276 // keyboard 2.
3277 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003278 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003279 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003280 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003281 std::shared_ptr<InputDevice> device2 =
3282 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3283 Flags<InputDeviceClass>(0));
3284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003285 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3286 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3287 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3288 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 KeyboardInputMapper& mapper =
3291 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3292 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003294 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003295 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003297 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3298 device2->reset(ARBITRARY_TIME);
3299
3300 // Prepared displays and associated info.
3301 constexpr uint8_t hdmi1 = 0;
3302 constexpr uint8_t hdmi2 = 1;
3303 const std::string SECONDARY_UNIQUE_ID = "local:1";
3304
3305 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3306 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3307
3308 // No associated display viewport found, should disable the device.
3309 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3310 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3311 ASSERT_FALSE(device2->isEnabled());
3312
3313 // Prepare second display.
3314 constexpr int32_t newDisplayId = 2;
3315 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003316 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003317 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003318 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003319 // Default device will reconfigure above, need additional reconfiguration for another device.
3320 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3321 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3322
3323 // Device should be enabled after the associated display is found.
3324 ASSERT_TRUE(mDevice->isEnabled());
3325 ASSERT_TRUE(device2->isEnabled());
3326
3327 // Test pad key events
3328 ASSERT_NO_FATAL_FAILURE(
3329 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3331 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3332 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3333 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3335 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3336
3337 ASSERT_NO_FATAL_FAILURE(
3338 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3340 AKEYCODE_DPAD_RIGHT, newDisplayId));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3342 AKEYCODE_DPAD_DOWN, newDisplayId));
3343 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3344 AKEYCODE_DPAD_LEFT, newDisplayId));
3345}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
arthurhungc903df12020-08-11 15:08:42 +08003347TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3348 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3349 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3350 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3351 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3352 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3353 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3354
3355 KeyboardInputMapper& mapper =
3356 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3357 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3358 // Initial metastate to AMETA_NONE.
3359 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3360 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3361
3362 // Initialization should have turned all of the lights off.
3363 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3364 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3365 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3366
3367 // Toggle caps lock on.
3368 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3369 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3370 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3371 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3372
3373 // Toggle num lock on.
3374 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3375 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3376 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3377 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3378
3379 // Toggle scroll lock on.
3380 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3381 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3382 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3383 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3384
3385 mFakeEventHub->removeDevice(EVENTHUB_ID);
3386 mReader->loopOnce();
3387
3388 // keyboard 2 should default toggle keys.
3389 const std::string USB2 = "USB2";
3390 const std::string DEVICE_NAME2 = "KEYBOARD2";
3391 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3392 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3393 std::shared_ptr<InputDevice> device2 =
3394 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3395 Flags<InputDeviceClass>(0));
3396 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3397 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3398 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3399 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3400 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3401 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3402
arthurhung6fe95782020-10-05 22:41:16 +08003403 KeyboardInputMapper& mapper2 =
3404 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3405 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003406 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3407 device2->reset(ARBITRARY_TIME);
3408
3409 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3410 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3411 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003412 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3413 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003414}
3415
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003416// --- KeyboardInputMapperTest_ExternalDevice ---
3417
3418class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3419protected:
Chris Yea52ade12020-08-27 16:49:20 -07003420 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003421};
3422
3423TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003424 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3425 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003426
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3430 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003431
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003432 KeyboardInputMapper& mapper =
3433 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3434 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003435
3436 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3437 NotifyKeyArgs args;
3438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3439 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3440
3441 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3443 ASSERT_EQ(uint32_t(0), args.policyFlags);
3444
3445 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3447 ASSERT_EQ(uint32_t(0), args.policyFlags);
3448
3449 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3451 ASSERT_EQ(uint32_t(0), args.policyFlags);
3452
3453 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3455 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3456
3457 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3460}
3461
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003462TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003463 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003464
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003465 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3466 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3467 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003468
Powei Fengd041c5d2019-05-03 17:11:33 -07003469 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003470 KeyboardInputMapper& mapper =
3471 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3472 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003473
3474 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3475 NotifyKeyArgs args;
3476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3477 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3478
3479 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3481 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3482
3483 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3485 ASSERT_EQ(uint32_t(0), args.policyFlags);
3486
3487 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3489 ASSERT_EQ(uint32_t(0), args.policyFlags);
3490
3491 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3493 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3494
3495 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3497 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3498}
3499
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500// --- CursorInputMapperTest ---
3501
3502class CursorInputMapperTest : public InputMapperTest {
3503protected:
3504 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3505
Michael Wright17db18e2020-06-26 20:51:44 +01003506 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Chris Yea52ade12020-08-27 16:49:20 -07003508 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509 InputMapperTest::SetUp();
3510
Michael Wright17db18e2020-06-26 20:51:44 +01003511 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003512 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 }
3514
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003515 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3516 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003517
3518 void prepareDisplay(int32_t orientation) {
3519 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003520 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003521 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3522 orientation, uniqueId, NO_PORT, viewportType);
3523 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524};
3525
3526const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003528void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3529 int32_t originalY, int32_t rotatedX,
3530 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 NotifyMotionArgs args;
3532
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003533 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3534 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3535 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3539 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3540 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3541 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3542}
3543
3544TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549}
3550
3551TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003553 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556}
3557
3558TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
3562 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003563 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564
3565 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003566 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3567 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3569 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3570
3571 // When the bounds are set, then there should be a valid motion range.
3572 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3573
3574 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003575 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576
3577 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3578 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3579 1, 800 - 1, 0.0f, 0.0f));
3580 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3581 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3582 2, 480 - 1, 0.0f, 0.0f));
3583 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3584 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3585 0.0f, 1.0f, 0.0f, 0.0f));
3586}
3587
3588TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003590 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
3592 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003593 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594
3595 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3596 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3597 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3598 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3599 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3600 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3601 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3602 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3603 0.0f, 1.0f, 0.0f, 0.0f));
3604}
3605
3606TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003608 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609
arthurhungdcef2dc2020-08-11 14:47:50 +08003610 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611
3612 NotifyMotionArgs args;
3613
3614 // Button press.
3615 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003616 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3617 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3619 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3620 ASSERT_EQ(DEVICE_ID, args.deviceId);
3621 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3622 ASSERT_EQ(uint32_t(0), args.policyFlags);
3623 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3624 ASSERT_EQ(0, args.flags);
3625 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3626 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3627 ASSERT_EQ(0, args.edgeFlags);
3628 ASSERT_EQ(uint32_t(1), args.pointerCount);
3629 ASSERT_EQ(0, args.pointerProperties[0].id);
3630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3632 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3633 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3634 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3635 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3636
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3638 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3639 ASSERT_EQ(DEVICE_ID, args.deviceId);
3640 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3641 ASSERT_EQ(uint32_t(0), args.policyFlags);
3642 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3643 ASSERT_EQ(0, args.flags);
3644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3645 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3646 ASSERT_EQ(0, args.edgeFlags);
3647 ASSERT_EQ(uint32_t(1), args.pointerCount);
3648 ASSERT_EQ(0, args.pointerProperties[0].id);
3649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3651 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3652 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3653 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3654 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3655
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003657 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3658 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3660 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3661 ASSERT_EQ(DEVICE_ID, args.deviceId);
3662 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3663 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3665 ASSERT_EQ(0, args.flags);
3666 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3667 ASSERT_EQ(0, args.buttonState);
3668 ASSERT_EQ(0, args.edgeFlags);
3669 ASSERT_EQ(uint32_t(1), args.pointerCount);
3670 ASSERT_EQ(0, args.pointerProperties[0].id);
3671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3673 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3674 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3675 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3676 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3677
3678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3679 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3680 ASSERT_EQ(DEVICE_ID, args.deviceId);
3681 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3682 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3684 ASSERT_EQ(0, args.flags);
3685 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3686 ASSERT_EQ(0, args.buttonState);
3687 ASSERT_EQ(0, args.edgeFlags);
3688 ASSERT_EQ(uint32_t(1), args.pointerCount);
3689 ASSERT_EQ(0, args.pointerProperties[0].id);
3690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3692 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3693 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3694 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3695 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3696}
3697
3698TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003700 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701
3702 NotifyMotionArgs args;
3703
3704 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003705 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3706 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3710 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3711
3712 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003713 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3714 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3718 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3719}
3720
3721TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724
3725 NotifyMotionArgs args;
3726
3727 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003728 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3729 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3731 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3733 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3734
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3736 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3738 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3739
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003741 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3742 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3746 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3747
3748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3751 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3752}
3753
3754TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003756 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757
3758 NotifyMotionArgs args;
3759
3760 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003761 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3762 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3763 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3764 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3766 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3768 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3769 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3770
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3772 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3774 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3775 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3776
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003778 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3779 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3780 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3784 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3785 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3786
3787 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003788 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3789 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3793 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3794
3795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3798 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3799}
3800
3801TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003805 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3807 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3808 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3809 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3810 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3811 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3812 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3813 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3814}
3815
3816TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817 addConfigurationProperty("cursor.mode", "navigation");
3818 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003819 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003820
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003821 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3823 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3824 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3825 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3826 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3827 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3828 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3829 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3830
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003831 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3833 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3834 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3835 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3836 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3837 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3838 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3839 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3840
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003841 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3843 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3844 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3845 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3846 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3847 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3848 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3849 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3850
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003851 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3853 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3854 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3855 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3856 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3857 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3858 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3859 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3860}
3861
3862TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
3866 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3867 mFakePointerController->setPosition(100, 200);
3868 mFakePointerController->setButtonState(0);
3869
3870 NotifyMotionArgs motionArgs;
3871 NotifyKeyArgs keyArgs;
3872
3873 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003874 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3875 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3878 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3879 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3881 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3882
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3885 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3886 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3888 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3889
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003890 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3891 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894 ASSERT_EQ(0, motionArgs.buttonState);
3895 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3897 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3898
3899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003900 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901 ASSERT_EQ(0, motionArgs.buttonState);
3902 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3904 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3905
3906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003907 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003908 ASSERT_EQ(0, motionArgs.buttonState);
3909 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3911 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3912
3913 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003914 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3915 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3916 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3918 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3919 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3920 motionArgs.buttonState);
3921 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3922 mFakePointerController->getButtonState());
3923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3924 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3925
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3927 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3928 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3929 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3930 mFakePointerController->getButtonState());
3931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3932 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3933
3934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3936 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3937 motionArgs.buttonState);
3938 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3939 mFakePointerController->getButtonState());
3940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3941 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3942
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003943 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3944 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003946 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3948 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3950 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3951
3952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003954 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3955 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3957 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3958
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003959 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3960 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3963 ASSERT_EQ(0, motionArgs.buttonState);
3964 ASSERT_EQ(0, mFakePointerController->getButtonState());
3965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3966 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003967 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3968 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003969
3970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971 ASSERT_EQ(0, motionArgs.buttonState);
3972 ASSERT_EQ(0, mFakePointerController->getButtonState());
3973 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3975 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003976
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3978 ASSERT_EQ(0, motionArgs.buttonState);
3979 ASSERT_EQ(0, mFakePointerController->getButtonState());
3980 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3982 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3983
3984 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003985 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3986 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3988 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3989 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003990
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3994 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3996 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3997
3998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4000 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4001 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4003 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4004
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004005 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
4006 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009 ASSERT_EQ(0, motionArgs.buttonState);
4010 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4012 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4013
4014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004016 ASSERT_EQ(0, motionArgs.buttonState);
4017 ASSERT_EQ(0, mFakePointerController->getButtonState());
4018
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4020 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4022 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4023 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4024
4025 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004026 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
4027 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4029 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4030 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004031
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004033 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4035 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4037 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4038
4039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4041 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4042 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4044 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4045
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004046 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
4047 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050 ASSERT_EQ(0, motionArgs.buttonState);
4051 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4053 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004054
4055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4056 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4057 ASSERT_EQ(0, motionArgs.buttonState);
4058 ASSERT_EQ(0, mFakePointerController->getButtonState());
4059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4060 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4061
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4063 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4064 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4065
4066 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004067 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
4068 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4070 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4071 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004072
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004074 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4076 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4078 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4079
4080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4082 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4083 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4085 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4086
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004087 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
4088 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 ASSERT_EQ(0, motionArgs.buttonState);
4092 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4094 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004095
4096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4097 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4098 ASSERT_EQ(0, motionArgs.buttonState);
4099 ASSERT_EQ(0, mFakePointerController->getButtonState());
4100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4101 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4102
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4104 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4105 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4106
4107 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004108 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
4109 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4111 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4112 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4117 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4119 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4120
4121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4123 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4124 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4126 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4127
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004128 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
4129 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 ASSERT_EQ(0, motionArgs.buttonState);
4133 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4135 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004136
4137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4139 ASSERT_EQ(0, motionArgs.buttonState);
4140 ASSERT_EQ(0, mFakePointerController->getButtonState());
4141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4142 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4143
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4146 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4147}
4148
4149TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
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 args;
4158
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004159 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4160 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4161 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004163 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4164 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4166 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 +01004167 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004168}
4169
4170TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004171 addConfigurationProperty("cursor.mode", "pointer");
4172 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004173 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004174
4175 NotifyDeviceResetArgs resetArgs;
4176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4177 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4178 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4179
4180 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4181 mFakePointerController->setPosition(100, 200);
4182 mFakePointerController->setButtonState(0);
4183
4184 NotifyMotionArgs args;
4185
4186 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004187 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4188 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4189 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4191 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4194 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 +01004195 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004196
4197 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004198 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4199 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4201 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4204 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4206 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4209 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4210
4211 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004212 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
4213 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4215 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4218 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4220 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4221 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4223 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4224
4225 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004226 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
4227 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
4228 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4230 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4233 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 +01004234 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004235
4236 // Disable pointer capture and check that the device generation got bumped
4237 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004238 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004239 mFakePolicy->setPointerCapture(false);
4240 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004241 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004242
4243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4244 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4245 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4246
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004247 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4248 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4249 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4251 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4254 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 +01004255 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256}
4257
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004258TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004259 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004260
Garfield Tan888a6a42020-01-09 11:39:16 -08004261 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004262 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004263 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4264 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004265 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4266 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004267 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4268 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4269
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004270 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4271 mFakePointerController->setPosition(100, 200);
4272 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004273
4274 NotifyMotionArgs args;
4275 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4276 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4277 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4279 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4280 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4282 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 +01004283 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004284 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4285}
4286
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287// --- TouchInputMapperTest ---
4288
4289class TouchInputMapperTest : public InputMapperTest {
4290protected:
4291 static const int32_t RAW_X_MIN;
4292 static const int32_t RAW_X_MAX;
4293 static const int32_t RAW_Y_MIN;
4294 static const int32_t RAW_Y_MAX;
4295 static const int32_t RAW_TOUCH_MIN;
4296 static const int32_t RAW_TOUCH_MAX;
4297 static const int32_t RAW_TOOL_MIN;
4298 static const int32_t RAW_TOOL_MAX;
4299 static const int32_t RAW_PRESSURE_MIN;
4300 static const int32_t RAW_PRESSURE_MAX;
4301 static const int32_t RAW_ORIENTATION_MIN;
4302 static const int32_t RAW_ORIENTATION_MAX;
4303 static const int32_t RAW_DISTANCE_MIN;
4304 static const int32_t RAW_DISTANCE_MAX;
4305 static const int32_t RAW_TILT_MIN;
4306 static const int32_t RAW_TILT_MAX;
4307 static const int32_t RAW_ID_MIN;
4308 static const int32_t RAW_ID_MAX;
4309 static const int32_t RAW_SLOT_MIN;
4310 static const int32_t RAW_SLOT_MAX;
4311 static const float X_PRECISION;
4312 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004313 static const float X_PRECISION_VIRTUAL;
4314 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315
4316 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004317 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318
4319 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4320
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004321 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004322 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004323
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 enum Axes {
4325 POSITION = 1 << 0,
4326 TOUCH = 1 << 1,
4327 TOOL = 1 << 2,
4328 PRESSURE = 1 << 3,
4329 ORIENTATION = 1 << 4,
4330 MINOR = 1 << 5,
4331 ID = 1 << 6,
4332 DISTANCE = 1 << 7,
4333 TILT = 1 << 8,
4334 SLOT = 1 << 9,
4335 TOOL_TYPE = 1 << 10,
4336 };
4337
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004338 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4339 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004340 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004342 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 int32_t toRawX(float displayX);
4344 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004345 float toCookedX(float rawX, float rawY);
4346 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004348 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004350 float toDisplayY(int32_t rawY, int32_t displayHeight);
4351
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352};
4353
4354const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4355const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4356const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4357const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4358const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4359const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4360const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4361const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004362const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4363const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4365const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4366const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4367const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4368const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4369const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4370const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4371const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4372const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4373const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4374const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4375const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004376const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4377 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4378const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4379 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004380const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4381 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383const float TouchInputMapperTest::GEOMETRIC_SCALE =
4384 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4385 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4386
4387const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4388 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4389 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4390};
4391
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004392void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004393 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4394 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004395}
4396
4397void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4398 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4399 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400}
4401
Santos Cordonfa5cf462017-04-05 10:37:00 -07004402void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004403 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4404 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4405 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004406}
4407
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004409 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4410 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4411 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4412 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413}
4414
Jason Gerecke489fda82012-09-07 17:19:40 -07004415void TouchInputMapperTest::prepareLocationCalibration() {
4416 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4417}
4418
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419int32_t TouchInputMapperTest::toRawX(float displayX) {
4420 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4421}
4422
4423int32_t TouchInputMapperTest::toRawY(float displayY) {
4424 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4425}
4426
Jason Gerecke489fda82012-09-07 17:19:40 -07004427float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4428 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4429 return rawX;
4430}
4431
4432float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4433 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4434 return rawY;
4435}
4436
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004438 return toDisplayX(rawX, DISPLAY_WIDTH);
4439}
4440
4441float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4442 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443}
4444
4445float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004446 return toDisplayY(rawY, DISPLAY_HEIGHT);
4447}
4448
4449float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4450 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451}
4452
4453
4454// --- SingleTouchInputMapperTest ---
4455
4456class SingleTouchInputMapperTest : public TouchInputMapperTest {
4457protected:
4458 void prepareButtons();
4459 void prepareAxes(int axes);
4460
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004461 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4462 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4463 void processUp(SingleTouchInputMapper& mappery);
4464 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4465 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4466 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4467 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4468 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4469 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470};
4471
4472void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004473 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474}
4475
4476void SingleTouchInputMapperTest::prepareAxes(int axes) {
4477 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004478 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4479 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 }
4481 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4483 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 }
4485 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4487 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 }
4489 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004490 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4491 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 }
4493 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004494 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4495 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496 }
4497}
4498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004499void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004500 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4501 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4502 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503}
4504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004505void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004506 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4507 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508}
4509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004510void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004511 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512}
4513
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004514void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004515 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516}
4517
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004518void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4519 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004520 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521}
4522
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004523void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004524 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525}
4526
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004527void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4528 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004529 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4530 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531}
4532
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004533void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4534 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004535 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536}
4537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004538void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004539 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540}
4541
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543 prepareButtons();
4544 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004545 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004547 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548}
4549
4550TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004551 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4552 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 prepareButtons();
4554 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004555 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004557 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558}
4559
4560TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561 prepareButtons();
4562 prepareAxes(POSITION);
4563 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004564 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004566 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567}
4568
4569TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 prepareButtons();
4571 prepareAxes(POSITION);
4572 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004573 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004575 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576}
4577
4578TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 addConfigurationProperty("touch.deviceType", "touchScreen");
4580 prepareDisplay(DISPLAY_ORIENTATION_0);
4581 prepareButtons();
4582 prepareAxes(POSITION);
4583 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004584 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585
4586 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004587 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
4589 // Virtual key is down.
4590 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4591 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4592 processDown(mapper, x, y);
4593 processSync(mapper);
4594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004596 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597
4598 // Virtual key is up.
4599 processUp(mapper);
4600 processSync(mapper);
4601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004603 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604}
4605
4606TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607 addConfigurationProperty("touch.deviceType", "touchScreen");
4608 prepareDisplay(DISPLAY_ORIENTATION_0);
4609 prepareButtons();
4610 prepareAxes(POSITION);
4611 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004612 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613
4614 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004615 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
4617 // Virtual key is down.
4618 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4619 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4620 processDown(mapper, x, y);
4621 processSync(mapper);
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4623
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004624 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625
4626 // Virtual key is up.
4627 processUp(mapper);
4628 processSync(mapper);
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4630
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004631 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632}
4633
4634TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 addConfigurationProperty("touch.deviceType", "touchScreen");
4636 prepareDisplay(DISPLAY_ORIENTATION_0);
4637 prepareButtons();
4638 prepareAxes(POSITION);
4639 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004640 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641
4642 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4643 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004644 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 ASSERT_TRUE(flags[0]);
4646 ASSERT_FALSE(flags[1]);
4647}
4648
4649TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 addConfigurationProperty("touch.deviceType", "touchScreen");
4651 prepareDisplay(DISPLAY_ORIENTATION_0);
4652 prepareButtons();
4653 prepareAxes(POSITION);
4654 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004655 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656
arthurhungdcef2dc2020-08-11 14:47:50 +08004657 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658
4659 NotifyKeyArgs args;
4660
4661 // Press virtual key.
4662 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4663 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4664 processDown(mapper, x, y);
4665 processSync(mapper);
4666
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4668 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4669 ASSERT_EQ(DEVICE_ID, args.deviceId);
4670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4671 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4672 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4673 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4674 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4675 ASSERT_EQ(KEY_HOME, args.scanCode);
4676 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4677 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4678
4679 // Release virtual key.
4680 processUp(mapper);
4681 processSync(mapper);
4682
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4684 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4685 ASSERT_EQ(DEVICE_ID, args.deviceId);
4686 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4687 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4688 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4689 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4690 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4691 ASSERT_EQ(KEY_HOME, args.scanCode);
4692 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4693 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4694
4695 // Should not have sent any motions.
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4697}
4698
4699TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 addConfigurationProperty("touch.deviceType", "touchScreen");
4701 prepareDisplay(DISPLAY_ORIENTATION_0);
4702 prepareButtons();
4703 prepareAxes(POSITION);
4704 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706
arthurhungdcef2dc2020-08-11 14:47:50 +08004707 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708
4709 NotifyKeyArgs keyArgs;
4710
4711 // Press virtual key.
4712 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4713 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4714 processDown(mapper, x, y);
4715 processSync(mapper);
4716
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4718 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4719 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4720 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4721 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4722 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4723 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4724 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4725 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4727 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4728
4729 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4730 // into the display area.
4731 y -= 100;
4732 processMove(mapper, x, y);
4733 processSync(mapper);
4734
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4736 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4737 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4738 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4739 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4740 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4741 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4742 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4743 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4744 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4746 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4747
4748 NotifyMotionArgs motionArgs;
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4751 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4752 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4753 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4755 ASSERT_EQ(0, motionArgs.flags);
4756 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4757 ASSERT_EQ(0, motionArgs.buttonState);
4758 ASSERT_EQ(0, motionArgs.edgeFlags);
4759 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4760 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4763 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4764 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4765 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4767
4768 // Keep moving out of bounds. Should generate a pointer move.
4769 y -= 50;
4770 processMove(mapper, x, y);
4771 processSync(mapper);
4772
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4774 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4775 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4776 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4777 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4779 ASSERT_EQ(0, motionArgs.flags);
4780 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4781 ASSERT_EQ(0, motionArgs.buttonState);
4782 ASSERT_EQ(0, motionArgs.edgeFlags);
4783 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4784 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4787 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4788 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4789 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4790 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4791
4792 // Release out of bounds. Should generate a pointer up.
4793 processUp(mapper);
4794 processSync(mapper);
4795
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4797 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4798 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4799 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4800 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4802 ASSERT_EQ(0, motionArgs.flags);
4803 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4804 ASSERT_EQ(0, motionArgs.buttonState);
4805 ASSERT_EQ(0, motionArgs.edgeFlags);
4806 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4810 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4811 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4812 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4814
4815 // Should not have sent any more keys or motions.
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4818}
4819
4820TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 addConfigurationProperty("touch.deviceType", "touchScreen");
4822 prepareDisplay(DISPLAY_ORIENTATION_0);
4823 prepareButtons();
4824 prepareAxes(POSITION);
4825 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004826 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827
arthurhungdcef2dc2020-08-11 14:47:50 +08004828 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829
4830 NotifyMotionArgs motionArgs;
4831
4832 // Initially go down out of bounds.
4833 int32_t x = -10;
4834 int32_t y = -10;
4835 processDown(mapper, x, y);
4836 processSync(mapper);
4837
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4839
4840 // Move into the display area. Should generate a pointer down.
4841 x = 50;
4842 y = 75;
4843 processMove(mapper, x, y);
4844 processSync(mapper);
4845
4846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4847 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4848 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4849 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4850 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4851 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4852 ASSERT_EQ(0, motionArgs.flags);
4853 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4854 ASSERT_EQ(0, motionArgs.buttonState);
4855 ASSERT_EQ(0, motionArgs.edgeFlags);
4856 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4857 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4860 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4861 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4862 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4863 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4864
4865 // Release. Should generate a pointer up.
4866 processUp(mapper);
4867 processSync(mapper);
4868
4869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4870 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4871 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4872 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4873 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4875 ASSERT_EQ(0, motionArgs.flags);
4876 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4877 ASSERT_EQ(0, motionArgs.buttonState);
4878 ASSERT_EQ(0, motionArgs.edgeFlags);
4879 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4880 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4883 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4884 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4885 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4886 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4887
4888 // Should not have sent any more keys or motions.
4889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4891}
4892
Santos Cordonfa5cf462017-04-05 10:37:00 -07004893TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004894 addConfigurationProperty("touch.deviceType", "touchScreen");
4895 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4896
4897 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4898 prepareButtons();
4899 prepareAxes(POSITION);
4900 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004901 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004902
arthurhungdcef2dc2020-08-11 14:47:50 +08004903 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004904
4905 NotifyMotionArgs motionArgs;
4906
4907 // Down.
4908 int32_t x = 100;
4909 int32_t y = 125;
4910 processDown(mapper, x, y);
4911 processSync(mapper);
4912
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4915 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4916 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4917 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4918 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4920 ASSERT_EQ(0, motionArgs.flags);
4921 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4922 ASSERT_EQ(0, motionArgs.buttonState);
4923 ASSERT_EQ(0, motionArgs.edgeFlags);
4924 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4925 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4928 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4929 1, 0, 0, 0, 0, 0, 0, 0));
4930 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4931 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4932 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4933
4934 // Move.
4935 x += 50;
4936 y += 75;
4937 processMove(mapper, x, y);
4938 processSync(mapper);
4939
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4941 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4942 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4943 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4944 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4945 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4947 ASSERT_EQ(0, motionArgs.flags);
4948 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4949 ASSERT_EQ(0, motionArgs.buttonState);
4950 ASSERT_EQ(0, motionArgs.edgeFlags);
4951 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4952 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4955 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4956 1, 0, 0, 0, 0, 0, 0, 0));
4957 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4958 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4959 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4960
4961 // Up.
4962 processUp(mapper);
4963 processSync(mapper);
4964
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4966 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4967 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4968 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4969 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4970 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4972 ASSERT_EQ(0, motionArgs.flags);
4973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4974 ASSERT_EQ(0, motionArgs.buttonState);
4975 ASSERT_EQ(0, motionArgs.edgeFlags);
4976 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4977 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4980 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4981 1, 0, 0, 0, 0, 0, 0, 0));
4982 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4983 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4984 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4985
4986 // Should not have sent any more keys or motions.
4987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4989}
4990
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 addConfigurationProperty("touch.deviceType", "touchScreen");
4993 prepareDisplay(DISPLAY_ORIENTATION_0);
4994 prepareButtons();
4995 prepareAxes(POSITION);
4996 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998
arthurhungdcef2dc2020-08-11 14:47:50 +08004999 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000
5001 NotifyMotionArgs motionArgs;
5002
5003 // Down.
5004 int32_t x = 100;
5005 int32_t y = 125;
5006 processDown(mapper, x, y);
5007 processSync(mapper);
5008
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5011 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5012 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5013 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5014 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5015 ASSERT_EQ(0, motionArgs.flags);
5016 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5017 ASSERT_EQ(0, motionArgs.buttonState);
5018 ASSERT_EQ(0, motionArgs.edgeFlags);
5019 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5020 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5023 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5024 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5025 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5026 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5027
5028 // Move.
5029 x += 50;
5030 y += 75;
5031 processMove(mapper, x, y);
5032 processSync(mapper);
5033
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5035 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5036 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5037 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5038 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5040 ASSERT_EQ(0, motionArgs.flags);
5041 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5042 ASSERT_EQ(0, motionArgs.buttonState);
5043 ASSERT_EQ(0, motionArgs.edgeFlags);
5044 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5045 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5048 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5049 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5050 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5051 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5052
5053 // Up.
5054 processUp(mapper);
5055 processSync(mapper);
5056
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5059 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5060 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5061 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5063 ASSERT_EQ(0, motionArgs.flags);
5064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5065 ASSERT_EQ(0, motionArgs.buttonState);
5066 ASSERT_EQ(0, motionArgs.edgeFlags);
5067 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5068 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5071 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5072 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5073 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5074 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5075
5076 // Should not have sent any more keys or motions.
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5079}
5080
5081TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 addConfigurationProperty("touch.deviceType", "touchScreen");
5083 prepareButtons();
5084 prepareAxes(POSITION);
5085 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005086 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087
5088 NotifyMotionArgs args;
5089
5090 // Rotation 90.
5091 prepareDisplay(DISPLAY_ORIENTATION_90);
5092 processDown(mapper, toRawX(50), toRawY(75));
5093 processSync(mapper);
5094
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5096 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5097 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5098
5099 processUp(mapper);
5100 processSync(mapper);
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5102}
5103
5104TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 addConfigurationProperty("touch.deviceType", "touchScreen");
5106 prepareButtons();
5107 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005108 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109
5110 NotifyMotionArgs args;
5111
5112 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005113 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114 prepareDisplay(DISPLAY_ORIENTATION_0);
5115 processDown(mapper, toRawX(50), toRawY(75));
5116 processSync(mapper);
5117
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5119 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5120 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5121
5122 processUp(mapper);
5123 processSync(mapper);
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5125
5126 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005127 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128 prepareDisplay(DISPLAY_ORIENTATION_90);
5129 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5130 processSync(mapper);
5131
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5133 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5134 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5135
5136 processUp(mapper);
5137 processSync(mapper);
5138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5139
5140 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005141 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 prepareDisplay(DISPLAY_ORIENTATION_180);
5143 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5144 processSync(mapper);
5145
5146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5147 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5148 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5149
5150 processUp(mapper);
5151 processSync(mapper);
5152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5153
5154 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005155 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156 prepareDisplay(DISPLAY_ORIENTATION_270);
5157 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5158 processSync(mapper);
5159
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5161 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5162 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5163
5164 processUp(mapper);
5165 processSync(mapper);
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5167}
5168
5169TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 addConfigurationProperty("touch.deviceType", "touchScreen");
5171 prepareDisplay(DISPLAY_ORIENTATION_0);
5172 prepareButtons();
5173 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005174 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175
5176 // These calculations are based on the input device calibration documentation.
5177 int32_t rawX = 100;
5178 int32_t rawY = 200;
5179 int32_t rawPressure = 10;
5180 int32_t rawToolMajor = 12;
5181 int32_t rawDistance = 2;
5182 int32_t rawTiltX = 30;
5183 int32_t rawTiltY = 110;
5184
5185 float x = toDisplayX(rawX);
5186 float y = toDisplayY(rawY);
5187 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5188 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5189 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5190 float distance = float(rawDistance);
5191
5192 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5193 float tiltScale = M_PI / 180;
5194 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5195 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5196 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5197 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5198
5199 processDown(mapper, rawX, rawY);
5200 processPressure(mapper, rawPressure);
5201 processToolMajor(mapper, rawToolMajor);
5202 processDistance(mapper, rawDistance);
5203 processTilt(mapper, rawTiltX, rawTiltY);
5204 processSync(mapper);
5205
5206 NotifyMotionArgs args;
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5209 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5210 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5211}
5212
Jason Gerecke489fda82012-09-07 17:19:40 -07005213TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005214 addConfigurationProperty("touch.deviceType", "touchScreen");
5215 prepareDisplay(DISPLAY_ORIENTATION_0);
5216 prepareLocationCalibration();
5217 prepareButtons();
5218 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005219 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005220
5221 int32_t rawX = 100;
5222 int32_t rawY = 200;
5223
5224 float x = toDisplayX(toCookedX(rawX, rawY));
5225 float y = toDisplayY(toCookedY(rawX, rawY));
5226
5227 processDown(mapper, rawX, rawY);
5228 processSync(mapper);
5229
5230 NotifyMotionArgs args;
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5233 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5234}
5235
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237 addConfigurationProperty("touch.deviceType", "touchScreen");
5238 prepareDisplay(DISPLAY_ORIENTATION_0);
5239 prepareButtons();
5240 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005241 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242
5243 NotifyMotionArgs motionArgs;
5244 NotifyKeyArgs keyArgs;
5245
5246 processDown(mapper, 100, 200);
5247 processSync(mapper);
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5250 ASSERT_EQ(0, motionArgs.buttonState);
5251
5252 // press BTN_LEFT, release BTN_LEFT
5253 processKey(mapper, BTN_LEFT, 1);
5254 processSync(mapper);
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5257 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5258
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5261 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5262
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263 processKey(mapper, BTN_LEFT, 0);
5264 processSync(mapper);
5265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005271 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272
5273 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5274 processKey(mapper, BTN_RIGHT, 1);
5275 processKey(mapper, BTN_MIDDLE, 1);
5276 processSync(mapper);
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5279 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5280 motionArgs.buttonState);
5281
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5284 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5285
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5288 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5289 motionArgs.buttonState);
5290
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 processKey(mapper, BTN_RIGHT, 0);
5292 processSync(mapper);
5293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005296
5297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005299 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300
5301 processKey(mapper, BTN_MIDDLE, 0);
5302 processSync(mapper);
5303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005306
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005309 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310
5311 // press BTN_BACK, release BTN_BACK
5312 processKey(mapper, BTN_BACK, 1);
5313 processSync(mapper);
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5316 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005317
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005320 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5321
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5324 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325
5326 processKey(mapper, BTN_BACK, 0);
5327 processSync(mapper);
5328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005331
5332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005334 ASSERT_EQ(0, motionArgs.buttonState);
5335
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5338 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5339
5340 // press BTN_SIDE, release BTN_SIDE
5341 processKey(mapper, BTN_SIDE, 1);
5342 processSync(mapper);
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5345 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005346
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005349 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5350
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5353 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354
5355 processKey(mapper, BTN_SIDE, 0);
5356 processSync(mapper);
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005360
5361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005363 ASSERT_EQ(0, motionArgs.buttonState);
5364
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5367 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5368
5369 // press BTN_FORWARD, release BTN_FORWARD
5370 processKey(mapper, BTN_FORWARD, 1);
5371 processSync(mapper);
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5373 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5374 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005375
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005378 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5379
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5382 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383
5384 processKey(mapper, BTN_FORWARD, 0);
5385 processSync(mapper);
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005389
5390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005392 ASSERT_EQ(0, motionArgs.buttonState);
5393
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5395 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5396 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5397
5398 // press BTN_EXTRA, release BTN_EXTRA
5399 processKey(mapper, BTN_EXTRA, 1);
5400 processSync(mapper);
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5402 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5403 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005404
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005407 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5408
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5411 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412
5413 processKey(mapper, BTN_EXTRA, 0);
5414 processSync(mapper);
5415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005418
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005421 ASSERT_EQ(0, motionArgs.buttonState);
5422
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5424 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5425 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5426
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5428
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 // press BTN_STYLUS, release BTN_STYLUS
5430 processKey(mapper, BTN_STYLUS, 1);
5431 processSync(mapper);
5432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005434 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5435
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5438 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439
5440 processKey(mapper, BTN_STYLUS, 0);
5441 processSync(mapper);
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005445
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005448 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449
5450 // press BTN_STYLUS2, release BTN_STYLUS2
5451 processKey(mapper, BTN_STYLUS2, 1);
5452 processSync(mapper);
5453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005455 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5456
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5459 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460
5461 processKey(mapper, BTN_STYLUS2, 0);
5462 processSync(mapper);
5463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005466
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005469 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470
5471 // release touch
5472 processUp(mapper);
5473 processSync(mapper);
5474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5475 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5476 ASSERT_EQ(0, motionArgs.buttonState);
5477}
5478
5479TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480 addConfigurationProperty("touch.deviceType", "touchScreen");
5481 prepareDisplay(DISPLAY_ORIENTATION_0);
5482 prepareButtons();
5483 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005484 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485
5486 NotifyMotionArgs motionArgs;
5487
5488 // default tool type is finger
5489 processDown(mapper, 100, 200);
5490 processSync(mapper);
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5494
5495 // eraser
5496 processKey(mapper, BTN_TOOL_RUBBER, 1);
5497 processSync(mapper);
5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5501
5502 // stylus
5503 processKey(mapper, BTN_TOOL_RUBBER, 0);
5504 processKey(mapper, BTN_TOOL_PEN, 1);
5505 processSync(mapper);
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5509
5510 // brush
5511 processKey(mapper, BTN_TOOL_PEN, 0);
5512 processKey(mapper, BTN_TOOL_BRUSH, 1);
5513 processSync(mapper);
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5517
5518 // pencil
5519 processKey(mapper, BTN_TOOL_BRUSH, 0);
5520 processKey(mapper, BTN_TOOL_PENCIL, 1);
5521 processSync(mapper);
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5525
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005526 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 processKey(mapper, BTN_TOOL_PENCIL, 0);
5528 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5529 processSync(mapper);
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5533
5534 // mouse
5535 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5536 processKey(mapper, BTN_TOOL_MOUSE, 1);
5537 processSync(mapper);
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5541
5542 // lens
5543 processKey(mapper, BTN_TOOL_MOUSE, 0);
5544 processKey(mapper, BTN_TOOL_LENS, 1);
5545 processSync(mapper);
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5549
5550 // double-tap
5551 processKey(mapper, BTN_TOOL_LENS, 0);
5552 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5557
5558 // triple-tap
5559 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5560 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5561 processSync(mapper);
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5565
5566 // quad-tap
5567 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5568 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5569 processSync(mapper);
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5573
5574 // finger
5575 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5576 processKey(mapper, BTN_TOOL_FINGER, 1);
5577 processSync(mapper);
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5581
5582 // stylus trumps finger
5583 processKey(mapper, BTN_TOOL_PEN, 1);
5584 processSync(mapper);
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5588
5589 // eraser trumps stylus
5590 processKey(mapper, BTN_TOOL_RUBBER, 1);
5591 processSync(mapper);
5592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5595
5596 // mouse trumps eraser
5597 processKey(mapper, BTN_TOOL_MOUSE, 1);
5598 processSync(mapper);
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5602
5603 // back to default tool type
5604 processKey(mapper, BTN_TOOL_MOUSE, 0);
5605 processKey(mapper, BTN_TOOL_RUBBER, 0);
5606 processKey(mapper, BTN_TOOL_PEN, 0);
5607 processKey(mapper, BTN_TOOL_FINGER, 0);
5608 processSync(mapper);
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5612}
5613
5614TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005615 addConfigurationProperty("touch.deviceType", "touchScreen");
5616 prepareDisplay(DISPLAY_ORIENTATION_0);
5617 prepareButtons();
5618 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005619 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005620 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621
5622 NotifyMotionArgs motionArgs;
5623
5624 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5625 processKey(mapper, BTN_TOOL_FINGER, 1);
5626 processMove(mapper, 100, 200);
5627 processSync(mapper);
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5631 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5632
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5636 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5637
5638 // move a little
5639 processMove(mapper, 150, 250);
5640 processSync(mapper);
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5644 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5645
5646 // down when BTN_TOUCH is pressed, pressure defaults to 1
5647 processKey(mapper, BTN_TOUCH, 1);
5648 processSync(mapper);
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5650 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5652 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5653
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5655 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5657 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5658
5659 // up when BTN_TOUCH is released, hover restored
5660 processKey(mapper, BTN_TOUCH, 0);
5661 processSync(mapper);
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5663 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5665 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5666
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5670 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5671
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5675 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5676
5677 // exit hover when pointer goes away
5678 processKey(mapper, BTN_TOOL_FINGER, 0);
5679 processSync(mapper);
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5684}
5685
5686TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687 addConfigurationProperty("touch.deviceType", "touchScreen");
5688 prepareDisplay(DISPLAY_ORIENTATION_0);
5689 prepareButtons();
5690 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005691 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692
5693 NotifyMotionArgs motionArgs;
5694
5695 // initially hovering because pressure is 0
5696 processDown(mapper, 100, 200);
5697 processPressure(mapper, 0);
5698 processSync(mapper);
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5702 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5703
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5707 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5708
5709 // move a little
5710 processMove(mapper, 150, 250);
5711 processSync(mapper);
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5715 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5716
5717 // down when pressure is non-zero
5718 processPressure(mapper, RAW_PRESSURE_MAX);
5719 processSync(mapper);
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5721 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5723 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5724
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5726 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5728 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5729
5730 // up when pressure becomes 0, hover restored
5731 processPressure(mapper, 0);
5732 processSync(mapper);
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5736 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5737
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5741 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5746 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5747
5748 // exit hover when pointer goes away
5749 processUp(mapper);
5750 processSync(mapper);
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5754 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5755}
5756
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757// --- MultiTouchInputMapperTest ---
5758
5759class MultiTouchInputMapperTest : public TouchInputMapperTest {
5760protected:
5761 void prepareAxes(int axes);
5762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005763 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5764 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5765 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5766 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5767 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5768 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5769 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5770 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5771 void processId(MultiTouchInputMapper& mapper, int32_t id);
5772 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5773 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5774 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5775 void processMTSync(MultiTouchInputMapper& mapper);
5776 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777};
5778
5779void MultiTouchInputMapperTest::prepareAxes(int axes) {
5780 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005781 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5782 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005783 }
5784 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005785 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5786 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005788 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5789 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790 }
5791 }
5792 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005793 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5794 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005796 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5797 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798 }
5799 }
5800 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005801 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5802 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803 }
5804 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005805 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5806 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 }
5808 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5810 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 }
5812 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005813 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5814 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 }
5816 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005817 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5818 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 }
5820 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005821 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822 }
5823}
5824
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005825void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5826 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005827 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5828 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829}
5830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005831void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5832 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005833 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834}
5835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005836void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5837 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005838 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839}
5840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005841void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005842 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843}
5844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005845void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005846 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847}
5848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005849void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5850 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005851 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852}
5853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005854void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005855 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856}
5857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005858void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005859 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860}
5861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005862void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005863 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005864}
5865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005866void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005867 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868}
5869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005870void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005871 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872}
5873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005874void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5875 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005876 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877}
5878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005879void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005880 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005881}
5882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005883void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005884 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885}
5886
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 addConfigurationProperty("touch.deviceType", "touchScreen");
5889 prepareDisplay(DISPLAY_ORIENTATION_0);
5890 prepareAxes(POSITION);
5891 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005892 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893
arthurhungdcef2dc2020-08-11 14:47:50 +08005894 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895
5896 NotifyMotionArgs motionArgs;
5897
5898 // Two fingers down at once.
5899 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5900 processPosition(mapper, x1, y1);
5901 processMTSync(mapper);
5902 processPosition(mapper, x2, y2);
5903 processMTSync(mapper);
5904 processSync(mapper);
5905
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5908 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5909 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5910 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5912 ASSERT_EQ(0, motionArgs.flags);
5913 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5914 ASSERT_EQ(0, motionArgs.buttonState);
5915 ASSERT_EQ(0, motionArgs.edgeFlags);
5916 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5917 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5920 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5921 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5922 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5923 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5927 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5928 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5929 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5931 motionArgs.action);
5932 ASSERT_EQ(0, motionArgs.flags);
5933 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5934 ASSERT_EQ(0, motionArgs.buttonState);
5935 ASSERT_EQ(0, motionArgs.edgeFlags);
5936 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5937 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5939 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5942 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5944 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5945 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5946 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5947 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5948
5949 // Move.
5950 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5951 processPosition(mapper, x1, y1);
5952 processMTSync(mapper);
5953 processPosition(mapper, x2, y2);
5954 processMTSync(mapper);
5955 processSync(mapper);
5956
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5959 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5960 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5961 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5963 ASSERT_EQ(0, motionArgs.flags);
5964 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5965 ASSERT_EQ(0, motionArgs.buttonState);
5966 ASSERT_EQ(0, motionArgs.edgeFlags);
5967 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5968 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5970 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5973 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5975 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5976 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5977 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5979
5980 // First finger up.
5981 x2 += 15; y2 -= 20;
5982 processPosition(mapper, x2, y2);
5983 processMTSync(mapper);
5984 processSync(mapper);
5985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5988 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5989 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5990 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5992 motionArgs.action);
5993 ASSERT_EQ(0, motionArgs.flags);
5994 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5995 ASSERT_EQ(0, motionArgs.buttonState);
5996 ASSERT_EQ(0, motionArgs.edgeFlags);
5997 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5998 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6000 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6003 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6005 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6006 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6007 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6008 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6009
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6011 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6012 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6013 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6014 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6016 ASSERT_EQ(0, motionArgs.flags);
6017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6018 ASSERT_EQ(0, motionArgs.buttonState);
6019 ASSERT_EQ(0, motionArgs.edgeFlags);
6020 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6021 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6024 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6025 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6026 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6028
6029 // Move.
6030 x2 += 20; y2 -= 25;
6031 processPosition(mapper, x2, y2);
6032 processMTSync(mapper);
6033 processSync(mapper);
6034
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6037 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6038 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6039 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6041 ASSERT_EQ(0, motionArgs.flags);
6042 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6043 ASSERT_EQ(0, motionArgs.buttonState);
6044 ASSERT_EQ(0, motionArgs.edgeFlags);
6045 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6046 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6049 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6050 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6051 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6052 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6053
6054 // New finger down.
6055 int32_t x3 = 700, y3 = 300;
6056 processPosition(mapper, x2, y2);
6057 processMTSync(mapper);
6058 processPosition(mapper, x3, y3);
6059 processMTSync(mapper);
6060 processSync(mapper);
6061
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6065 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6066 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6068 motionArgs.action);
6069 ASSERT_EQ(0, motionArgs.flags);
6070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6071 ASSERT_EQ(0, motionArgs.buttonState);
6072 ASSERT_EQ(0, motionArgs.edgeFlags);
6073 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6074 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6076 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6079 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6081 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6082 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6083 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6084 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6085
6086 // Second finger up.
6087 x3 += 30; y3 -= 20;
6088 processPosition(mapper, x3, y3);
6089 processMTSync(mapper);
6090 processSync(mapper);
6091
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6094 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6095 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6096 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6097 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6098 motionArgs.action);
6099 ASSERT_EQ(0, motionArgs.flags);
6100 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6101 ASSERT_EQ(0, motionArgs.buttonState);
6102 ASSERT_EQ(0, motionArgs.edgeFlags);
6103 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6104 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6106 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6109 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6111 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6112 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6113 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6114 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6115
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6118 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6119 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6120 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6122 ASSERT_EQ(0, motionArgs.flags);
6123 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6124 ASSERT_EQ(0, motionArgs.buttonState);
6125 ASSERT_EQ(0, motionArgs.edgeFlags);
6126 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6127 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6130 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6131 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6132 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6134
6135 // Last finger up.
6136 processMTSync(mapper);
6137 processSync(mapper);
6138
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6141 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6142 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6143 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6145 ASSERT_EQ(0, motionArgs.flags);
6146 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6147 ASSERT_EQ(0, motionArgs.buttonState);
6148 ASSERT_EQ(0, motionArgs.edgeFlags);
6149 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6150 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6153 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6154 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6155 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6157
6158 // Should not have sent any more keys or motions.
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6161}
6162
6163TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 addConfigurationProperty("touch.deviceType", "touchScreen");
6165 prepareDisplay(DISPLAY_ORIENTATION_0);
6166 prepareAxes(POSITION | ID);
6167 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006168 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169
arthurhungdcef2dc2020-08-11 14:47:50 +08006170 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171
6172 NotifyMotionArgs motionArgs;
6173
6174 // Two fingers down at once.
6175 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6176 processPosition(mapper, x1, y1);
6177 processId(mapper, 1);
6178 processMTSync(mapper);
6179 processPosition(mapper, x2, y2);
6180 processId(mapper, 2);
6181 processMTSync(mapper);
6182 processSync(mapper);
6183
6184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6185 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6186 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6187 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6190 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6191
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6193 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6194 motionArgs.action);
6195 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6196 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6198 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6201 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6203 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6204
6205 // Move.
6206 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6207 processPosition(mapper, x1, y1);
6208 processId(mapper, 1);
6209 processMTSync(mapper);
6210 processPosition(mapper, x2, y2);
6211 processId(mapper, 2);
6212 processMTSync(mapper);
6213 processSync(mapper);
6214
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6217 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6218 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6220 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6223 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6225 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6226
6227 // First finger up.
6228 x2 += 15; y2 -= 20;
6229 processPosition(mapper, x2, y2);
6230 processId(mapper, 2);
6231 processMTSync(mapper);
6232 processSync(mapper);
6233
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6236 motionArgs.action);
6237 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6240 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6243 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6245 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6249 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6250 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6253 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6254
6255 // Move.
6256 x2 += 20; y2 -= 25;
6257 processPosition(mapper, x2, y2);
6258 processId(mapper, 2);
6259 processMTSync(mapper);
6260 processSync(mapper);
6261
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6264 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6265 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6268 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6269
6270 // New finger down.
6271 int32_t x3 = 700, y3 = 300;
6272 processPosition(mapper, x2, y2);
6273 processId(mapper, 2);
6274 processMTSync(mapper);
6275 processPosition(mapper, x3, y3);
6276 processId(mapper, 3);
6277 processMTSync(mapper);
6278 processSync(mapper);
6279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6282 motionArgs.action);
6283 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6286 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6289 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6292
6293 // Second finger up.
6294 x3 += 30; y3 -= 20;
6295 processPosition(mapper, x3, y3);
6296 processId(mapper, 3);
6297 processMTSync(mapper);
6298 processSync(mapper);
6299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6302 motionArgs.action);
6303 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6304 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6306 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6309 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6311 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6312
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6315 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6316 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6319 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6320
6321 // Last finger up.
6322 processMTSync(mapper);
6323 processSync(mapper);
6324
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6327 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6328 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6331 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6332
6333 // Should not have sent any more keys or motions.
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6336}
6337
6338TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 addConfigurationProperty("touch.deviceType", "touchScreen");
6340 prepareDisplay(DISPLAY_ORIENTATION_0);
6341 prepareAxes(POSITION | ID | SLOT);
6342 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006343 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344
arthurhungdcef2dc2020-08-11 14:47:50 +08006345 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346
6347 NotifyMotionArgs motionArgs;
6348
6349 // Two fingers down at once.
6350 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6351 processPosition(mapper, x1, y1);
6352 processId(mapper, 1);
6353 processSlot(mapper, 1);
6354 processPosition(mapper, x2, y2);
6355 processId(mapper, 2);
6356 processSync(mapper);
6357
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6360 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6361 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6364 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6365
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6368 motionArgs.action);
6369 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6370 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6372 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6375 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6377 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6378
6379 // Move.
6380 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6381 processSlot(mapper, 0);
6382 processPosition(mapper, x1, y1);
6383 processSlot(mapper, 1);
6384 processPosition(mapper, x2, y2);
6385 processSync(mapper);
6386
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6389 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6390 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6392 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6395 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6397 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6398
6399 // First finger up.
6400 x2 += 15; y2 -= 20;
6401 processSlot(mapper, 0);
6402 processId(mapper, -1);
6403 processSlot(mapper, 1);
6404 processPosition(mapper, x2, y2);
6405 processSync(mapper);
6406
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6409 motionArgs.action);
6410 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6411 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6413 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6418 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6419
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6422 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6423 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6426 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6427
6428 // Move.
6429 x2 += 20; y2 -= 25;
6430 processPosition(mapper, x2, y2);
6431 processSync(mapper);
6432
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6435 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6436 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6439 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6440
6441 // New finger down.
6442 int32_t x3 = 700, y3 = 300;
6443 processPosition(mapper, x2, y2);
6444 processSlot(mapper, 0);
6445 processId(mapper, 3);
6446 processPosition(mapper, x3, y3);
6447 processSync(mapper);
6448
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6451 motionArgs.action);
6452 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6453 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6455 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6458 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6460 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6461
6462 // Second finger up.
6463 x3 += 30; y3 -= 20;
6464 processSlot(mapper, 1);
6465 processId(mapper, -1);
6466 processSlot(mapper, 0);
6467 processPosition(mapper, x3, y3);
6468 processSync(mapper);
6469
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6472 motionArgs.action);
6473 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6474 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6476 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6479 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6481 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6482
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6485 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6489 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6490
6491 // Last finger up.
6492 processId(mapper, -1);
6493 processSync(mapper);
6494
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6497 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6498 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6501 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6502
6503 // Should not have sent any more keys or motions.
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6506}
6507
6508TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006509 addConfigurationProperty("touch.deviceType", "touchScreen");
6510 prepareDisplay(DISPLAY_ORIENTATION_0);
6511 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006512 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006513
6514 // These calculations are based on the input device calibration documentation.
6515 int32_t rawX = 100;
6516 int32_t rawY = 200;
6517 int32_t rawTouchMajor = 7;
6518 int32_t rawTouchMinor = 6;
6519 int32_t rawToolMajor = 9;
6520 int32_t rawToolMinor = 8;
6521 int32_t rawPressure = 11;
6522 int32_t rawDistance = 0;
6523 int32_t rawOrientation = 3;
6524 int32_t id = 5;
6525
6526 float x = toDisplayX(rawX);
6527 float y = toDisplayY(rawY);
6528 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6529 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6530 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6531 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6532 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6533 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6534 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6535 float distance = float(rawDistance);
6536
6537 processPosition(mapper, rawX, rawY);
6538 processTouchMajor(mapper, rawTouchMajor);
6539 processTouchMinor(mapper, rawTouchMinor);
6540 processToolMajor(mapper, rawToolMajor);
6541 processToolMinor(mapper, rawToolMinor);
6542 processPressure(mapper, rawPressure);
6543 processOrientation(mapper, rawOrientation);
6544 processDistance(mapper, rawDistance);
6545 processId(mapper, id);
6546 processMTSync(mapper);
6547 processSync(mapper);
6548
6549 NotifyMotionArgs args;
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6551 ASSERT_EQ(0, args.pointerProperties[0].id);
6552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6553 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6554 orientation, distance));
6555}
6556
6557TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558 addConfigurationProperty("touch.deviceType", "touchScreen");
6559 prepareDisplay(DISPLAY_ORIENTATION_0);
6560 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6561 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563
6564 // These calculations are based on the input device calibration documentation.
6565 int32_t rawX = 100;
6566 int32_t rawY = 200;
6567 int32_t rawTouchMajor = 140;
6568 int32_t rawTouchMinor = 120;
6569 int32_t rawToolMajor = 180;
6570 int32_t rawToolMinor = 160;
6571
6572 float x = toDisplayX(rawX);
6573 float y = toDisplayY(rawY);
6574 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6575 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6576 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6577 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6578 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6579
6580 processPosition(mapper, rawX, rawY);
6581 processTouchMajor(mapper, rawTouchMajor);
6582 processTouchMinor(mapper, rawTouchMinor);
6583 processToolMajor(mapper, rawToolMajor);
6584 processToolMinor(mapper, rawToolMinor);
6585 processMTSync(mapper);
6586 processSync(mapper);
6587
6588 NotifyMotionArgs args;
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6591 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6592}
6593
6594TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006595 addConfigurationProperty("touch.deviceType", "touchScreen");
6596 prepareDisplay(DISPLAY_ORIENTATION_0);
6597 prepareAxes(POSITION | TOUCH | TOOL);
6598 addConfigurationProperty("touch.size.calibration", "diameter");
6599 addConfigurationProperty("touch.size.scale", "10");
6600 addConfigurationProperty("touch.size.bias", "160");
6601 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006602 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006603
6604 // These calculations are based on the input device calibration documentation.
6605 // Note: We only provide a single common touch/tool value because the device is assumed
6606 // not to emit separate values for each pointer (isSummed = 1).
6607 int32_t rawX = 100;
6608 int32_t rawY = 200;
6609 int32_t rawX2 = 150;
6610 int32_t rawY2 = 250;
6611 int32_t rawTouchMajor = 5;
6612 int32_t rawToolMajor = 8;
6613
6614 float x = toDisplayX(rawX);
6615 float y = toDisplayY(rawY);
6616 float x2 = toDisplayX(rawX2);
6617 float y2 = toDisplayY(rawY2);
6618 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6619 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6620 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6621
6622 processPosition(mapper, rawX, rawY);
6623 processTouchMajor(mapper, rawTouchMajor);
6624 processToolMajor(mapper, rawToolMajor);
6625 processMTSync(mapper);
6626 processPosition(mapper, rawX2, rawY2);
6627 processTouchMajor(mapper, rawTouchMajor);
6628 processToolMajor(mapper, rawToolMajor);
6629 processMTSync(mapper);
6630 processSync(mapper);
6631
6632 NotifyMotionArgs args;
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6635
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6638 args.action);
6639 ASSERT_EQ(size_t(2), args.pointerCount);
6640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6641 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6643 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6644}
6645
6646TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006647 addConfigurationProperty("touch.deviceType", "touchScreen");
6648 prepareDisplay(DISPLAY_ORIENTATION_0);
6649 prepareAxes(POSITION | TOUCH | TOOL);
6650 addConfigurationProperty("touch.size.calibration", "area");
6651 addConfigurationProperty("touch.size.scale", "43");
6652 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006653 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006654
6655 // These calculations are based on the input device calibration documentation.
6656 int32_t rawX = 100;
6657 int32_t rawY = 200;
6658 int32_t rawTouchMajor = 5;
6659 int32_t rawToolMajor = 8;
6660
6661 float x = toDisplayX(rawX);
6662 float y = toDisplayY(rawY);
6663 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6664 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6665 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6666
6667 processPosition(mapper, rawX, rawY);
6668 processTouchMajor(mapper, rawTouchMajor);
6669 processToolMajor(mapper, rawToolMajor);
6670 processMTSync(mapper);
6671 processSync(mapper);
6672
6673 NotifyMotionArgs args;
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6676 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6677}
6678
6679TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006680 addConfigurationProperty("touch.deviceType", "touchScreen");
6681 prepareDisplay(DISPLAY_ORIENTATION_0);
6682 prepareAxes(POSITION | PRESSURE);
6683 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6684 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006685 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006686
Michael Wrightaa449c92017-12-13 21:21:43 +00006687 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006688 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006689 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6690 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6691 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6692
Michael Wrightd02c5b62014-02-10 15:10:22 -08006693 // These calculations are based on the input device calibration documentation.
6694 int32_t rawX = 100;
6695 int32_t rawY = 200;
6696 int32_t rawPressure = 60;
6697
6698 float x = toDisplayX(rawX);
6699 float y = toDisplayY(rawY);
6700 float pressure = float(rawPressure) * 0.01f;
6701
6702 processPosition(mapper, rawX, rawY);
6703 processPressure(mapper, rawPressure);
6704 processMTSync(mapper);
6705 processSync(mapper);
6706
6707 NotifyMotionArgs args;
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6710 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6711}
6712
6713TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006714 addConfigurationProperty("touch.deviceType", "touchScreen");
6715 prepareDisplay(DISPLAY_ORIENTATION_0);
6716 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006717 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718
6719 NotifyMotionArgs motionArgs;
6720 NotifyKeyArgs keyArgs;
6721
6722 processId(mapper, 1);
6723 processPosition(mapper, 100, 200);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6727 ASSERT_EQ(0, motionArgs.buttonState);
6728
6729 // press BTN_LEFT, release BTN_LEFT
6730 processKey(mapper, BTN_LEFT, 1);
6731 processSync(mapper);
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6734 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6735
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6737 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6738 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6739
Michael Wrightd02c5b62014-02-10 15:10:22 -08006740 processKey(mapper, BTN_LEFT, 0);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006744 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006745
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006748 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006749
6750 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6751 processKey(mapper, BTN_RIGHT, 1);
6752 processKey(mapper, BTN_MIDDLE, 1);
6753 processSync(mapper);
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6756 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6757 motionArgs.buttonState);
6758
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6761 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6762
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6765 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6766 motionArgs.buttonState);
6767
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768 processKey(mapper, BTN_RIGHT, 0);
6769 processSync(mapper);
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006772 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006773
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006776 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006777
6778 processKey(mapper, BTN_MIDDLE, 0);
6779 processSync(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006781 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006782 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006783
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006786 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006787
6788 // press BTN_BACK, release BTN_BACK
6789 processKey(mapper, BTN_BACK, 1);
6790 processSync(mapper);
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6792 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6793 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006794
Michael Wrightd02c5b62014-02-10 15:10:22 -08006795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006797 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6798
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6801 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006802
6803 processKey(mapper, BTN_BACK, 0);
6804 processSync(mapper);
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006807 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006808
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006811 ASSERT_EQ(0, motionArgs.buttonState);
6812
Michael Wrightd02c5b62014-02-10 15:10:22 -08006813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6814 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6815 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6816
6817 // press BTN_SIDE, release BTN_SIDE
6818 processKey(mapper, BTN_SIDE, 1);
6819 processSync(mapper);
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6821 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6822 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006823
Michael Wrightd02c5b62014-02-10 15:10:22 -08006824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006826 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6827
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6830 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006831
6832 processKey(mapper, BTN_SIDE, 0);
6833 processSync(mapper);
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006837
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006840 ASSERT_EQ(0, motionArgs.buttonState);
6841
Michael Wrightd02c5b62014-02-10 15:10:22 -08006842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6843 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6844 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6845
6846 // press BTN_FORWARD, release BTN_FORWARD
6847 processKey(mapper, BTN_FORWARD, 1);
6848 processSync(mapper);
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6850 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6851 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006852
Michael Wrightd02c5b62014-02-10 15:10:22 -08006853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006855 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6856
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6859 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006860
6861 processKey(mapper, BTN_FORWARD, 0);
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006865 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006866
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006869 ASSERT_EQ(0, motionArgs.buttonState);
6870
Michael Wrightd02c5b62014-02-10 15:10:22 -08006871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6872 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6873 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6874
6875 // press BTN_EXTRA, release BTN_EXTRA
6876 processKey(mapper, BTN_EXTRA, 1);
6877 processSync(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6879 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6880 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006881
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006884 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6885
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6888 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006889
6890 processKey(mapper, BTN_EXTRA, 0);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006894 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006895
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006898 ASSERT_EQ(0, motionArgs.buttonState);
6899
Michael Wrightd02c5b62014-02-10 15:10:22 -08006900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6901 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6902 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6903
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6905
Michael Wrightd02c5b62014-02-10 15:10:22 -08006906 // press BTN_STYLUS, release BTN_STYLUS
6907 processKey(mapper, BTN_STYLUS, 1);
6908 processSync(mapper);
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006911 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6912
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6915 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006916
6917 processKey(mapper, BTN_STYLUS, 0);
6918 processSync(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006922
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006925 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006926
6927 // press BTN_STYLUS2, release BTN_STYLUS2
6928 processKey(mapper, BTN_STYLUS2, 1);
6929 processSync(mapper);
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006932 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6933
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6936 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006937
6938 processKey(mapper, BTN_STYLUS2, 0);
6939 processSync(mapper);
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006942 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006943
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006946 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947
6948 // release touch
6949 processId(mapper, -1);
6950 processSync(mapper);
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6952 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6953 ASSERT_EQ(0, motionArgs.buttonState);
6954}
6955
6956TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006957 addConfigurationProperty("touch.deviceType", "touchScreen");
6958 prepareDisplay(DISPLAY_ORIENTATION_0);
6959 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961
6962 NotifyMotionArgs motionArgs;
6963
6964 // default tool type is finger
6965 processId(mapper, 1);
6966 processPosition(mapper, 100, 200);
6967 processSync(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6969 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6971
6972 // eraser
6973 processKey(mapper, BTN_TOOL_RUBBER, 1);
6974 processSync(mapper);
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6978
6979 // stylus
6980 processKey(mapper, BTN_TOOL_RUBBER, 0);
6981 processKey(mapper, BTN_TOOL_PEN, 1);
6982 processSync(mapper);
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6985 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6986
6987 // brush
6988 processKey(mapper, BTN_TOOL_PEN, 0);
6989 processKey(mapper, BTN_TOOL_BRUSH, 1);
6990 processSync(mapper);
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6994
6995 // pencil
6996 processKey(mapper, BTN_TOOL_BRUSH, 0);
6997 processKey(mapper, BTN_TOOL_PENCIL, 1);
6998 processSync(mapper);
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7000 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7002
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007003 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004 processKey(mapper, BTN_TOOL_PENCIL, 0);
7005 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7006 processSync(mapper);
7007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7010
7011 // mouse
7012 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7013 processKey(mapper, BTN_TOOL_MOUSE, 1);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7018
7019 // lens
7020 processKey(mapper, BTN_TOOL_MOUSE, 0);
7021 processKey(mapper, BTN_TOOL_LENS, 1);
7022 processSync(mapper);
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7025 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7026
7027 // double-tap
7028 processKey(mapper, BTN_TOOL_LENS, 0);
7029 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7034
7035 // triple-tap
7036 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7037 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7038 processSync(mapper);
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7042
7043 // quad-tap
7044 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7045 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7050
7051 // finger
7052 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7053 processKey(mapper, BTN_TOOL_FINGER, 1);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7058
7059 // stylus trumps finger
7060 processKey(mapper, BTN_TOOL_PEN, 1);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7065
7066 // eraser trumps stylus
7067 processKey(mapper, BTN_TOOL_RUBBER, 1);
7068 processSync(mapper);
7069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7072
7073 // mouse trumps eraser
7074 processKey(mapper, BTN_TOOL_MOUSE, 1);
7075 processSync(mapper);
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7079
7080 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7081 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7086
7087 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7088 processToolType(mapper, MT_TOOL_PEN);
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7093
7094 // back to default tool type
7095 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7096 processKey(mapper, BTN_TOOL_MOUSE, 0);
7097 processKey(mapper, BTN_TOOL_RUBBER, 0);
7098 processKey(mapper, BTN_TOOL_PEN, 0);
7099 processKey(mapper, BTN_TOOL_FINGER, 0);
7100 processSync(mapper);
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7104}
7105
7106TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107 addConfigurationProperty("touch.deviceType", "touchScreen");
7108 prepareDisplay(DISPLAY_ORIENTATION_0);
7109 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007110 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007111 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112
7113 NotifyMotionArgs motionArgs;
7114
7115 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7116 processId(mapper, 1);
7117 processPosition(mapper, 100, 200);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7122 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7123
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7125 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7127 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7128
7129 // move a little
7130 processPosition(mapper, 150, 250);
7131 processSync(mapper);
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7133 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7135 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7136
7137 // down when BTN_TOUCH is pressed, pressure defaults to 1
7138 processKey(mapper, BTN_TOUCH, 1);
7139 processSync(mapper);
7140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7141 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7143 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7144
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7148 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7149
7150 // up when BTN_TOUCH is released, hover restored
7151 processKey(mapper, BTN_TOUCH, 0);
7152 processSync(mapper);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7154 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7156 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7157
7158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7159 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7161 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7162
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7164 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7166 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7167
7168 // exit hover when pointer goes away
7169 processId(mapper, -1);
7170 processSync(mapper);
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7172 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7174 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7175}
7176
7177TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007178 addConfigurationProperty("touch.deviceType", "touchScreen");
7179 prepareDisplay(DISPLAY_ORIENTATION_0);
7180 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007181 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182
7183 NotifyMotionArgs motionArgs;
7184
7185 // initially hovering because pressure is 0
7186 processId(mapper, 1);
7187 processPosition(mapper, 100, 200);
7188 processPressure(mapper, 0);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7193 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7194
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7198 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7199
7200 // move a little
7201 processPosition(mapper, 150, 250);
7202 processSync(mapper);
7203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7206 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7207
7208 // down when pressure becomes non-zero
7209 processPressure(mapper, RAW_PRESSURE_MAX);
7210 processSync(mapper);
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7212 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7214 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7215
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7219 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7220
7221 // up when pressure becomes 0, hover restored
7222 processPressure(mapper, 0);
7223 processSync(mapper);
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7225 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7227 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7228
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7232 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7233
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7237 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7238
7239 // exit hover when pointer goes away
7240 processId(mapper, -1);
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7245 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7246}
7247
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007248/**
7249 * Set the input device port <--> display port associations, and check that the
7250 * events are routed to the display that matches the display port.
7251 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7252 */
7253TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007254 const std::string usb2 = "USB2";
7255 const uint8_t hdmi1 = 0;
7256 const uint8_t hdmi2 = 1;
7257 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007258 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007259
7260 addConfigurationProperty("touch.deviceType", "touchScreen");
7261 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007262 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007263
7264 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7265 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7266
7267 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7268 // for this input device is specified, and the matching viewport is not present,
7269 // the input device should be disabled (at the mapper level).
7270
7271 // Add viewport for display 2 on hdmi2
7272 prepareSecondaryDisplay(type, hdmi2);
7273 // Send a touch event
7274 processPosition(mapper, 100, 100);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7277
7278 // Add viewport for display 1 on hdmi1
7279 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7280 // Send a touch event again
7281 processPosition(mapper, 100, 100);
7282 processSync(mapper);
7283
7284 NotifyMotionArgs args;
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7286 ASSERT_EQ(DISPLAY_ID, args.displayId);
7287}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007288
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007289TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007290 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007291 std::shared_ptr<FakePointerController> fakePointerController =
7292 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007293 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007294 fakePointerController->setPosition(100, 200);
7295 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007296 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7297
Garfield Tan888a6a42020-01-09 11:39:16 -08007298 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007299 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007300
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007301 prepareDisplay(DISPLAY_ORIENTATION_0);
7302 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007304
7305 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007306 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007307
7308 NotifyMotionArgs motionArgs;
7309 processPosition(mapper, 100, 100);
7310 processSync(mapper);
7311
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7313 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7314 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7315}
7316
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007317/**
7318 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7319 * events should not be delivered to the listener.
7320 */
7321TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7322 addConfigurationProperty("touch.deviceType", "touchScreen");
7323 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7324 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7325 ViewportType::INTERNAL);
7326 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7327 prepareAxes(POSITION);
7328 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7329
7330 NotifyMotionArgs motionArgs;
7331 processPosition(mapper, 100, 100);
7332 processSync(mapper);
7333
7334 mFakeListener->assertNotifyMotionWasNotCalled();
7335}
7336
Arthur Hung7c645402019-01-25 17:45:42 +08007337TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7338 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007339 prepareAxes(POSITION | ID | SLOT);
7340 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007341 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007342
7343 // Create the second touch screen device, and enable multi fingers.
7344 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007345 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007346 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007347 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007348 std::shared_ptr<InputDevice> device2 =
7349 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7350 Flags<InputDeviceClass>(0));
7351
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007352 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7353 0 /*flat*/, 0 /*fuzz*/);
7354 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7355 0 /*flat*/, 0 /*fuzz*/);
7356 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7357 0 /*flat*/, 0 /*fuzz*/);
7358 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7359 0 /*flat*/, 0 /*fuzz*/);
7360 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7361 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7362 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007363
7364 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007365 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007366 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7367 device2->reset(ARBITRARY_TIME);
7368
7369 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007370 std::shared_ptr<FakePointerController> fakePointerController =
7371 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007372 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7373 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7374
7375 // Setup policy for associated displays and show touches.
7376 const uint8_t hdmi1 = 0;
7377 const uint8_t hdmi2 = 1;
7378 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7379 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7380 mFakePolicy->setShowTouches(true);
7381
7382 // Create displays.
7383 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007384 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007385
7386 // Default device will reconfigure above, need additional reconfiguration for another device.
7387 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007388 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007389
7390 // Two fingers down at default display.
7391 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7392 processPosition(mapper, x1, y1);
7393 processId(mapper, 1);
7394 processSlot(mapper, 1);
7395 processPosition(mapper, x2, y2);
7396 processId(mapper, 2);
7397 processSync(mapper);
7398
7399 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7400 fakePointerController->getSpots().find(DISPLAY_ID);
7401 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7402 ASSERT_EQ(size_t(2), iter->second.size());
7403
7404 // Two fingers down at second display.
7405 processPosition(mapper2, x1, y1);
7406 processId(mapper2, 1);
7407 processSlot(mapper2, 1);
7408 processPosition(mapper2, x2, y2);
7409 processId(mapper2, 2);
7410 processSync(mapper2);
7411
7412 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7413 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7414 ASSERT_EQ(size_t(2), iter->second.size());
7415}
7416
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007417TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007418 prepareAxes(POSITION);
7419 addConfigurationProperty("touch.deviceType", "touchScreen");
7420 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007421 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007422
7423 NotifyMotionArgs motionArgs;
7424 // Unrotated video frame
7425 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7426 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007427 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007428 processPosition(mapper, 100, 200);
7429 processSync(mapper);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(frames, motionArgs.videoFrames);
7432
7433 // Subsequent touch events should not have any videoframes
7434 // This is implemented separately in FakeEventHub,
7435 // but that should match the behaviour of TouchVideoDevice.
7436 processPosition(mapper, 200, 200);
7437 processSync(mapper);
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7439 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7440}
7441
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007442TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007443 prepareAxes(POSITION);
7444 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007445 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007446 // Unrotated video frame
7447 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7448 NotifyMotionArgs motionArgs;
7449
7450 // Test all 4 orientations
7451 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7452 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7453 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7454 clearViewports();
7455 prepareDisplay(orientation);
7456 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007457 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007458 processPosition(mapper, 100, 200);
7459 processSync(mapper);
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7461 frames[0].rotate(orientation);
7462 ASSERT_EQ(frames, motionArgs.videoFrames);
7463 }
7464}
7465
7466TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007467 prepareAxes(POSITION);
7468 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007469 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007470 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7471 // so mix these.
7472 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7473 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7474 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7475 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7476 NotifyMotionArgs motionArgs;
7477
7478 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007479 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007480 processPosition(mapper, 100, 200);
7481 processSync(mapper);
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 std::for_each(frames.begin(), frames.end(),
7484 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7485 ASSERT_EQ(frames, motionArgs.videoFrames);
7486}
7487
Arthur Hung9da14732019-09-02 16:16:58 +08007488/**
7489 * If we had defined port associations, but the viewport is not ready, the touch device would be
7490 * expected to be disabled, and it should be enabled after the viewport has found.
7491 */
7492TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007493 constexpr uint8_t hdmi2 = 1;
7494 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007495 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007496
7497 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7498
7499 addConfigurationProperty("touch.deviceType", "touchScreen");
7500 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007501 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007502
7503 ASSERT_EQ(mDevice->isEnabled(), false);
7504
7505 // Add display on hdmi2, the device should be enabled and can receive touch event.
7506 prepareSecondaryDisplay(type, hdmi2);
7507 ASSERT_EQ(mDevice->isEnabled(), true);
7508
7509 // Send a touch event.
7510 processPosition(mapper, 100, 100);
7511 processSync(mapper);
7512
7513 NotifyMotionArgs args;
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7515 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7516}
7517
Arthur Hung6cd19a42019-08-30 19:04:12 +08007518
Arthur Hung6cd19a42019-08-30 19:04:12 +08007519
Arthur Hung421eb1c2020-01-16 00:09:42 +08007520TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007521 addConfigurationProperty("touch.deviceType", "touchScreen");
7522 prepareDisplay(DISPLAY_ORIENTATION_0);
7523 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007524 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007525
7526 NotifyMotionArgs motionArgs;
7527
7528 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7529 // finger down
7530 processId(mapper, 1);
7531 processPosition(mapper, x1, y1);
7532 processSync(mapper);
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7534 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7536
7537 // finger move
7538 processId(mapper, 1);
7539 processPosition(mapper, x2, y2);
7540 processSync(mapper);
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7544
7545 // finger up.
7546 processId(mapper, -1);
7547 processSync(mapper);
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7549 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7551
7552 // new finger down
7553 processId(mapper, 1);
7554 processPosition(mapper, x3, y3);
7555 processSync(mapper);
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7559}
7560
7561/**
arthurhungcc7f9802020-04-30 17:55:40 +08007562 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7563 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007564 */
arthurhungcc7f9802020-04-30 17:55:40 +08007565TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007566 addConfigurationProperty("touch.deviceType", "touchScreen");
7567 prepareDisplay(DISPLAY_ORIENTATION_0);
7568 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007569 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007570
7571 NotifyMotionArgs motionArgs;
7572
7573 // default tool type is finger
7574 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007575 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007576 processPosition(mapper, x1, y1);
7577 processSync(mapper);
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7579 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7581
7582 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7583 processToolType(mapper, MT_TOOL_PALM);
7584 processSync(mapper);
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7586 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7587
7588 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007589 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007590 processPosition(mapper, x2, y2);
7591 processSync(mapper);
7592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7593
7594 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007595 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007596 processSync(mapper);
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7598
7599 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007600 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007601 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007602 processPosition(mapper, x3, y3);
7603 processSync(mapper);
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7607}
7608
arthurhungbf89a482020-04-17 17:37:55 +08007609/**
arthurhungcc7f9802020-04-30 17:55:40 +08007610 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7611 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007612 */
arthurhungcc7f9802020-04-30 17:55:40 +08007613TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007614 addConfigurationProperty("touch.deviceType", "touchScreen");
7615 prepareDisplay(DISPLAY_ORIENTATION_0);
7616 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7617 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7618
7619 NotifyMotionArgs motionArgs;
7620
7621 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007622 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7623 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007624 processPosition(mapper, x1, y1);
7625 processSync(mapper);
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7629
7630 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007631 processSlot(mapper, SECOND_SLOT);
7632 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007633 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007634 processSync(mapper);
7635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7636 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7637 motionArgs.action);
7638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7639
7640 // If the tool type of the first finger changes to MT_TOOL_PALM,
7641 // we expect to receive ACTION_POINTER_UP with cancel flag.
7642 processSlot(mapper, FIRST_SLOT);
7643 processId(mapper, FIRST_TRACKING_ID);
7644 processToolType(mapper, MT_TOOL_PALM);
7645 processSync(mapper);
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7647 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7648 motionArgs.action);
7649 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7650
7651 // The following MOVE events of second finger should be processed.
7652 processSlot(mapper, SECOND_SLOT);
7653 processId(mapper, SECOND_TRACKING_ID);
7654 processPosition(mapper, x2 + 1, y2 + 1);
7655 processSync(mapper);
7656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7658 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7659
7660 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7661 // it. Second finger receive move.
7662 processSlot(mapper, FIRST_SLOT);
7663 processId(mapper, INVALID_TRACKING_ID);
7664 processSync(mapper);
7665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7667 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7668
7669 // Second finger keeps moving.
7670 processSlot(mapper, SECOND_SLOT);
7671 processId(mapper, SECOND_TRACKING_ID);
7672 processPosition(mapper, x2 + 2, y2 + 2);
7673 processSync(mapper);
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7676 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7677
7678 // Second finger up.
7679 processId(mapper, INVALID_TRACKING_ID);
7680 processSync(mapper);
7681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7682 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7683 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7684}
7685
7686/**
7687 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7688 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7689 */
7690TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7691 addConfigurationProperty("touch.deviceType", "touchScreen");
7692 prepareDisplay(DISPLAY_ORIENTATION_0);
7693 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7694 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7695
7696 NotifyMotionArgs motionArgs;
7697
7698 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7699 // First finger down.
7700 processId(mapper, FIRST_TRACKING_ID);
7701 processPosition(mapper, x1, y1);
7702 processSync(mapper);
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7704 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7705 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7706
7707 // Second finger down.
7708 processSlot(mapper, SECOND_SLOT);
7709 processId(mapper, SECOND_TRACKING_ID);
7710 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007711 processSync(mapper);
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7713 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7714 motionArgs.action);
7715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7716
arthurhungcc7f9802020-04-30 17:55:40 +08007717 // If the tool type of the first finger changes to MT_TOOL_PALM,
7718 // we expect to receive ACTION_POINTER_UP with cancel flag.
7719 processSlot(mapper, FIRST_SLOT);
7720 processId(mapper, FIRST_TRACKING_ID);
7721 processToolType(mapper, MT_TOOL_PALM);
7722 processSync(mapper);
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7724 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7725 motionArgs.action);
7726 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7727
7728 // Second finger keeps moving.
7729 processSlot(mapper, SECOND_SLOT);
7730 processId(mapper, SECOND_TRACKING_ID);
7731 processPosition(mapper, x2 + 1, y2 + 1);
7732 processSync(mapper);
7733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7735
7736 // second finger becomes palm, receive cancel due to only 1 finger is active.
7737 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007738 processToolType(mapper, MT_TOOL_PALM);
7739 processSync(mapper);
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7741 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7742
arthurhungcc7f9802020-04-30 17:55:40 +08007743 // third finger down.
7744 processSlot(mapper, THIRD_SLOT);
7745 processId(mapper, THIRD_TRACKING_ID);
7746 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007747 processPosition(mapper, x3, y3);
7748 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007752 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7753
7754 // third finger move
7755 processId(mapper, THIRD_TRACKING_ID);
7756 processPosition(mapper, x3 + 1, y3 + 1);
7757 processSync(mapper);
7758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7760
7761 // first finger up, third finger receive move.
7762 processSlot(mapper, FIRST_SLOT);
7763 processId(mapper, INVALID_TRACKING_ID);
7764 processSync(mapper);
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7767 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7768
7769 // second finger up, third finger receive move.
7770 processSlot(mapper, SECOND_SLOT);
7771 processId(mapper, INVALID_TRACKING_ID);
7772 processSync(mapper);
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7775 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7776
7777 // third finger up.
7778 processSlot(mapper, THIRD_SLOT);
7779 processId(mapper, INVALID_TRACKING_ID);
7780 processSync(mapper);
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7782 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7783 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7784}
7785
7786/**
7787 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7788 * and the active finger could still be allowed to receive the events
7789 */
7790TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7791 addConfigurationProperty("touch.deviceType", "touchScreen");
7792 prepareDisplay(DISPLAY_ORIENTATION_0);
7793 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7794 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7795
7796 NotifyMotionArgs motionArgs;
7797
7798 // default tool type is finger
7799 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7800 processId(mapper, FIRST_TRACKING_ID);
7801 processPosition(mapper, x1, y1);
7802 processSync(mapper);
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7806
7807 // Second finger down.
7808 processSlot(mapper, SECOND_SLOT);
7809 processId(mapper, SECOND_TRACKING_ID);
7810 processPosition(mapper, x2, y2);
7811 processSync(mapper);
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7813 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7814 motionArgs.action);
7815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7816
7817 // If the tool type of the second finger changes to MT_TOOL_PALM,
7818 // we expect to receive ACTION_POINTER_UP with cancel flag.
7819 processId(mapper, SECOND_TRACKING_ID);
7820 processToolType(mapper, MT_TOOL_PALM);
7821 processSync(mapper);
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7823 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7824 motionArgs.action);
7825 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7826
7827 // The following MOVE event should be processed.
7828 processSlot(mapper, FIRST_SLOT);
7829 processId(mapper, FIRST_TRACKING_ID);
7830 processPosition(mapper, x1 + 1, y1 + 1);
7831 processSync(mapper);
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7834 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7835
7836 // second finger up.
7837 processSlot(mapper, SECOND_SLOT);
7838 processId(mapper, INVALID_TRACKING_ID);
7839 processSync(mapper);
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7842
7843 // first finger keep moving
7844 processSlot(mapper, FIRST_SLOT);
7845 processId(mapper, FIRST_TRACKING_ID);
7846 processPosition(mapper, x1 + 2, y1 + 2);
7847 processSync(mapper);
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7850
7851 // first finger up.
7852 processId(mapper, INVALID_TRACKING_ID);
7853 processSync(mapper);
7854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7855 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7856 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007857}
7858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007859// --- MultiTouchInputMapperTest_ExternalDevice ---
7860
7861class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7862protected:
Chris Yea52ade12020-08-27 16:49:20 -07007863 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007864};
7865
7866/**
7867 * Expect fallback to internal viewport if device is external and external viewport is not present.
7868 */
7869TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7870 prepareAxes(POSITION);
7871 addConfigurationProperty("touch.deviceType", "touchScreen");
7872 prepareDisplay(DISPLAY_ORIENTATION_0);
7873 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7874
7875 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7876
7877 NotifyMotionArgs motionArgs;
7878
7879 // Expect the event to be sent to the internal viewport,
7880 // because an external viewport is not present.
7881 processPosition(mapper, 100, 100);
7882 processSync(mapper);
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7884 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7885
7886 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007887 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007888 processPosition(mapper, 100, 100);
7889 processSync(mapper);
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7891 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7892}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007893
7894/**
7895 * Test touch should not work if outside of surface.
7896 */
7897class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7898protected:
7899 void halfDisplayToCenterHorizontal(int32_t orientation) {
7900 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007901 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007902
7903 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7904 internalViewport->orientation = orientation;
7905 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7906 internalViewport->logicalLeft = 0;
7907 internalViewport->logicalTop = 0;
7908 internalViewport->logicalRight = DISPLAY_HEIGHT;
7909 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7910
7911 internalViewport->physicalLeft = 0;
7912 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7913 internalViewport->physicalRight = DISPLAY_HEIGHT;
7914 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7915
7916 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7917 internalViewport->deviceHeight = DISPLAY_WIDTH;
7918 } else {
7919 internalViewport->logicalLeft = 0;
7920 internalViewport->logicalTop = 0;
7921 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7922 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7923
7924 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7925 internalViewport->physicalTop = 0;
7926 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7927 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7928
7929 internalViewport->deviceWidth = DISPLAY_WIDTH;
7930 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7931 }
7932
7933 mFakePolicy->updateViewport(internalViewport.value());
7934 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7935 }
7936
arthurhung5d547942020-12-14 17:04:45 +08007937 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
7938 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08007939 int32_t yExpected) {
7940 // touch on outside area should not work.
7941 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7942 processSync(mapper);
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7944
7945 // touch on inside area should receive the event.
7946 NotifyMotionArgs args;
7947 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7950 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7951 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7952
7953 // Reset.
7954 mapper.reset(ARBITRARY_TIME);
7955 }
7956};
7957
arthurhung5d547942020-12-14 17:04:45 +08007958TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08007959 addConfigurationProperty("touch.deviceType", "touchScreen");
7960 prepareDisplay(DISPLAY_ORIENTATION_0);
7961 prepareAxes(POSITION);
7962 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7963
7964 // Touch on center of normal display should work.
7965 const int32_t x = DISPLAY_WIDTH / 4;
7966 const int32_t y = DISPLAY_HEIGHT / 2;
7967 processPosition(mapper, toRawX(x), toRawY(y));
7968 processSync(mapper);
7969 NotifyMotionArgs args;
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7972 0.0f, 0.0f, 0.0f, 0.0f));
7973 // Reset.
7974 mapper.reset(ARBITRARY_TIME);
7975
7976 // Let physical display be different to device, and make surface and physical could be 1:1.
7977 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7978
7979 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7980 const int32_t yExpected = y;
7981 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7982}
7983
arthurhung5d547942020-12-14 17:04:45 +08007984TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08007985 addConfigurationProperty("touch.deviceType", "touchScreen");
7986 prepareDisplay(DISPLAY_ORIENTATION_0);
7987 prepareAxes(POSITION);
7988 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7989
7990 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7991 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7992
7993 const int32_t x = DISPLAY_WIDTH / 4;
7994 const int32_t y = DISPLAY_HEIGHT / 2;
7995
7996 // expect x/y = swap x/y then reverse y.
7997 const int32_t xExpected = y;
7998 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7999 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8000}
8001
arthurhung5d547942020-12-14 17:04:45 +08008002TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008003 addConfigurationProperty("touch.deviceType", "touchScreen");
8004 prepareDisplay(DISPLAY_ORIENTATION_0);
8005 prepareAxes(POSITION);
8006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8007
8008 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8009 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8010
8011 const int32_t x = DISPLAY_WIDTH / 4;
8012 const int32_t y = DISPLAY_HEIGHT / 2;
8013
8014 // expect x/y = swap x/y then reverse x.
8015 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8016 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8017 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8018}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008019
8020TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8021 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8022 std::shared_ptr<FakePointerController> fakePointerController =
8023 std::make_shared<FakePointerController>();
8024 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8025 fakePointerController->setPosition(0, 0);
8026 fakePointerController->setButtonState(0);
8027
8028 // prepare device and capture
8029 prepareDisplay(DISPLAY_ORIENTATION_0);
8030 prepareAxes(POSITION | ID | SLOT);
8031 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8032 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8033 mFakePolicy->setPointerCapture(true);
8034 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8035 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8036
8037 // captured touchpad should be a touchpad source
8038 NotifyDeviceResetArgs resetArgs;
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8040 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8041
Chris Yef74dc422020-09-02 22:41:50 -07008042 InputDeviceInfo deviceInfo;
8043 mDevice->getDeviceInfo(&deviceInfo);
8044
8045 const InputDeviceInfo::MotionRange* relRangeX =
8046 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8047 ASSERT_NE(relRangeX, nullptr);
8048 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8049 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8050 const InputDeviceInfo::MotionRange* relRangeY =
8051 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8052 ASSERT_NE(relRangeY, nullptr);
8053 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8054 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8055
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008056 // run captured pointer tests - note that this is unscaled, so input listener events should be
8057 // identical to what the hardware sends (accounting for any
8058 // calibration).
8059 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008060 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008061 processId(mapper, 1);
8062 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8063 processKey(mapper, BTN_TOUCH, 1);
8064 processSync(mapper);
8065
8066 // expect coord[0] to contain initial location of touch 0
8067 NotifyMotionArgs args;
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8070 ASSERT_EQ(1U, args.pointerCount);
8071 ASSERT_EQ(0, args.pointerProperties[0].id);
8072 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8073 ASSERT_NO_FATAL_FAILURE(
8074 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8075
8076 // FINGER 1 DOWN
8077 processSlot(mapper, 1);
8078 processId(mapper, 2);
8079 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8080 processSync(mapper);
8081
8082 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008084 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8085 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008086 ASSERT_EQ(2U, args.pointerCount);
8087 ASSERT_EQ(0, args.pointerProperties[0].id);
8088 ASSERT_EQ(1, args.pointerProperties[1].id);
8089 ASSERT_NO_FATAL_FAILURE(
8090 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8091 ASSERT_NO_FATAL_FAILURE(
8092 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8093
8094 // FINGER 1 MOVE
8095 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8096 processSync(mapper);
8097
8098 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8099 // from move
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8102 ASSERT_NO_FATAL_FAILURE(
8103 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8104 ASSERT_NO_FATAL_FAILURE(
8105 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8106
8107 // FINGER 0 MOVE
8108 processSlot(mapper, 0);
8109 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8110 processSync(mapper);
8111
8112 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8115 ASSERT_NO_FATAL_FAILURE(
8116 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8117 ASSERT_NO_FATAL_FAILURE(
8118 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8119
8120 // BUTTON DOWN
8121 processKey(mapper, BTN_LEFT, 1);
8122 processSync(mapper);
8123
8124 // touchinputmapper design sends a move before button press
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8128 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8129
8130 // BUTTON UP
8131 processKey(mapper, BTN_LEFT, 0);
8132 processSync(mapper);
8133
8134 // touchinputmapper design sends a move after button release
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8136 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8139
8140 // FINGER 0 UP
8141 processId(mapper, -1);
8142 processSync(mapper);
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8145
8146 // FINGER 1 MOVE
8147 processSlot(mapper, 1);
8148 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8149 processSync(mapper);
8150
8151 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8154 ASSERT_EQ(1U, args.pointerCount);
8155 ASSERT_EQ(1, args.pointerProperties[0].id);
8156 ASSERT_NO_FATAL_FAILURE(
8157 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8158
8159 // FINGER 1 UP
8160 processId(mapper, -1);
8161 processKey(mapper, BTN_TOUCH, 0);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8165
8166 // non captured touchpad should be a mouse source
8167 mFakePolicy->setPointerCapture(false);
8168 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8170 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8171}
8172
8173TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8174 std::shared_ptr<FakePointerController> fakePointerController =
8175 std::make_shared<FakePointerController>();
8176 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8177 fakePointerController->setPosition(0, 0);
8178 fakePointerController->setButtonState(0);
8179
8180 // prepare device and capture
8181 prepareDisplay(DISPLAY_ORIENTATION_0);
8182 prepareAxes(POSITION | ID | SLOT);
8183 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8184 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8185 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8186 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8187 // run uncaptured pointer tests - pushes out generic events
8188 // FINGER 0 DOWN
8189 processId(mapper, 3);
8190 processPosition(mapper, 100, 100);
8191 processKey(mapper, BTN_TOUCH, 1);
8192 processSync(mapper);
8193
8194 // start at (100,100), cursor should be at (0,0) * scale
8195 NotifyMotionArgs args;
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8198 ASSERT_NO_FATAL_FAILURE(
8199 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8200
8201 // FINGER 0 MOVE
8202 processPosition(mapper, 200, 200);
8203 processSync(mapper);
8204
8205 // compute scaling to help with touch position checking
8206 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8207 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8208 float scale =
8209 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8210
8211 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8215 0, 0, 0, 0, 0, 0, 0));
8216}
8217
8218TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8219 std::shared_ptr<FakePointerController> fakePointerController =
8220 std::make_shared<FakePointerController>();
8221
8222 prepareDisplay(DISPLAY_ORIENTATION_0);
8223 prepareAxes(POSITION | ID | SLOT);
8224 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8225 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8226 mFakePolicy->setPointerCapture(false);
8227 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8228
8229 // uncaptured touchpad should be a pointer device
8230 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8231
8232 // captured touchpad should be a touchpad device
8233 mFakePolicy->setPointerCapture(true);
8234 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8235 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8236}
8237
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238} // namespace android