blob: 409c62a0e2cbef4db2223cc83ca4337e5043e0e6 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Kim Low03ea0352020-11-06 12:45:07 -080017#include <BatteryInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070018#include <CursorInputMapper.h>
19#include <InputDevice.h>
20#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.h>
Chris Yef59a2f42020-10-16 12:55:26 -070026#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <SingleTouchInputMapper.h>
28#include <SwitchInputMapper.h>
29#include <TestInputListener.h>
30#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080031#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000032#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070033#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080035#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036#include <math.h>
37
Michael Wright17db18e2020-06-26 20:51:44 +010038#include <memory>
Michael Wrightdde67b82020-10-27 16:09:22 +000039#include "input/DisplayViewport.h"
40#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010041
Michael Wrightd02c5b62014-02-10 15:10:22 -080042namespace android {
43
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070044using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070045using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070046
47// Timeout for waiting for an expected event
48static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
49
Michael Wrightd02c5b62014-02-10 15:10:22 -080050// An arbitrary time value.
51static const nsecs_t ARBITRARY_TIME = 1234;
52
53// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080054static constexpr int32_t DISPLAY_ID = 0;
55static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
56static constexpr int32_t DISPLAY_WIDTH = 480;
57static constexpr int32_t DISPLAY_HEIGHT = 800;
58static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
59static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
60static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070061static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070062static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080063
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t FIRST_SLOT = 0;
65static constexpr int32_t SECOND_SLOT = 1;
66static constexpr int32_t THIRD_SLOT = 2;
67static constexpr int32_t INVALID_TRACKING_ID = -1;
68static constexpr int32_t FIRST_TRACKING_ID = 0;
69static constexpr int32_t SECOND_TRACKING_ID = 1;
70static constexpr int32_t THIRD_TRACKING_ID = 2;
Kim Low03ea0352020-11-06 12:45:07 -080071static constexpr int32_t BATTERY_STATUS = 4;
72static constexpr int32_t BATTERY_CAPACITY = 66;
arthurhungcc7f9802020-04-30 17:55:40 +080073
Michael Wrightd02c5b62014-02-10 15:10:22 -080074// Error tolerance for floating point assertions.
75static const float EPSILON = 0.001f;
76
77template<typename T>
78static inline T min(T a, T b) {
79 return a < b ? a : b;
80}
81
82static inline float avg(float x, float y) {
83 return (x + y) / 2;
84}
85
86
87// --- FakePointerController ---
88
89class FakePointerController : public PointerControllerInterface {
90 bool mHaveBounds;
91 float mMinX, mMinY, mMaxX, mMaxY;
92 float mX, mY;
93 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080094 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
Michael Wrightd02c5b62014-02-10 15:10:22 -080096public:
97 FakePointerController() :
98 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080099 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100 }
101
Michael Wright17db18e2020-06-26 20:51:44 +0100102 virtual ~FakePointerController() {}
103
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104 void setBounds(float minX, float minY, float maxX, float maxY) {
105 mHaveBounds = true;
106 mMinX = minX;
107 mMinY = minY;
108 mMaxX = maxX;
109 mMaxY = maxY;
110 }
111
Chris Yea52ade12020-08-27 16:49:20 -0700112 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113 mX = x;
114 mY = y;
115 }
116
Chris Yea52ade12020-08-27 16:49:20 -0700117 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Chris Yea52ade12020-08-27 16:49:20 -0700119 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120
Chris Yea52ade12020-08-27 16:49:20 -0700121 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122 *outX = mX;
123 *outY = mY;
124 }
125
Chris Yea52ade12020-08-27 16:49:20 -0700126 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800127
Chris Yea52ade12020-08-27 16:49:20 -0700128 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800129 mDisplayId = viewport.displayId;
130 }
131
Arthur Hung7c645402019-01-25 17:45:42 +0800132 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
133 return mSpotsByDisplay;
134 }
135
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136private:
Chris Yea52ade12020-08-27 16:49:20 -0700137 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138 *outMinX = mMinX;
139 *outMinY = mMinY;
140 *outMaxX = mMaxX;
141 *outMaxY = mMaxY;
142 return mHaveBounds;
143 }
144
Chris Yea52ade12020-08-27 16:49:20 -0700145 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146 mX += deltaX;
147 if (mX < mMinX) mX = mMinX;
148 if (mX > mMaxX) mX = mMaxX;
149 mY += deltaY;
150 if (mY < mMinY) mY = mMinY;
151 if (mY > mMaxY) mY = mMaxY;
152 }
153
Chris Yea52ade12020-08-27 16:49:20 -0700154 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155
Chris Yea52ade12020-08-27 16:49:20 -0700156 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157
Chris Yea52ade12020-08-27 16:49:20 -0700158 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159
Chris Yea52ade12020-08-27 16:49:20 -0700160 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
161 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800162 std::vector<int32_t> newSpots;
163 // Add spots for fingers that are down.
164 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
165 uint32_t id = idBits.clearFirstMarkedBit();
166 newSpots.push_back(id);
167 }
168
169 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 }
171
Chris Yea52ade12020-08-27 16:49:20 -0700172 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800173
174 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175};
176
177
178// --- FakeInputReaderPolicy ---
179
180class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700181 std::mutex mLock;
182 std::condition_variable mDevicesChangedCondition;
183
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100185 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700186 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
187 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100188 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700189 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191protected:
Chris Yea52ade12020-08-27 16:49:20 -0700192 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193
194public:
195 FakeInputReaderPolicy() {
196 }
197
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700198 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800199 waitForInputDevices([](bool devicesChanged) {
200 if (!devicesChanged) {
201 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
202 }
203 });
204 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700205
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800206 void assertInputDevicesNotChanged() {
207 waitForInputDevices([](bool devicesChanged) {
208 if (devicesChanged) {
209 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
210 }
211 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700212 }
213
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700214 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100215 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100216 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700217 }
218
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700219 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
220 return mConfig.getDisplayViewportByUniqueId(uniqueId);
221 }
222 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
223 return mConfig.getDisplayViewportByType(type);
224 }
225
226 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
227 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700228 }
229
230 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000231 bool isActive, const std::string& uniqueId,
232 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
233 const DisplayViewport viewport =
234 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
235 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700236 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100237 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 }
239
Arthur Hung6cd19a42019-08-30 19:04:12 +0800240 bool updateViewport(const DisplayViewport& viewport) {
241 size_t count = mViewports.size();
242 for (size_t i = 0; i < count; i++) {
243 const DisplayViewport& currentViewport = mViewports[i];
244 if (currentViewport.displayId == viewport.displayId) {
245 mViewports[i] = viewport;
246 mConfig.setDisplayViewports(mViewports);
247 return true;
248 }
249 }
250 // no viewport found.
251 return false;
252 }
253
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100254 void addExcludedDeviceName(const std::string& deviceName) {
255 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 }
257
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700258 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
259 mConfig.portAssociations.insert({inputPort, displayPort});
260 }
261
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000262 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700263
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000264 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700265
Michael Wright17db18e2020-06-26 20:51:44 +0100266 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
267 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 }
269
270 const InputReaderConfiguration* getReaderConfiguration() const {
271 return &mConfig;
272 }
273
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800274 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 return mInputDevices;
276 }
277
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100278 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700279 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700280 return transform;
281 }
282
283 void setTouchAffineTransformation(const TouchAffineTransformation t) {
284 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800285 }
286
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800287 void setPointerCapture(bool enabled) {
288 mConfig.pointerCapture = enabled;
289 }
290
Arthur Hung7c645402019-01-25 17:45:42 +0800291 void setShowTouches(bool enabled) {
292 mConfig.showTouches = enabled;
293 }
294
Garfield Tan888a6a42020-01-09 11:39:16 -0800295 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
296 mConfig.defaultPointerDisplayId = pointerDisplayId;
297 }
298
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800299 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
300
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700302 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000303 int32_t orientation, bool isActive,
304 const std::string& uniqueId,
305 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700306 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
307 || orientation == DISPLAY_ORIENTATION_270);
308 DisplayViewport v;
309 v.displayId = displayId;
310 v.orientation = orientation;
311 v.logicalLeft = 0;
312 v.logicalTop = 0;
313 v.logicalRight = isRotated ? height : width;
314 v.logicalBottom = isRotated ? width : height;
315 v.physicalLeft = 0;
316 v.physicalTop = 0;
317 v.physicalRight = isRotated ? height : width;
318 v.physicalBottom = isRotated ? width : height;
319 v.deviceWidth = isRotated ? height : width;
320 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000321 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700322 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700323 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100324 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700325 return v;
326 }
327
Chris Yea52ade12020-08-27 16:49:20 -0700328 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 *outConfig = mConfig;
330 }
331
Chris Yea52ade12020-08-27 16:49:20 -0700332 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100333 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Chris Yea52ade12020-08-27 16:49:20 -0700336 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700337 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700339 mInputDevicesChanged = true;
340 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 }
342
Chris Yea52ade12020-08-27 16:49:20 -0700343 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
344 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700345 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 }
347
Chris Yea52ade12020-08-27 16:49:20 -0700348 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800349
350 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
351 std::unique_lock<std::mutex> lock(mLock);
352 base::ScopedLockAssertion assumeLocked(mLock);
353
354 const bool devicesChanged =
355 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
356 return mInputDevicesChanged;
357 });
358 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
359 mInputDevicesChanged = false;
360 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361};
362
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363// --- FakeEventHub ---
364
365class FakeEventHub : public EventHubInterface {
366 struct KeyInfo {
367 int32_t keyCode;
368 uint32_t flags;
369 };
370
Chris Yef59a2f42020-10-16 12:55:26 -0700371 struct SensorInfo {
372 InputDeviceSensorType sensorType;
373 int32_t sensorDataIndex;
374 };
375
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 struct Device {
377 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700378 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379 PropertyMap configuration;
380 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
381 KeyedVector<int, bool> relativeAxes;
382 KeyedVector<int32_t, int32_t> keyCodeStates;
383 KeyedVector<int32_t, int32_t> scanCodeStates;
384 KeyedVector<int32_t, int32_t> switchStates;
385 KeyedVector<int32_t, int32_t> absoluteAxisValue;
386 KeyedVector<int32_t, KeyInfo> keysByScanCode;
387 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
388 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700389 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
390 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800391 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700392 bool enabled;
393
394 status_t enable() {
395 enabled = true;
396 return OK;
397 }
398
399 status_t disable() {
400 enabled = false;
401 return OK;
402 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403
Chris Ye1b0c7342020-07-28 21:57:03 -0700404 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 };
406
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700407 std::mutex mLock;
408 std::condition_variable mEventsCondition;
409
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100411 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000412 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600413 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000414 std::vector<int32_t> mVibrators = {0, 1};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700416public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 virtual ~FakeEventHub() {
418 for (size_t i = 0; i < mDevices.size(); i++) {
419 delete mDevices.valueAt(i);
420 }
421 }
422
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 FakeEventHub() { }
424
Chris Ye1b0c7342020-07-28 21:57:03 -0700425 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426 Device* device = new Device(classes);
427 device->identifier.name = name;
428 mDevices.add(deviceId, device);
429
430 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
431 }
432
433 void removeDevice(int32_t deviceId) {
434 delete mDevices.valueFor(deviceId);
435 mDevices.removeItem(deviceId);
436
437 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
438 }
439
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700440 bool isDeviceEnabled(int32_t deviceId) {
441 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700442 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700443 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
444 return false;
445 }
446 return device->enabled;
447 }
448
449 status_t enableDevice(int32_t deviceId) {
450 status_t result;
451 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700452 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
454 return BAD_VALUE;
455 }
456 if (device->enabled) {
457 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
458 return OK;
459 }
460 result = device->enable();
461 return result;
462 }
463
464 status_t disableDevice(int32_t deviceId) {
465 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700466 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700467 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
468 return BAD_VALUE;
469 }
470 if (!device->enabled) {
471 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
472 return OK;
473 }
474 return device->disable();
475 }
476
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477 void finishDeviceScan() {
478 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
479 }
480
481 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
482 Device* device = getDevice(deviceId);
483 device->configuration.addProperty(key, value);
484 }
485
486 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
487 Device* device = getDevice(deviceId);
488 device->configuration.addAll(configuration);
489 }
490
491 void addAbsoluteAxis(int32_t deviceId, int axis,
492 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
493 Device* device = getDevice(deviceId);
494
495 RawAbsoluteAxisInfo info;
496 info.valid = true;
497 info.minValue = minValue;
498 info.maxValue = maxValue;
499 info.flat = flat;
500 info.fuzz = fuzz;
501 info.resolution = resolution;
502 device->absoluteAxes.add(axis, info);
503 }
504
505 void addRelativeAxis(int32_t deviceId, int32_t axis) {
506 Device* device = getDevice(deviceId);
507 device->relativeAxes.add(axis, true);
508 }
509
510 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
511 Device* device = getDevice(deviceId);
512 device->keyCodeStates.replaceValueFor(keyCode, state);
513 }
514
515 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
516 Device* device = getDevice(deviceId);
517 device->scanCodeStates.replaceValueFor(scanCode, state);
518 }
519
520 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
521 Device* device = getDevice(deviceId);
522 device->switchStates.replaceValueFor(switchCode, state);
523 }
524
525 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
526 Device* device = getDevice(deviceId);
527 device->absoluteAxisValue.replaceValueFor(axis, value);
528 }
529
530 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
531 int32_t keyCode, uint32_t flags) {
532 Device* device = getDevice(deviceId);
533 KeyInfo info;
534 info.keyCode = keyCode;
535 info.flags = flags;
536 if (scanCode) {
537 device->keysByScanCode.add(scanCode, info);
538 }
539 if (usageCode) {
540 device->keysByUsageCode.add(usageCode, info);
541 }
542 }
543
544 void addLed(int32_t deviceId, int32_t led, bool initialState) {
545 Device* device = getDevice(deviceId);
546 device->leds.add(led, initialState);
547 }
548
Chris Yef59a2f42020-10-16 12:55:26 -0700549 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
550 int32_t sensorDataIndex) {
551 Device* device = getDevice(deviceId);
552 SensorInfo info;
553 info.sensorType = sensorType;
554 info.sensorDataIndex = sensorDataIndex;
555 device->sensorsByAbsCode.emplace(absCode, info);
556 }
557
558 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
559 Device* device = getDevice(deviceId);
560 typename BitArray<MSC_MAX>::Buffer buffer;
561 buffer[mscEvent / 32] = 1 << mscEvent % 32;
562 device->mscBitmask.loadFromBuffer(buffer);
563 }
564
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 bool getLedState(int32_t deviceId, int32_t led) {
566 Device* device = getDevice(deviceId);
567 return device->leds.valueFor(led);
568 }
569
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100570 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 return mExcludedDevices;
572 }
573
574 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
575 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800576 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
579 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
580 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700581 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582 RawEvent event;
583 event.when = when;
584 event.deviceId = deviceId;
585 event.type = type;
586 event.code = code;
587 event.value = value;
588 mEvents.push_back(event);
589
590 if (type == EV_ABS) {
591 setAbsoluteAxisValue(deviceId, code, value);
592 }
593 }
594
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600595 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
596 std::vector<TouchVideoFrame>> videoFrames) {
597 mVideoFrames = std::move(videoFrames);
598 }
599
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700601 std::unique_lock<std::mutex> lock(mLock);
602 base::ScopedLockAssertion assumeLocked(mLock);
603 const bool queueIsEmpty =
604 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
605 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
606 if (!queueIsEmpty) {
607 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
608 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
611private:
612 Device* getDevice(int32_t deviceId) const {
613 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100614 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 }
616
Chris Yea52ade12020-08-27 16:49:20 -0700617 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700619 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 }
621
Chris Yea52ade12020-08-27 16:49:20 -0700622 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 Device* device = getDevice(deviceId);
624 return device ? device->identifier : InputDeviceIdentifier();
625 }
626
Chris Yea52ade12020-08-27 16:49:20 -0700627 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628
Chris Yea52ade12020-08-27 16:49:20 -0700629 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 Device* device = getDevice(deviceId);
631 if (device) {
632 *outConfiguration = device->configuration;
633 }
634 }
635
Chris Yea52ade12020-08-27 16:49:20 -0700636 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
637 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800639 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 ssize_t index = device->absoluteAxes.indexOfKey(axis);
641 if (index >= 0) {
642 *outAxisInfo = device->absoluteAxes.valueAt(index);
643 return OK;
644 }
645 }
646 outAxisInfo->clear();
647 return -1;
648 }
649
Chris Yea52ade12020-08-27 16:49:20 -0700650 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 Device* device = getDevice(deviceId);
652 if (device) {
653 return device->relativeAxes.indexOfKey(axis) >= 0;
654 }
655 return false;
656 }
657
Chris Yea52ade12020-08-27 16:49:20 -0700658 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659
Chris Yef59a2f42020-10-16 12:55:26 -0700660 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
661 Device* device = getDevice(deviceId);
662 if (device) {
663 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
664 }
665 return false;
666 }
667
Chris Yea52ade12020-08-27 16:49:20 -0700668 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
669 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 Device* device = getDevice(deviceId);
671 if (device) {
672 const KeyInfo* key = getKey(device, scanCode, usageCode);
673 if (key) {
674 if (outKeycode) {
675 *outKeycode = key->keyCode;
676 }
677 if (outFlags) {
678 *outFlags = key->flags;
679 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700680 if (outMetaState) {
681 *outMetaState = metaState;
682 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 return OK;
684 }
685 }
686 return NAME_NOT_FOUND;
687 }
688
689 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
690 if (usageCode) {
691 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
692 if (index >= 0) {
693 return &device->keysByUsageCode.valueAt(index);
694 }
695 }
696 if (scanCode) {
697 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
698 if (index >= 0) {
699 return &device->keysByScanCode.valueAt(index);
700 }
701 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700702 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 }
704
Chris Yea52ade12020-08-27 16:49:20 -0700705 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706
Chris Yef59a2f42020-10-16 12:55:26 -0700707 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
708 int32_t absCode) {
709 Device* device = getDevice(deviceId);
710 if (!device) {
711 return Errorf("Sensor device not found.");
712 }
713 auto it = device->sensorsByAbsCode.find(absCode);
714 if (it == device->sensorsByAbsCode.end()) {
715 return Errorf("Sensor map not found.");
716 }
717 const SensorInfo& info = it->second;
718 return std::make_pair(info.sensorType, info.sensorDataIndex);
719 }
720
Chris Yea52ade12020-08-27 16:49:20 -0700721 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 mExcludedDevices = devices;
723 }
724
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000725 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
726 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000728 const size_t filledSize = std::min(mEvents.size(), bufferSize);
729 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
730
731 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700732 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000733 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 }
735
Chris Yea52ade12020-08-27 16:49:20 -0700736 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600737 auto it = mVideoFrames.find(deviceId);
738 if (it != mVideoFrames.end()) {
739 std::vector<TouchVideoFrame> frames = std::move(it->second);
740 mVideoFrames.erase(deviceId);
741 return frames;
742 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800743 return {};
744 }
745
Chris Yea52ade12020-08-27 16:49:20 -0700746 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 Device* device = getDevice(deviceId);
748 if (device) {
749 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
750 if (index >= 0) {
751 return device->scanCodeStates.valueAt(index);
752 }
753 }
754 return AKEY_STATE_UNKNOWN;
755 }
756
Chris Yea52ade12020-08-27 16:49:20 -0700757 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 Device* device = getDevice(deviceId);
759 if (device) {
760 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
761 if (index >= 0) {
762 return device->keyCodeStates.valueAt(index);
763 }
764 }
765 return AKEY_STATE_UNKNOWN;
766 }
767
Chris Yea52ade12020-08-27 16:49:20 -0700768 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769 Device* device = getDevice(deviceId);
770 if (device) {
771 ssize_t index = device->switchStates.indexOfKey(sw);
772 if (index >= 0) {
773 return device->switchStates.valueAt(index);
774 }
775 }
776 return AKEY_STATE_UNKNOWN;
777 }
778
Chris Yea52ade12020-08-27 16:49:20 -0700779 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
780 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781 Device* device = getDevice(deviceId);
782 if (device) {
783 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
784 if (index >= 0) {
785 *outValue = device->absoluteAxisValue.valueAt(index);
786 return OK;
787 }
788 }
789 *outValue = 0;
790 return -1;
791 }
792
Chris Yea52ade12020-08-27 16:49:20 -0700793 // Return true if the device has non-empty key layout.
794 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
795 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796 bool result = false;
797 Device* device = getDevice(deviceId);
798 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700799 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 for (size_t i = 0; i < numCodes; i++) {
801 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
802 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
803 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 }
805 }
806 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
807 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
808 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 }
810 }
811 }
812 }
813 return result;
814 }
815
Chris Yea52ade12020-08-27 16:49:20 -0700816 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 Device* device = getDevice(deviceId);
818 if (device) {
819 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
820 return index >= 0;
821 }
822 return false;
823 }
824
Chris Yea52ade12020-08-27 16:49:20 -0700825 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 Device* device = getDevice(deviceId);
827 return device && device->leds.indexOfKey(led) >= 0;
828 }
829
Chris Yea52ade12020-08-27 16:49:20 -0700830 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 Device* device = getDevice(deviceId);
832 if (device) {
833 ssize_t index = device->leds.indexOfKey(led);
834 if (index >= 0) {
835 device->leds.replaceValueAt(led, on);
836 } else {
837 ADD_FAILURE()
838 << "Attempted to set the state of an LED that the EventHub declared "
839 "was not present. led=" << led;
840 }
841 }
842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 void getVirtualKeyDefinitions(
845 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846 outVirtualKeys.clear();
847
848 Device* device = getDevice(deviceId);
849 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800850 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 }
852 }
853
Chris Yea52ade12020-08-27 16:49:20 -0700854 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700855 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 }
857
Chris Yea52ade12020-08-27 16:49:20 -0700858 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859 return false;
860 }
861
Chris Yea52ade12020-08-27 16:49:20 -0700862 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863
Chris Yea52ade12020-08-27 16:49:20 -0700864 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865
Chris Ye87143712020-11-10 05:05:58 +0000866 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
867
Kim Low03ea0352020-11-06 12:45:07 -0800868 std::optional<int32_t> getBatteryCapacity(int32_t) const override { return BATTERY_CAPACITY; }
869
870 std::optional<int32_t> getBatteryStatus(int32_t) const override { return BATTERY_STATUS; }
871
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100872 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 return false;
874 }
875
Chris Yea52ade12020-08-27 16:49:20 -0700876 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877
Chris Yea52ade12020-08-27 16:49:20 -0700878 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879
Chris Yea52ade12020-08-27 16:49:20 -0700880 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881
Chris Yea52ade12020-08-27 16:49:20 -0700882 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883};
884
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885// --- FakeInputMapper ---
886
887class FakeInputMapper : public InputMapper {
888 uint32_t mSources;
889 int32_t mKeyboardType;
890 int32_t mMetaState;
891 KeyedVector<int32_t, int32_t> mKeyCodeStates;
892 KeyedVector<int32_t, int32_t> mScanCodeStates;
893 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800894 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700896 std::mutex mLock;
897 std::condition_variable mStateChangedCondition;
898 bool mConfigureWasCalled GUARDED_BY(mLock);
899 bool mResetWasCalled GUARDED_BY(mLock);
900 bool mProcessWasCalled GUARDED_BY(mLock);
901 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902
Arthur Hungc23540e2018-11-29 20:42:11 +0800903 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800905 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
906 : InputMapper(deviceContext),
907 mSources(sources),
908 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800910 mConfigureWasCalled(false),
911 mResetWasCalled(false),
912 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913
Chris Yea52ade12020-08-27 16:49:20 -0700914 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
916 void setKeyboardType(int32_t keyboardType) {
917 mKeyboardType = keyboardType;
918 }
919
920 void setMetaState(int32_t metaState) {
921 mMetaState = metaState;
922 }
923
924 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700925 std::unique_lock<std::mutex> lock(mLock);
926 base::ScopedLockAssertion assumeLocked(mLock);
927 const bool configureCalled =
928 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
929 return mConfigureWasCalled;
930 });
931 if (!configureCalled) {
932 FAIL() << "Expected configure() to have been called.";
933 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 mConfigureWasCalled = false;
935 }
936
937 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700938 std::unique_lock<std::mutex> lock(mLock);
939 base::ScopedLockAssertion assumeLocked(mLock);
940 const bool resetCalled =
941 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
942 return mResetWasCalled;
943 });
944 if (!resetCalled) {
945 FAIL() << "Expected reset() to have been called.";
946 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 mResetWasCalled = false;
948 }
949
Yi Kong9b14ac62018-07-17 13:48:38 -0700950 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700951 std::unique_lock<std::mutex> lock(mLock);
952 base::ScopedLockAssertion assumeLocked(mLock);
953 const bool processCalled =
954 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
955 return mProcessWasCalled;
956 });
957 if (!processCalled) {
958 FAIL() << "Expected process() to have been called.";
959 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 if (outLastEvent) {
961 *outLastEvent = mLastEvent;
962 }
963 mProcessWasCalled = false;
964 }
965
966 void setKeyCodeState(int32_t keyCode, int32_t state) {
967 mKeyCodeStates.replaceValueFor(keyCode, state);
968 }
969
970 void setScanCodeState(int32_t scanCode, int32_t state) {
971 mScanCodeStates.replaceValueFor(scanCode, state);
972 }
973
974 void setSwitchState(int32_t switchCode, int32_t state) {
975 mSwitchStates.replaceValueFor(switchCode, state);
976 }
977
978 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800979 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 }
981
982private:
Chris Yea52ade12020-08-27 16:49:20 -0700983 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984
Chris Yea52ade12020-08-27 16:49:20 -0700985 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986 InputMapper::populateDeviceInfo(deviceInfo);
987
988 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
989 deviceInfo->setKeyboardType(mKeyboardType);
990 }
991 }
992
Chris Yea52ade12020-08-27 16:49:20 -0700993 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700994 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800996
997 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800998 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800999 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1000 mViewport = config->getDisplayViewportByPort(*displayPort);
1001 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002
1003 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 }
1005
Chris Yea52ade12020-08-27 16:49:20 -07001006 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001007 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001009 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 }
1011
Chris Yea52ade12020-08-27 16:49:20 -07001012 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014 mLastEvent = *rawEvent;
1015 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001016 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017 }
1018
Chris Yea52ade12020-08-27 16:49:20 -07001019 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1021 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1022 }
1023
Chris Yea52ade12020-08-27 16:49:20 -07001024 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1026 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1027 }
1028
Chris Yea52ade12020-08-27 16:49:20 -07001029 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1031 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1032 }
1033
Chris Yea52ade12020-08-27 16:49:20 -07001034 // Return true if the device has non-empty key layout.
1035 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1036 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037 for (size_t i = 0; i < numCodes; i++) {
1038 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1039 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1040 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 }
1042 }
1043 }
Chris Yea52ade12020-08-27 16:49:20 -07001044 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045 return result;
1046 }
1047
1048 virtual int32_t getMetaState() {
1049 return mMetaState;
1050 }
1051
1052 virtual void fadePointer() {
1053 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001054
1055 virtual std::optional<int32_t> getAssociatedDisplay() {
1056 if (mViewport) {
1057 return std::make_optional(mViewport->displayId);
1058 }
1059 return std::nullopt;
1060 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061};
1062
1063
1064// --- InstrumentedInputReader ---
1065
1066class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001067 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068
1069public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001070 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1071 const sp<InputReaderPolicyInterface>& policy,
1072 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001073 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001075 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001077 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001079 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001080 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 InputDeviceIdentifier identifier;
1082 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001083 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001085 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 }
1087
Prabir Pradhan28efc192019-11-05 01:10:04 +00001088 // Make the protected loopOnce method accessible to tests.
1089 using InputReader::loopOnce;
1090
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001092 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1093 const InputDeviceIdentifier& identifier)
1094 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001095 if (!mNextDevices.empty()) {
1096 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1097 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098 return device;
1099 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001100 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101 }
1102
arthurhungdcef2dc2020-08-11 14:47:50 +08001103 // --- FakeInputReaderContext ---
1104 class FakeInputReaderContext : public ContextImpl {
1105 int32_t mGlobalMetaState;
1106 bool mUpdateGlobalMetaStateWasCalled;
1107 int32_t mGeneration;
1108
1109 public:
1110 FakeInputReaderContext(InputReader* reader)
1111 : ContextImpl(reader),
1112 mGlobalMetaState(0),
1113 mUpdateGlobalMetaStateWasCalled(false),
1114 mGeneration(1) {}
1115
1116 virtual ~FakeInputReaderContext() {}
1117
1118 void assertUpdateGlobalMetaStateWasCalled() {
1119 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1120 << "Expected updateGlobalMetaState() to have been called.";
1121 mUpdateGlobalMetaStateWasCalled = false;
1122 }
1123
1124 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1125
1126 uint32_t getGeneration() { return mGeneration; }
1127
1128 void updateGlobalMetaState() override {
1129 mUpdateGlobalMetaStateWasCalled = true;
1130 ContextImpl::updateGlobalMetaState();
1131 }
1132
1133 int32_t getGlobalMetaState() override {
1134 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1135 }
1136
1137 int32_t bumpGeneration() override {
1138 mGeneration = ContextImpl::bumpGeneration();
1139 return mGeneration;
1140 }
1141 } mFakeContext;
1142
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001144
1145public:
1146 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147};
1148
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001149// --- InputReaderPolicyTest ---
1150class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001151protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001152 sp<FakeInputReaderPolicy> mFakePolicy;
1153
Chris Yea52ade12020-08-27 16:49:20 -07001154 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1155 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156};
1157
1158/**
1159 * Check that empty set of viewports is an acceptable configuration.
1160 * Also try to get internal viewport two different ways - by type and by uniqueId.
1161 *
1162 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1163 * Such configuration is not currently allowed.
1164 */
1165TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001166 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001167
1168 // We didn't add any viewports yet, so there shouldn't be any.
1169 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001170 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171 ASSERT_FALSE(internalViewport);
1172
1173 // Add an internal viewport, then clear it
1174 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001175 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001176 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177
1178 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001179 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001180 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001181 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001182
1183 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001184 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001186 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187
1188 mFakePolicy->clearViewports();
1189 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001190 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001191 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001192 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001193 ASSERT_FALSE(internalViewport);
1194}
1195
1196TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1197 const std::string internalUniqueId = "local:0";
1198 const std::string externalUniqueId = "local:1";
1199 const std::string virtualUniqueId1 = "virtual:2";
1200 const std::string virtualUniqueId2 = "virtual:3";
1201 constexpr int32_t virtualDisplayId1 = 2;
1202 constexpr int32_t virtualDisplayId2 = 3;
1203
1204 // Add an internal viewport
1205 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001206 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1207 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001208 // Add an external viewport
1209 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001210 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1211 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212 // Add an virtual viewport
1213 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001214 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1215 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001216 // Add another virtual viewport
1217 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001218 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1219 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001220
1221 // Check matching by type for internal
1222 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001223 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001224 ASSERT_TRUE(internalViewport);
1225 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1226
1227 // Check matching by type for external
1228 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001229 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001230 ASSERT_TRUE(externalViewport);
1231 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1232
1233 // Check matching by uniqueId for virtual viewport #1
1234 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001235 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001236 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001237 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1239 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1240
1241 // Check matching by uniqueId for virtual viewport #2
1242 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001243 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001244 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001245 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1247 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1248}
1249
1250
1251/**
1252 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1253 * that lookup works by checking display id.
1254 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1255 */
1256TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1257 const std::string uniqueId1 = "uniqueId1";
1258 const std::string uniqueId2 = "uniqueId2";
1259 constexpr int32_t displayId1 = 2;
1260 constexpr int32_t displayId2 = 3;
1261
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001262 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1263 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264 for (const ViewportType& type : types) {
1265 mFakePolicy->clearViewports();
1266 // Add a viewport
1267 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001268 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1269 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270 // Add another viewport
1271 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001272 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1273 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001274
1275 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001276 std::optional<DisplayViewport> viewport1 =
1277 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001278 ASSERT_TRUE(viewport1);
1279 ASSERT_EQ(displayId1, viewport1->displayId);
1280 ASSERT_EQ(type, viewport1->type);
1281
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001282 std::optional<DisplayViewport> viewport2 =
1283 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001284 ASSERT_TRUE(viewport2);
1285 ASSERT_EQ(displayId2, viewport2->displayId);
1286 ASSERT_EQ(type, viewport2->type);
1287
1288 // When there are multiple viewports of the same kind, and uniqueId is not specified
1289 // in the call to getDisplayViewport, then that situation is not supported.
1290 // The viewports can be stored in any order, so we cannot rely on the order, since that
1291 // is just implementation detail.
1292 // However, we can check that it still returns *a* viewport, we just cannot assert
1293 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001294 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001295 ASSERT_TRUE(someViewport);
1296 }
1297}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001298
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001299/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001300 * When we have multiple internal displays make sure we always return the default display when
1301 * querying by type.
1302 */
1303TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1304 const std::string uniqueId1 = "uniqueId1";
1305 const std::string uniqueId2 = "uniqueId2";
1306 constexpr int32_t nonDefaultDisplayId = 2;
1307 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1308 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1309
1310 // Add the default display first and ensure it gets returned.
1311 mFakePolicy->clearViewports();
1312 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001313 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001314 ViewportType::INTERNAL);
1315 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001316 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001317 ViewportType::INTERNAL);
1318
1319 std::optional<DisplayViewport> viewport =
1320 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1321 ASSERT_TRUE(viewport);
1322 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1323 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1324
1325 // Add the default display second to make sure order doesn't matter.
1326 mFakePolicy->clearViewports();
1327 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001328 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001329 ViewportType::INTERNAL);
1330 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001331 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001332 ViewportType::INTERNAL);
1333
1334 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1335 ASSERT_TRUE(viewport);
1336 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1337 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1338}
1339
1340/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001341 * Check getDisplayViewportByPort
1342 */
1343TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001344 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001345 const std::string uniqueId1 = "uniqueId1";
1346 const std::string uniqueId2 = "uniqueId2";
1347 constexpr int32_t displayId1 = 1;
1348 constexpr int32_t displayId2 = 2;
1349 const uint8_t hdmi1 = 0;
1350 const uint8_t hdmi2 = 1;
1351 const uint8_t hdmi3 = 2;
1352
1353 mFakePolicy->clearViewports();
1354 // Add a viewport that's associated with some display port that's not of interest.
1355 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001356 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1357 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001358 // Add another viewport, connected to HDMI1 port
1359 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001360 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1361 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001362
1363 // Check that correct display viewport was returned by comparing the display ports.
1364 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1365 ASSERT_TRUE(hdmi1Viewport);
1366 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1367 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1368
1369 // Check that we can still get the same viewport using the uniqueId
1370 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1371 ASSERT_TRUE(hdmi1Viewport);
1372 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1373 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1374 ASSERT_EQ(type, hdmi1Viewport->type);
1375
1376 // Check that we cannot find a port with "HDMI2", because we never added one
1377 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1378 ASSERT_FALSE(hdmi2Viewport);
1379}
1380
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381// --- InputReaderTest ---
1382
1383class InputReaderTest : public testing::Test {
1384protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001385 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001387 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001388 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001389
Chris Yea52ade12020-08-27 16:49:20 -07001390 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001391 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001392 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001393 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394
Prabir Pradhan28efc192019-11-05 01:10:04 +00001395 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1396 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397 }
1398
Chris Yea52ade12020-08-27 16:49:20 -07001399 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001400 mFakeListener.clear();
1401 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001402 }
1403
Chris Ye1b0c7342020-07-28 21:57:03 -07001404 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001405 const PropertyMap* configuration) {
1406 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001407
1408 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001409 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410 }
1411 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001412 mReader->loopOnce();
1413 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001414 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1415 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416 }
1417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001418 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001419 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001420 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001421 }
1422
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001423 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001424 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001425 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001426 }
1427
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001428 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001429 const std::string& name,
1430 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001431 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001432 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1433 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001434 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001435 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001436 return mapper;
1437 }
1438};
1439
Chris Ye98d3f532020-10-01 21:48:59 -07001440TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001441 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1442 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1443 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001444
Chris Ye98d3f532020-10-01 21:48:59 -07001445 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001446 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001447 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001448 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001449 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1450 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1451 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001452}
1453
1454TEST_F(InputReaderTest, PolicyGetInputDevices) {
1455 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1456 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1457 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458
1459 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001460 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001461 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001462 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001463 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001464 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1465 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1466 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1467}
1468
Chris Yee7310032020-09-22 15:36:28 -07001469TEST_F(InputReaderTest, GetMergedInputDevices) {
1470 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1471 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1472 // Add two subdevices to device
1473 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1474 // Must add at least one mapper or the device will be ignored!
1475 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1476 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1477
1478 // Push same device instance for next device to be added, so they'll have same identifier.
1479 mReader->pushNextDevice(device);
1480 mReader->pushNextDevice(device);
1481 ASSERT_NO_FATAL_FAILURE(
1482 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1483 ASSERT_NO_FATAL_FAILURE(
1484 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1485
1486 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001487 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001488}
1489
Chris Yee14523a2020-12-19 13:46:00 -08001490TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1491 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1492 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1493 // Add two subdevices to device
1494 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1495 // Must add at least one mapper or the device will be ignored!
1496 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1497 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1498
1499 // Push same device instance for next device to be added, so they'll have same identifier.
1500 mReader->pushNextDevice(device);
1501 mReader->pushNextDevice(device);
1502 // Sensor device is initially disabled
1503 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1504 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1505 nullptr));
1506 // Device is disabled because the only sub device is a sensor device and disabled initially.
1507 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1508 ASSERT_FALSE(device->isEnabled());
1509 ASSERT_NO_FATAL_FAILURE(
1510 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1511 // The merged device is enabled if any sub device is enabled
1512 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1513 ASSERT_TRUE(device->isEnabled());
1514}
1515
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001516TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001517 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001518 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 constexpr int32_t eventHubId = 1;
1520 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001521 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001522 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001523 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001525
Yi Kong9b14ac62018-07-17 13:48:38 -07001526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001527
1528 NotifyDeviceResetArgs resetArgs;
1529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001530 ASSERT_EQ(deviceId, resetArgs.deviceId);
1531
1532 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001533 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001534 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001535
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001537 ASSERT_EQ(deviceId, resetArgs.deviceId);
1538 ASSERT_EQ(device->isEnabled(), false);
1539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001540 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001541 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001544 ASSERT_EQ(device->isEnabled(), false);
1545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001546 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001547 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001549 ASSERT_EQ(deviceId, resetArgs.deviceId);
1550 ASSERT_EQ(device->isEnabled(), true);
1551}
1552
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001554 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001555 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001556 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001557 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001558 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001559 AINPUT_SOURCE_KEYBOARD, nullptr);
1560 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
1562 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1563 AINPUT_SOURCE_ANY, AKEYCODE_A))
1564 << "Should return unknown when the device id is >= 0 but unknown.";
1565
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001566 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1567 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1568 << "Should return unknown when the device id is valid but the sources are not "
1569 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001571 ASSERT_EQ(AKEY_STATE_DOWN,
1572 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1573 AKEYCODE_A))
1574 << "Should return value provided by mapper when device id is valid and the device "
1575 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576
1577 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1578 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1579 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1580
1581 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1582 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1583 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1584}
1585
1586TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001588 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001589 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001590 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001592 AINPUT_SOURCE_KEYBOARD, nullptr);
1593 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594
1595 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1596 AINPUT_SOURCE_ANY, KEY_A))
1597 << "Should return unknown when the device id is >= 0 but unknown.";
1598
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001599 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1600 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1601 << "Should return unknown when the device id is valid but the sources are not "
1602 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001604 ASSERT_EQ(AKEY_STATE_DOWN,
1605 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1606 KEY_A))
1607 << "Should return value provided by mapper when device id is valid and the device "
1608 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609
1610 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1611 AINPUT_SOURCE_TRACKBALL, KEY_A))
1612 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1613
1614 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1615 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1616 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1617}
1618
1619TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001620 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001621 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001622 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001623 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001624 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001625 AINPUT_SOURCE_KEYBOARD, nullptr);
1626 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627
1628 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1629 AINPUT_SOURCE_ANY, SW_LID))
1630 << "Should return unknown when the device id is >= 0 but unknown.";
1631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1633 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1634 << "Should return unknown when the device id is valid but the sources are not "
1635 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 ASSERT_EQ(AKEY_STATE_DOWN,
1638 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1639 SW_LID))
1640 << "Should return value provided by mapper when device id is valid and the device "
1641 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642
1643 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1644 AINPUT_SOURCE_TRACKBALL, SW_LID))
1645 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1646
1647 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1648 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1649 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1650}
1651
1652TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001654 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001656 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001657 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001658 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001659
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001660 mapper.addSupportedKeyCode(AKEYCODE_A);
1661 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001662
1663 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1664 uint8_t flags[4] = { 0, 0, 0, 1 };
1665
1666 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1667 << "Should return false when device id is >= 0 but unknown.";
1668 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1669
1670 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001671 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1672 << "Should return false when device id is valid but the sources are not supported by "
1673 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001674 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1675
1676 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001677 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1678 keyCodes, flags))
1679 << "Should return value provided by mapper when device id is valid and the device "
1680 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1682
1683 flags[3] = 1;
1684 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1685 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1686 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1687
1688 flags[3] = 1;
1689 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1690 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1691 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1692}
1693
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001694TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001695 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001696 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001697
1698 NotifyConfigurationChangedArgs args;
1699
1700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1701 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1702}
1703
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001704TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001705 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001706 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001708 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001713 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1715
1716 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001717 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001719 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720 ASSERT_EQ(EV_KEY, event.type);
1721 ASSERT_EQ(KEY_A, event.code);
1722 ASSERT_EQ(1, event.value);
1723}
1724
Garfield Tan1c7bc862020-01-28 13:24:04 -08001725TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001727 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001728 constexpr int32_t eventHubId = 1;
1729 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001730 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001732 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001733 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001734
1735 NotifyDeviceResetArgs resetArgs;
1736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001737 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001739 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001740 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001742 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001743 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001744
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001746 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001748 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001749 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001751 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001752 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001754 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001755 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001756}
1757
Garfield Tan1c7bc862020-01-28 13:24:04 -08001758TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1759 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001760 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001761 constexpr int32_t eventHubId = 1;
1762 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1763 // Must add at least one mapper or the device will be ignored!
1764 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001765 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001766 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1767
1768 NotifyDeviceResetArgs resetArgs;
1769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1770 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1771}
1772
Arthur Hungc23540e2018-11-29 20:42:11 +08001773TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001774 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001775 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001776 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001777 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001778 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1779 FakeInputMapper& mapper =
1780 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001781 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001782
1783 const uint8_t hdmi1 = 1;
1784
1785 // Associated touch screen with second display.
1786 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1787
1788 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001789 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001790 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001791 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001792 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001793 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001794 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001795 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001796 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001797 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001798
1799 // Add the device, and make sure all of the callbacks are triggered.
1800 // The device is added after the input port associations are processed since
1801 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001805 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001806
Arthur Hung2c9a3342019-07-23 14:18:59 +08001807 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001808 ASSERT_EQ(deviceId, device->getId());
1809 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1810 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001811
1812 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001814 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001815 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001816}
1817
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001818TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1819 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1820 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1821 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1822 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1823 // Must add at least one mapper or the device will be ignored!
1824 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1825 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1826 mReader->pushNextDevice(device);
1827 mReader->pushNextDevice(device);
1828 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1829 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1830
1831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1832
1833 NotifyDeviceResetArgs resetArgs;
1834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1835 ASSERT_EQ(deviceId, resetArgs.deviceId);
1836 ASSERT_TRUE(device->isEnabled());
1837 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1838 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1839
1840 disableDevice(deviceId);
1841 mReader->loopOnce();
1842
1843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1844 ASSERT_EQ(deviceId, resetArgs.deviceId);
1845 ASSERT_FALSE(device->isEnabled());
1846 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1847 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1848
1849 enableDevice(deviceId);
1850 mReader->loopOnce();
1851
1852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1853 ASSERT_EQ(deviceId, resetArgs.deviceId);
1854 ASSERT_TRUE(device->isEnabled());
1855 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1856 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1857}
1858
1859TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1860 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1861 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1862 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1863 // Add two subdevices to device
1864 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1865 FakeInputMapper& mapperDevice1 =
1866 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1867 FakeInputMapper& mapperDevice2 =
1868 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1869 mReader->pushNextDevice(device);
1870 mReader->pushNextDevice(device);
1871 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1872 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1873
1874 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1875 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1876
1877 ASSERT_EQ(AKEY_STATE_DOWN,
1878 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1879 ASSERT_EQ(AKEY_STATE_DOWN,
1880 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1881 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1882 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1883}
1884
Prabir Pradhan7e186182020-11-10 13:56:45 -08001885TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1886 NotifyPointerCaptureChangedArgs args;
1887
1888 mFakePolicy->setPointerCapture(true);
1889 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1890 mReader->loopOnce();
1891 mFakeListener->assertNotifyCaptureWasCalled(&args);
1892 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1893
1894 mFakePolicy->setPointerCapture(false);
1895 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1896 mReader->loopOnce();
1897 mFakeListener->assertNotifyCaptureWasCalled(&args);
1898 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1899
1900 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1901 // does not change.
1902 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1903 mReader->loopOnce();
1904 mFakeListener->assertNotifyCaptureWasCalled(&args);
1905 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1906}
1907
Chris Ye87143712020-11-10 05:05:58 +00001908class FakeVibratorInputMapper : public FakeInputMapper {
1909public:
1910 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1911 : FakeInputMapper(deviceContext, sources) {}
1912
1913 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1914};
1915
1916TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1917 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1918 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1919 constexpr int32_t eventHubId = 1;
1920 const char* DEVICE_LOCATION = "BLUETOOTH";
1921 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1922 FakeVibratorInputMapper& mapper =
1923 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1924 mReader->pushNextDevice(device);
1925
1926 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1927 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1928
1929 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1930 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1931}
1932
Kim Low03ea0352020-11-06 12:45:07 -08001933class FakeBatteryInputMapper : public FakeInputMapper {
1934public:
1935 FakeBatteryInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1936 : FakeInputMapper(deviceContext, sources) {}
1937
1938 std::optional<int32_t> getBatteryCapacity() override {
1939 return getDeviceContext().getBatteryCapacity();
1940 }
1941
1942 std::optional<int32_t> getBatteryStatus() override {
1943 return getDeviceContext().getBatteryStatus();
1944 }
1945};
1946
1947TEST_F(InputReaderTest, BatteryGetCapacity) {
1948 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1949 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1950 constexpr int32_t eventHubId = 1;
1951 const char* DEVICE_LOCATION = "BLUETOOTH";
1952 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1953 FakeBatteryInputMapper& mapper =
1954 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1955 mReader->pushNextDevice(device);
1956
1957 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1958 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1959
1960 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
1961}
1962
1963TEST_F(InputReaderTest, BatteryGetStatus) {
1964 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1965 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1966 constexpr int32_t eventHubId = 1;
1967 const char* DEVICE_LOCATION = "BLUETOOTH";
1968 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1969 FakeBatteryInputMapper& mapper =
1970 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1971 mReader->pushNextDevice(device);
1972
1973 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1974 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1975
1976 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
1977}
1978
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001979// --- InputReaderIntegrationTest ---
1980
1981// These tests create and interact with the InputReader only through its interface.
1982// The InputReader is started during SetUp(), which starts its processing in its own
1983// thread. The tests use linux uinput to emulate input devices.
1984// NOTE: Interacting with the physical device while these tests are running may cause
1985// the tests to fail.
1986class InputReaderIntegrationTest : public testing::Test {
1987protected:
1988 sp<TestInputListener> mTestListener;
1989 sp<FakeInputReaderPolicy> mFakePolicy;
1990 sp<InputReaderInterface> mReader;
1991
Chris Yea52ade12020-08-27 16:49:20 -07001992 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001993 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001994 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1995 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001996
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001997 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001998 ASSERT_EQ(mReader->start(), OK);
1999
2000 // Since this test is run on a real device, all the input devices connected
2001 // to the test device will show up in mReader. We wait for those input devices to
2002 // show up before beginning the tests.
2003 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2005 }
2006
Chris Yea52ade12020-08-27 16:49:20 -07002007 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002008 ASSERT_EQ(mReader->stop(), OK);
2009 mTestListener.clear();
2010 mFakePolicy.clear();
2011 }
2012};
2013
2014TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2015 // An invalid input device that is only used for this test.
2016 class InvalidUinputDevice : public UinputDevice {
2017 public:
2018 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2019
2020 private:
2021 void configureDevice(int fd, uinput_user_dev* device) override {}
2022 };
2023
2024 const size_t numDevices = mFakePolicy->getInputDevices().size();
2025
2026 // UinputDevice does not set any event or key bits, so InputReader should not
2027 // consider it as a valid device.
2028 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2029 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2030 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2031 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2032
2033 invalidDevice.reset();
2034 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2035 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2036 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2037}
2038
2039TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2040 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2041
2042 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2043 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2044 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2045 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2046
2047 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002048 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2049 const auto& it =
2050 std::find_if(inputDevices.begin(), inputDevices.end(),
2051 [&keyboard](const InputDeviceInfo& info) {
2052 return info.getIdentifier().name == keyboard->getName();
2053 });
2054
2055 ASSERT_NE(it, inputDevices.end());
2056 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2057 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2058 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002059
2060 keyboard.reset();
2061 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2062 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2063 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2064}
2065
2066TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2067 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2068 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2069
2070 NotifyConfigurationChangedArgs configChangedArgs;
2071 ASSERT_NO_FATAL_FAILURE(
2072 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002073 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002074 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2075
2076 NotifyKeyArgs keyArgs;
2077 keyboard->pressAndReleaseHomeKey();
2078 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2079 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002080 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002081 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002082 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2083 prevTimestamp = keyArgs.eventTime;
2084
2085 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2086 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002087 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002088 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2089}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002090
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002091/**
2092 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2093 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2094 * are passed to the listener.
2095 */
2096static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2097TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2098 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2099 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2100 NotifyKeyArgs keyArgs;
2101
2102 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2103 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2104 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2105 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2106
2107 controller->pressAndReleaseKey(BTN_GEAR_UP);
2108 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2109 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2110 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2111}
2112
Arthur Hungaab25622020-01-16 11:22:11 +08002113// --- TouchProcessTest ---
2114class TouchIntegrationTest : public InputReaderIntegrationTest {
2115protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002116 const std::string UNIQUE_ID = "local:0";
2117
Chris Yea52ade12020-08-27 16:49:20 -07002118 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002119 InputReaderIntegrationTest::SetUp();
2120 // At least add an internal display.
2121 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2122 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002123 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002124
2125 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2126 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2127 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2128 }
2129
2130 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2131 int32_t orientation, const std::string& uniqueId,
2132 std::optional<uint8_t> physicalPort,
2133 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002134 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2135 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002136 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2137 }
2138
2139 std::unique_ptr<UinputTouchScreen> mDevice;
2140};
2141
2142TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2143 NotifyMotionArgs args;
2144 const Point centerPoint = mDevice->getCenterPoint();
2145
2146 // ACTION_DOWN
2147 mDevice->sendDown(centerPoint);
2148 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2149 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2150
2151 // ACTION_MOVE
2152 mDevice->sendMove(centerPoint + Point(1, 1));
2153 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2155
2156 // ACTION_UP
2157 mDevice->sendUp();
2158 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2159 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2160}
2161
2162TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2163 NotifyMotionArgs args;
2164 const Point centerPoint = mDevice->getCenterPoint();
2165
2166 // ACTION_DOWN
2167 mDevice->sendDown(centerPoint);
2168 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2170
2171 // ACTION_POINTER_DOWN (Second slot)
2172 const Point secondPoint = centerPoint + Point(100, 100);
2173 mDevice->sendSlot(SECOND_SLOT);
2174 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2175 mDevice->sendDown(secondPoint + Point(1, 1));
2176 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2177 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2178 args.action);
2179
2180 // ACTION_MOVE (Second slot)
2181 mDevice->sendMove(secondPoint);
2182 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2184
2185 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002186 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002188 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002189 args.action);
2190
2191 // ACTION_UP
2192 mDevice->sendSlot(FIRST_SLOT);
2193 mDevice->sendUp();
2194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2195 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2196}
2197
2198TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2199 NotifyMotionArgs args;
2200 const Point centerPoint = mDevice->getCenterPoint();
2201
2202 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002203 mDevice->sendSlot(FIRST_SLOT);
2204 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002205 mDevice->sendDown(centerPoint);
2206 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2208
arthurhungcc7f9802020-04-30 17:55:40 +08002209 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002210 const Point secondPoint = centerPoint + Point(100, 100);
2211 mDevice->sendSlot(SECOND_SLOT);
2212 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2213 mDevice->sendDown(secondPoint);
2214 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2215 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2216 args.action);
2217
arthurhungcc7f9802020-04-30 17:55:40 +08002218 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002219 mDevice->sendMove(secondPoint + Point(1, 1));
2220 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2222
arthurhungcc7f9802020-04-30 17:55:40 +08002223 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2224 // a palm event.
2225 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002226 mDevice->sendToolType(MT_TOOL_PALM);
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002228 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2229 args.action);
2230 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002231
arthurhungcc7f9802020-04-30 17:55:40 +08002232 // Send up to second slot, expect first slot send moving.
2233 mDevice->sendPointerUp();
2234 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002236
arthurhungcc7f9802020-04-30 17:55:40 +08002237 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002238 mDevice->sendSlot(FIRST_SLOT);
2239 mDevice->sendUp();
2240
arthurhungcc7f9802020-04-30 17:55:40 +08002241 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2242 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002243}
2244
Michael Wrightd02c5b62014-02-10 15:10:22 -08002245// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246class InputDeviceTest : public testing::Test {
2247protected:
2248 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002249 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002250 static const int32_t DEVICE_ID;
2251 static const int32_t DEVICE_GENERATION;
2252 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002253 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002254 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002255
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002256 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002258 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002259 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002260 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002261
Chris Yea52ade12020-08-27 16:49:20 -07002262 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002263 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002265 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002266 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2267 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268 InputDeviceIdentifier identifier;
2269 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002270 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002271 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002272 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002273 mReader->pushNextDevice(mDevice);
2274 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2275 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002276 }
2277
Chris Yea52ade12020-08-27 16:49:20 -07002278 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279 mFakeListener.clear();
2280 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281 }
2282};
2283
2284const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002285const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002286const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2288const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002289const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2290 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002291const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002292
2293TEST_F(InputDeviceTest, ImmutableProperties) {
2294 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002295 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002296 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297}
2298
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002299TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2300 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002301}
2302
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2304 // Configuration.
2305 InputReaderConfiguration config;
2306 mDevice->configure(ARBITRARY_TIME, &config, 0);
2307
2308 // Reset.
2309 mDevice->reset(ARBITRARY_TIME);
2310
2311 NotifyDeviceResetArgs resetArgs;
2312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2313 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2314 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2315
2316 // Metadata.
2317 ASSERT_TRUE(mDevice->isIgnored());
2318 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2319
2320 InputDeviceInfo info;
2321 mDevice->getDeviceInfo(&info);
2322 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002323 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002324 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2325 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2326
2327 // State queries.
2328 ASSERT_EQ(0, mDevice->getMetaState());
2329
2330 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2331 << "Ignored device should return unknown key code state.";
2332 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2333 << "Ignored device should return unknown scan code state.";
2334 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2335 << "Ignored device should return unknown switch state.";
2336
2337 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2338 uint8_t flags[2] = { 0, 1 };
2339 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2340 << "Ignored device should never mark any key codes.";
2341 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2342 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2343}
2344
2345TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2346 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002347 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002348
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002349 FakeInputMapper& mapper1 =
2350 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002351 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2352 mapper1.setMetaState(AMETA_ALT_ON);
2353 mapper1.addSupportedKeyCode(AKEYCODE_A);
2354 mapper1.addSupportedKeyCode(AKEYCODE_B);
2355 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2356 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2357 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2358 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2359 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002361 FakeInputMapper& mapper2 =
2362 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002363 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002364
2365 InputReaderConfiguration config;
2366 mDevice->configure(ARBITRARY_TIME, &config, 0);
2367
2368 String8 propertyValue;
2369 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2370 << "Device should have read configuration during configuration phase.";
2371 ASSERT_STREQ("value", propertyValue.string());
2372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002373 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2374 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375
2376 // Reset
2377 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002378 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2379 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002380
2381 NotifyDeviceResetArgs resetArgs;
2382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2383 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2384 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2385
2386 // Metadata.
2387 ASSERT_FALSE(mDevice->isIgnored());
2388 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2389
2390 InputDeviceInfo info;
2391 mDevice->getDeviceInfo(&info);
2392 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002393 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2395 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2396
2397 // State queries.
2398 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2399 << "Should query mappers and combine meta states.";
2400
2401 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2402 << "Should return unknown key code state when source not supported.";
2403 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2404 << "Should return unknown scan code state when source not supported.";
2405 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2406 << "Should return unknown switch state when source not supported.";
2407
2408 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2409 << "Should query mapper when source is supported.";
2410 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2411 << "Should query mapper when source is supported.";
2412 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2413 << "Should query mapper when source is supported.";
2414
2415 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2416 uint8_t flags[4] = { 0, 0, 0, 1 };
2417 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2418 << "Should do nothing when source is unsupported.";
2419 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2420 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2421 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2422 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2423
2424 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2425 << "Should query mapper when source is supported.";
2426 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2427 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2428 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2429 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2430
2431 // Event handling.
2432 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002433 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 mDevice->process(&event, 1);
2435
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002436 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2437 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438}
2439
Arthur Hung2c9a3342019-07-23 14:18:59 +08002440// A single input device is associated with a specific display. Check that:
2441// 1. Device is disabled if the viewport corresponding to the associated display is not found
2442// 2. Device is disabled when setEnabled API is called
2443TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002444 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002445
2446 // First Configuration.
2447 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2448
2449 // Device should be enabled by default.
2450 ASSERT_TRUE(mDevice->isEnabled());
2451
2452 // Prepare associated info.
2453 constexpr uint8_t hdmi = 1;
2454 const std::string UNIQUE_ID = "local:1";
2455
2456 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2457 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2458 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2459 // Device should be disabled because it is associated with a specific display via
2460 // input port <-> display port association, but the corresponding display is not found
2461 ASSERT_FALSE(mDevice->isEnabled());
2462
2463 // Prepare displays.
2464 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002465 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2466 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002467 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2468 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2469 ASSERT_TRUE(mDevice->isEnabled());
2470
2471 // Device should be disabled after set disable.
2472 mFakePolicy->addDisabledDevice(mDevice->getId());
2473 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2474 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2475 ASSERT_FALSE(mDevice->isEnabled());
2476
2477 // Device should still be disabled even found the associated display.
2478 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2479 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2480 ASSERT_FALSE(mDevice->isEnabled());
2481}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482
2483// --- InputMapperTest ---
2484
2485class InputMapperTest : public testing::Test {
2486protected:
2487 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002488 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 static const int32_t DEVICE_ID;
2490 static const int32_t DEVICE_GENERATION;
2491 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002492 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002493 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002495 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002497 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002498 std::unique_ptr<InstrumentedInputReader> mReader;
2499 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500
Chris Ye1b0c7342020-07-28 21:57:03 -07002501 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002502 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002503 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002504 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002505 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2506 mFakeListener);
2507 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508 }
2509
Chris Yea52ade12020-08-27 16:49:20 -07002510 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002511
Chris Yea52ade12020-08-27 16:49:20 -07002512 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513 mFakeListener.clear();
2514 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 }
2516
2517 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002518 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002519 }
2520
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002521 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002522 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002523 mReader->requestRefreshConfiguration(changes);
2524 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002525 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002526 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2527 }
2528
arthurhungdcef2dc2020-08-11 14:47:50 +08002529 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2530 const std::string& location, int32_t eventHubId,
2531 Flags<InputDeviceClass> classes) {
2532 InputDeviceIdentifier identifier;
2533 identifier.name = name;
2534 identifier.location = location;
2535 std::shared_ptr<InputDevice> device =
2536 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2537 identifier);
2538 mReader->pushNextDevice(device);
2539 mFakeEventHub->addDevice(eventHubId, name, classes);
2540 mReader->loopOnce();
2541 return device;
2542 }
2543
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002544 template <class T, typename... Args>
2545 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002546 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002547 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002548 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002549 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002550 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551 }
2552
2553 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002554 int32_t orientation, const std::string& uniqueId,
2555 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002556 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2557 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002558 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2559 }
2560
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002561 void clearViewports() {
2562 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563 }
2564
arthurhungdcef2dc2020-08-11 14:47:50 +08002565 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566 RawEvent event;
2567 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002568 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002569 event.type = type;
2570 event.code = code;
2571 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002572 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002573 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574 }
2575
2576 static void assertMotionRange(const InputDeviceInfo& info,
2577 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2578 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002579 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2581 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2582 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2583 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2584 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2585 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2586 }
2587
2588 static void assertPointerCoords(const PointerCoords& coords,
2589 float x, float y, float pressure, float size,
2590 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2591 float orientation, float distance) {
2592 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2593 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2594 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2595 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2596 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2597 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2598 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2599 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2600 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2601 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2602 }
2603
Michael Wright17db18e2020-06-26 20:51:44 +01002604 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002606 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607 ASSERT_NEAR(x, actualX, 1);
2608 ASSERT_NEAR(y, actualY, 1);
2609 }
2610};
2611
2612const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002613const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002614const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002615const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2616const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002617const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2618 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002619const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620
2621// --- SwitchInputMapperTest ---
2622
2623class SwitchInputMapperTest : public InputMapperTest {
2624protected:
2625};
2626
2627TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002628 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002629
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002630 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631}
2632
2633TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002634 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002636 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002637 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002639 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002640 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641}
2642
2643TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002644 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002646 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2647 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2648 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2649 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650
2651 NotifySwitchArgs args;
2652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2653 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002654 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2655 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 args.switchMask);
2657 ASSERT_EQ(uint32_t(0), args.policyFlags);
2658}
2659
Chris Ye87143712020-11-10 05:05:58 +00002660// --- VibratorInputMapperTest ---
2661class VibratorInputMapperTest : public InputMapperTest {
2662protected:
2663 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2664};
2665
2666TEST_F(VibratorInputMapperTest, GetSources) {
2667 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2668
2669 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2670}
2671
2672TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2673 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2674
2675 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2676}
2677
2678TEST_F(VibratorInputMapperTest, Vibrate) {
2679 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002680 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002681 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2682
2683 VibrationElement pattern(2);
2684 VibrationSequence sequence(2);
2685 pattern.duration = std::chrono::milliseconds(200);
2686 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2687 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2688 sequence.addElement(pattern);
2689 pattern.duration = std::chrono::milliseconds(500);
2690 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2691 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2692 sequence.addElement(pattern);
2693
2694 std::vector<int64_t> timings = {0, 1};
2695 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2696
2697 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002698 // Start vibrating
2699 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002700 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002701 // Verify vibrator state listener was notified.
2702 mReader->loopOnce();
2703 NotifyVibratorStateArgs args;
2704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2705 ASSERT_EQ(DEVICE_ID, args.deviceId);
2706 ASSERT_TRUE(args.isOn);
2707 // Stop vibrating
2708 mapper.cancelVibrate(VIBRATION_TOKEN);
2709 ASSERT_FALSE(mapper.isVibrating());
2710 // Verify vibrator state listener was notified.
2711 mReader->loopOnce();
2712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2713 ASSERT_EQ(DEVICE_ID, args.deviceId);
2714 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002715}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716
Chris Yef59a2f42020-10-16 12:55:26 -07002717// --- SensorInputMapperTest ---
2718
2719class SensorInputMapperTest : public InputMapperTest {
2720protected:
2721 static const int32_t ACCEL_RAW_MIN;
2722 static const int32_t ACCEL_RAW_MAX;
2723 static const int32_t ACCEL_RAW_FUZZ;
2724 static const int32_t ACCEL_RAW_FLAT;
2725 static const int32_t ACCEL_RAW_RESOLUTION;
2726
2727 static const int32_t GYRO_RAW_MIN;
2728 static const int32_t GYRO_RAW_MAX;
2729 static const int32_t GYRO_RAW_FUZZ;
2730 static const int32_t GYRO_RAW_FLAT;
2731 static const int32_t GYRO_RAW_RESOLUTION;
2732
2733 static const float GRAVITY_MS2_UNIT;
2734 static const float DEGREE_RADIAN_UNIT;
2735
2736 void prepareAccelAxes();
2737 void prepareGyroAxes();
2738 void setAccelProperties();
2739 void setGyroProperties();
2740 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2741};
2742
2743const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2744const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2745const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2746const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2747const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2748
2749const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2750const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2751const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2752const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2753const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2754
2755const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2756const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2757
2758void SensorInputMapperTest::prepareAccelAxes() {
2759 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2760 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2761 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2762 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2763 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2764 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2765}
2766
2767void SensorInputMapperTest::prepareGyroAxes() {
2768 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2769 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2770 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2771 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2772 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2773 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2774}
2775
2776void SensorInputMapperTest::setAccelProperties() {
2777 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2778 /* sensorDataIndex */ 0);
2779 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2780 /* sensorDataIndex */ 1);
2781 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2782 /* sensorDataIndex */ 2);
2783 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2784 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2785 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2786 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2787 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2788}
2789
2790void SensorInputMapperTest::setGyroProperties() {
2791 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2792 /* sensorDataIndex */ 0);
2793 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2794 /* sensorDataIndex */ 1);
2795 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2796 /* sensorDataIndex */ 2);
2797 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2798 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2799 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2800 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2801 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2802}
2803
2804TEST_F(SensorInputMapperTest, GetSources) {
2805 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2806
2807 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2808}
2809
2810TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2811 setAccelProperties();
2812 prepareAccelAxes();
2813 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2814
2815 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2816 std::chrono::microseconds(10000),
2817 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002818 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002819 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, 20000);
2820 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, -20000);
2821 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Z, 40000);
2822 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2823 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2824
2825 NotifySensorArgs args;
2826 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2827 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2828 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2829
2830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2831 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2832 ASSERT_EQ(args.deviceId, DEVICE_ID);
2833 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2834 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2835 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2836 ASSERT_EQ(args.values, values);
2837 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2838}
2839
2840TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2841 setGyroProperties();
2842 prepareGyroAxes();
2843 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2844
2845 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2846 std::chrono::microseconds(10000),
2847 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002848 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002849 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RX, 20000);
2850 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RY, -20000);
2851 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RZ, 40000);
2852 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2853 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2854
2855 NotifySensorArgs args;
2856 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2857 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2858 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2859
2860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2861 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2862 ASSERT_EQ(args.deviceId, DEVICE_ID);
2863 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2864 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2865 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2866 ASSERT_EQ(args.values, values);
2867 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2868}
2869
Kim Low03ea0352020-11-06 12:45:07 -08002870// --- BatteryInputMapperTest ---
2871class BatteryInputMapperTest : public InputMapperTest {
2872protected:
2873 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY); }
2874};
2875
2876TEST_F(BatteryInputMapperTest, GetSources) {
2877 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2878
2879 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2880}
2881
2882TEST_F(BatteryInputMapperTest, GetBatteryCapacity) {
2883 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2884
2885 ASSERT_TRUE(mapper.getBatteryCapacity());
2886 ASSERT_EQ(*mapper.getBatteryCapacity(), BATTERY_CAPACITY);
2887}
2888
2889TEST_F(BatteryInputMapperTest, GetBatteryStatus) {
2890 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2891
2892 ASSERT_TRUE(mapper.getBatteryStatus());
2893 ASSERT_EQ(*mapper.getBatteryStatus(), BATTERY_STATUS);
2894}
2895
Michael Wrightd02c5b62014-02-10 15:10:22 -08002896// --- KeyboardInputMapperTest ---
2897
2898class KeyboardInputMapperTest : public InputMapperTest {
2899protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002900 const std::string UNIQUE_ID = "local:0";
2901
2902 void prepareDisplay(int32_t orientation);
2903
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002904 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002905 int32_t originalKeyCode, int32_t rotatedKeyCode,
2906 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907};
2908
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002909/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2910 * orientation.
2911 */
2912void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002913 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2914 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002915}
2916
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002917void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002918 int32_t originalScanCode, int32_t originalKeyCode,
2919 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920 NotifyKeyArgs args;
2921
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002922 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2924 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2925 ASSERT_EQ(originalScanCode, args.scanCode);
2926 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002927 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002929 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2931 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2932 ASSERT_EQ(originalScanCode, args.scanCode);
2933 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002934 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935}
2936
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002938 KeyboardInputMapper& mapper =
2939 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2940 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002941
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002942 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943}
2944
2945TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2946 const int32_t USAGE_A = 0x070004;
2947 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2949 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002950 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2951 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2952 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002954 KeyboardInputMapper& mapper =
2955 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2956 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002957 // Initial metastate to AMETA_NONE.
2958 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2959 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960
2961 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002962 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 NotifyKeyArgs args;
2964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2965 ASSERT_EQ(DEVICE_ID, args.deviceId);
2966 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2967 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2968 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2969 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2970 ASSERT_EQ(KEY_HOME, args.scanCode);
2971 ASSERT_EQ(AMETA_NONE, args.metaState);
2972 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2973 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2974 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2975
2976 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002977 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2979 ASSERT_EQ(DEVICE_ID, args.deviceId);
2980 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2981 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2982 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2983 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2984 ASSERT_EQ(KEY_HOME, args.scanCode);
2985 ASSERT_EQ(AMETA_NONE, args.metaState);
2986 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2987 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2988 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2989
2990 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002991 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2992 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2994 ASSERT_EQ(DEVICE_ID, args.deviceId);
2995 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2996 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2997 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2998 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2999 ASSERT_EQ(0, args.scanCode);
3000 ASSERT_EQ(AMETA_NONE, args.metaState);
3001 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3002 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3003 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3004
3005 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003006 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3007 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3009 ASSERT_EQ(DEVICE_ID, args.deviceId);
3010 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3011 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3012 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3013 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3014 ASSERT_EQ(0, args.scanCode);
3015 ASSERT_EQ(AMETA_NONE, args.metaState);
3016 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3017 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3018 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3019
3020 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003021 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3022 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3024 ASSERT_EQ(DEVICE_ID, args.deviceId);
3025 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3026 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3027 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3028 ASSERT_EQ(0, args.keyCode);
3029 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3030 ASSERT_EQ(AMETA_NONE, args.metaState);
3031 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3032 ASSERT_EQ(0U, args.policyFlags);
3033 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3034
3035 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003036 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3037 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3039 ASSERT_EQ(DEVICE_ID, args.deviceId);
3040 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3041 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3043 ASSERT_EQ(0, args.keyCode);
3044 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3045 ASSERT_EQ(AMETA_NONE, args.metaState);
3046 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3047 ASSERT_EQ(0U, args.policyFlags);
3048 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3049}
3050
3051TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003054 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3055 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3056 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003058 KeyboardInputMapper& mapper =
3059 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3060 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061
arthurhungc903df12020-08-11 15:08:42 +08003062 // Initial metastate to AMETA_NONE.
3063 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3064 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065
3066 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003067 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 NotifyKeyArgs args;
3069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003072 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
3074 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003075 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
3080 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003081 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085
3086 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003087 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3089 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003090 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003091 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
3094TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 KeyboardInputMapper& mapper =
3101 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3102 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003104 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3106 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3108 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3110 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3111 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3112 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3113}
3114
3115TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003116 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3117 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3118 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3119 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003122 KeyboardInputMapper& mapper =
3123 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3124 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003126 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003127 ASSERT_NO_FATAL_FAILURE(
3128 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3129 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3130 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3131 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3132 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3133 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3134 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003136 clearViewports();
3137 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003138 ASSERT_NO_FATAL_FAILURE(
3139 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3140 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3141 AKEYCODE_DPAD_UP, DISPLAY_ID));
3142 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3143 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3144 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3145 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003147 clearViewports();
3148 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003149 ASSERT_NO_FATAL_FAILURE(
3150 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3151 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3152 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3153 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3154 AKEYCODE_DPAD_UP, DISPLAY_ID));
3155 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3156 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003158 clearViewports();
3159 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003160 ASSERT_NO_FATAL_FAILURE(
3161 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3162 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3163 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3164 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3165 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3166 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3167 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168
3169 // Special case: if orientation changes while key is down, we still emit the same keycode
3170 // in the key up as we did in the key down.
3171 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003172 clearViewports();
3173 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003174 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3177 ASSERT_EQ(KEY_UP, args.scanCode);
3178 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3179
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003180 clearViewports();
3181 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003182 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3184 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3185 ASSERT_EQ(KEY_UP, args.scanCode);
3186 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3187}
3188
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003189TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3190 // If the keyboard is not orientation aware,
3191 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003194 KeyboardInputMapper& mapper =
3195 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3196 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003197 NotifyKeyArgs args;
3198
3199 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003200 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003202 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3204 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3205
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003206 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003207 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003209 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3211 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3212}
3213
3214TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3215 // If the keyboard is orientation aware,
3216 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003217 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003218
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003219 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003220 KeyboardInputMapper& mapper =
3221 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3222 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003223 NotifyKeyArgs args;
3224
3225 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3226 // ^--- already checked by the previous test
3227
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003228 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003229 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003230 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003232 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3234 ASSERT_EQ(DISPLAY_ID, args.displayId);
3235
3236 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003237 clearViewports();
3238 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003239 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003240 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003242 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3244 ASSERT_EQ(newDisplayId, args.displayId);
3245}
3246
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 KeyboardInputMapper& mapper =
3249 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3250 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003252 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003253 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003255 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003256 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257}
3258
3259TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003260 KeyboardInputMapper& mapper =
3261 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3262 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003264 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003267 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269}
3270
3271TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 KeyboardInputMapper& mapper =
3273 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3274 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003276 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277
3278 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3279 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003280 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 ASSERT_TRUE(flags[0]);
3282 ASSERT_FALSE(flags[1]);
3283}
3284
3285TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3287 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3288 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3291 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 KeyboardInputMapper& mapper =
3294 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3295 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003296 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003297 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3298 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299
3300 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003301 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3302 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3303 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304
3305 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003306 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3307 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003308 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3309 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3310 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003311 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312
3313 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003314 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3315 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003316 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3317 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3318 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
3321 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003322 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3323 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003324 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3325 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3326 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
3329 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003330 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3331 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3333 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3334 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
3337 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003338 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3339 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003340 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3341 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3342 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344
3345 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003346 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3347 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003348 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3349 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3350 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352}
3353
Chris Yea52ade12020-08-27 16:49:20 -07003354TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3355 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3356 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3357 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3358 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3359
3360 KeyboardInputMapper& mapper =
3361 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3362 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3363
3364 // Initial metastate should be AMETA_NONE as no meta keys added.
3365 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3366 // Meta state should be AMETA_NONE after reset
3367 mapper.reset(ARBITRARY_TIME);
3368 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3369 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3370 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3371 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3372
3373 NotifyKeyArgs args;
3374 // Press button "A"
3375 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
3376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3377 ASSERT_EQ(AMETA_NONE, args.metaState);
3378 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3379 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3380 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3381
3382 // Button up.
3383 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
3384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3385 ASSERT_EQ(AMETA_NONE, args.metaState);
3386 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3387 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3388 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3389}
3390
Arthur Hung2c9a3342019-07-23 14:18:59 +08003391TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3392 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003393 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3394 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3395 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3396 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397
3398 // keyboard 2.
3399 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003400 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003401 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003402 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003403 std::shared_ptr<InputDevice> device2 =
3404 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3405 Flags<InputDeviceClass>(0));
3406
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003407 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3408 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3409 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3410 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003412 KeyboardInputMapper& mapper =
3413 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3414 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003415
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003416 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003417 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003418 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003419 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3420 device2->reset(ARBITRARY_TIME);
3421
3422 // Prepared displays and associated info.
3423 constexpr uint8_t hdmi1 = 0;
3424 constexpr uint8_t hdmi2 = 1;
3425 const std::string SECONDARY_UNIQUE_ID = "local:1";
3426
3427 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3428 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3429
3430 // No associated display viewport found, should disable the device.
3431 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3432 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3433 ASSERT_FALSE(device2->isEnabled());
3434
3435 // Prepare second display.
3436 constexpr int32_t newDisplayId = 2;
3437 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003438 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003439 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003440 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003441 // Default device will reconfigure above, need additional reconfiguration for another device.
3442 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3443 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3444
3445 // Device should be enabled after the associated display is found.
3446 ASSERT_TRUE(mDevice->isEnabled());
3447 ASSERT_TRUE(device2->isEnabled());
3448
3449 // Test pad key events
3450 ASSERT_NO_FATAL_FAILURE(
3451 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3452 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3453 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3454 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3455 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3456 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3457 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3458
3459 ASSERT_NO_FATAL_FAILURE(
3460 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3461 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3462 AKEYCODE_DPAD_RIGHT, newDisplayId));
3463 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3464 AKEYCODE_DPAD_DOWN, newDisplayId));
3465 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3466 AKEYCODE_DPAD_LEFT, newDisplayId));
3467}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
arthurhungc903df12020-08-11 15:08:42 +08003469TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3470 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3471 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3472 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3474 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3476
3477 KeyboardInputMapper& mapper =
3478 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3479 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3480 // Initial metastate to AMETA_NONE.
3481 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3482 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3483
3484 // Initialization should have turned all of the lights off.
3485 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3486 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3487 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3488
3489 // Toggle caps lock on.
3490 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3491 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3492 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3493 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3494
3495 // Toggle num lock on.
3496 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3497 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3498 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3499 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3500
3501 // Toggle scroll lock on.
3502 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3503 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3504 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3505 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3506
3507 mFakeEventHub->removeDevice(EVENTHUB_ID);
3508 mReader->loopOnce();
3509
3510 // keyboard 2 should default toggle keys.
3511 const std::string USB2 = "USB2";
3512 const std::string DEVICE_NAME2 = "KEYBOARD2";
3513 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3514 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3515 std::shared_ptr<InputDevice> device2 =
3516 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3517 Flags<InputDeviceClass>(0));
3518 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3519 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3520 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3521 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3522 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3523 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3524
arthurhung6fe95782020-10-05 22:41:16 +08003525 KeyboardInputMapper& mapper2 =
3526 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3527 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003528 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3529 device2->reset(ARBITRARY_TIME);
3530
3531 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3532 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3533 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003534 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3535 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003536}
3537
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003538// --- KeyboardInputMapperTest_ExternalDevice ---
3539
3540class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3541protected:
Chris Yea52ade12020-08-27 16:49:20 -07003542 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003543};
3544
3545TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003546 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3547 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003548
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3552 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 KeyboardInputMapper& mapper =
3555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003557
3558 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3559 NotifyKeyArgs args;
3560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3561 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3562
3563 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3565 ASSERT_EQ(uint32_t(0), args.policyFlags);
3566
3567 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3569 ASSERT_EQ(uint32_t(0), args.policyFlags);
3570
3571 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3573 ASSERT_EQ(uint32_t(0), args.policyFlags);
3574
3575 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3577 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3578
3579 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3581 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3582}
3583
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003584TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003585 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003586
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003587 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3588 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003590
Powei Fengd041c5d2019-05-03 17:11:33 -07003591 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003592 KeyboardInputMapper& mapper =
3593 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3594 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003595
3596 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3597 NotifyKeyArgs args;
3598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3599 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3600
3601 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3603 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3604
3605 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3607 ASSERT_EQ(uint32_t(0), args.policyFlags);
3608
3609 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3611 ASSERT_EQ(uint32_t(0), args.policyFlags);
3612
3613 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3615 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3616
3617 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3619 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3620}
3621
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622// --- CursorInputMapperTest ---
3623
3624class CursorInputMapperTest : public InputMapperTest {
3625protected:
3626 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3627
Michael Wright17db18e2020-06-26 20:51:44 +01003628 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
Chris Yea52ade12020-08-27 16:49:20 -07003630 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631 InputMapperTest::SetUp();
3632
Michael Wright17db18e2020-06-26 20:51:44 +01003633 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003634 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 }
3636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3638 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003639
3640 void prepareDisplay(int32_t orientation) {
3641 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003642 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003643 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3644 orientation, uniqueId, NO_PORT, viewportType);
3645 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646};
3647
3648const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003650void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3651 int32_t originalY, int32_t rotatedX,
3652 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653 NotifyMotionArgs args;
3654
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003655 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3656 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3657 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3661 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3662 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3663 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3664}
3665
3666TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003667 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003668 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671}
3672
3673TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003675 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003677 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678}
3679
3680TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003682 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683
3684 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003685 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686
3687 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003688 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3689 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3691 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3692
3693 // When the bounds are set, then there should be a valid motion range.
3694 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3695
3696 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698
3699 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3700 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3701 1, 800 - 1, 0.0f, 0.0f));
3702 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3703 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3704 2, 480 - 1, 0.0f, 0.0f));
3705 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3706 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3707 0.0f, 1.0f, 0.0f, 0.0f));
3708}
3709
3710TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003712 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713
3714 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003715 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
3717 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3718 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3719 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3720 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3721 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3722 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3723 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3724 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3725 0.0f, 1.0f, 0.0f, 0.0f));
3726}
3727
3728TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
arthurhungdcef2dc2020-08-11 14:47:50 +08003732 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
3734 NotifyMotionArgs args;
3735
3736 // Button press.
3737 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003738 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3739 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3741 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3742 ASSERT_EQ(DEVICE_ID, args.deviceId);
3743 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3744 ASSERT_EQ(uint32_t(0), args.policyFlags);
3745 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3746 ASSERT_EQ(0, args.flags);
3747 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3748 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3749 ASSERT_EQ(0, args.edgeFlags);
3750 ASSERT_EQ(uint32_t(1), args.pointerCount);
3751 ASSERT_EQ(0, args.pointerProperties[0].id);
3752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3754 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3755 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3756 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3757 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3758
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3760 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3761 ASSERT_EQ(DEVICE_ID, args.deviceId);
3762 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3763 ASSERT_EQ(uint32_t(0), args.policyFlags);
3764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3765 ASSERT_EQ(0, args.flags);
3766 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3767 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3768 ASSERT_EQ(0, args.edgeFlags);
3769 ASSERT_EQ(uint32_t(1), args.pointerCount);
3770 ASSERT_EQ(0, args.pointerProperties[0].id);
3771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3773 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3774 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3775 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3776 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3777
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003779 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3780 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3782 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3783 ASSERT_EQ(DEVICE_ID, args.deviceId);
3784 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3785 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003786 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3787 ASSERT_EQ(0, args.flags);
3788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3789 ASSERT_EQ(0, args.buttonState);
3790 ASSERT_EQ(0, args.edgeFlags);
3791 ASSERT_EQ(uint32_t(1), args.pointerCount);
3792 ASSERT_EQ(0, args.pointerProperties[0].id);
3793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3795 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3796 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3797 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3798 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3799
3800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3801 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3802 ASSERT_EQ(DEVICE_ID, args.deviceId);
3803 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3804 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3806 ASSERT_EQ(0, args.flags);
3807 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3808 ASSERT_EQ(0, args.buttonState);
3809 ASSERT_EQ(0, args.edgeFlags);
3810 ASSERT_EQ(uint32_t(1), args.pointerCount);
3811 ASSERT_EQ(0, args.pointerProperties[0].id);
3812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3814 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3815 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3816 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3817 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3818}
3819
3820TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003822 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823
3824 NotifyMotionArgs args;
3825
3826 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003827 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3828 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3832 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3833
3834 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003835 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3836 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3840 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3841}
3842
3843TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003844 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003845 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846
3847 NotifyMotionArgs args;
3848
3849 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003850 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3851 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3853 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3855 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3856
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3860 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3861
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003863 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3864 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3868 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3869
3870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3873 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3874}
3875
3876TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003878 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879
3880 NotifyMotionArgs args;
3881
3882 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003883 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3884 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3885 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3886 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3888 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3890 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3891 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3892
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3896 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3897 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3898
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003900 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3901 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3902 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3906 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3907 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3908
3909 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003910 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3911 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3915 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3916
3917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3920 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3921}
3922
3923TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003925 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003927 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3930 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3931 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3932 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3933 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3934 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3935 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3936}
3937
3938TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939 addConfigurationProperty("cursor.mode", "navigation");
3940 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003941 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003943 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3945 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3946 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3947 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3948 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3949 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3950 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3951 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3952
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003953 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3955 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3956 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3957 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3958 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3959 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3960 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3961 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3962
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003963 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3965 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3966 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3967 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3968 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3969 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3970 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3971 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3972
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003973 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3975 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3976 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3977 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3978 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3979 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3982}
3983
3984TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003986 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987
3988 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3989 mFakePointerController->setPosition(100, 200);
3990 mFakePointerController->setButtonState(0);
3991
3992 NotifyMotionArgs motionArgs;
3993 NotifyKeyArgs keyArgs;
3994
3995 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003996 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3997 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3999 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4000 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4001 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4003 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4004
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4007 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4008 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4010 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4011
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004012 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
4013 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 ASSERT_EQ(0, motionArgs.buttonState);
4017 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4019 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4020
4021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004022 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 ASSERT_EQ(0, motionArgs.buttonState);
4024 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4026 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4027
4028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004030 ASSERT_EQ(0, motionArgs.buttonState);
4031 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4033 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4034
4035 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004036 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
4037 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
4038 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4040 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4041 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4042 motionArgs.buttonState);
4043 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4044 mFakePointerController->getButtonState());
4045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4046 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4047
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4050 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4051 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4052 mFakePointerController->getButtonState());
4053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4054 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4055
4056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4058 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4059 motionArgs.buttonState);
4060 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4061 mFakePointerController->getButtonState());
4062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4063 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4064
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004065 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
4066 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4070 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4072 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4073
4074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004076 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4077 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4079 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4080
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004081 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
4082 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4085 ASSERT_EQ(0, motionArgs.buttonState);
4086 ASSERT_EQ(0, mFakePointerController->getButtonState());
4087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4088 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 -08004089 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
4090 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004091
4092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 ASSERT_EQ(0, motionArgs.buttonState);
4094 ASSERT_EQ(0, mFakePointerController->getButtonState());
4095 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4097 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 -08004098
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4100 ASSERT_EQ(0, motionArgs.buttonState);
4101 ASSERT_EQ(0, mFakePointerController->getButtonState());
4102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4104 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4105
4106 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004107 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
4108 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4110 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4111 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004112
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004114 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4116 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4118 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4119
4120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4122 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4123 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4125 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4126
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004127 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
4128 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 ASSERT_EQ(0, motionArgs.buttonState);
4132 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4134 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4135
4136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004138 ASSERT_EQ(0, motionArgs.buttonState);
4139 ASSERT_EQ(0, mFakePointerController->getButtonState());
4140
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4144 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4145 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4146
4147 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004148 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
4149 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4151 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4152 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004153
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4157 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4159 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4160
4161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4163 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4164 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4166 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4167
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004168 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
4169 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004171 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 ASSERT_EQ(0, motionArgs.buttonState);
4173 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4175 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 -08004176
4177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4179 ASSERT_EQ(0, motionArgs.buttonState);
4180 ASSERT_EQ(0, mFakePointerController->getButtonState());
4181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4182 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4183
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4185 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4186 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4187
4188 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004189 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
4190 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4192 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4193 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4198 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4200 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4201
4202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4203 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4204 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4205 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4207 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4208
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004209 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
4210 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 ASSERT_EQ(0, motionArgs.buttonState);
4214 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4216 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 -08004217
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4219 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4220 ASSERT_EQ(0, motionArgs.buttonState);
4221 ASSERT_EQ(0, mFakePointerController->getButtonState());
4222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4223 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4224
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4227 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4228
4229 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004230 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
4231 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4233 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4234 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004235
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004237 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4239 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4241 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4242
4243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4245 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4246 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4248 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4249
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004250 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
4251 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004253 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_EQ(0, motionArgs.buttonState);
4255 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4257 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 -08004258
4259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4260 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4261 ASSERT_EQ(0, motionArgs.buttonState);
4262 ASSERT_EQ(0, mFakePointerController->getButtonState());
4263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4264 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4265
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4267 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4268 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4269}
4270
4271TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
4275 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4276 mFakePointerController->setPosition(100, 200);
4277 mFakePointerController->setButtonState(0);
4278
4279 NotifyMotionArgs args;
4280
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004281 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4282 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4283 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004285 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4288 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 +01004289 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004290}
4291
4292TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004293 addConfigurationProperty("cursor.mode", "pointer");
4294 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004295 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004296
4297 NotifyDeviceResetArgs resetArgs;
4298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4299 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4300 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4301
4302 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4303 mFakePointerController->setPosition(100, 200);
4304 mFakePointerController->setButtonState(0);
4305
4306 NotifyMotionArgs args;
4307
4308 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004309 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4310 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4311 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4313 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4316 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 +01004317 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004318
4319 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004320 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4321 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4323 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4326 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4328 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4331 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4332
4333 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004334 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
4335 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4337 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4340 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4342 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4345 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4346
4347 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004348 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
4349 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
4350 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4352 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4355 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 +01004356 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004357
4358 // Disable pointer capture and check that the device generation got bumped
4359 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004360 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004361 mFakePolicy->setPointerCapture(false);
4362 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004363 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004364
4365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4366 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4367 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4368
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004369 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4370 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4371 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4373 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4376 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 +01004377 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378}
4379
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004380TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004381 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004382
Garfield Tan888a6a42020-01-09 11:39:16 -08004383 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004384 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004385 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4386 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004387 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4388 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004389 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4390 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4391
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004392 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4393 mFakePointerController->setPosition(100, 200);
4394 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004395
4396 NotifyMotionArgs args;
4397 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4398 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4399 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4401 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4404 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 +01004405 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004406 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4407}
4408
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409// --- TouchInputMapperTest ---
4410
4411class TouchInputMapperTest : public InputMapperTest {
4412protected:
4413 static const int32_t RAW_X_MIN;
4414 static const int32_t RAW_X_MAX;
4415 static const int32_t RAW_Y_MIN;
4416 static const int32_t RAW_Y_MAX;
4417 static const int32_t RAW_TOUCH_MIN;
4418 static const int32_t RAW_TOUCH_MAX;
4419 static const int32_t RAW_TOOL_MIN;
4420 static const int32_t RAW_TOOL_MAX;
4421 static const int32_t RAW_PRESSURE_MIN;
4422 static const int32_t RAW_PRESSURE_MAX;
4423 static const int32_t RAW_ORIENTATION_MIN;
4424 static const int32_t RAW_ORIENTATION_MAX;
4425 static const int32_t RAW_DISTANCE_MIN;
4426 static const int32_t RAW_DISTANCE_MAX;
4427 static const int32_t RAW_TILT_MIN;
4428 static const int32_t RAW_TILT_MAX;
4429 static const int32_t RAW_ID_MIN;
4430 static const int32_t RAW_ID_MAX;
4431 static const int32_t RAW_SLOT_MIN;
4432 static const int32_t RAW_SLOT_MAX;
4433 static const float X_PRECISION;
4434 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004435 static const float X_PRECISION_VIRTUAL;
4436 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437
4438 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004439 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440
4441 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4442
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004443 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004444 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004445
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 enum Axes {
4447 POSITION = 1 << 0,
4448 TOUCH = 1 << 1,
4449 TOOL = 1 << 2,
4450 PRESSURE = 1 << 3,
4451 ORIENTATION = 1 << 4,
4452 MINOR = 1 << 5,
4453 ID = 1 << 6,
4454 DISTANCE = 1 << 7,
4455 TILT = 1 << 8,
4456 SLOT = 1 << 9,
4457 TOOL_TYPE = 1 << 10,
4458 };
4459
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004460 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4461 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004462 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004464 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 int32_t toRawX(float displayX);
4466 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004467 float toCookedX(float rawX, float rawY);
4468 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004470 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004472 float toDisplayY(int32_t rawY, int32_t displayHeight);
4473
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474};
4475
4476const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4477const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4478const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4479const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4480const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4481const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4482const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4483const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004484const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4485const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4487const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4488const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4489const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4490const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4491const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4492const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4493const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4494const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4495const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4496const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4497const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004498const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4499 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4500const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4501 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004502const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4503 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504
4505const float TouchInputMapperTest::GEOMETRIC_SCALE =
4506 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4507 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4508
4509const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4510 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4511 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4512};
4513
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004514void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004515 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4516 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004517}
4518
4519void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4520 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4521 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522}
4523
Santos Cordonfa5cf462017-04-05 10:37:00 -07004524void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004525 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4526 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4527 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004528}
4529
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004531 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4532 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4533 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535}
4536
Jason Gerecke489fda82012-09-07 17:19:40 -07004537void TouchInputMapperTest::prepareLocationCalibration() {
4538 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4539}
4540
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541int32_t TouchInputMapperTest::toRawX(float displayX) {
4542 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4543}
4544
4545int32_t TouchInputMapperTest::toRawY(float displayY) {
4546 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4547}
4548
Jason Gerecke489fda82012-09-07 17:19:40 -07004549float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4550 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4551 return rawX;
4552}
4553
4554float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4555 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4556 return rawY;
4557}
4558
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004560 return toDisplayX(rawX, DISPLAY_WIDTH);
4561}
4562
4563float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4564 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565}
4566
4567float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004568 return toDisplayY(rawY, DISPLAY_HEIGHT);
4569}
4570
4571float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4572 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573}
4574
4575
4576// --- SingleTouchInputMapperTest ---
4577
4578class SingleTouchInputMapperTest : public TouchInputMapperTest {
4579protected:
4580 void prepareButtons();
4581 void prepareAxes(int axes);
4582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004583 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4584 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4585 void processUp(SingleTouchInputMapper& mappery);
4586 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4587 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4588 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4589 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4590 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4591 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592};
4593
4594void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004595 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596}
4597
4598void SingleTouchInputMapperTest::prepareAxes(int axes) {
4599 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004600 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4601 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 }
4603 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004604 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4605 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 }
4607 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004608 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4609 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 }
4611 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004612 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4613 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 }
4615 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004616 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4617 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 }
4619}
4620
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004621void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004622 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4623 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4624 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625}
4626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004627void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004628 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4629 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630}
4631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004632void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004633 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634}
4635
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004636void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004637 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638}
4639
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004640void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4641 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004642 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643}
4644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004645void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004646 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647}
4648
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004649void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4650 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004651 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4652 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653}
4654
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004655void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4656 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004657 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658}
4659
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004660void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004661 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662}
4663
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 prepareButtons();
4666 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004667 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004669 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670}
4671
4672TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004673 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4674 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 prepareButtons();
4676 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004677 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004679 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680}
4681
4682TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 prepareButtons();
4684 prepareAxes(POSITION);
4685 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004686 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004688 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689}
4690
4691TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 prepareButtons();
4693 prepareAxes(POSITION);
4694 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004695 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004697 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698}
4699
4700TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 addConfigurationProperty("touch.deviceType", "touchScreen");
4702 prepareDisplay(DISPLAY_ORIENTATION_0);
4703 prepareButtons();
4704 prepareAxes(POSITION);
4705 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004706 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707
4708 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004709 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710
4711 // Virtual key is down.
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4717
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004718 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719
4720 // Virtual key is up.
4721 processUp(mapper);
4722 processSync(mapper);
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4724
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004725 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726}
4727
4728TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729 addConfigurationProperty("touch.deviceType", "touchScreen");
4730 prepareDisplay(DISPLAY_ORIENTATION_0);
4731 prepareButtons();
4732 prepareAxes(POSITION);
4733 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004734 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735
4736 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004737 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738
4739 // Virtual key is down.
4740 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4741 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4742 processDown(mapper, x, y);
4743 processSync(mapper);
4744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004746 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747
4748 // Virtual key is up.
4749 processUp(mapper);
4750 processSync(mapper);
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004753 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754}
4755
4756TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 addConfigurationProperty("touch.deviceType", "touchScreen");
4758 prepareDisplay(DISPLAY_ORIENTATION_0);
4759 prepareButtons();
4760 prepareAxes(POSITION);
4761 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004762 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763
4764 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4765 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004766 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767 ASSERT_TRUE(flags[0]);
4768 ASSERT_FALSE(flags[1]);
4769}
4770
4771TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 addConfigurationProperty("touch.deviceType", "touchScreen");
4773 prepareDisplay(DISPLAY_ORIENTATION_0);
4774 prepareButtons();
4775 prepareAxes(POSITION);
4776 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004777 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778
arthurhungdcef2dc2020-08-11 14:47:50 +08004779 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780
4781 NotifyKeyArgs args;
4782
4783 // Press virtual key.
4784 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4785 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4786 processDown(mapper, x, y);
4787 processSync(mapper);
4788
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4790 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4791 ASSERT_EQ(DEVICE_ID, args.deviceId);
4792 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4793 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4794 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4795 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4796 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4797 ASSERT_EQ(KEY_HOME, args.scanCode);
4798 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4799 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4800
4801 // Release virtual key.
4802 processUp(mapper);
4803 processSync(mapper);
4804
4805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4806 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4807 ASSERT_EQ(DEVICE_ID, args.deviceId);
4808 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4809 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4810 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4811 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4812 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4813 ASSERT_EQ(KEY_HOME, args.scanCode);
4814 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4815 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4816
4817 // Should not have sent any motions.
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4819}
4820
4821TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 addConfigurationProperty("touch.deviceType", "touchScreen");
4823 prepareDisplay(DISPLAY_ORIENTATION_0);
4824 prepareButtons();
4825 prepareAxes(POSITION);
4826 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004827 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828
arthurhungdcef2dc2020-08-11 14:47:50 +08004829 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830
4831 NotifyKeyArgs keyArgs;
4832
4833 // Press virtual key.
4834 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4835 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4836 processDown(mapper, x, y);
4837 processSync(mapper);
4838
4839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4840 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4841 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4842 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4843 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4844 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4845 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4846 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4847 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4848 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4849 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4850
4851 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4852 // into the display area.
4853 y -= 100;
4854 processMove(mapper, x, y);
4855 processSync(mapper);
4856
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4858 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4859 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4860 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4861 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4862 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4863 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4864 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4865 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4866 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4868 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4869
4870 NotifyMotionArgs motionArgs;
4871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4872 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4873 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4874 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4875 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4876 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4877 ASSERT_EQ(0, motionArgs.flags);
4878 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4879 ASSERT_EQ(0, motionArgs.buttonState);
4880 ASSERT_EQ(0, motionArgs.edgeFlags);
4881 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4882 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4885 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4886 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4887 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4888 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4889
4890 // Keep moving out of bounds. Should generate a pointer move.
4891 y -= 50;
4892 processMove(mapper, x, y);
4893 processSync(mapper);
4894
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4897 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4898 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4899 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4901 ASSERT_EQ(0, motionArgs.flags);
4902 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4903 ASSERT_EQ(0, motionArgs.buttonState);
4904 ASSERT_EQ(0, motionArgs.edgeFlags);
4905 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4909 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4910 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4911 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4913
4914 // Release out of bounds. Should generate a pointer up.
4915 processUp(mapper);
4916 processSync(mapper);
4917
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4920 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4921 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4922 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4923 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4924 ASSERT_EQ(0, motionArgs.flags);
4925 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4926 ASSERT_EQ(0, motionArgs.buttonState);
4927 ASSERT_EQ(0, motionArgs.edgeFlags);
4928 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4929 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4932 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4933 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4934 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4936
4937 // Should not have sent any more keys or motions.
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4940}
4941
4942TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943 addConfigurationProperty("touch.deviceType", "touchScreen");
4944 prepareDisplay(DISPLAY_ORIENTATION_0);
4945 prepareButtons();
4946 prepareAxes(POSITION);
4947 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004948 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949
arthurhungdcef2dc2020-08-11 14:47:50 +08004950 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951
4952 NotifyMotionArgs motionArgs;
4953
4954 // Initially go down out of bounds.
4955 int32_t x = -10;
4956 int32_t y = -10;
4957 processDown(mapper, x, y);
4958 processSync(mapper);
4959
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4961
4962 // Move into the display area. Should generate a pointer down.
4963 x = 50;
4964 y = 75;
4965 processMove(mapper, x, y);
4966 processSync(mapper);
4967
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4970 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4971 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4972 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4973 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4974 ASSERT_EQ(0, motionArgs.flags);
4975 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4976 ASSERT_EQ(0, motionArgs.buttonState);
4977 ASSERT_EQ(0, motionArgs.edgeFlags);
4978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4982 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4983 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4984 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4985 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4986
4987 // Release. Should generate a pointer up.
4988 processUp(mapper);
4989 processSync(mapper);
4990
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4992 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4993 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4994 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4995 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4996 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4997 ASSERT_EQ(0, motionArgs.flags);
4998 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4999 ASSERT_EQ(0, motionArgs.buttonState);
5000 ASSERT_EQ(0, motionArgs.edgeFlags);
5001 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5002 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5005 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5006 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5007 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5008 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5009
5010 // Should not have sent any more keys or motions.
5011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5013}
5014
Santos Cordonfa5cf462017-04-05 10:37:00 -07005015TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005016 addConfigurationProperty("touch.deviceType", "touchScreen");
5017 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5018
5019 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5020 prepareButtons();
5021 prepareAxes(POSITION);
5022 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005023 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005024
arthurhungdcef2dc2020-08-11 14:47:50 +08005025 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005026
5027 NotifyMotionArgs motionArgs;
5028
5029 // Down.
5030 int32_t x = 100;
5031 int32_t y = 125;
5032 processDown(mapper, x, y);
5033 processSync(mapper);
5034
5035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5036 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5037 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5038 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5039 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5040 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5042 ASSERT_EQ(0, motionArgs.flags);
5043 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5044 ASSERT_EQ(0, motionArgs.buttonState);
5045 ASSERT_EQ(0, motionArgs.edgeFlags);
5046 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5047 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5048 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5050 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5051 1, 0, 0, 0, 0, 0, 0, 0));
5052 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5053 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5055
5056 // Move.
5057 x += 50;
5058 y += 75;
5059 processMove(mapper, x, y);
5060 processSync(mapper);
5061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5065 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5066 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5067 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5069 ASSERT_EQ(0, motionArgs.flags);
5070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5071 ASSERT_EQ(0, motionArgs.buttonState);
5072 ASSERT_EQ(0, motionArgs.edgeFlags);
5073 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5074 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5077 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5078 1, 0, 0, 0, 0, 0, 0, 0));
5079 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5080 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5082
5083 // Up.
5084 processUp(mapper);
5085 processSync(mapper);
5086
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5089 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5090 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5091 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5092 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5093 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5094 ASSERT_EQ(0, motionArgs.flags);
5095 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5096 ASSERT_EQ(0, motionArgs.buttonState);
5097 ASSERT_EQ(0, motionArgs.edgeFlags);
5098 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5099 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5102 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5103 1, 0, 0, 0, 0, 0, 0, 0));
5104 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5105 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5106 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5107
5108 // Should not have sent any more keys or motions.
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5111}
5112
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114 addConfigurationProperty("touch.deviceType", "touchScreen");
5115 prepareDisplay(DISPLAY_ORIENTATION_0);
5116 prepareButtons();
5117 prepareAxes(POSITION);
5118 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005119 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120
arthurhungdcef2dc2020-08-11 14:47:50 +08005121 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122
5123 NotifyMotionArgs motionArgs;
5124
5125 // Down.
5126 int32_t x = 100;
5127 int32_t y = 125;
5128 processDown(mapper, x, y);
5129 processSync(mapper);
5130
5131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5132 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5133 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5134 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5135 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5136 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5137 ASSERT_EQ(0, motionArgs.flags);
5138 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5139 ASSERT_EQ(0, motionArgs.buttonState);
5140 ASSERT_EQ(0, motionArgs.edgeFlags);
5141 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5142 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5145 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5146 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5147 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5149
5150 // Move.
5151 x += 50;
5152 y += 75;
5153 processMove(mapper, x, y);
5154 processSync(mapper);
5155
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5158 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5159 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5160 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5162 ASSERT_EQ(0, motionArgs.flags);
5163 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5164 ASSERT_EQ(0, motionArgs.buttonState);
5165 ASSERT_EQ(0, motionArgs.edgeFlags);
5166 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5167 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5170 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5171 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5172 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5173 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5174
5175 // Up.
5176 processUp(mapper);
5177 processSync(mapper);
5178
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5180 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5181 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5182 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5183 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5184 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5185 ASSERT_EQ(0, motionArgs.flags);
5186 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5187 ASSERT_EQ(0, motionArgs.buttonState);
5188 ASSERT_EQ(0, motionArgs.edgeFlags);
5189 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5190 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5193 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5194 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5195 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5196 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5197
5198 // Should not have sent any more keys or motions.
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5201}
5202
5203TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204 addConfigurationProperty("touch.deviceType", "touchScreen");
5205 prepareButtons();
5206 prepareAxes(POSITION);
5207 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005208 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209
5210 NotifyMotionArgs args;
5211
5212 // Rotation 90.
5213 prepareDisplay(DISPLAY_ORIENTATION_90);
5214 processDown(mapper, toRawX(50), toRawY(75));
5215 processSync(mapper);
5216
5217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5218 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5219 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5220
5221 processUp(mapper);
5222 processSync(mapper);
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5224}
5225
5226TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227 addConfigurationProperty("touch.deviceType", "touchScreen");
5228 prepareButtons();
5229 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005230 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005231
5232 NotifyMotionArgs args;
5233
5234 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005235 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236 prepareDisplay(DISPLAY_ORIENTATION_0);
5237 processDown(mapper, toRawX(50), toRawY(75));
5238 processSync(mapper);
5239
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5241 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5242 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5243
5244 processUp(mapper);
5245 processSync(mapper);
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5247
5248 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005249 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250 prepareDisplay(DISPLAY_ORIENTATION_90);
5251 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5252 processSync(mapper);
5253
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5255 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5256 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5257
5258 processUp(mapper);
5259 processSync(mapper);
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5261
5262 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005263 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 prepareDisplay(DISPLAY_ORIENTATION_180);
5265 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5266 processSync(mapper);
5267
5268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5269 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5270 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5271
5272 processUp(mapper);
5273 processSync(mapper);
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5275
5276 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005277 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278 prepareDisplay(DISPLAY_ORIENTATION_270);
5279 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5280 processSync(mapper);
5281
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5283 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5284 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5285
5286 processUp(mapper);
5287 processSync(mapper);
5288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5289}
5290
5291TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292 addConfigurationProperty("touch.deviceType", "touchScreen");
5293 prepareDisplay(DISPLAY_ORIENTATION_0);
5294 prepareButtons();
5295 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005296 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297
5298 // These calculations are based on the input device calibration documentation.
5299 int32_t rawX = 100;
5300 int32_t rawY = 200;
5301 int32_t rawPressure = 10;
5302 int32_t rawToolMajor = 12;
5303 int32_t rawDistance = 2;
5304 int32_t rawTiltX = 30;
5305 int32_t rawTiltY = 110;
5306
5307 float x = toDisplayX(rawX);
5308 float y = toDisplayY(rawY);
5309 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5310 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5311 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5312 float distance = float(rawDistance);
5313
5314 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5315 float tiltScale = M_PI / 180;
5316 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5317 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5318 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5319 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5320
5321 processDown(mapper, rawX, rawY);
5322 processPressure(mapper, rawPressure);
5323 processToolMajor(mapper, rawToolMajor);
5324 processDistance(mapper, rawDistance);
5325 processTilt(mapper, rawTiltX, rawTiltY);
5326 processSync(mapper);
5327
5328 NotifyMotionArgs args;
5329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5331 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5332 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5333}
5334
Jason Gerecke489fda82012-09-07 17:19:40 -07005335TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005336 addConfigurationProperty("touch.deviceType", "touchScreen");
5337 prepareDisplay(DISPLAY_ORIENTATION_0);
5338 prepareLocationCalibration();
5339 prepareButtons();
5340 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005341 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005342
5343 int32_t rawX = 100;
5344 int32_t rawY = 200;
5345
5346 float x = toDisplayX(toCookedX(rawX, rawY));
5347 float y = toDisplayY(toCookedY(rawX, rawY));
5348
5349 processDown(mapper, rawX, rawY);
5350 processSync(mapper);
5351
5352 NotifyMotionArgs args;
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5355 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5356}
5357
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359 addConfigurationProperty("touch.deviceType", "touchScreen");
5360 prepareDisplay(DISPLAY_ORIENTATION_0);
5361 prepareButtons();
5362 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005363 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364
5365 NotifyMotionArgs motionArgs;
5366 NotifyKeyArgs keyArgs;
5367
5368 processDown(mapper, 100, 200);
5369 processSync(mapper);
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5372 ASSERT_EQ(0, motionArgs.buttonState);
5373
5374 // press BTN_LEFT, release BTN_LEFT
5375 processKey(mapper, BTN_LEFT, 1);
5376 processSync(mapper);
5377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5379 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5380
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5383 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5384
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385 processKey(mapper, BTN_LEFT, 0);
5386 processSync(mapper);
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005390
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005393 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394
5395 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5396 processKey(mapper, BTN_RIGHT, 1);
5397 processKey(mapper, BTN_MIDDLE, 1);
5398 processSync(mapper);
5399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5401 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5402 motionArgs.buttonState);
5403
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5406 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5407
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5410 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5411 motionArgs.buttonState);
5412
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 processKey(mapper, BTN_RIGHT, 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(AMOTION_EVENT_BUTTON_TERTIARY, 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(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422
5423 processKey(mapper, BTN_MIDDLE, 0);
5424 processSync(mapper);
5425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005426 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005428
5429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005431 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432
5433 // press BTN_BACK, release BTN_BACK
5434 processKey(mapper, BTN_BACK, 1);
5435 processSync(mapper);
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5438 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005439
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005442 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5443
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5446 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447
5448 processKey(mapper, BTN_BACK, 0);
5449 processSync(mapper);
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005453
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005456 ASSERT_EQ(0, motionArgs.buttonState);
5457
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5460 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5461
5462 // press BTN_SIDE, release BTN_SIDE
5463 processKey(mapper, BTN_SIDE, 1);
5464 processSync(mapper);
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5466 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5467 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005468
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005471 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5472
5473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5475 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476
5477 processKey(mapper, BTN_SIDE, 0);
5478 processSync(mapper);
5479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005482
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005485 ASSERT_EQ(0, motionArgs.buttonState);
5486
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5488 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5489 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5490
5491 // press BTN_FORWARD, release BTN_FORWARD
5492 processKey(mapper, BTN_FORWARD, 1);
5493 processSync(mapper);
5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5495 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5496 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005497
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005500 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5501
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5504 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505
5506 processKey(mapper, BTN_FORWARD, 0);
5507 processSync(mapper);
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005511
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005514 ASSERT_EQ(0, motionArgs.buttonState);
5515
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5517 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5518 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5519
5520 // press BTN_EXTRA, release BTN_EXTRA
5521 processKey(mapper, BTN_EXTRA, 1);
5522 processSync(mapper);
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5524 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5525 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005526
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005529 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5530
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5533 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534
5535 processKey(mapper, BTN_EXTRA, 0);
5536 processSync(mapper);
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005543 ASSERT_EQ(0, motionArgs.buttonState);
5544
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5546 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5547 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5548
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5550
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551 // press BTN_STYLUS, release BTN_STYLUS
5552 processKey(mapper, BTN_STYLUS, 1);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005556 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5560 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561
5562 processKey(mapper, BTN_STYLUS, 0);
5563 processSync(mapper);
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005570 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571
5572 // press BTN_STYLUS2, release BTN_STYLUS2
5573 processKey(mapper, BTN_STYLUS2, 1);
5574 processSync(mapper);
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005577 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5578
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5581 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582
5583 processKey(mapper, BTN_STYLUS2, 0);
5584 processSync(mapper);
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005588
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005591 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592
5593 // release touch
5594 processUp(mapper);
5595 processSync(mapper);
5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5597 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5598 ASSERT_EQ(0, motionArgs.buttonState);
5599}
5600
5601TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602 addConfigurationProperty("touch.deviceType", "touchScreen");
5603 prepareDisplay(DISPLAY_ORIENTATION_0);
5604 prepareButtons();
5605 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005606 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607
5608 NotifyMotionArgs motionArgs;
5609
5610 // default tool type is finger
5611 processDown(mapper, 100, 200);
5612 processSync(mapper);
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5616
5617 // eraser
5618 processKey(mapper, BTN_TOOL_RUBBER, 1);
5619 processSync(mapper);
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5623
5624 // stylus
5625 processKey(mapper, BTN_TOOL_RUBBER, 0);
5626 processKey(mapper, BTN_TOOL_PEN, 1);
5627 processSync(mapper);
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5631
5632 // brush
5633 processKey(mapper, BTN_TOOL_PEN, 0);
5634 processKey(mapper, BTN_TOOL_BRUSH, 1);
5635 processSync(mapper);
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5639
5640 // pencil
5641 processKey(mapper, BTN_TOOL_BRUSH, 0);
5642 processKey(mapper, BTN_TOOL_PENCIL, 1);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5647
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005648 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 processKey(mapper, BTN_TOOL_PENCIL, 0);
5650 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5651 processSync(mapper);
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5655
5656 // mouse
5657 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5658 processKey(mapper, BTN_TOOL_MOUSE, 1);
5659 processSync(mapper);
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5663
5664 // lens
5665 processKey(mapper, BTN_TOOL_MOUSE, 0);
5666 processKey(mapper, BTN_TOOL_LENS, 1);
5667 processSync(mapper);
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5671
5672 // double-tap
5673 processKey(mapper, BTN_TOOL_LENS, 0);
5674 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5675 processSync(mapper);
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5679
5680 // triple-tap
5681 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5682 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5683 processSync(mapper);
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5687
5688 // quad-tap
5689 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5690 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5691 processSync(mapper);
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5695
5696 // finger
5697 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5698 processKey(mapper, BTN_TOOL_FINGER, 1);
5699 processSync(mapper);
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5703
5704 // stylus trumps finger
5705 processKey(mapper, BTN_TOOL_PEN, 1);
5706 processSync(mapper);
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5710
5711 // eraser trumps stylus
5712 processKey(mapper, BTN_TOOL_RUBBER, 1);
5713 processSync(mapper);
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5717
5718 // mouse trumps eraser
5719 processKey(mapper, BTN_TOOL_MOUSE, 1);
5720 processSync(mapper);
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5724
5725 // back to default tool type
5726 processKey(mapper, BTN_TOOL_MOUSE, 0);
5727 processKey(mapper, BTN_TOOL_RUBBER, 0);
5728 processKey(mapper, BTN_TOOL_PEN, 0);
5729 processKey(mapper, BTN_TOOL_FINGER, 0);
5730 processSync(mapper);
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5734}
5735
5736TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737 addConfigurationProperty("touch.deviceType", "touchScreen");
5738 prepareDisplay(DISPLAY_ORIENTATION_0);
5739 prepareButtons();
5740 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005741 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005742 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005743
5744 NotifyMotionArgs motionArgs;
5745
5746 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5747 processKey(mapper, BTN_TOOL_FINGER, 1);
5748 processMove(mapper, 100, 200);
5749 processSync(mapper);
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5753 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5754
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5758 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5759
5760 // move a little
5761 processMove(mapper, 150, 250);
5762 processSync(mapper);
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5764 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5766 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5767
5768 // down when BTN_TOUCH is pressed, pressure defaults to 1
5769 processKey(mapper, BTN_TOUCH, 1);
5770 processSync(mapper);
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5774 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5775
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5777 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5779 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5780
5781 // up when BTN_TOUCH is released, hover restored
5782 processKey(mapper, BTN_TOUCH, 0);
5783 processSync(mapper);
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5787 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5788
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5792 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5793
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5795 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5797 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5798
5799 // exit hover when pointer goes away
5800 processKey(mapper, BTN_TOOL_FINGER, 0);
5801 processSync(mapper);
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5803 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5805 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5806}
5807
5808TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809 addConfigurationProperty("touch.deviceType", "touchScreen");
5810 prepareDisplay(DISPLAY_ORIENTATION_0);
5811 prepareButtons();
5812 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005813 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814
5815 NotifyMotionArgs motionArgs;
5816
5817 // initially hovering because pressure is 0
5818 processDown(mapper, 100, 200);
5819 processPressure(mapper, 0);
5820 processSync(mapper);
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5824 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5825
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5827 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5829 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5830
5831 // move a little
5832 processMove(mapper, 150, 250);
5833 processSync(mapper);
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5838
5839 // down when pressure is non-zero
5840 processPressure(mapper, RAW_PRESSURE_MAX);
5841 processSync(mapper);
5842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5845 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5846
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5850 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5851
5852 // up when pressure becomes 0, hover restored
5853 processPressure(mapper, 0);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5856 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5858 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5859
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5863 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5868 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5869
5870 // exit hover when pointer goes away
5871 processUp(mapper);
5872 processSync(mapper);
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5874 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5876 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5877}
5878
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879// --- MultiTouchInputMapperTest ---
5880
5881class MultiTouchInputMapperTest : public TouchInputMapperTest {
5882protected:
5883 void prepareAxes(int axes);
5884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005885 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5886 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5887 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5888 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5889 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5890 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5891 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5892 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5893 void processId(MultiTouchInputMapper& mapper, int32_t id);
5894 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5895 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5896 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5897 void processMTSync(MultiTouchInputMapper& mapper);
5898 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899};
5900
5901void MultiTouchInputMapperTest::prepareAxes(int axes) {
5902 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5904 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 }
5906 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005907 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5908 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5911 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 }
5913 }
5914 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005915 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5916 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5919 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 }
5921 }
5922 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005923 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5924 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925 }
5926 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005927 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5928 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929 }
5930 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005931 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5932 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933 }
5934 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005935 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5936 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 }
5938 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005939 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5940 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 }
5942 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005943 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 }
5945}
5946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005947void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5948 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005949 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5950 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951}
5952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005953void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5954 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005955 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956}
5957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005958void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5959 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005960 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961}
5962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005963void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005964 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965}
5966
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005967void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005968 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969}
5970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005971void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5972 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005973 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974}
5975
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005976void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005977 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978}
5979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005980void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005981 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982}
5983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005984void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005985 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986}
5987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005988void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005989 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005990}
5991
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005992void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005993 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994}
5995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005996void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5997 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005998 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999}
6000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006001void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006002 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003}
6004
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006005void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006006 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007}
6008
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 addConfigurationProperty("touch.deviceType", "touchScreen");
6011 prepareDisplay(DISPLAY_ORIENTATION_0);
6012 prepareAxes(POSITION);
6013 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006014 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015
arthurhungdcef2dc2020-08-11 14:47:50 +08006016 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017
6018 NotifyMotionArgs motionArgs;
6019
6020 // Two fingers down at once.
6021 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6022 processPosition(mapper, x1, y1);
6023 processMTSync(mapper);
6024 processPosition(mapper, x2, y2);
6025 processMTSync(mapper);
6026 processSync(mapper);
6027
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6030 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6031 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6032 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6034 ASSERT_EQ(0, motionArgs.flags);
6035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6036 ASSERT_EQ(0, motionArgs.buttonState);
6037 ASSERT_EQ(0, motionArgs.edgeFlags);
6038 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6039 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6042 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6043 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6044 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6045 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6046
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6049 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6050 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6051 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6053 motionArgs.action);
6054 ASSERT_EQ(0, motionArgs.flags);
6055 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6056 ASSERT_EQ(0, motionArgs.buttonState);
6057 ASSERT_EQ(0, motionArgs.edgeFlags);
6058 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6061 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6064 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6066 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6067 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6068 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6069 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6070
6071 // Move.
6072 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6073 processPosition(mapper, x1, y1);
6074 processMTSync(mapper);
6075 processPosition(mapper, x2, y2);
6076 processMTSync(mapper);
6077 processSync(mapper);
6078
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6081 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6082 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6083 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6085 ASSERT_EQ(0, motionArgs.flags);
6086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6087 ASSERT_EQ(0, motionArgs.buttonState);
6088 ASSERT_EQ(0, motionArgs.edgeFlags);
6089 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6090 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6092 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6095 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6097 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6098 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6099 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6100 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6101
6102 // First finger up.
6103 x2 += 15; y2 -= 20;
6104 processPosition(mapper, x2, y2);
6105 processMTSync(mapper);
6106 processSync(mapper);
6107
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6109 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6110 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6111 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6112 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6113 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6114 motionArgs.action);
6115 ASSERT_EQ(0, motionArgs.flags);
6116 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6117 ASSERT_EQ(0, motionArgs.buttonState);
6118 ASSERT_EQ(0, motionArgs.edgeFlags);
6119 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6120 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6122 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6125 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6127 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6128 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6129 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6130 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6131
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6134 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6135 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6136 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6138 ASSERT_EQ(0, motionArgs.flags);
6139 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6140 ASSERT_EQ(0, motionArgs.buttonState);
6141 ASSERT_EQ(0, motionArgs.edgeFlags);
6142 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6143 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6146 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6147 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6148 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6149 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6150
6151 // Move.
6152 x2 += 20; y2 -= 25;
6153 processPosition(mapper, x2, y2);
6154 processMTSync(mapper);
6155 processSync(mapper);
6156
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6158 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6159 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6160 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6161 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6163 ASSERT_EQ(0, motionArgs.flags);
6164 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6165 ASSERT_EQ(0, motionArgs.buttonState);
6166 ASSERT_EQ(0, motionArgs.edgeFlags);
6167 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6168 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6171 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6172 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6173 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6174 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6175
6176 // New finger down.
6177 int32_t x3 = 700, y3 = 300;
6178 processPosition(mapper, x2, y2);
6179 processMTSync(mapper);
6180 processPosition(mapper, x3, y3);
6181 processMTSync(mapper);
6182 processSync(mapper);
6183
6184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6186 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6187 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6188 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6190 motionArgs.action);
6191 ASSERT_EQ(0, motionArgs.flags);
6192 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6193 ASSERT_EQ(0, motionArgs.buttonState);
6194 ASSERT_EQ(0, motionArgs.edgeFlags);
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(x3), toDisplayY(y3), 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 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6205 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6207
6208 // Second finger up.
6209 x3 += 30; y3 -= 20;
6210 processPosition(mapper, x3, y3);
6211 processMTSync(mapper);
6212 processSync(mapper);
6213
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6216 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6217 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6218 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6220 motionArgs.action);
6221 ASSERT_EQ(0, motionArgs.flags);
6222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6223 ASSERT_EQ(0, motionArgs.buttonState);
6224 ASSERT_EQ(0, motionArgs.edgeFlags);
6225 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6228 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6231 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6233 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6234 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6235 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6237
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6240 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6241 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6242 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6244 ASSERT_EQ(0, motionArgs.flags);
6245 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6246 ASSERT_EQ(0, motionArgs.buttonState);
6247 ASSERT_EQ(0, motionArgs.edgeFlags);
6248 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6249 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6252 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6253 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6254 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6256
6257 // Last finger up.
6258 processMTSync(mapper);
6259 processSync(mapper);
6260
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6263 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6264 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6265 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6267 ASSERT_EQ(0, motionArgs.flags);
6268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6269 ASSERT_EQ(0, motionArgs.buttonState);
6270 ASSERT_EQ(0, motionArgs.edgeFlags);
6271 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6272 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6275 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6276 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6277 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6278 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6279
6280 // Should not have sent any more keys or motions.
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6283}
6284
6285TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006286 addConfigurationProperty("touch.deviceType", "touchScreen");
6287 prepareDisplay(DISPLAY_ORIENTATION_0);
6288 prepareAxes(POSITION | ID);
6289 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006290 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291
arthurhungdcef2dc2020-08-11 14:47:50 +08006292 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293
6294 NotifyMotionArgs motionArgs;
6295
6296 // Two fingers down at once.
6297 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6298 processPosition(mapper, x1, y1);
6299 processId(mapper, 1);
6300 processMTSync(mapper);
6301 processPosition(mapper, x2, y2);
6302 processId(mapper, 2);
6303 processMTSync(mapper);
6304 processSync(mapper);
6305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6308 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6309 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6312 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6313
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6316 motionArgs.action);
6317 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6318 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6320 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6323 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6325 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6326
6327 // Move.
6328 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6329 processPosition(mapper, x1, y1);
6330 processId(mapper, 1);
6331 processMTSync(mapper);
6332 processPosition(mapper, x2, y2);
6333 processId(mapper, 2);
6334 processMTSync(mapper);
6335 processSync(mapper);
6336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6339 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6342 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6345 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6347 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6348
6349 // First finger up.
6350 x2 += 15; y2 -= 20;
6351 processPosition(mapper, x2, y2);
6352 processId(mapper, 2);
6353 processMTSync(mapper);
6354 processSync(mapper);
6355
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6358 motionArgs.action);
6359 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6362 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6365 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6367 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6368
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6372 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6376
6377 // Move.
6378 x2 += 20; y2 -= 25;
6379 processPosition(mapper, x2, y2);
6380 processId(mapper, 2);
6381 processMTSync(mapper);
6382 processSync(mapper);
6383
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6386 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6387 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6390 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6391
6392 // New finger down.
6393 int32_t x3 = 700, y3 = 300;
6394 processPosition(mapper, x2, y2);
6395 processId(mapper, 2);
6396 processMTSync(mapper);
6397 processPosition(mapper, x3, y3);
6398 processId(mapper, 3);
6399 processMTSync(mapper);
6400 processSync(mapper);
6401
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6404 motionArgs.action);
6405 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6406 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6408 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6411 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6413 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6414
6415 // Second finger up.
6416 x3 += 30; y3 -= 20;
6417 processPosition(mapper, x3, y3);
6418 processId(mapper, 3);
6419 processMTSync(mapper);
6420 processSync(mapper);
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6424 motionArgs.action);
6425 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6426 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6428 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6431 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6433 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6434
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6437 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6438 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6441 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6442
6443 // Last finger up.
6444 processMTSync(mapper);
6445 processSync(mapper);
6446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6453 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6454
6455 // Should not have sent any more keys or motions.
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6458}
6459
6460TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461 addConfigurationProperty("touch.deviceType", "touchScreen");
6462 prepareDisplay(DISPLAY_ORIENTATION_0);
6463 prepareAxes(POSITION | ID | SLOT);
6464 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006465 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006466
arthurhungdcef2dc2020-08-11 14:47:50 +08006467 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006468
6469 NotifyMotionArgs motionArgs;
6470
6471 // Two fingers down at once.
6472 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6473 processPosition(mapper, x1, y1);
6474 processId(mapper, 1);
6475 processSlot(mapper, 1);
6476 processPosition(mapper, x2, y2);
6477 processId(mapper, 2);
6478 processSync(mapper);
6479
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6482 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6487
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6490 motionArgs.action);
6491 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6492 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6494 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6497 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6499 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6500
6501 // Move.
6502 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6503 processSlot(mapper, 0);
6504 processPosition(mapper, x1, y1);
6505 processSlot(mapper, 1);
6506 processPosition(mapper, x2, y2);
6507 processSync(mapper);
6508
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6511 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6512 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6514 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6517 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6519 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6520
6521 // First finger up.
6522 x2 += 15; y2 -= 20;
6523 processSlot(mapper, 0);
6524 processId(mapper, -1);
6525 processSlot(mapper, 1);
6526 processPosition(mapper, x2, y2);
6527 processSync(mapper);
6528
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6530 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6531 motionArgs.action);
6532 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6535 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6538 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6540 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6541
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6544 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6545 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6548 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6549
6550 // Move.
6551 x2 += 20; y2 -= 25;
6552 processPosition(mapper, x2, y2);
6553 processSync(mapper);
6554
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6557 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6558 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6561 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6562
6563 // New finger down.
6564 int32_t x3 = 700, y3 = 300;
6565 processPosition(mapper, x2, y2);
6566 processSlot(mapper, 0);
6567 processId(mapper, 3);
6568 processPosition(mapper, x3, y3);
6569 processSync(mapper);
6570
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6573 motionArgs.action);
6574 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6575 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6577 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6580 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6582 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6583
6584 // Second finger up.
6585 x3 += 30; y3 -= 20;
6586 processSlot(mapper, 1);
6587 processId(mapper, -1);
6588 processSlot(mapper, 0);
6589 processPosition(mapper, x3, y3);
6590 processSync(mapper);
6591
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6594 motionArgs.action);
6595 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6596 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6598 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6604
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6612
6613 // Last finger up.
6614 processId(mapper, -1);
6615 processSync(mapper);
6616
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6619 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6620 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6623 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6624
6625 // Should not have sent any more keys or motions.
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6628}
6629
6630TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631 addConfigurationProperty("touch.deviceType", "touchScreen");
6632 prepareDisplay(DISPLAY_ORIENTATION_0);
6633 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006634 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006635
6636 // These calculations are based on the input device calibration documentation.
6637 int32_t rawX = 100;
6638 int32_t rawY = 200;
6639 int32_t rawTouchMajor = 7;
6640 int32_t rawTouchMinor = 6;
6641 int32_t rawToolMajor = 9;
6642 int32_t rawToolMinor = 8;
6643 int32_t rawPressure = 11;
6644 int32_t rawDistance = 0;
6645 int32_t rawOrientation = 3;
6646 int32_t id = 5;
6647
6648 float x = toDisplayX(rawX);
6649 float y = toDisplayY(rawY);
6650 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6651 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6652 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6653 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6654 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6655 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6656 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6657 float distance = float(rawDistance);
6658
6659 processPosition(mapper, rawX, rawY);
6660 processTouchMajor(mapper, rawTouchMajor);
6661 processTouchMinor(mapper, rawTouchMinor);
6662 processToolMajor(mapper, rawToolMajor);
6663 processToolMinor(mapper, rawToolMinor);
6664 processPressure(mapper, rawPressure);
6665 processOrientation(mapper, rawOrientation);
6666 processDistance(mapper, rawDistance);
6667 processId(mapper, id);
6668 processMTSync(mapper);
6669 processSync(mapper);
6670
6671 NotifyMotionArgs args;
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6673 ASSERT_EQ(0, args.pointerProperties[0].id);
6674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6675 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6676 orientation, distance));
6677}
6678
6679TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006680 addConfigurationProperty("touch.deviceType", "touchScreen");
6681 prepareDisplay(DISPLAY_ORIENTATION_0);
6682 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6683 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006684 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006685
6686 // These calculations are based on the input device calibration documentation.
6687 int32_t rawX = 100;
6688 int32_t rawY = 200;
6689 int32_t rawTouchMajor = 140;
6690 int32_t rawTouchMinor = 120;
6691 int32_t rawToolMajor = 180;
6692 int32_t rawToolMinor = 160;
6693
6694 float x = toDisplayX(rawX);
6695 float y = toDisplayY(rawY);
6696 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6697 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6698 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6699 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6700 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6701
6702 processPosition(mapper, rawX, rawY);
6703 processTouchMajor(mapper, rawTouchMajor);
6704 processTouchMinor(mapper, rawTouchMinor);
6705 processToolMajor(mapper, rawToolMajor);
6706 processToolMinor(mapper, rawToolMinor);
6707 processMTSync(mapper);
6708 processSync(mapper);
6709
6710 NotifyMotionArgs args;
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6713 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6714}
6715
6716TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006717 addConfigurationProperty("touch.deviceType", "touchScreen");
6718 prepareDisplay(DISPLAY_ORIENTATION_0);
6719 prepareAxes(POSITION | TOUCH | TOOL);
6720 addConfigurationProperty("touch.size.calibration", "diameter");
6721 addConfigurationProperty("touch.size.scale", "10");
6722 addConfigurationProperty("touch.size.bias", "160");
6723 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006725
6726 // These calculations are based on the input device calibration documentation.
6727 // Note: We only provide a single common touch/tool value because the device is assumed
6728 // not to emit separate values for each pointer (isSummed = 1).
6729 int32_t rawX = 100;
6730 int32_t rawY = 200;
6731 int32_t rawX2 = 150;
6732 int32_t rawY2 = 250;
6733 int32_t rawTouchMajor = 5;
6734 int32_t rawToolMajor = 8;
6735
6736 float x = toDisplayX(rawX);
6737 float y = toDisplayY(rawY);
6738 float x2 = toDisplayX(rawX2);
6739 float y2 = toDisplayY(rawY2);
6740 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6741 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6742 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6743
6744 processPosition(mapper, rawX, rawY);
6745 processTouchMajor(mapper, rawTouchMajor);
6746 processToolMajor(mapper, rawToolMajor);
6747 processMTSync(mapper);
6748 processPosition(mapper, rawX2, rawY2);
6749 processTouchMajor(mapper, rawTouchMajor);
6750 processToolMajor(mapper, rawToolMajor);
6751 processMTSync(mapper);
6752 processSync(mapper);
6753
6754 NotifyMotionArgs args;
6755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6757
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6760 args.action);
6761 ASSERT_EQ(size_t(2), args.pointerCount);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6763 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6765 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6766}
6767
6768TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006769 addConfigurationProperty("touch.deviceType", "touchScreen");
6770 prepareDisplay(DISPLAY_ORIENTATION_0);
6771 prepareAxes(POSITION | TOUCH | TOOL);
6772 addConfigurationProperty("touch.size.calibration", "area");
6773 addConfigurationProperty("touch.size.scale", "43");
6774 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006775 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006776
6777 // These calculations are based on the input device calibration documentation.
6778 int32_t rawX = 100;
6779 int32_t rawY = 200;
6780 int32_t rawTouchMajor = 5;
6781 int32_t rawToolMajor = 8;
6782
6783 float x = toDisplayX(rawX);
6784 float y = toDisplayY(rawY);
6785 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6786 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6787 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6788
6789 processPosition(mapper, rawX, rawY);
6790 processTouchMajor(mapper, rawTouchMajor);
6791 processToolMajor(mapper, rawToolMajor);
6792 processMTSync(mapper);
6793 processSync(mapper);
6794
6795 NotifyMotionArgs args;
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6798 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6799}
6800
6801TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006802 addConfigurationProperty("touch.deviceType", "touchScreen");
6803 prepareDisplay(DISPLAY_ORIENTATION_0);
6804 prepareAxes(POSITION | PRESSURE);
6805 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6806 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006807 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006808
Michael Wrightaa449c92017-12-13 21:21:43 +00006809 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006810 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006811 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6812 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6813 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6814
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815 // These calculations are based on the input device calibration documentation.
6816 int32_t rawX = 100;
6817 int32_t rawY = 200;
6818 int32_t rawPressure = 60;
6819
6820 float x = toDisplayX(rawX);
6821 float y = toDisplayY(rawY);
6822 float pressure = float(rawPressure) * 0.01f;
6823
6824 processPosition(mapper, rawX, rawY);
6825 processPressure(mapper, rawPressure);
6826 processMTSync(mapper);
6827 processSync(mapper);
6828
6829 NotifyMotionArgs args;
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6832 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6833}
6834
6835TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836 addConfigurationProperty("touch.deviceType", "touchScreen");
6837 prepareDisplay(DISPLAY_ORIENTATION_0);
6838 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006840
6841 NotifyMotionArgs motionArgs;
6842 NotifyKeyArgs keyArgs;
6843
6844 processId(mapper, 1);
6845 processPosition(mapper, 100, 200);
6846 processSync(mapper);
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6849 ASSERT_EQ(0, motionArgs.buttonState);
6850
6851 // press BTN_LEFT, release BTN_LEFT
6852 processKey(mapper, BTN_LEFT, 1);
6853 processSync(mapper);
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6856 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6857
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6859 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6860 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6861
Michael Wrightd02c5b62014-02-10 15:10:22 -08006862 processKey(mapper, BTN_LEFT, 0);
6863 processSync(mapper);
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006865 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006866 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006867
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006870 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006871
6872 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6873 processKey(mapper, BTN_RIGHT, 1);
6874 processKey(mapper, BTN_MIDDLE, 1);
6875 processSync(mapper);
6876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6878 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6879 motionArgs.buttonState);
6880
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6882 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6883 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6884
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6887 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6888 motionArgs.buttonState);
6889
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890 processKey(mapper, BTN_RIGHT, 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(AMOTION_EVENT_BUTTON_TERTIARY, 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(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899
6900 processKey(mapper, BTN_MIDDLE, 0);
6901 processSync(mapper);
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006904 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006905
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006908 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006909
6910 // press BTN_BACK, release BTN_BACK
6911 processKey(mapper, BTN_BACK, 1);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6914 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6915 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006916
Michael Wrightd02c5b62014-02-10 15:10:22 -08006917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006919 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6920
6921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6923 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924
6925 processKey(mapper, BTN_BACK, 0);
6926 processSync(mapper);
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006928 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006929 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006930
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006933 ASSERT_EQ(0, motionArgs.buttonState);
6934
Michael Wrightd02c5b62014-02-10 15:10:22 -08006935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6936 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6937 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6938
6939 // press BTN_SIDE, release BTN_SIDE
6940 processKey(mapper, BTN_SIDE, 1);
6941 processSync(mapper);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6943 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6944 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006945
Michael Wrightd02c5b62014-02-10 15:10:22 -08006946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006948 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6949
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6952 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006953
6954 processKey(mapper, BTN_SIDE, 0);
6955 processSync(mapper);
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006958 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006959
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006962 ASSERT_EQ(0, motionArgs.buttonState);
6963
Michael Wrightd02c5b62014-02-10 15:10:22 -08006964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6965 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6966 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6967
6968 // press BTN_FORWARD, release BTN_FORWARD
6969 processKey(mapper, BTN_FORWARD, 1);
6970 processSync(mapper);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6972 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6973 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006974
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006977 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6978
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6981 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006982
6983 processKey(mapper, BTN_FORWARD, 0);
6984 processSync(mapper);
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006987 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006988
6989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006991 ASSERT_EQ(0, motionArgs.buttonState);
6992
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6994 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6995 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6996
6997 // press BTN_EXTRA, release BTN_EXTRA
6998 processKey(mapper, BTN_EXTRA, 1);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7001 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7002 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007003
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007006 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7007
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7010 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007011
7012 processKey(mapper, BTN_EXTRA, 0);
7013 processSync(mapper);
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007016 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007017
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007020 ASSERT_EQ(0, motionArgs.buttonState);
7021
Michael Wrightd02c5b62014-02-10 15:10:22 -08007022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7023 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7024 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7025
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7027
Michael Wrightd02c5b62014-02-10 15:10:22 -08007028 // press BTN_STYLUS, release BTN_STYLUS
7029 processKey(mapper, BTN_STYLUS, 1);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007033 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7034
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7036 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7037 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007038
7039 processKey(mapper, BTN_STYLUS, 0);
7040 processSync(mapper);
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007044
7045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007047 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007048
7049 // press BTN_STYLUS2, release BTN_STYLUS2
7050 processKey(mapper, BTN_STYLUS2, 1);
7051 processSync(mapper);
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007054 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7055
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7058 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059
7060 processKey(mapper, BTN_STYLUS2, 0);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007065
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007068 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069
7070 // release touch
7071 processId(mapper, -1);
7072 processSync(mapper);
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7074 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7075 ASSERT_EQ(0, motionArgs.buttonState);
7076}
7077
7078TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079 addConfigurationProperty("touch.deviceType", "touchScreen");
7080 prepareDisplay(DISPLAY_ORIENTATION_0);
7081 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007083
7084 NotifyMotionArgs motionArgs;
7085
7086 // default tool type is finger
7087 processId(mapper, 1);
7088 processPosition(mapper, 100, 200);
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7093
7094 // eraser
7095 processKey(mapper, BTN_TOOL_RUBBER, 1);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7100
7101 // stylus
7102 processKey(mapper, BTN_TOOL_RUBBER, 0);
7103 processKey(mapper, BTN_TOOL_PEN, 1);
7104 processSync(mapper);
7105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7108
7109 // brush
7110 processKey(mapper, BTN_TOOL_PEN, 0);
7111 processKey(mapper, BTN_TOOL_BRUSH, 1);
7112 processSync(mapper);
7113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7116
7117 // pencil
7118 processKey(mapper, BTN_TOOL_BRUSH, 0);
7119 processKey(mapper, BTN_TOOL_PENCIL, 1);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7124
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007125 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 processKey(mapper, BTN_TOOL_PENCIL, 0);
7127 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7128 processSync(mapper);
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7132
7133 // mouse
7134 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7135 processKey(mapper, BTN_TOOL_MOUSE, 1);
7136 processSync(mapper);
7137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7140
7141 // lens
7142 processKey(mapper, BTN_TOOL_MOUSE, 0);
7143 processKey(mapper, BTN_TOOL_LENS, 1);
7144 processSync(mapper);
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7148
7149 // double-tap
7150 processKey(mapper, BTN_TOOL_LENS, 0);
7151 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7152 processSync(mapper);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7156
7157 // triple-tap
7158 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7159 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7160 processSync(mapper);
7161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7164
7165 // quad-tap
7166 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7167 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7168 processSync(mapper);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7172
7173 // finger
7174 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7175 processKey(mapper, BTN_TOOL_FINGER, 1);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7180
7181 // stylus trumps finger
7182 processKey(mapper, BTN_TOOL_PEN, 1);
7183 processSync(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7187
7188 // eraser trumps stylus
7189 processKey(mapper, BTN_TOOL_RUBBER, 1);
7190 processSync(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7194
7195 // mouse trumps eraser
7196 processKey(mapper, BTN_TOOL_MOUSE, 1);
7197 processSync(mapper);
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7201
7202 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7203 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7204 processSync(mapper);
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7208
7209 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7210 processToolType(mapper, MT_TOOL_PEN);
7211 processSync(mapper);
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7215
7216 // back to default tool type
7217 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7218 processKey(mapper, BTN_TOOL_MOUSE, 0);
7219 processKey(mapper, BTN_TOOL_RUBBER, 0);
7220 processKey(mapper, BTN_TOOL_PEN, 0);
7221 processKey(mapper, BTN_TOOL_FINGER, 0);
7222 processSync(mapper);
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7226}
7227
7228TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229 addConfigurationProperty("touch.deviceType", "touchScreen");
7230 prepareDisplay(DISPLAY_ORIENTATION_0);
7231 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007232 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007233 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234
7235 NotifyMotionArgs motionArgs;
7236
7237 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7238 processId(mapper, 1);
7239 processPosition(mapper, 100, 200);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7244 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7245
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7249 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7250
7251 // move a little
7252 processPosition(mapper, 150, 250);
7253 processSync(mapper);
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7257 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7258
7259 // down when BTN_TOUCH is pressed, pressure defaults to 1
7260 processKey(mapper, BTN_TOUCH, 1);
7261 processSync(mapper);
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7265 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7266
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7268 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7270 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7271
7272 // up when BTN_TOUCH is released, hover restored
7273 processKey(mapper, BTN_TOUCH, 0);
7274 processSync(mapper);
7275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7276 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7278 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7279
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7283 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7284
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7288 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7289
7290 // exit hover when pointer goes away
7291 processId(mapper, -1);
7292 processSync(mapper);
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7294 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7296 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7297}
7298
7299TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300 addConfigurationProperty("touch.deviceType", "touchScreen");
7301 prepareDisplay(DISPLAY_ORIENTATION_0);
7302 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304
7305 NotifyMotionArgs motionArgs;
7306
7307 // initially hovering because pressure is 0
7308 processId(mapper, 1);
7309 processPosition(mapper, 100, 200);
7310 processPressure(mapper, 0);
7311 processSync(mapper);
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7313 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7315 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7316
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7320 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7321
7322 // move a little
7323 processPosition(mapper, 150, 250);
7324 processSync(mapper);
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7328 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7329
7330 // down when pressure becomes non-zero
7331 processPressure(mapper, RAW_PRESSURE_MAX);
7332 processSync(mapper);
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7336 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7337
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7341 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7342
7343 // up when pressure becomes 0, hover restored
7344 processPressure(mapper, 0);
7345 processSync(mapper);
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7347 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7349 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7350
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7354 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7355
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7359 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7360
7361 // exit hover when pointer goes away
7362 processId(mapper, -1);
7363 processSync(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7367 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7368}
7369
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007370/**
7371 * Set the input device port <--> display port associations, and check that the
7372 * events are routed to the display that matches the display port.
7373 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7374 */
7375TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007376 const std::string usb2 = "USB2";
7377 const uint8_t hdmi1 = 0;
7378 const uint8_t hdmi2 = 1;
7379 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007380 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007381
7382 addConfigurationProperty("touch.deviceType", "touchScreen");
7383 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007384 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007385
7386 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7387 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7388
7389 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7390 // for this input device is specified, and the matching viewport is not present,
7391 // the input device should be disabled (at the mapper level).
7392
7393 // Add viewport for display 2 on hdmi2
7394 prepareSecondaryDisplay(type, hdmi2);
7395 // Send a touch event
7396 processPosition(mapper, 100, 100);
7397 processSync(mapper);
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7399
7400 // Add viewport for display 1 on hdmi1
7401 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7402 // Send a touch event again
7403 processPosition(mapper, 100, 100);
7404 processSync(mapper);
7405
7406 NotifyMotionArgs args;
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7408 ASSERT_EQ(DISPLAY_ID, args.displayId);
7409}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007410
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007411TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007412 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007413 std::shared_ptr<FakePointerController> fakePointerController =
7414 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007415 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007416 fakePointerController->setPosition(100, 200);
7417 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007418 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7419
Garfield Tan888a6a42020-01-09 11:39:16 -08007420 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007421 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007422
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007423 prepareDisplay(DISPLAY_ORIENTATION_0);
7424 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007426
7427 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007428 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007429
7430 NotifyMotionArgs motionArgs;
7431 processPosition(mapper, 100, 100);
7432 processSync(mapper);
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7436 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7437}
7438
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007439/**
7440 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7441 * events should not be delivered to the listener.
7442 */
7443TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7444 addConfigurationProperty("touch.deviceType", "touchScreen");
7445 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7446 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7447 ViewportType::INTERNAL);
7448 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7449 prepareAxes(POSITION);
7450 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7451
7452 NotifyMotionArgs motionArgs;
7453 processPosition(mapper, 100, 100);
7454 processSync(mapper);
7455
7456 mFakeListener->assertNotifyMotionWasNotCalled();
7457}
7458
Garfield Tanc734e4f2021-01-15 20:01:39 -08007459TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7460 addConfigurationProperty("touch.deviceType", "touchScreen");
7461 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7462 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7463 ViewportType::INTERNAL);
7464 std::optional<DisplayViewport> optionalDisplayViewport =
7465 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7466 ASSERT_TRUE(optionalDisplayViewport.has_value());
7467 DisplayViewport displayViewport = *optionalDisplayViewport;
7468
7469 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7470 prepareAxes(POSITION);
7471 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7472
7473 // Finger down
7474 int32_t x = 100, y = 100;
7475 processPosition(mapper, x, y);
7476 processSync(mapper);
7477
7478 NotifyMotionArgs motionArgs;
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7480 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7481
7482 // Deactivate display viewport
7483 displayViewport.isActive = false;
7484 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7485 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7486
7487 // Finger move
7488 x += 10, y += 10;
7489 processPosition(mapper, x, y);
7490 processSync(mapper);
7491
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7494
7495 // Reactivate display viewport
7496 displayViewport.isActive = true;
7497 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7498 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7499
7500 // Finger move again
7501 x += 10, y += 10;
7502 processPosition(mapper, x, y);
7503 processSync(mapper);
7504
7505 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7506 // no pointer on the touch device.
7507 mFakeListener->assertNotifyMotionWasNotCalled();
7508}
7509
Arthur Hung7c645402019-01-25 17:45:42 +08007510TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7511 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007512 prepareAxes(POSITION | ID | SLOT);
7513 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007514 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007515
7516 // Create the second touch screen device, and enable multi fingers.
7517 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007518 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007519 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007520 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007521 std::shared_ptr<InputDevice> device2 =
7522 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7523 Flags<InputDeviceClass>(0));
7524
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007525 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7526 0 /*flat*/, 0 /*fuzz*/);
7527 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7528 0 /*flat*/, 0 /*fuzz*/);
7529 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7530 0 /*flat*/, 0 /*fuzz*/);
7531 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7532 0 /*flat*/, 0 /*fuzz*/);
7533 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7534 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7535 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007536
7537 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007538 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007539 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7540 device2->reset(ARBITRARY_TIME);
7541
7542 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007543 std::shared_ptr<FakePointerController> fakePointerController =
7544 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007545 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7546 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7547
7548 // Setup policy for associated displays and show touches.
7549 const uint8_t hdmi1 = 0;
7550 const uint8_t hdmi2 = 1;
7551 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7552 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7553 mFakePolicy->setShowTouches(true);
7554
7555 // Create displays.
7556 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007557 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007558
7559 // Default device will reconfigure above, need additional reconfiguration for another device.
7560 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007561 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007562
7563 // Two fingers down at default display.
7564 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7565 processPosition(mapper, x1, y1);
7566 processId(mapper, 1);
7567 processSlot(mapper, 1);
7568 processPosition(mapper, x2, y2);
7569 processId(mapper, 2);
7570 processSync(mapper);
7571
7572 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7573 fakePointerController->getSpots().find(DISPLAY_ID);
7574 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7575 ASSERT_EQ(size_t(2), iter->second.size());
7576
7577 // Two fingers down at second display.
7578 processPosition(mapper2, x1, y1);
7579 processId(mapper2, 1);
7580 processSlot(mapper2, 1);
7581 processPosition(mapper2, x2, y2);
7582 processId(mapper2, 2);
7583 processSync(mapper2);
7584
7585 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7586 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7587 ASSERT_EQ(size_t(2), iter->second.size());
7588}
7589
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007590TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007591 prepareAxes(POSITION);
7592 addConfigurationProperty("touch.deviceType", "touchScreen");
7593 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007594 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007595
7596 NotifyMotionArgs motionArgs;
7597 // Unrotated video frame
7598 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7599 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007600 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007601 processPosition(mapper, 100, 200);
7602 processSync(mapper);
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7604 ASSERT_EQ(frames, motionArgs.videoFrames);
7605
7606 // Subsequent touch events should not have any videoframes
7607 // This is implemented separately in FakeEventHub,
7608 // but that should match the behaviour of TouchVideoDevice.
7609 processPosition(mapper, 200, 200);
7610 processSync(mapper);
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7612 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7613}
7614
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007615TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007616 prepareAxes(POSITION);
7617 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007618 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007619 // Unrotated video frame
7620 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7621 NotifyMotionArgs motionArgs;
7622
7623 // Test all 4 orientations
7624 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7625 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7626 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7627 clearViewports();
7628 prepareDisplay(orientation);
7629 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007630 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007631 processPosition(mapper, 100, 200);
7632 processSync(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7634 frames[0].rotate(orientation);
7635 ASSERT_EQ(frames, motionArgs.videoFrames);
7636 }
7637}
7638
7639TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007640 prepareAxes(POSITION);
7641 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007643 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7644 // so mix these.
7645 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7646 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7647 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7648 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7649 NotifyMotionArgs motionArgs;
7650
7651 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007652 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007653 processPosition(mapper, 100, 200);
7654 processSync(mapper);
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7656 std::for_each(frames.begin(), frames.end(),
7657 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7658 ASSERT_EQ(frames, motionArgs.videoFrames);
7659}
7660
Arthur Hung9da14732019-09-02 16:16:58 +08007661/**
7662 * If we had defined port associations, but the viewport is not ready, the touch device would be
7663 * expected to be disabled, and it should be enabled after the viewport has found.
7664 */
7665TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007666 constexpr uint8_t hdmi2 = 1;
7667 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007668 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007669
7670 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7671
7672 addConfigurationProperty("touch.deviceType", "touchScreen");
7673 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007674 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007675
7676 ASSERT_EQ(mDevice->isEnabled(), false);
7677
7678 // Add display on hdmi2, the device should be enabled and can receive touch event.
7679 prepareSecondaryDisplay(type, hdmi2);
7680 ASSERT_EQ(mDevice->isEnabled(), true);
7681
7682 // Send a touch event.
7683 processPosition(mapper, 100, 100);
7684 processSync(mapper);
7685
7686 NotifyMotionArgs args;
7687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7688 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7689}
7690
Arthur Hung6cd19a42019-08-30 19:04:12 +08007691
Arthur Hung6cd19a42019-08-30 19:04:12 +08007692
Arthur Hung421eb1c2020-01-16 00:09:42 +08007693TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007694 addConfigurationProperty("touch.deviceType", "touchScreen");
7695 prepareDisplay(DISPLAY_ORIENTATION_0);
7696 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007697 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007698
7699 NotifyMotionArgs motionArgs;
7700
7701 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7702 // finger down
7703 processId(mapper, 1);
7704 processPosition(mapper, x1, y1);
7705 processSync(mapper);
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7709
7710 // finger move
7711 processId(mapper, 1);
7712 processPosition(mapper, x2, y2);
7713 processSync(mapper);
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7717
7718 // finger up.
7719 processId(mapper, -1);
7720 processSync(mapper);
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7722 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7724
7725 // new finger down
7726 processId(mapper, 1);
7727 processPosition(mapper, x3, y3);
7728 processSync(mapper);
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7730 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7732}
7733
7734/**
arthurhungcc7f9802020-04-30 17:55:40 +08007735 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7736 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007737 */
arthurhungcc7f9802020-04-30 17:55:40 +08007738TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007739 addConfigurationProperty("touch.deviceType", "touchScreen");
7740 prepareDisplay(DISPLAY_ORIENTATION_0);
7741 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007742 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007743
7744 NotifyMotionArgs motionArgs;
7745
7746 // default tool type is finger
7747 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007748 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007749 processPosition(mapper, x1, y1);
7750 processSync(mapper);
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7752 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7754
7755 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7756 processToolType(mapper, MT_TOOL_PALM);
7757 processSync(mapper);
7758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7759 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7760
7761 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007762 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007763 processPosition(mapper, x2, y2);
7764 processSync(mapper);
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7766
7767 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007768 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007769 processSync(mapper);
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7771
7772 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007773 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007774 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007775 processPosition(mapper, x3, y3);
7776 processSync(mapper);
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7780}
7781
arthurhungbf89a482020-04-17 17:37:55 +08007782/**
arthurhungcc7f9802020-04-30 17:55:40 +08007783 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7784 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007785 */
arthurhungcc7f9802020-04-30 17:55:40 +08007786TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007787 addConfigurationProperty("touch.deviceType", "touchScreen");
7788 prepareDisplay(DISPLAY_ORIENTATION_0);
7789 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7790 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7791
7792 NotifyMotionArgs motionArgs;
7793
7794 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007795 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7796 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007797 processPosition(mapper, x1, y1);
7798 processSync(mapper);
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7800 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7802
7803 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007804 processSlot(mapper, SECOND_SLOT);
7805 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007806 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007807 processSync(mapper);
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7809 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7810 motionArgs.action);
7811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7812
7813 // If the tool type of the first finger changes to MT_TOOL_PALM,
7814 // we expect to receive ACTION_POINTER_UP with cancel flag.
7815 processSlot(mapper, FIRST_SLOT);
7816 processId(mapper, FIRST_TRACKING_ID);
7817 processToolType(mapper, MT_TOOL_PALM);
7818 processSync(mapper);
7819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7820 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7821 motionArgs.action);
7822 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7823
7824 // The following MOVE events of second finger should be processed.
7825 processSlot(mapper, SECOND_SLOT);
7826 processId(mapper, SECOND_TRACKING_ID);
7827 processPosition(mapper, x2 + 1, y2 + 1);
7828 processSync(mapper);
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7831 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7832
7833 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7834 // it. Second finger receive move.
7835 processSlot(mapper, FIRST_SLOT);
7836 processId(mapper, INVALID_TRACKING_ID);
7837 processSync(mapper);
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7840 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7841
7842 // Second finger keeps moving.
7843 processSlot(mapper, SECOND_SLOT);
7844 processId(mapper, SECOND_TRACKING_ID);
7845 processPosition(mapper, x2 + 2, y2 + 2);
7846 processSync(mapper);
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7849 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7850
7851 // Second 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);
7857}
7858
7859/**
7860 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7861 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7862 */
7863TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7864 addConfigurationProperty("touch.deviceType", "touchScreen");
7865 prepareDisplay(DISPLAY_ORIENTATION_0);
7866 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7867 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7868
7869 NotifyMotionArgs motionArgs;
7870
7871 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7872 // First finger down.
7873 processId(mapper, FIRST_TRACKING_ID);
7874 processPosition(mapper, x1, y1);
7875 processSync(mapper);
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7879
7880 // Second finger down.
7881 processSlot(mapper, SECOND_SLOT);
7882 processId(mapper, SECOND_TRACKING_ID);
7883 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007884 processSync(mapper);
7885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7886 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7887 motionArgs.action);
7888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7889
arthurhungcc7f9802020-04-30 17:55:40 +08007890 // If the tool type of the first finger changes to MT_TOOL_PALM,
7891 // we expect to receive ACTION_POINTER_UP with cancel flag.
7892 processSlot(mapper, FIRST_SLOT);
7893 processId(mapper, FIRST_TRACKING_ID);
7894 processToolType(mapper, MT_TOOL_PALM);
7895 processSync(mapper);
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7898 motionArgs.action);
7899 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7900
7901 // Second finger keeps moving.
7902 processSlot(mapper, SECOND_SLOT);
7903 processId(mapper, SECOND_TRACKING_ID);
7904 processPosition(mapper, x2 + 1, y2 + 1);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7908
7909 // second finger becomes palm, receive cancel due to only 1 finger is active.
7910 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007911 processToolType(mapper, MT_TOOL_PALM);
7912 processSync(mapper);
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7914 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7915
arthurhungcc7f9802020-04-30 17:55:40 +08007916 // third finger down.
7917 processSlot(mapper, THIRD_SLOT);
7918 processId(mapper, THIRD_TRACKING_ID);
7919 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007920 processPosition(mapper, x3, y3);
7921 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007925 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7926
7927 // third finger move
7928 processId(mapper, THIRD_TRACKING_ID);
7929 processPosition(mapper, x3 + 1, y3 + 1);
7930 processSync(mapper);
7931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7933
7934 // first finger up, third finger receive move.
7935 processSlot(mapper, FIRST_SLOT);
7936 processId(mapper, INVALID_TRACKING_ID);
7937 processSync(mapper);
7938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7940 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7941
7942 // second finger up, third finger receive move.
7943 processSlot(mapper, SECOND_SLOT);
7944 processId(mapper, INVALID_TRACKING_ID);
7945 processSync(mapper);
7946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7948 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7949
7950 // third finger up.
7951 processSlot(mapper, THIRD_SLOT);
7952 processId(mapper, INVALID_TRACKING_ID);
7953 processSync(mapper);
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7955 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7956 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7957}
7958
7959/**
7960 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7961 * and the active finger could still be allowed to receive the events
7962 */
7963TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7964 addConfigurationProperty("touch.deviceType", "touchScreen");
7965 prepareDisplay(DISPLAY_ORIENTATION_0);
7966 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7967 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7968
7969 NotifyMotionArgs motionArgs;
7970
7971 // default tool type is finger
7972 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7973 processId(mapper, FIRST_TRACKING_ID);
7974 processPosition(mapper, x1, y1);
7975 processSync(mapper);
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7979
7980 // Second finger down.
7981 processSlot(mapper, SECOND_SLOT);
7982 processId(mapper, SECOND_TRACKING_ID);
7983 processPosition(mapper, x2, y2);
7984 processSync(mapper);
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7986 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7987 motionArgs.action);
7988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7989
7990 // If the tool type of the second finger changes to MT_TOOL_PALM,
7991 // we expect to receive ACTION_POINTER_UP with cancel flag.
7992 processId(mapper, SECOND_TRACKING_ID);
7993 processToolType(mapper, MT_TOOL_PALM);
7994 processSync(mapper);
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7997 motionArgs.action);
7998 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7999
8000 // The following MOVE event should be processed.
8001 processSlot(mapper, FIRST_SLOT);
8002 processId(mapper, FIRST_TRACKING_ID);
8003 processPosition(mapper, x1 + 1, y1 + 1);
8004 processSync(mapper);
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8007 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8008
8009 // second finger up.
8010 processSlot(mapper, SECOND_SLOT);
8011 processId(mapper, INVALID_TRACKING_ID);
8012 processSync(mapper);
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8015
8016 // first finger keep moving
8017 processSlot(mapper, FIRST_SLOT);
8018 processId(mapper, FIRST_TRACKING_ID);
8019 processPosition(mapper, x1 + 2, y1 + 2);
8020 processSync(mapper);
8021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8023
8024 // first finger up.
8025 processId(mapper, INVALID_TRACKING_ID);
8026 processSync(mapper);
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8028 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8029 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008030}
8031
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008032// --- MultiTouchInputMapperTest_ExternalDevice ---
8033
8034class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8035protected:
Chris Yea52ade12020-08-27 16:49:20 -07008036 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008037};
8038
8039/**
8040 * Expect fallback to internal viewport if device is external and external viewport is not present.
8041 */
8042TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8043 prepareAxes(POSITION);
8044 addConfigurationProperty("touch.deviceType", "touchScreen");
8045 prepareDisplay(DISPLAY_ORIENTATION_0);
8046 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8047
8048 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8049
8050 NotifyMotionArgs motionArgs;
8051
8052 // Expect the event to be sent to the internal viewport,
8053 // because an external viewport is not present.
8054 processPosition(mapper, 100, 100);
8055 processSync(mapper);
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8058
8059 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008060 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008061 processPosition(mapper, 100, 100);
8062 processSync(mapper);
8063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8064 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8065}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008066
8067/**
8068 * Test touch should not work if outside of surface.
8069 */
8070class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8071protected:
8072 void halfDisplayToCenterHorizontal(int32_t orientation) {
8073 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008074 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008075
8076 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8077 internalViewport->orientation = orientation;
8078 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8079 internalViewport->logicalLeft = 0;
8080 internalViewport->logicalTop = 0;
8081 internalViewport->logicalRight = DISPLAY_HEIGHT;
8082 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8083
8084 internalViewport->physicalLeft = 0;
8085 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8086 internalViewport->physicalRight = DISPLAY_HEIGHT;
8087 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8088
8089 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8090 internalViewport->deviceHeight = DISPLAY_WIDTH;
8091 } else {
8092 internalViewport->logicalLeft = 0;
8093 internalViewport->logicalTop = 0;
8094 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8095 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8096
8097 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8098 internalViewport->physicalTop = 0;
8099 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8100 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8101
8102 internalViewport->deviceWidth = DISPLAY_WIDTH;
8103 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8104 }
8105
8106 mFakePolicy->updateViewport(internalViewport.value());
8107 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8108 }
8109
arthurhung5d547942020-12-14 17:04:45 +08008110 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8111 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008112 int32_t yExpected) {
8113 // touch on outside area should not work.
8114 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8117
8118 // touch on inside area should receive the event.
8119 NotifyMotionArgs args;
8120 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8121 processSync(mapper);
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8123 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8124 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8125
8126 // Reset.
8127 mapper.reset(ARBITRARY_TIME);
8128 }
8129};
8130
arthurhung5d547942020-12-14 17:04:45 +08008131TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008132 addConfigurationProperty("touch.deviceType", "touchScreen");
8133 prepareDisplay(DISPLAY_ORIENTATION_0);
8134 prepareAxes(POSITION);
8135 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8136
8137 // Touch on center of normal display should work.
8138 const int32_t x = DISPLAY_WIDTH / 4;
8139 const int32_t y = DISPLAY_HEIGHT / 2;
8140 processPosition(mapper, toRawX(x), toRawY(y));
8141 processSync(mapper);
8142 NotifyMotionArgs args;
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8145 0.0f, 0.0f, 0.0f, 0.0f));
8146 // Reset.
8147 mapper.reset(ARBITRARY_TIME);
8148
8149 // Let physical display be different to device, and make surface and physical could be 1:1.
8150 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8151
8152 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8153 const int32_t yExpected = y;
8154 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8155}
8156
arthurhung5d547942020-12-14 17:04:45 +08008157TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008158 addConfigurationProperty("touch.deviceType", "touchScreen");
8159 prepareDisplay(DISPLAY_ORIENTATION_0);
8160 prepareAxes(POSITION);
8161 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8162
8163 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8164 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8165
8166 const int32_t x = DISPLAY_WIDTH / 4;
8167 const int32_t y = DISPLAY_HEIGHT / 2;
8168
8169 // expect x/y = swap x/y then reverse y.
8170 const int32_t xExpected = y;
8171 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8172 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8173}
8174
arthurhung5d547942020-12-14 17:04:45 +08008175TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008176 addConfigurationProperty("touch.deviceType", "touchScreen");
8177 prepareDisplay(DISPLAY_ORIENTATION_0);
8178 prepareAxes(POSITION);
8179 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8180
8181 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8182 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8183
8184 const int32_t x = DISPLAY_WIDTH / 4;
8185 const int32_t y = DISPLAY_HEIGHT / 2;
8186
8187 // expect x/y = swap x/y then reverse x.
8188 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8189 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8190 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8191}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008192
arthurhunga36b28e2020-12-29 20:28:15 +08008193TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8194 addConfigurationProperty("touch.deviceType", "touchScreen");
8195 prepareDisplay(DISPLAY_ORIENTATION_0);
8196 prepareAxes(POSITION);
8197 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8198
8199 const int32_t x = 0;
8200 const int32_t y = 0;
8201
8202 const int32_t xExpected = x;
8203 const int32_t yExpected = y;
8204 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8205
8206 clearViewports();
8207 prepareDisplay(DISPLAY_ORIENTATION_90);
8208 // expect x/y = swap x/y then reverse y.
8209 const int32_t xExpected90 = y;
8210 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8211 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8212
8213 clearViewports();
8214 prepareDisplay(DISPLAY_ORIENTATION_270);
8215 // expect x/y = swap x/y then reverse x.
8216 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8217 const int32_t yExpected270 = x;
8218 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8219}
8220
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008221TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8222 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8223 std::shared_ptr<FakePointerController> fakePointerController =
8224 std::make_shared<FakePointerController>();
8225 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8226 fakePointerController->setPosition(0, 0);
8227 fakePointerController->setButtonState(0);
8228
8229 // prepare device and capture
8230 prepareDisplay(DISPLAY_ORIENTATION_0);
8231 prepareAxes(POSITION | ID | SLOT);
8232 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8233 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8234 mFakePolicy->setPointerCapture(true);
8235 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8236 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8237
8238 // captured touchpad should be a touchpad source
8239 NotifyDeviceResetArgs resetArgs;
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8241 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8242
Chris Yef74dc422020-09-02 22:41:50 -07008243 InputDeviceInfo deviceInfo;
8244 mDevice->getDeviceInfo(&deviceInfo);
8245
8246 const InputDeviceInfo::MotionRange* relRangeX =
8247 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8248 ASSERT_NE(relRangeX, nullptr);
8249 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8250 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8251 const InputDeviceInfo::MotionRange* relRangeY =
8252 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8253 ASSERT_NE(relRangeY, nullptr);
8254 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8255 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8256
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008257 // run captured pointer tests - note that this is unscaled, so input listener events should be
8258 // identical to what the hardware sends (accounting for any
8259 // calibration).
8260 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008261 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008262 processId(mapper, 1);
8263 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8264 processKey(mapper, BTN_TOUCH, 1);
8265 processSync(mapper);
8266
8267 // expect coord[0] to contain initial location of touch 0
8268 NotifyMotionArgs args;
8269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8270 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8271 ASSERT_EQ(1U, args.pointerCount);
8272 ASSERT_EQ(0, args.pointerProperties[0].id);
8273 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8274 ASSERT_NO_FATAL_FAILURE(
8275 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8276
8277 // FINGER 1 DOWN
8278 processSlot(mapper, 1);
8279 processId(mapper, 2);
8280 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8281 processSync(mapper);
8282
8283 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008285 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8286 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008287 ASSERT_EQ(2U, args.pointerCount);
8288 ASSERT_EQ(0, args.pointerProperties[0].id);
8289 ASSERT_EQ(1, args.pointerProperties[1].id);
8290 ASSERT_NO_FATAL_FAILURE(
8291 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8292 ASSERT_NO_FATAL_FAILURE(
8293 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8294
8295 // FINGER 1 MOVE
8296 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8297 processSync(mapper);
8298
8299 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8300 // from move
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8303 ASSERT_NO_FATAL_FAILURE(
8304 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8305 ASSERT_NO_FATAL_FAILURE(
8306 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8307
8308 // FINGER 0 MOVE
8309 processSlot(mapper, 0);
8310 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8311 processSync(mapper);
8312
8313 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8316 ASSERT_NO_FATAL_FAILURE(
8317 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8318 ASSERT_NO_FATAL_FAILURE(
8319 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8320
8321 // BUTTON DOWN
8322 processKey(mapper, BTN_LEFT, 1);
8323 processSync(mapper);
8324
8325 // touchinputmapper design sends a move before button press
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8330
8331 // BUTTON UP
8332 processKey(mapper, BTN_LEFT, 0);
8333 processSync(mapper);
8334
8335 // touchinputmapper design sends a move after button release
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8340
8341 // FINGER 0 UP
8342 processId(mapper, -1);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8345 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8346
8347 // FINGER 1 MOVE
8348 processSlot(mapper, 1);
8349 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8350 processSync(mapper);
8351
8352 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8355 ASSERT_EQ(1U, args.pointerCount);
8356 ASSERT_EQ(1, args.pointerProperties[0].id);
8357 ASSERT_NO_FATAL_FAILURE(
8358 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8359
8360 // FINGER 1 UP
8361 processId(mapper, -1);
8362 processKey(mapper, BTN_TOUCH, 0);
8363 processSync(mapper);
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8365 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8366
8367 // non captured touchpad should be a mouse source
8368 mFakePolicy->setPointerCapture(false);
8369 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8371 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8372}
8373
8374TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8375 std::shared_ptr<FakePointerController> fakePointerController =
8376 std::make_shared<FakePointerController>();
8377 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8378 fakePointerController->setPosition(0, 0);
8379 fakePointerController->setButtonState(0);
8380
8381 // prepare device and capture
8382 prepareDisplay(DISPLAY_ORIENTATION_0);
8383 prepareAxes(POSITION | ID | SLOT);
8384 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8385 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8386 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8387 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8388 // run uncaptured pointer tests - pushes out generic events
8389 // FINGER 0 DOWN
8390 processId(mapper, 3);
8391 processPosition(mapper, 100, 100);
8392 processKey(mapper, BTN_TOUCH, 1);
8393 processSync(mapper);
8394
8395 // start at (100,100), cursor should be at (0,0) * scale
8396 NotifyMotionArgs args;
8397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8398 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8399 ASSERT_NO_FATAL_FAILURE(
8400 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8401
8402 // FINGER 0 MOVE
8403 processPosition(mapper, 200, 200);
8404 processSync(mapper);
8405
8406 // compute scaling to help with touch position checking
8407 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8408 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8409 float scale =
8410 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8411
8412 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8416 0, 0, 0, 0, 0, 0, 0));
8417}
8418
8419TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8420 std::shared_ptr<FakePointerController> fakePointerController =
8421 std::make_shared<FakePointerController>();
8422
8423 prepareDisplay(DISPLAY_ORIENTATION_0);
8424 prepareAxes(POSITION | ID | SLOT);
8425 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8426 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8427 mFakePolicy->setPointerCapture(false);
8428 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8429
8430 // uncaptured touchpad should be a pointer device
8431 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8432
8433 // captured touchpad should be a touchpad device
8434 mFakePolicy->setPointerCapture(true);
8435 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8436 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8437}
8438
Michael Wrightd02c5b62014-02-10 15:10:22 -08008439} // namespace android