blob: 0e88312e9666d494e6c214fb049fe0eedee6f105 [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;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700412 List<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
Chris Yea52ade12020-08-27 16:49:20 -0700725 size_t getEvents(int, RawEvent* buffer, size_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700726 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 if (mEvents.empty()) {
728 return 0;
729 }
730
731 *buffer = *mEvents.begin();
732 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700733 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 return 1;
735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600738 auto it = mVideoFrames.find(deviceId);
739 if (it != mVideoFrames.end()) {
740 std::vector<TouchVideoFrame> frames = std::move(it->second);
741 mVideoFrames.erase(deviceId);
742 return frames;
743 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800744 return {};
745 }
746
Chris Yea52ade12020-08-27 16:49:20 -0700747 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 Device* device = getDevice(deviceId);
749 if (device) {
750 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
751 if (index >= 0) {
752 return device->scanCodeStates.valueAt(index);
753 }
754 }
755 return AKEY_STATE_UNKNOWN;
756 }
757
Chris Yea52ade12020-08-27 16:49:20 -0700758 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759 Device* device = getDevice(deviceId);
760 if (device) {
761 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
762 if (index >= 0) {
763 return device->keyCodeStates.valueAt(index);
764 }
765 }
766 return AKEY_STATE_UNKNOWN;
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 Device* device = getDevice(deviceId);
771 if (device) {
772 ssize_t index = device->switchStates.indexOfKey(sw);
773 if (index >= 0) {
774 return device->switchStates.valueAt(index);
775 }
776 }
777 return AKEY_STATE_UNKNOWN;
778 }
779
Chris Yea52ade12020-08-27 16:49:20 -0700780 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
781 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782 Device* device = getDevice(deviceId);
783 if (device) {
784 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
785 if (index >= 0) {
786 *outValue = device->absoluteAxisValue.valueAt(index);
787 return OK;
788 }
789 }
790 *outValue = 0;
791 return -1;
792 }
793
Chris Yea52ade12020-08-27 16:49:20 -0700794 // Return true if the device has non-empty key layout.
795 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
796 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797 bool result = false;
798 Device* device = getDevice(deviceId);
799 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700800 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801 for (size_t i = 0; i < numCodes; i++) {
802 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
803 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
804 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 }
806 }
807 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
808 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
809 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811 }
812 }
813 }
814 return result;
815 }
816
Chris Yea52ade12020-08-27 16:49:20 -0700817 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 Device* device = getDevice(deviceId);
819 if (device) {
820 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
821 return index >= 0;
822 }
823 return false;
824 }
825
Chris Yea52ade12020-08-27 16:49:20 -0700826 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 Device* device = getDevice(deviceId);
828 return device && device->leds.indexOfKey(led) >= 0;
829 }
830
Chris Yea52ade12020-08-27 16:49:20 -0700831 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 Device* device = getDevice(deviceId);
833 if (device) {
834 ssize_t index = device->leds.indexOfKey(led);
835 if (index >= 0) {
836 device->leds.replaceValueAt(led, on);
837 } else {
838 ADD_FAILURE()
839 << "Attempted to set the state of an LED that the EventHub declared "
840 "was not present. led=" << led;
841 }
842 }
843 }
844
Chris Yea52ade12020-08-27 16:49:20 -0700845 void getVirtualKeyDefinitions(
846 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 outVirtualKeys.clear();
848
849 Device* device = getDevice(deviceId);
850 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800851 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 }
853 }
854
Chris Yea52ade12020-08-27 16:49:20 -0700855 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700856 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 }
858
Chris Yea52ade12020-08-27 16:49:20 -0700859 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860 return false;
861 }
862
Chris Yea52ade12020-08-27 16:49:20 -0700863 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864
Chris Yea52ade12020-08-27 16:49:20 -0700865 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866
Chris Ye87143712020-11-10 05:05:58 +0000867 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
868
Kim Low03ea0352020-11-06 12:45:07 -0800869 std::optional<int32_t> getBatteryCapacity(int32_t) const override { return BATTERY_CAPACITY; }
870
871 std::optional<int32_t> getBatteryStatus(int32_t) const override { return BATTERY_STATUS; }
872
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100873 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874 return false;
875 }
876
Chris Yea52ade12020-08-27 16:49:20 -0700877 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Chris Yea52ade12020-08-27 16:49:20 -0700879 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880
Chris Yea52ade12020-08-27 16:49:20 -0700881 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882
Chris Yea52ade12020-08-27 16:49:20 -0700883 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884};
885
Michael Wrightd02c5b62014-02-10 15:10:22 -0800886// --- FakeInputMapper ---
887
888class FakeInputMapper : public InputMapper {
889 uint32_t mSources;
890 int32_t mKeyboardType;
891 int32_t mMetaState;
892 KeyedVector<int32_t, int32_t> mKeyCodeStates;
893 KeyedVector<int32_t, int32_t> mScanCodeStates;
894 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800895 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700897 std::mutex mLock;
898 std::condition_variable mStateChangedCondition;
899 bool mConfigureWasCalled GUARDED_BY(mLock);
900 bool mResetWasCalled GUARDED_BY(mLock);
901 bool mProcessWasCalled GUARDED_BY(mLock);
902 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903
Arthur Hungc23540e2018-11-29 20:42:11 +0800904 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800906 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
907 : InputMapper(deviceContext),
908 mSources(sources),
909 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800911 mConfigureWasCalled(false),
912 mResetWasCalled(false),
913 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914
Chris Yea52ade12020-08-27 16:49:20 -0700915 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916
917 void setKeyboardType(int32_t keyboardType) {
918 mKeyboardType = keyboardType;
919 }
920
921 void setMetaState(int32_t metaState) {
922 mMetaState = metaState;
923 }
924
925 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700926 std::unique_lock<std::mutex> lock(mLock);
927 base::ScopedLockAssertion assumeLocked(mLock);
928 const bool configureCalled =
929 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
930 return mConfigureWasCalled;
931 });
932 if (!configureCalled) {
933 FAIL() << "Expected configure() to have been called.";
934 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 mConfigureWasCalled = false;
936 }
937
938 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700939 std::unique_lock<std::mutex> lock(mLock);
940 base::ScopedLockAssertion assumeLocked(mLock);
941 const bool resetCalled =
942 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
943 return mResetWasCalled;
944 });
945 if (!resetCalled) {
946 FAIL() << "Expected reset() to have been called.";
947 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948 mResetWasCalled = false;
949 }
950
Yi Kong9b14ac62018-07-17 13:48:38 -0700951 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700952 std::unique_lock<std::mutex> lock(mLock);
953 base::ScopedLockAssertion assumeLocked(mLock);
954 const bool processCalled =
955 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
956 return mProcessWasCalled;
957 });
958 if (!processCalled) {
959 FAIL() << "Expected process() to have been called.";
960 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 if (outLastEvent) {
962 *outLastEvent = mLastEvent;
963 }
964 mProcessWasCalled = false;
965 }
966
967 void setKeyCodeState(int32_t keyCode, int32_t state) {
968 mKeyCodeStates.replaceValueFor(keyCode, state);
969 }
970
971 void setScanCodeState(int32_t scanCode, int32_t state) {
972 mScanCodeStates.replaceValueFor(scanCode, state);
973 }
974
975 void setSwitchState(int32_t switchCode, int32_t state) {
976 mSwitchStates.replaceValueFor(switchCode, state);
977 }
978
979 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800980 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981 }
982
983private:
Chris Yea52ade12020-08-27 16:49:20 -0700984 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985
Chris Yea52ade12020-08-27 16:49:20 -0700986 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 InputMapper::populateDeviceInfo(deviceInfo);
988
989 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
990 deviceInfo->setKeyboardType(mKeyboardType);
991 }
992 }
993
Chris Yea52ade12020-08-27 16:49:20 -0700994 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700995 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800996 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800997
998 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800999 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001000 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1001 mViewport = config->getDisplayViewportByPort(*displayPort);
1002 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001003
1004 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 }
1006
Chris Yea52ade12020-08-27 16:49:20 -07001007 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001008 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001010 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 }
1012
Chris Yea52ade12020-08-27 16:49:20 -07001013 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015 mLastEvent = *rawEvent;
1016 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001017 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 }
1019
Chris Yea52ade12020-08-27 16:49:20 -07001020 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1022 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1023 }
1024
Chris Yea52ade12020-08-27 16:49:20 -07001025 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1027 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1028 }
1029
Chris Yea52ade12020-08-27 16:49:20 -07001030 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1032 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1033 }
1034
Chris Yea52ade12020-08-27 16:49:20 -07001035 // Return true if the device has non-empty key layout.
1036 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1037 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038 for (size_t i = 0; i < numCodes; i++) {
1039 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1040 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1041 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 }
1043 }
1044 }
Chris Yea52ade12020-08-27 16:49:20 -07001045 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046 return result;
1047 }
1048
1049 virtual int32_t getMetaState() {
1050 return mMetaState;
1051 }
1052
1053 virtual void fadePointer() {
1054 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001055
1056 virtual std::optional<int32_t> getAssociatedDisplay() {
1057 if (mViewport) {
1058 return std::make_optional(mViewport->displayId);
1059 }
1060 return std::nullopt;
1061 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062};
1063
1064
1065// --- InstrumentedInputReader ---
1066
1067class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069
1070public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001071 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1072 const sp<InputReaderPolicyInterface>& policy,
1073 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001074 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001076 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001080 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001081 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 InputDeviceIdentifier identifier;
1083 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001084 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001086 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087 }
1088
Prabir Pradhan28efc192019-11-05 01:10:04 +00001089 // Make the protected loopOnce method accessible to tests.
1090 using InputReader::loopOnce;
1091
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001093 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1094 const InputDeviceIdentifier& identifier)
1095 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001096 if (!mNextDevices.empty()) {
1097 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1098 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 return device;
1100 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001101 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 }
1103
arthurhungdcef2dc2020-08-11 14:47:50 +08001104 // --- FakeInputReaderContext ---
1105 class FakeInputReaderContext : public ContextImpl {
1106 int32_t mGlobalMetaState;
1107 bool mUpdateGlobalMetaStateWasCalled;
1108 int32_t mGeneration;
1109
1110 public:
1111 FakeInputReaderContext(InputReader* reader)
1112 : ContextImpl(reader),
1113 mGlobalMetaState(0),
1114 mUpdateGlobalMetaStateWasCalled(false),
1115 mGeneration(1) {}
1116
1117 virtual ~FakeInputReaderContext() {}
1118
1119 void assertUpdateGlobalMetaStateWasCalled() {
1120 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1121 << "Expected updateGlobalMetaState() to have been called.";
1122 mUpdateGlobalMetaStateWasCalled = false;
1123 }
1124
1125 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1126
1127 uint32_t getGeneration() { return mGeneration; }
1128
1129 void updateGlobalMetaState() override {
1130 mUpdateGlobalMetaStateWasCalled = true;
1131 ContextImpl::updateGlobalMetaState();
1132 }
1133
1134 int32_t getGlobalMetaState() override {
1135 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1136 }
1137
1138 int32_t bumpGeneration() override {
1139 mGeneration = ContextImpl::bumpGeneration();
1140 return mGeneration;
1141 }
1142 } mFakeContext;
1143
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001145
1146public:
1147 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148};
1149
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150// --- InputReaderPolicyTest ---
1151class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001152protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001153 sp<FakeInputReaderPolicy> mFakePolicy;
1154
Chris Yea52ade12020-08-27 16:49:20 -07001155 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1156 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001157};
1158
1159/**
1160 * Check that empty set of viewports is an acceptable configuration.
1161 * Also try to get internal viewport two different ways - by type and by uniqueId.
1162 *
1163 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1164 * Such configuration is not currently allowed.
1165 */
1166TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001167 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168
1169 // We didn't add any viewports yet, so there shouldn't be any.
1170 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001171 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001172 ASSERT_FALSE(internalViewport);
1173
1174 // Add an internal viewport, then clear it
1175 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001176 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001177 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001178
1179 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001182 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001183
1184 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001185 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001186 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001187 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001188
1189 mFakePolicy->clearViewports();
1190 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001193 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001194 ASSERT_FALSE(internalViewport);
1195}
1196
1197TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1198 const std::string internalUniqueId = "local:0";
1199 const std::string externalUniqueId = "local:1";
1200 const std::string virtualUniqueId1 = "virtual:2";
1201 const std::string virtualUniqueId2 = "virtual:3";
1202 constexpr int32_t virtualDisplayId1 = 2;
1203 constexpr int32_t virtualDisplayId2 = 3;
1204
1205 // Add an internal viewport
1206 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001207 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1208 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209 // Add an external viewport
1210 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001211 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1212 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 // Add an virtual viewport
1214 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001215 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1216 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001217 // Add another virtual viewport
1218 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001219 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1220 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221
1222 // Check matching by type for internal
1223 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001224 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001225 ASSERT_TRUE(internalViewport);
1226 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1227
1228 // Check matching by type for external
1229 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001230 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001231 ASSERT_TRUE(externalViewport);
1232 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1233
1234 // Check matching by uniqueId for virtual viewport #1
1235 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001236 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001238 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1240 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1241
1242 // Check matching by uniqueId for virtual viewport #2
1243 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001244 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001246 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001247 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1248 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1249}
1250
1251
1252/**
1253 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1254 * that lookup works by checking display id.
1255 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1256 */
1257TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1258 const std::string uniqueId1 = "uniqueId1";
1259 const std::string uniqueId2 = "uniqueId2";
1260 constexpr int32_t displayId1 = 2;
1261 constexpr int32_t displayId2 = 3;
1262
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001263 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1264 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001265 for (const ViewportType& type : types) {
1266 mFakePolicy->clearViewports();
1267 // Add a viewport
1268 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001269 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1270 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271 // Add another viewport
1272 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001273 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1274 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275
1276 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001277 std::optional<DisplayViewport> viewport1 =
1278 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001279 ASSERT_TRUE(viewport1);
1280 ASSERT_EQ(displayId1, viewport1->displayId);
1281 ASSERT_EQ(type, viewport1->type);
1282
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001283 std::optional<DisplayViewport> viewport2 =
1284 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285 ASSERT_TRUE(viewport2);
1286 ASSERT_EQ(displayId2, viewport2->displayId);
1287 ASSERT_EQ(type, viewport2->type);
1288
1289 // When there are multiple viewports of the same kind, and uniqueId is not specified
1290 // in the call to getDisplayViewport, then that situation is not supported.
1291 // The viewports can be stored in any order, so we cannot rely on the order, since that
1292 // is just implementation detail.
1293 // However, we can check that it still returns *a* viewport, we just cannot assert
1294 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001295 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001296 ASSERT_TRUE(someViewport);
1297 }
1298}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001300/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001301 * When we have multiple internal displays make sure we always return the default display when
1302 * querying by type.
1303 */
1304TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1305 const std::string uniqueId1 = "uniqueId1";
1306 const std::string uniqueId2 = "uniqueId2";
1307 constexpr int32_t nonDefaultDisplayId = 2;
1308 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1309 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1310
1311 // Add the default display first and ensure it gets returned.
1312 mFakePolicy->clearViewports();
1313 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001314 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001315 ViewportType::INTERNAL);
1316 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001317 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001318 ViewportType::INTERNAL);
1319
1320 std::optional<DisplayViewport> viewport =
1321 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1322 ASSERT_TRUE(viewport);
1323 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1324 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1325
1326 // Add the default display second to make sure order doesn't matter.
1327 mFakePolicy->clearViewports();
1328 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001329 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001330 ViewportType::INTERNAL);
1331 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001332 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001333 ViewportType::INTERNAL);
1334
1335 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1336 ASSERT_TRUE(viewport);
1337 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1338 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1339}
1340
1341/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001342 * Check getDisplayViewportByPort
1343 */
1344TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001345 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001346 const std::string uniqueId1 = "uniqueId1";
1347 const std::string uniqueId2 = "uniqueId2";
1348 constexpr int32_t displayId1 = 1;
1349 constexpr int32_t displayId2 = 2;
1350 const uint8_t hdmi1 = 0;
1351 const uint8_t hdmi2 = 1;
1352 const uint8_t hdmi3 = 2;
1353
1354 mFakePolicy->clearViewports();
1355 // Add a viewport that's associated with some display port that's not of interest.
1356 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001357 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1358 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001359 // Add another viewport, connected to HDMI1 port
1360 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001361 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1362 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001363
1364 // Check that correct display viewport was returned by comparing the display ports.
1365 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1366 ASSERT_TRUE(hdmi1Viewport);
1367 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1368 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1369
1370 // Check that we can still get the same viewport using the uniqueId
1371 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1372 ASSERT_TRUE(hdmi1Viewport);
1373 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1374 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1375 ASSERT_EQ(type, hdmi1Viewport->type);
1376
1377 // Check that we cannot find a port with "HDMI2", because we never added one
1378 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1379 ASSERT_FALSE(hdmi2Viewport);
1380}
1381
Michael Wrightd02c5b62014-02-10 15:10:22 -08001382// --- InputReaderTest ---
1383
1384class InputReaderTest : public testing::Test {
1385protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001386 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001388 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001389 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001390
Chris Yea52ade12020-08-27 16:49:20 -07001391 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001392 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001393 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001394 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001395
Prabir Pradhan28efc192019-11-05 01:10:04 +00001396 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1397 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001398 }
1399
Chris Yea52ade12020-08-27 16:49:20 -07001400 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401 mFakeListener.clear();
1402 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001403 }
1404
Chris Ye1b0c7342020-07-28 21:57:03 -07001405 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001406 const PropertyMap* configuration) {
1407 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001408
1409 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001410 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001411 }
1412 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001413 mReader->loopOnce();
1414 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1416 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001417 }
1418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001419 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001420 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001421 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001422 }
1423
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001424 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001425 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001426 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001427 }
1428
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001430 const std::string& name,
1431 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001432 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001433 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1434 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001435 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001436 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001437 return mapper;
1438 }
1439};
1440
Chris Ye98d3f532020-10-01 21:48:59 -07001441TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001442 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1443 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1444 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001445
Chris Ye98d3f532020-10-01 21:48:59 -07001446 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001447 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001448 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001449 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001450 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1451 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1452 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001453}
1454
1455TEST_F(InputReaderTest, PolicyGetInputDevices) {
1456 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1457 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1458 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001459
1460 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001461 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001462 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001463 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001464 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001465 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1466 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1467 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1468}
1469
Chris Yee7310032020-09-22 15:36:28 -07001470TEST_F(InputReaderTest, GetMergedInputDevices) {
1471 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1472 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1473 // Add two subdevices to device
1474 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1475 // Must add at least one mapper or the device will be ignored!
1476 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1477 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1478
1479 // Push same device instance for next device to be added, so they'll have same identifier.
1480 mReader->pushNextDevice(device);
1481 mReader->pushNextDevice(device);
1482 ASSERT_NO_FATAL_FAILURE(
1483 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1484 ASSERT_NO_FATAL_FAILURE(
1485 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1486
1487 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001488 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001489}
1490
Chris Yee14523a2020-12-19 13:46:00 -08001491TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1492 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1493 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1494 // Add two subdevices to device
1495 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1496 // Must add at least one mapper or the device will be ignored!
1497 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1498 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1499
1500 // Push same device instance for next device to be added, so they'll have same identifier.
1501 mReader->pushNextDevice(device);
1502 mReader->pushNextDevice(device);
1503 // Sensor device is initially disabled
1504 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1505 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1506 nullptr));
1507 // Device is disabled because the only sub device is a sensor device and disabled initially.
1508 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1509 ASSERT_FALSE(device->isEnabled());
1510 ASSERT_NO_FATAL_FAILURE(
1511 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1512 // The merged device is enabled if any sub device is enabled
1513 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1514 ASSERT_TRUE(device->isEnabled());
1515}
1516
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001517TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001518 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001519 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001520 constexpr int32_t eventHubId = 1;
1521 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001522 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001523 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001524 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001525 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001526
Yi Kong9b14ac62018-07-17 13:48:38 -07001527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001528
1529 NotifyDeviceResetArgs resetArgs;
1530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001531 ASSERT_EQ(deviceId, resetArgs.deviceId);
1532
1533 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001534 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001535 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001536
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001538 ASSERT_EQ(deviceId, resetArgs.deviceId);
1539 ASSERT_EQ(device->isEnabled(), false);
1540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001541 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001542 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001545 ASSERT_EQ(device->isEnabled(), false);
1546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001547 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001548 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001550 ASSERT_EQ(deviceId, resetArgs.deviceId);
1551 ASSERT_EQ(device->isEnabled(), true);
1552}
1553
Michael Wrightd02c5b62014-02-10 15:10:22 -08001554TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001555 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001556 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001558 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001559 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001560 AINPUT_SOURCE_KEYBOARD, nullptr);
1561 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562
1563 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1564 AINPUT_SOURCE_ANY, AKEYCODE_A))
1565 << "Should return unknown when the device id is >= 0 but unknown.";
1566
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001567 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1568 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1569 << "Should return unknown when the device id is valid but the sources are not "
1570 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001572 ASSERT_EQ(AKEY_STATE_DOWN,
1573 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1574 AKEYCODE_A))
1575 << "Should return value provided by mapper when device id is valid and the device "
1576 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577
1578 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1579 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1580 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1581
1582 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1583 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1584 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1585}
1586
1587TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001588 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001589 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001591 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001592 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001593 AINPUT_SOURCE_KEYBOARD, nullptr);
1594 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595
1596 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1597 AINPUT_SOURCE_ANY, KEY_A))
1598 << "Should return unknown when the device id is >= 0 but unknown.";
1599
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001600 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1601 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1602 << "Should return unknown when the device id is valid but the sources are not "
1603 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 ASSERT_EQ(AKEY_STATE_DOWN,
1606 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1607 KEY_A))
1608 << "Should return value provided by mapper when device id is valid and the device "
1609 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001610
1611 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1612 AINPUT_SOURCE_TRACKBALL, KEY_A))
1613 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1614
1615 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1616 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1617 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1618}
1619
1620TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001621 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001622 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001623 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001624 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001626 AINPUT_SOURCE_KEYBOARD, nullptr);
1627 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628
1629 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1630 AINPUT_SOURCE_ANY, SW_LID))
1631 << "Should return unknown when the device id is >= 0 but unknown.";
1632
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1634 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1635 << "Should return unknown when the device id is valid but the sources are not "
1636 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001637
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 ASSERT_EQ(AKEY_STATE_DOWN,
1639 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1640 SW_LID))
1641 << "Should return value provided by mapper when device id is valid and the device "
1642 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643
1644 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1645 AINPUT_SOURCE_TRACKBALL, SW_LID))
1646 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1647
1648 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1649 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1650 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1651}
1652
1653TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001654 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001655 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001656 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001657 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001659 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001661 mapper.addSupportedKeyCode(AKEYCODE_A);
1662 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663
1664 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1665 uint8_t flags[4] = { 0, 0, 0, 1 };
1666
1667 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1668 << "Should return false when device id is >= 0 but unknown.";
1669 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1670
1671 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001672 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1673 << "Should return false when device id is valid but the sources are not supported by "
1674 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1676
1677 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1679 keyCodes, flags))
1680 << "Should return value provided by mapper when device id is valid and the device "
1681 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1683
1684 flags[3] = 1;
1685 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1686 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1687 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1688
1689 flags[3] = 1;
1690 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1691 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1692 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1693}
1694
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001695TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001697 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001698
1699 NotifyConfigurationChangedArgs args;
1700
1701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1702 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1703}
1704
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001705TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001707 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001709 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001711 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001714 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001715 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1716
1717 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001718 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721 ASSERT_EQ(EV_KEY, event.type);
1722 ASSERT_EQ(KEY_A, event.code);
1723 ASSERT_EQ(1, event.value);
1724}
1725
Garfield Tan1c7bc862020-01-28 13:24:04 -08001726TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001728 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001729 constexpr int32_t eventHubId = 1;
1730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001731 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001732 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001733 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001734 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001735
1736 NotifyDeviceResetArgs resetArgs;
1737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001738 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001739
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001740 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001741 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001743 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001744 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001746 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001747 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001749 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001750 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001752 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001753 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001755 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001756 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001757}
1758
Garfield Tan1c7bc862020-01-28 13:24:04 -08001759TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1760 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001761 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001762 constexpr int32_t eventHubId = 1;
1763 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1764 // Must add at least one mapper or the device will be ignored!
1765 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001766 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001767 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1768
1769 NotifyDeviceResetArgs resetArgs;
1770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1771 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1772}
1773
Arthur Hungc23540e2018-11-29 20:42:11 +08001774TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001775 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001776 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001778 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1780 FakeInputMapper& mapper =
1781 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001782 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001783
1784 const uint8_t hdmi1 = 1;
1785
1786 // Associated touch screen with second display.
1787 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1788
1789 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001790 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001791 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001792 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001793 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001794 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001795 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001796 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001797 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001798 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001799
1800 // Add the device, and make sure all of the callbacks are triggered.
1801 // The device is added after the input port associations are processed since
1802 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001806 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001807
Arthur Hung2c9a3342019-07-23 14:18:59 +08001808 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001809 ASSERT_EQ(deviceId, device->getId());
1810 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1811 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001812
1813 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001815 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001816 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001817}
1818
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001819TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1820 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1821 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1822 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1823 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1824 // Must add at least one mapper or the device will be ignored!
1825 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1826 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1827 mReader->pushNextDevice(device);
1828 mReader->pushNextDevice(device);
1829 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1830 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1831
1832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1833
1834 NotifyDeviceResetArgs resetArgs;
1835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1836 ASSERT_EQ(deviceId, resetArgs.deviceId);
1837 ASSERT_TRUE(device->isEnabled());
1838 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1839 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1840
1841 disableDevice(deviceId);
1842 mReader->loopOnce();
1843
1844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1845 ASSERT_EQ(deviceId, resetArgs.deviceId);
1846 ASSERT_FALSE(device->isEnabled());
1847 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1848 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1849
1850 enableDevice(deviceId);
1851 mReader->loopOnce();
1852
1853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1854 ASSERT_EQ(deviceId, resetArgs.deviceId);
1855 ASSERT_TRUE(device->isEnabled());
1856 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1857 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1858}
1859
1860TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1861 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1862 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1863 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1864 // Add two subdevices to device
1865 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1866 FakeInputMapper& mapperDevice1 =
1867 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1868 FakeInputMapper& mapperDevice2 =
1869 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1870 mReader->pushNextDevice(device);
1871 mReader->pushNextDevice(device);
1872 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1873 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1874
1875 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1876 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1877
1878 ASSERT_EQ(AKEY_STATE_DOWN,
1879 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1880 ASSERT_EQ(AKEY_STATE_DOWN,
1881 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1882 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1883 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1884}
1885
Prabir Pradhan7e186182020-11-10 13:56:45 -08001886TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1887 NotifyPointerCaptureChangedArgs args;
1888
1889 mFakePolicy->setPointerCapture(true);
1890 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1891 mReader->loopOnce();
1892 mFakeListener->assertNotifyCaptureWasCalled(&args);
1893 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1894
1895 mFakePolicy->setPointerCapture(false);
1896 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1897 mReader->loopOnce();
1898 mFakeListener->assertNotifyCaptureWasCalled(&args);
1899 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1900
1901 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1902 // does not change.
1903 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1904 mReader->loopOnce();
1905 mFakeListener->assertNotifyCaptureWasCalled(&args);
1906 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1907}
1908
Chris Ye87143712020-11-10 05:05:58 +00001909class FakeVibratorInputMapper : public FakeInputMapper {
1910public:
1911 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1912 : FakeInputMapper(deviceContext, sources) {}
1913
1914 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1915};
1916
1917TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1918 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1919 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1920 constexpr int32_t eventHubId = 1;
1921 const char* DEVICE_LOCATION = "BLUETOOTH";
1922 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1923 FakeVibratorInputMapper& mapper =
1924 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1925 mReader->pushNextDevice(device);
1926
1927 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1928 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1929
1930 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1931 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1932}
1933
Kim Low03ea0352020-11-06 12:45:07 -08001934class FakeBatteryInputMapper : public FakeInputMapper {
1935public:
1936 FakeBatteryInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1937 : FakeInputMapper(deviceContext, sources) {}
1938
1939 std::optional<int32_t> getBatteryCapacity() override {
1940 return getDeviceContext().getBatteryCapacity();
1941 }
1942
1943 std::optional<int32_t> getBatteryStatus() override {
1944 return getDeviceContext().getBatteryStatus();
1945 }
1946};
1947
1948TEST_F(InputReaderTest, BatteryGetCapacity) {
1949 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1950 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1951 constexpr int32_t eventHubId = 1;
1952 const char* DEVICE_LOCATION = "BLUETOOTH";
1953 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1954 FakeBatteryInputMapper& mapper =
1955 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1956 mReader->pushNextDevice(device);
1957
1958 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1959 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1960
1961 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
1962}
1963
1964TEST_F(InputReaderTest, BatteryGetStatus) {
1965 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1966 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1967 constexpr int32_t eventHubId = 1;
1968 const char* DEVICE_LOCATION = "BLUETOOTH";
1969 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1970 FakeBatteryInputMapper& mapper =
1971 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1972 mReader->pushNextDevice(device);
1973
1974 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1975 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1976
1977 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
1978}
1979
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001980// --- InputReaderIntegrationTest ---
1981
1982// These tests create and interact with the InputReader only through its interface.
1983// The InputReader is started during SetUp(), which starts its processing in its own
1984// thread. The tests use linux uinput to emulate input devices.
1985// NOTE: Interacting with the physical device while these tests are running may cause
1986// the tests to fail.
1987class InputReaderIntegrationTest : public testing::Test {
1988protected:
1989 sp<TestInputListener> mTestListener;
1990 sp<FakeInputReaderPolicy> mFakePolicy;
1991 sp<InputReaderInterface> mReader;
1992
Chris Yea52ade12020-08-27 16:49:20 -07001993 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001994 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001995 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1996 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001997
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001998 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001999 ASSERT_EQ(mReader->start(), OK);
2000
2001 // Since this test is run on a real device, all the input devices connected
2002 // to the test device will show up in mReader. We wait for those input devices to
2003 // show up before beginning the tests.
2004 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2005 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2006 }
2007
Chris Yea52ade12020-08-27 16:49:20 -07002008 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002009 ASSERT_EQ(mReader->stop(), OK);
2010 mTestListener.clear();
2011 mFakePolicy.clear();
2012 }
2013};
2014
2015TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2016 // An invalid input device that is only used for this test.
2017 class InvalidUinputDevice : public UinputDevice {
2018 public:
2019 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2020
2021 private:
2022 void configureDevice(int fd, uinput_user_dev* device) override {}
2023 };
2024
2025 const size_t numDevices = mFakePolicy->getInputDevices().size();
2026
2027 // UinputDevice does not set any event or key bits, so InputReader should not
2028 // consider it as a valid device.
2029 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2030 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2031 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2032 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2033
2034 invalidDevice.reset();
2035 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2036 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2037 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2038}
2039
2040TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2041 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2042
2043 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2044 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2045 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2046 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2047
2048 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002049 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2050 const auto& it =
2051 std::find_if(inputDevices.begin(), inputDevices.end(),
2052 [&keyboard](const InputDeviceInfo& info) {
2053 return info.getIdentifier().name == keyboard->getName();
2054 });
2055
2056 ASSERT_NE(it, inputDevices.end());
2057 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2058 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2059 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002060
2061 keyboard.reset();
2062 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2063 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2064 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2065}
2066
2067TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2068 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2069 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2070
2071 NotifyConfigurationChangedArgs configChangedArgs;
2072 ASSERT_NO_FATAL_FAILURE(
2073 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002074 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002075 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2076
2077 NotifyKeyArgs keyArgs;
2078 keyboard->pressAndReleaseHomeKey();
2079 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2080 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002081 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002082 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002083 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2084 prevTimestamp = keyArgs.eventTime;
2085
2086 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2087 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002088 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002089 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2090}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002091
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002092/**
2093 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2094 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2095 * are passed to the listener.
2096 */
2097static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2098TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2099 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2100 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2101 NotifyKeyArgs keyArgs;
2102
2103 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2104 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2105 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2106 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2107
2108 controller->pressAndReleaseKey(BTN_GEAR_UP);
2109 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2110 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2111 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2112}
2113
Arthur Hungaab25622020-01-16 11:22:11 +08002114// --- TouchProcessTest ---
2115class TouchIntegrationTest : public InputReaderIntegrationTest {
2116protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002117 const std::string UNIQUE_ID = "local:0";
2118
Chris Yea52ade12020-08-27 16:49:20 -07002119 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002120 InputReaderIntegrationTest::SetUp();
2121 // At least add an internal display.
2122 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2123 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002124 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002125
2126 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2127 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2128 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2129 }
2130
2131 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2132 int32_t orientation, const std::string& uniqueId,
2133 std::optional<uint8_t> physicalPort,
2134 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002135 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2136 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002137 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2138 }
2139
2140 std::unique_ptr<UinputTouchScreen> mDevice;
2141};
2142
2143TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2144 NotifyMotionArgs args;
2145 const Point centerPoint = mDevice->getCenterPoint();
2146
2147 // ACTION_DOWN
2148 mDevice->sendDown(centerPoint);
2149 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2151
2152 // ACTION_MOVE
2153 mDevice->sendMove(centerPoint + Point(1, 1));
2154 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2156
2157 // ACTION_UP
2158 mDevice->sendUp();
2159 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2161}
2162
2163TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2164 NotifyMotionArgs args;
2165 const Point centerPoint = mDevice->getCenterPoint();
2166
2167 // ACTION_DOWN
2168 mDevice->sendDown(centerPoint);
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2171
2172 // ACTION_POINTER_DOWN (Second slot)
2173 const Point secondPoint = centerPoint + Point(100, 100);
2174 mDevice->sendSlot(SECOND_SLOT);
2175 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2176 mDevice->sendDown(secondPoint + Point(1, 1));
2177 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2178 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2179 args.action);
2180
2181 // ACTION_MOVE (Second slot)
2182 mDevice->sendMove(secondPoint);
2183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2185
2186 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002187 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002188 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002189 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002190 args.action);
2191
2192 // ACTION_UP
2193 mDevice->sendSlot(FIRST_SLOT);
2194 mDevice->sendUp();
2195 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2197}
2198
2199TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2200 NotifyMotionArgs args;
2201 const Point centerPoint = mDevice->getCenterPoint();
2202
2203 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002204 mDevice->sendSlot(FIRST_SLOT);
2205 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002206 mDevice->sendDown(centerPoint);
2207 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2209
arthurhungcc7f9802020-04-30 17:55:40 +08002210 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002211 const Point secondPoint = centerPoint + Point(100, 100);
2212 mDevice->sendSlot(SECOND_SLOT);
2213 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2214 mDevice->sendDown(secondPoint);
2215 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2216 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2217 args.action);
2218
arthurhungcc7f9802020-04-30 17:55:40 +08002219 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002220 mDevice->sendMove(secondPoint + Point(1, 1));
2221 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2223
arthurhungcc7f9802020-04-30 17:55:40 +08002224 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2225 // a palm event.
2226 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002227 mDevice->sendToolType(MT_TOOL_PALM);
2228 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002229 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2230 args.action);
2231 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002232
arthurhungcc7f9802020-04-30 17:55:40 +08002233 // Send up to second slot, expect first slot send moving.
2234 mDevice->sendPointerUp();
2235 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002237
arthurhungcc7f9802020-04-30 17:55:40 +08002238 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002239 mDevice->sendSlot(FIRST_SLOT);
2240 mDevice->sendUp();
2241
arthurhungcc7f9802020-04-30 17:55:40 +08002242 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2243 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002244}
2245
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002247class InputDeviceTest : public testing::Test {
2248protected:
2249 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002250 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251 static const int32_t DEVICE_ID;
2252 static const int32_t DEVICE_GENERATION;
2253 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002254 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002255 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002257 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002259 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002260 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002261 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262
Chris Yea52ade12020-08-27 16:49:20 -07002263 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002264 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002265 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002266 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002267 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2268 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269 InputDeviceIdentifier identifier;
2270 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002271 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002272 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002273 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002274 mReader->pushNextDevice(mDevice);
2275 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2276 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277 }
2278
Chris Yea52ade12020-08-27 16:49:20 -07002279 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280 mFakeListener.clear();
2281 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282 }
2283};
2284
2285const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002286const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002287const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2289const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002290const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2291 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002292const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002293
2294TEST_F(InputDeviceTest, ImmutableProperties) {
2295 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002296 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002297 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298}
2299
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002300TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2301 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002302}
2303
Michael Wrightd02c5b62014-02-10 15:10:22 -08002304TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2305 // Configuration.
2306 InputReaderConfiguration config;
2307 mDevice->configure(ARBITRARY_TIME, &config, 0);
2308
2309 // Reset.
2310 mDevice->reset(ARBITRARY_TIME);
2311
2312 NotifyDeviceResetArgs resetArgs;
2313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2314 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2315 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2316
2317 // Metadata.
2318 ASSERT_TRUE(mDevice->isIgnored());
2319 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2320
2321 InputDeviceInfo info;
2322 mDevice->getDeviceInfo(&info);
2323 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002324 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2326 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2327
2328 // State queries.
2329 ASSERT_EQ(0, mDevice->getMetaState());
2330
2331 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2332 << "Ignored device should return unknown key code state.";
2333 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2334 << "Ignored device should return unknown scan code state.";
2335 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2336 << "Ignored device should return unknown switch state.";
2337
2338 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2339 uint8_t flags[2] = { 0, 1 };
2340 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2341 << "Ignored device should never mark any key codes.";
2342 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2343 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2344}
2345
2346TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2347 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002348 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002349
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002350 FakeInputMapper& mapper1 =
2351 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002352 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2353 mapper1.setMetaState(AMETA_ALT_ON);
2354 mapper1.addSupportedKeyCode(AKEYCODE_A);
2355 mapper1.addSupportedKeyCode(AKEYCODE_B);
2356 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2357 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2358 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2359 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2360 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002362 FakeInputMapper& mapper2 =
2363 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002364 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365
2366 InputReaderConfiguration config;
2367 mDevice->configure(ARBITRARY_TIME, &config, 0);
2368
2369 String8 propertyValue;
2370 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2371 << "Device should have read configuration during configuration phase.";
2372 ASSERT_STREQ("value", propertyValue.string());
2373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002374 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2375 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376
2377 // Reset
2378 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002379 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2380 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381
2382 NotifyDeviceResetArgs resetArgs;
2383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2384 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2385 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2386
2387 // Metadata.
2388 ASSERT_FALSE(mDevice->isIgnored());
2389 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2390
2391 InputDeviceInfo info;
2392 mDevice->getDeviceInfo(&info);
2393 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002394 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2396 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2397
2398 // State queries.
2399 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2400 << "Should query mappers and combine meta states.";
2401
2402 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2403 << "Should return unknown key code state when source not supported.";
2404 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2405 << "Should return unknown scan code state when source not supported.";
2406 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2407 << "Should return unknown switch state when source not supported.";
2408
2409 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2410 << "Should query mapper when source is supported.";
2411 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2412 << "Should query mapper when source is supported.";
2413 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2414 << "Should query mapper when source is supported.";
2415
2416 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2417 uint8_t flags[4] = { 0, 0, 0, 1 };
2418 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2419 << "Should do nothing when source is unsupported.";
2420 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2421 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2422 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2423 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2424
2425 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2426 << "Should query mapper when source is supported.";
2427 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2428 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2429 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2430 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2431
2432 // Event handling.
2433 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002434 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435 mDevice->process(&event, 1);
2436
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002437 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2438 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439}
2440
Arthur Hung2c9a3342019-07-23 14:18:59 +08002441// A single input device is associated with a specific display. Check that:
2442// 1. Device is disabled if the viewport corresponding to the associated display is not found
2443// 2. Device is disabled when setEnabled API is called
2444TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002445 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002446
2447 // First Configuration.
2448 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2449
2450 // Device should be enabled by default.
2451 ASSERT_TRUE(mDevice->isEnabled());
2452
2453 // Prepare associated info.
2454 constexpr uint8_t hdmi = 1;
2455 const std::string UNIQUE_ID = "local:1";
2456
2457 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2458 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2459 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2460 // Device should be disabled because it is associated with a specific display via
2461 // input port <-> display port association, but the corresponding display is not found
2462 ASSERT_FALSE(mDevice->isEnabled());
2463
2464 // Prepare displays.
2465 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002466 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2467 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002468 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2469 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2470 ASSERT_TRUE(mDevice->isEnabled());
2471
2472 // Device should be disabled after set disable.
2473 mFakePolicy->addDisabledDevice(mDevice->getId());
2474 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2475 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2476 ASSERT_FALSE(mDevice->isEnabled());
2477
2478 // Device should still be disabled even found the associated display.
2479 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2480 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2481 ASSERT_FALSE(mDevice->isEnabled());
2482}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
2484// --- InputMapperTest ---
2485
2486class InputMapperTest : public testing::Test {
2487protected:
2488 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002489 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490 static const int32_t DEVICE_ID;
2491 static const int32_t DEVICE_GENERATION;
2492 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002493 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002494 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002496 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002498 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002499 std::unique_ptr<InstrumentedInputReader> mReader;
2500 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002501
Chris Ye1b0c7342020-07-28 21:57:03 -07002502 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002503 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002504 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002505 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002506 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2507 mFakeListener);
2508 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509 }
2510
Chris Yea52ade12020-08-27 16:49:20 -07002511 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002512
Chris Yea52ade12020-08-27 16:49:20 -07002513 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 mFakeListener.clear();
2515 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 }
2517
2518 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002519 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002520 }
2521
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002522 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002523 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002524 mReader->requestRefreshConfiguration(changes);
2525 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002526 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002527 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2528 }
2529
arthurhungdcef2dc2020-08-11 14:47:50 +08002530 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2531 const std::string& location, int32_t eventHubId,
2532 Flags<InputDeviceClass> classes) {
2533 InputDeviceIdentifier identifier;
2534 identifier.name = name;
2535 identifier.location = location;
2536 std::shared_ptr<InputDevice> device =
2537 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2538 identifier);
2539 mReader->pushNextDevice(device);
2540 mFakeEventHub->addDevice(eventHubId, name, classes);
2541 mReader->loopOnce();
2542 return device;
2543 }
2544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002545 template <class T, typename... Args>
2546 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002547 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002548 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002550 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002551 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552 }
2553
2554 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002555 int32_t orientation, const std::string& uniqueId,
2556 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002557 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2558 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002559 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2560 }
2561
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002562 void clearViewports() {
2563 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564 }
2565
arthurhungdcef2dc2020-08-11 14:47:50 +08002566 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 RawEvent event;
2568 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002569 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570 event.type = type;
2571 event.code = code;
2572 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002573 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002574 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 }
2576
2577 static void assertMotionRange(const InputDeviceInfo& info,
2578 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2579 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002580 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2582 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2583 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2584 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2585 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2586 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2587 }
2588
2589 static void assertPointerCoords(const PointerCoords& coords,
2590 float x, float y, float pressure, float size,
2591 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2592 float orientation, float distance) {
2593 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2594 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2595 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2596 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2597 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2598 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2599 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2600 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2601 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2602 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2603 }
2604
Michael Wright17db18e2020-06-26 20:51:44 +01002605 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002607 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608 ASSERT_NEAR(x, actualX, 1);
2609 ASSERT_NEAR(y, actualY, 1);
2610 }
2611};
2612
2613const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002614const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002615const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2617const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002618const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2619 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002620const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621
2622// --- SwitchInputMapperTest ---
2623
2624class SwitchInputMapperTest : public InputMapperTest {
2625protected:
2626};
2627
2628TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002629 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002631 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632}
2633
2634TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002635 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002637 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002638 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002640 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002641 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642}
2643
2644TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002645 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002647 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2648 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2649 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2650 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
2652 NotifySwitchArgs args;
2653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2654 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002655 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2656 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657 args.switchMask);
2658 ASSERT_EQ(uint32_t(0), args.policyFlags);
2659}
2660
Chris Ye87143712020-11-10 05:05:58 +00002661// --- VibratorInputMapperTest ---
2662class VibratorInputMapperTest : public InputMapperTest {
2663protected:
2664 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2665};
2666
2667TEST_F(VibratorInputMapperTest, GetSources) {
2668 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2669
2670 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2671}
2672
2673TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2674 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2675
2676 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2677}
2678
2679TEST_F(VibratorInputMapperTest, Vibrate) {
2680 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
2681 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());
2698 mapper.vibrate(sequence, -1 /* repeat */, 0 /* token */);
2699 ASSERT_TRUE(mapper.isVibrating());
2700}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701
Chris Yef59a2f42020-10-16 12:55:26 -07002702// --- SensorInputMapperTest ---
2703
2704class SensorInputMapperTest : public InputMapperTest {
2705protected:
2706 static const int32_t ACCEL_RAW_MIN;
2707 static const int32_t ACCEL_RAW_MAX;
2708 static const int32_t ACCEL_RAW_FUZZ;
2709 static const int32_t ACCEL_RAW_FLAT;
2710 static const int32_t ACCEL_RAW_RESOLUTION;
2711
2712 static const int32_t GYRO_RAW_MIN;
2713 static const int32_t GYRO_RAW_MAX;
2714 static const int32_t GYRO_RAW_FUZZ;
2715 static const int32_t GYRO_RAW_FLAT;
2716 static const int32_t GYRO_RAW_RESOLUTION;
2717
2718 static const float GRAVITY_MS2_UNIT;
2719 static const float DEGREE_RADIAN_UNIT;
2720
2721 void prepareAccelAxes();
2722 void prepareGyroAxes();
2723 void setAccelProperties();
2724 void setGyroProperties();
2725 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2726};
2727
2728const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2729const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2730const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2731const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2732const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2733
2734const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2735const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2736const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2737const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2738const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2739
2740const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2741const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2742
2743void SensorInputMapperTest::prepareAccelAxes() {
2744 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2745 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2746 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2747 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2748 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2749 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2750}
2751
2752void SensorInputMapperTest::prepareGyroAxes() {
2753 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2754 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2756 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2757 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2758 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2759}
2760
2761void SensorInputMapperTest::setAccelProperties() {
2762 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2763 /* sensorDataIndex */ 0);
2764 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2765 /* sensorDataIndex */ 1);
2766 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2767 /* sensorDataIndex */ 2);
2768 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2769 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2770 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2771 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2772 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2773}
2774
2775void SensorInputMapperTest::setGyroProperties() {
2776 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2777 /* sensorDataIndex */ 0);
2778 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2779 /* sensorDataIndex */ 1);
2780 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2781 /* sensorDataIndex */ 2);
2782 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2783 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2784 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2785 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2786 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2787}
2788
2789TEST_F(SensorInputMapperTest, GetSources) {
2790 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2791
2792 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2793}
2794
2795TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2796 setAccelProperties();
2797 prepareAccelAxes();
2798 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2799
2800 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2801 std::chrono::microseconds(10000),
2802 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002803 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002804 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, 20000);
2805 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, -20000);
2806 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Z, 40000);
2807 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2808 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2809
2810 NotifySensorArgs args;
2811 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2812 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2813 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2814
2815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2816 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2817 ASSERT_EQ(args.deviceId, DEVICE_ID);
2818 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2819 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2820 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2821 ASSERT_EQ(args.values, values);
2822 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2823}
2824
2825TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2826 setGyroProperties();
2827 prepareGyroAxes();
2828 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2829
2830 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2831 std::chrono::microseconds(10000),
2832 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002833 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002834 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RX, 20000);
2835 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RY, -20000);
2836 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RZ, 40000);
2837 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2838 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2839
2840 NotifySensorArgs args;
2841 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2842 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2843 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2844
2845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2846 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2847 ASSERT_EQ(args.deviceId, DEVICE_ID);
2848 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2849 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2850 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2851 ASSERT_EQ(args.values, values);
2852 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2853}
2854
Kim Low03ea0352020-11-06 12:45:07 -08002855// --- BatteryInputMapperTest ---
2856class BatteryInputMapperTest : public InputMapperTest {
2857protected:
2858 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY); }
2859};
2860
2861TEST_F(BatteryInputMapperTest, GetSources) {
2862 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2863
2864 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2865}
2866
2867TEST_F(BatteryInputMapperTest, GetBatteryCapacity) {
2868 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2869
2870 ASSERT_TRUE(mapper.getBatteryCapacity());
2871 ASSERT_EQ(*mapper.getBatteryCapacity(), BATTERY_CAPACITY);
2872}
2873
2874TEST_F(BatteryInputMapperTest, GetBatteryStatus) {
2875 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2876
2877 ASSERT_TRUE(mapper.getBatteryStatus());
2878 ASSERT_EQ(*mapper.getBatteryStatus(), BATTERY_STATUS);
2879}
2880
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881// --- KeyboardInputMapperTest ---
2882
2883class KeyboardInputMapperTest : public InputMapperTest {
2884protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002885 const std::string UNIQUE_ID = "local:0";
2886
2887 void prepareDisplay(int32_t orientation);
2888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002889 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002890 int32_t originalKeyCode, int32_t rotatedKeyCode,
2891 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892};
2893
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002894/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2895 * orientation.
2896 */
2897void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002898 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2899 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002900}
2901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002902void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002903 int32_t originalScanCode, int32_t originalKeyCode,
2904 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905 NotifyKeyArgs args;
2906
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002907 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2909 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2910 ASSERT_EQ(originalScanCode, args.scanCode);
2911 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002912 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002914 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2916 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2917 ASSERT_EQ(originalScanCode, args.scanCode);
2918 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002919 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920}
2921
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002923 KeyboardInputMapper& mapper =
2924 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2925 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002927 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928}
2929
2930TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2931 const int32_t USAGE_A = 0x070004;
2932 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002933 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2934 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002935 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2936 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2937 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002939 KeyboardInputMapper& mapper =
2940 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2941 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002942 // Initial metastate to AMETA_NONE.
2943 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2944 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945
2946 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002947 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 NotifyKeyArgs args;
2949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2950 ASSERT_EQ(DEVICE_ID, args.deviceId);
2951 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2952 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2953 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2954 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2955 ASSERT_EQ(KEY_HOME, args.scanCode);
2956 ASSERT_EQ(AMETA_NONE, args.metaState);
2957 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2958 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2959 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2960
2961 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002962 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2964 ASSERT_EQ(DEVICE_ID, args.deviceId);
2965 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2966 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2968 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2969 ASSERT_EQ(KEY_HOME, args.scanCode);
2970 ASSERT_EQ(AMETA_NONE, args.metaState);
2971 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2972 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2973 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2974
2975 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002976 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2977 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
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, args.eventTime);
2982 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2983 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2984 ASSERT_EQ(0, 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 up 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 + 1, EV_KEY, 0, 0);
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 + 1, args.eventTime);
2997 ASSERT_EQ(AKEY_EVENT_ACTION_UP, 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 down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003006 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3007 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
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, args.eventTime);
3012 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3013 ASSERT_EQ(0, args.keyCode);
3014 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3015 ASSERT_EQ(AMETA_NONE, args.metaState);
3016 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3017 ASSERT_EQ(0U, args.policyFlags);
3018 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3019
3020 // Key up 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 + 1, EV_KEY, KEY_UNKNOWN, 0);
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 + 1, args.eventTime);
3027 ASSERT_EQ(AKEY_EVENT_ACTION_UP, 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
3036TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003037 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3038 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003039 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3040 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3041 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003043 KeyboardInputMapper& mapper =
3044 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3045 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046
arthurhungc903df12020-08-11 15:08:42 +08003047 // Initial metastate to AMETA_NONE.
3048 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3049 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050
3051 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003052 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 NotifyKeyArgs args;
3054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3055 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003057 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058
3059 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003060 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3062 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003063 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064
3065 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003066 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070
3071 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003072 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3074 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003075 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003076 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077}
3078
3079TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003080 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3081 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3082 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3083 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 KeyboardInputMapper& mapper =
3086 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3087 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003089 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3091 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3092 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3093 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3094 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3095 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3096 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3097 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3098}
3099
3100TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003101 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3102 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3103 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3104 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
Michael Wrightd02c5b62014-02-10 15:10:22 -08003106 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003107 KeyboardInputMapper& mapper =
3108 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3109 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003111 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003112 ASSERT_NO_FATAL_FAILURE(
3113 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3114 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3115 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3116 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3117 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3118 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3119 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003121 clearViewports();
3122 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003123 ASSERT_NO_FATAL_FAILURE(
3124 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3125 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3126 AKEYCODE_DPAD_UP, DISPLAY_ID));
3127 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3128 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3129 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3130 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003132 clearViewports();
3133 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003134 ASSERT_NO_FATAL_FAILURE(
3135 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3136 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3137 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3138 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3139 AKEYCODE_DPAD_UP, DISPLAY_ID));
3140 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3141 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003143 clearViewports();
3144 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003145 ASSERT_NO_FATAL_FAILURE(
3146 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3147 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3148 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3149 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3150 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3151 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3152 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153
3154 // Special case: if orientation changes while key is down, we still emit the same keycode
3155 // in the key up as we did in the key down.
3156 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003157 clearViewports();
3158 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003159 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3161 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3162 ASSERT_EQ(KEY_UP, args.scanCode);
3163 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3164
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003165 clearViewports();
3166 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003167 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3169 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3170 ASSERT_EQ(KEY_UP, args.scanCode);
3171 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3172}
3173
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003174TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3175 // If the keyboard is not orientation aware,
3176 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003177 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 KeyboardInputMapper& mapper =
3180 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3181 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003182 NotifyKeyArgs args;
3183
3184 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003185 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003187 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3189 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3190
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003191 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003192 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003194 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3196 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3197}
3198
3199TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3200 // If the keyboard is orientation aware,
3201 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003202 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003203
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003204 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003205 KeyboardInputMapper& mapper =
3206 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3207 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003208 NotifyKeyArgs args;
3209
3210 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3211 // ^--- already checked by the previous test
3212
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003213 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003214 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003215 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003217 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3219 ASSERT_EQ(DISPLAY_ID, args.displayId);
3220
3221 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003222 clearViewports();
3223 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003224 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003225 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003227 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3229 ASSERT_EQ(newDisplayId, args.displayId);
3230}
3231
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003233 KeyboardInputMapper& mapper =
3234 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3235 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003237 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003238 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003240 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003241 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242}
3243
3244TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 KeyboardInputMapper& mapper =
3246 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3247 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003252 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003253 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254}
3255
3256TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 KeyboardInputMapper& mapper =
3258 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3259 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003261 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262
3263 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3264 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_TRUE(flags[0]);
3267 ASSERT_FALSE(flags[1]);
3268}
3269
3270TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003271 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3272 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3273 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3274 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3275 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3276 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 KeyboardInputMapper& mapper =
3279 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3280 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003281 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003282 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3283 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284
3285 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3287 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3288 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
3290 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003291 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3292 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003293 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3294 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3295 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
3298 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003299 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3300 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003301 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3302 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3303 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305
3306 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003307 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3308 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003309 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3310 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3311 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003312 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313
3314 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003315 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3316 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3318 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3319 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003320 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321
3322 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003323 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3324 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003325 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3326 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3327 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003328 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
3330 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003331 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3332 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003333 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3334 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3335 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003336 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337}
3338
Chris Yea52ade12020-08-27 16:49:20 -07003339TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3340 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3341 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3342 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3343 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3344
3345 KeyboardInputMapper& mapper =
3346 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3347 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3348
3349 // Initial metastate should be AMETA_NONE as no meta keys added.
3350 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3351 // Meta state should be AMETA_NONE after reset
3352 mapper.reset(ARBITRARY_TIME);
3353 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3354 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3355 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3356 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3357
3358 NotifyKeyArgs args;
3359 // Press button "A"
3360 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
3361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3362 ASSERT_EQ(AMETA_NONE, args.metaState);
3363 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3364 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3365 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3366
3367 // Button up.
3368 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
3369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3370 ASSERT_EQ(AMETA_NONE, args.metaState);
3371 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3372 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3373 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3374}
3375
Arthur Hung2c9a3342019-07-23 14:18:59 +08003376TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3377 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003378 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3379 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3380 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3381 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003382
3383 // keyboard 2.
3384 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003385 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003386 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003387 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003388 std::shared_ptr<InputDevice> device2 =
3389 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3390 Flags<InputDeviceClass>(0));
3391
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003392 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3393 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3394 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3395 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003397 KeyboardInputMapper& mapper =
3398 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3399 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003402 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003404 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3405 device2->reset(ARBITRARY_TIME);
3406
3407 // Prepared displays and associated info.
3408 constexpr uint8_t hdmi1 = 0;
3409 constexpr uint8_t hdmi2 = 1;
3410 const std::string SECONDARY_UNIQUE_ID = "local:1";
3411
3412 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3413 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3414
3415 // No associated display viewport found, should disable the device.
3416 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3417 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3418 ASSERT_FALSE(device2->isEnabled());
3419
3420 // Prepare second display.
3421 constexpr int32_t newDisplayId = 2;
3422 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003423 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003424 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003425 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003426 // Default device will reconfigure above, need additional reconfiguration for another device.
3427 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3428 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3429
3430 // Device should be enabled after the associated display is found.
3431 ASSERT_TRUE(mDevice->isEnabled());
3432 ASSERT_TRUE(device2->isEnabled());
3433
3434 // Test pad key events
3435 ASSERT_NO_FATAL_FAILURE(
3436 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3437 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3438 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3439 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3440 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3441 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3442 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3443
3444 ASSERT_NO_FATAL_FAILURE(
3445 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3446 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3447 AKEYCODE_DPAD_RIGHT, newDisplayId));
3448 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3449 AKEYCODE_DPAD_DOWN, newDisplayId));
3450 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3451 AKEYCODE_DPAD_LEFT, newDisplayId));
3452}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453
arthurhungc903df12020-08-11 15:08:42 +08003454TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3455 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3456 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3457 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3458 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3460 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3461
3462 KeyboardInputMapper& mapper =
3463 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3464 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3465 // Initial metastate to AMETA_NONE.
3466 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3467 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3468
3469 // Initialization should have turned all of the lights off.
3470 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3471 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3472 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3473
3474 // Toggle caps lock on.
3475 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3476 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3477 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3478 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3479
3480 // Toggle num lock on.
3481 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3482 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3483 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3484 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3485
3486 // Toggle scroll lock on.
3487 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3488 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3489 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3490 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3491
3492 mFakeEventHub->removeDevice(EVENTHUB_ID);
3493 mReader->loopOnce();
3494
3495 // keyboard 2 should default toggle keys.
3496 const std::string USB2 = "USB2";
3497 const std::string DEVICE_NAME2 = "KEYBOARD2";
3498 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3499 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3500 std::shared_ptr<InputDevice> device2 =
3501 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3502 Flags<InputDeviceClass>(0));
3503 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3504 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3505 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3506 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3507 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3508 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3509
arthurhung6fe95782020-10-05 22:41:16 +08003510 KeyboardInputMapper& mapper2 =
3511 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3512 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003513 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3514 device2->reset(ARBITRARY_TIME);
3515
3516 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3517 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3518 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003519 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3520 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003521}
3522
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003523// --- KeyboardInputMapperTest_ExternalDevice ---
3524
3525class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3526protected:
Chris Yea52ade12020-08-27 16:49:20 -07003527 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003528};
3529
3530TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003531 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3532 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003533
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3537 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539 KeyboardInputMapper& mapper =
3540 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3541 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003542
3543 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3544 NotifyKeyArgs args;
3545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3546 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3547
3548 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3550 ASSERT_EQ(uint32_t(0), args.policyFlags);
3551
3552 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3554 ASSERT_EQ(uint32_t(0), args.policyFlags);
3555
3556 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3558 ASSERT_EQ(uint32_t(0), args.policyFlags);
3559
3560 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3562 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3563
3564 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3566 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3567}
3568
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003569TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003570 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003571
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003572 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003575
Powei Fengd041c5d2019-05-03 17:11:33 -07003576 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003577 KeyboardInputMapper& mapper =
3578 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3579 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003580
3581 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3582 NotifyKeyArgs args;
3583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3584 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3585
3586 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3588 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3589
3590 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3592 ASSERT_EQ(uint32_t(0), args.policyFlags);
3593
3594 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3596 ASSERT_EQ(uint32_t(0), args.policyFlags);
3597
3598 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3600 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3601
3602 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3604 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3605}
3606
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607// --- CursorInputMapperTest ---
3608
3609class CursorInputMapperTest : public InputMapperTest {
3610protected:
3611 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3612
Michael Wright17db18e2020-06-26 20:51:44 +01003613 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003614
Chris Yea52ade12020-08-27 16:49:20 -07003615 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616 InputMapperTest::SetUp();
3617
Michael Wright17db18e2020-06-26 20:51:44 +01003618 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003619 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 }
3621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003622 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3623 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003624
3625 void prepareDisplay(int32_t orientation) {
3626 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003627 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003628 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3629 orientation, uniqueId, NO_PORT, viewportType);
3630 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631};
3632
3633const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3634
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003635void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3636 int32_t originalY, int32_t rotatedX,
3637 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 NotifyMotionArgs args;
3639
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003640 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3641 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3642 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3646 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3647 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3648 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3649}
3650
3651TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003653 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003655 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656}
3657
3658TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003660 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003663}
3664
3665TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003667 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668
3669 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671
3672 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003673 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3674 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3676 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3677
3678 // When the bounds are set, then there should be a valid motion range.
3679 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3680
3681 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003682 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683
3684 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3685 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3686 1, 800 - 1, 0.0f, 0.0f));
3687 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3688 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3689 2, 480 - 1, 0.0f, 0.0f));
3690 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3691 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3692 0.0f, 1.0f, 0.0f, 0.0f));
3693}
3694
3695TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698
3699 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003700 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701
3702 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3703 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3704 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3705 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3706 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3707 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3708 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3709 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3710 0.0f, 1.0f, 0.0f, 0.0f));
3711}
3712
3713TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003715 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
arthurhungdcef2dc2020-08-11 14:47:50 +08003717 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718
3719 NotifyMotionArgs args;
3720
3721 // Button press.
3722 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003723 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3724 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3726 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3727 ASSERT_EQ(DEVICE_ID, args.deviceId);
3728 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3729 ASSERT_EQ(uint32_t(0), args.policyFlags);
3730 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3731 ASSERT_EQ(0, args.flags);
3732 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3733 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3734 ASSERT_EQ(0, args.edgeFlags);
3735 ASSERT_EQ(uint32_t(1), args.pointerCount);
3736 ASSERT_EQ(0, args.pointerProperties[0].id);
3737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3739 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3740 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3741 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3742 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3743
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3745 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3746 ASSERT_EQ(DEVICE_ID, args.deviceId);
3747 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3748 ASSERT_EQ(uint32_t(0), args.policyFlags);
3749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3750 ASSERT_EQ(0, args.flags);
3751 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3752 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3753 ASSERT_EQ(0, args.edgeFlags);
3754 ASSERT_EQ(uint32_t(1), args.pointerCount);
3755 ASSERT_EQ(0, args.pointerProperties[0].id);
3756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3758 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3759 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3760 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3761 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3762
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003764 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3765 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3767 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3768 ASSERT_EQ(DEVICE_ID, args.deviceId);
3769 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3770 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3772 ASSERT_EQ(0, args.flags);
3773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3774 ASSERT_EQ(0, args.buttonState);
3775 ASSERT_EQ(0, args.edgeFlags);
3776 ASSERT_EQ(uint32_t(1), args.pointerCount);
3777 ASSERT_EQ(0, args.pointerProperties[0].id);
3778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3780 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3781 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3782 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3783 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3784
3785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3786 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3787 ASSERT_EQ(DEVICE_ID, args.deviceId);
3788 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3789 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3791 ASSERT_EQ(0, args.flags);
3792 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3793 ASSERT_EQ(0, args.buttonState);
3794 ASSERT_EQ(0, args.edgeFlags);
3795 ASSERT_EQ(uint32_t(1), args.pointerCount);
3796 ASSERT_EQ(0, args.pointerProperties[0].id);
3797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3799 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3800 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3801 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3802 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3803}
3804
3805TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
3809 NotifyMotionArgs args;
3810
3811 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003812 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3813 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3817 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3818
3819 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003820 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3821 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3825 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3826}
3827
3828TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003830 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831
3832 NotifyMotionArgs args;
3833
3834 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003835 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
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_DOWN, args.action);
3839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3840 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3841
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3845 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3846
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003848 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3849 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3853 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3854
3855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3858 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3859}
3860
3861TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864
3865 NotifyMotionArgs args;
3866
3867 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003868 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3869 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3870 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3871 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3873 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3875 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3876 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3877
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3881 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3882 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3883
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003885 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3886 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3887 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3891 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3892 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3893
3894 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003895 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3896 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3900 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3901
3902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3905 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3906}
3907
3908TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003910 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003912 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3914 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3915 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3916 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3917 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3918 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3919 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3920 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3921}
3922
3923TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924 addConfigurationProperty("cursor.mode", "navigation");
3925 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003926 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003928 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3930 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3931 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3932 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3933 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3934 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3935 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3936 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3937
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003938 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3940 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3941 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3942 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3943 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3944 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3945 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3946 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3947
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003948 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3950 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3951 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3952 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3953 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3954 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3955 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3956 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3957
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003958 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3960 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3961 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3962 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3963 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3964 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3965 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3966 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3967}
3968
3969TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003971 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972
3973 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3974 mFakePointerController->setPosition(100, 200);
3975 mFakePointerController->setButtonState(0);
3976
3977 NotifyMotionArgs motionArgs;
3978 NotifyKeyArgs keyArgs;
3979
3980 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003981 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3982 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3984 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3985 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3986 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3988 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3989
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3992 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3993 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3995 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3996
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003997 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3998 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 ASSERT_EQ(0, motionArgs.buttonState);
4002 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4004 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4005
4006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004007 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008 ASSERT_EQ(0, motionArgs.buttonState);
4009 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4011 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4012
4013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004015 ASSERT_EQ(0, motionArgs.buttonState);
4016 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4018 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4019
4020 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004021 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
4022 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
4023 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4025 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4026 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4027 motionArgs.buttonState);
4028 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4029 mFakePointerController->getButtonState());
4030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4031 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4032
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4035 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4036 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4037 mFakePointerController->getButtonState());
4038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4039 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4040
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4043 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4044 motionArgs.buttonState);
4045 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4046 mFakePointerController->getButtonState());
4047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4048 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4049
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004050 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
4051 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4055 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4057 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4058
4059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4062 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4064 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4065
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004066 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
4067 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4070 ASSERT_EQ(0, motionArgs.buttonState);
4071 ASSERT_EQ(0, mFakePointerController->getButtonState());
4072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4073 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 -08004074 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
4075 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004076
4077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 ASSERT_EQ(0, motionArgs.buttonState);
4079 ASSERT_EQ(0, mFakePointerController->getButtonState());
4080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4082 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 -08004083
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4085 ASSERT_EQ(0, motionArgs.buttonState);
4086 ASSERT_EQ(0, mFakePointerController->getButtonState());
4087 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4089 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4090
4091 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004092 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
4093 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4095 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4096 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004097
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004099 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4101 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4103 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4104
4105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4107 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4108 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4110 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4111
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004112 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
4113 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 ASSERT_EQ(0, motionArgs.buttonState);
4117 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4119 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4120
4121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004123 ASSERT_EQ(0, motionArgs.buttonState);
4124 ASSERT_EQ(0, mFakePointerController->getButtonState());
4125
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4127 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4129 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4130 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4131
4132 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004133 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
4134 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4136 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4137 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004138
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004140 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4142 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4144 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4145
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4148 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4149 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4151 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4152
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004153 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
4154 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_EQ(0, motionArgs.buttonState);
4158 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4160 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 -08004161
4162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4164 ASSERT_EQ(0, motionArgs.buttonState);
4165 ASSERT_EQ(0, mFakePointerController->getButtonState());
4166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4167 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4168
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4170 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4171 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4172
4173 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004174 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
4175 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4177 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4178 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004179
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004181 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4183 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4185 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4186
4187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4189 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4190 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4192 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4193
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004194 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
4195 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198 ASSERT_EQ(0, motionArgs.buttonState);
4199 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4201 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 -08004202
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4205 ASSERT_EQ(0, motionArgs.buttonState);
4206 ASSERT_EQ(0, mFakePointerController->getButtonState());
4207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4208 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4209
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4211 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4212 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4213
4214 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004215 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
4216 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4218 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4219 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004220
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4224 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4226 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4227
4228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4230 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4231 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4233 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4234
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004235 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
4236 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_EQ(0, motionArgs.buttonState);
4240 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4242 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 -08004243
4244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4245 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4246 ASSERT_EQ(0, motionArgs.buttonState);
4247 ASSERT_EQ(0, mFakePointerController->getButtonState());
4248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4249 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4250
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4252 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4253 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4254}
4255
4256TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004258 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259
4260 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4261 mFakePointerController->setPosition(100, 200);
4262 mFakePointerController->setButtonState(0);
4263
4264 NotifyMotionArgs args;
4265
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004266 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4267 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4268 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004270 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4271 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4273 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 +01004274 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004275}
4276
4277TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004278 addConfigurationProperty("cursor.mode", "pointer");
4279 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004280 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004281
4282 NotifyDeviceResetArgs resetArgs;
4283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4284 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4285 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4286
4287 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4288 mFakePointerController->setPosition(100, 200);
4289 mFakePointerController->setButtonState(0);
4290
4291 NotifyMotionArgs args;
4292
4293 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004294 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4295 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4296 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4298 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4301 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 +01004302 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004303
4304 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004305 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4306 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4308 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4309 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4311 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4313 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4316 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4317
4318 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004319 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
4320 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4322 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4325 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4327 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4328 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4330 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4331
4332 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004333 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
4334 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
4335 process(mapper, ARBITRARY_TIME, 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_MOVE, args.action);
4339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4340 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 +01004341 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004342
4343 // Disable pointer capture and check that the device generation got bumped
4344 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004345 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004346 mFakePolicy->setPointerCapture(false);
4347 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004348 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004349
4350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4351 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4352 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4353
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004354 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4355 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4356 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4358 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4361 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 +01004362 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363}
4364
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004365TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004366 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004367
Garfield Tan888a6a42020-01-09 11:39:16 -08004368 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004369 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004370 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4371 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004372 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4373 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004374 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4375 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4376
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004377 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4378 mFakePointerController->setPosition(100, 200);
4379 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004380
4381 NotifyMotionArgs args;
4382 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4383 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4384 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4386 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4389 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 +01004390 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004391 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4392}
4393
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394// --- TouchInputMapperTest ---
4395
4396class TouchInputMapperTest : public InputMapperTest {
4397protected:
4398 static const int32_t RAW_X_MIN;
4399 static const int32_t RAW_X_MAX;
4400 static const int32_t RAW_Y_MIN;
4401 static const int32_t RAW_Y_MAX;
4402 static const int32_t RAW_TOUCH_MIN;
4403 static const int32_t RAW_TOUCH_MAX;
4404 static const int32_t RAW_TOOL_MIN;
4405 static const int32_t RAW_TOOL_MAX;
4406 static const int32_t RAW_PRESSURE_MIN;
4407 static const int32_t RAW_PRESSURE_MAX;
4408 static const int32_t RAW_ORIENTATION_MIN;
4409 static const int32_t RAW_ORIENTATION_MAX;
4410 static const int32_t RAW_DISTANCE_MIN;
4411 static const int32_t RAW_DISTANCE_MAX;
4412 static const int32_t RAW_TILT_MIN;
4413 static const int32_t RAW_TILT_MAX;
4414 static const int32_t RAW_ID_MIN;
4415 static const int32_t RAW_ID_MAX;
4416 static const int32_t RAW_SLOT_MIN;
4417 static const int32_t RAW_SLOT_MAX;
4418 static const float X_PRECISION;
4419 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004420 static const float X_PRECISION_VIRTUAL;
4421 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422
4423 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004424 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425
4426 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4427
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004428 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004429 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004430
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 enum Axes {
4432 POSITION = 1 << 0,
4433 TOUCH = 1 << 1,
4434 TOOL = 1 << 2,
4435 PRESSURE = 1 << 3,
4436 ORIENTATION = 1 << 4,
4437 MINOR = 1 << 5,
4438 ID = 1 << 6,
4439 DISTANCE = 1 << 7,
4440 TILT = 1 << 8,
4441 SLOT = 1 << 9,
4442 TOOL_TYPE = 1 << 10,
4443 };
4444
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004445 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4446 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004447 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004449 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 int32_t toRawX(float displayX);
4451 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004452 float toCookedX(float rawX, float rawY);
4453 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004455 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004457 float toDisplayY(int32_t rawY, int32_t displayHeight);
4458
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459};
4460
4461const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4462const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4463const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4464const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4465const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4466const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4467const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4468const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004469const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4470const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4472const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4473const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4474const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4475const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4476const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4477const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4478const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4479const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4480const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4481const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4482const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004483const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4484 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4485const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4486 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004487const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4488 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489
4490const float TouchInputMapperTest::GEOMETRIC_SCALE =
4491 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4492 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4493
4494const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4495 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4496 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4497};
4498
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004499void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004500 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4501 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004502}
4503
4504void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4505 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4506 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507}
4508
Santos Cordonfa5cf462017-04-05 10:37:00 -07004509void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004510 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4511 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4512 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004513}
4514
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004516 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4517 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4518 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4519 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520}
4521
Jason Gerecke489fda82012-09-07 17:19:40 -07004522void TouchInputMapperTest::prepareLocationCalibration() {
4523 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4524}
4525
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526int32_t TouchInputMapperTest::toRawX(float displayX) {
4527 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4528}
4529
4530int32_t TouchInputMapperTest::toRawY(float displayY) {
4531 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4532}
4533
Jason Gerecke489fda82012-09-07 17:19:40 -07004534float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4535 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4536 return rawX;
4537}
4538
4539float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4540 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4541 return rawY;
4542}
4543
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004545 return toDisplayX(rawX, DISPLAY_WIDTH);
4546}
4547
4548float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4549 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550}
4551
4552float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004553 return toDisplayY(rawY, DISPLAY_HEIGHT);
4554}
4555
4556float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4557 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558}
4559
4560
4561// --- SingleTouchInputMapperTest ---
4562
4563class SingleTouchInputMapperTest : public TouchInputMapperTest {
4564protected:
4565 void prepareButtons();
4566 void prepareAxes(int axes);
4567
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004568 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4569 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4570 void processUp(SingleTouchInputMapper& mappery);
4571 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4572 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4573 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4574 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4575 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4576 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577};
4578
4579void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004580 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581}
4582
4583void SingleTouchInputMapperTest::prepareAxes(int axes) {
4584 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004585 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4586 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587 }
4588 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004589 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4590 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 }
4592 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004593 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4594 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 }
4596 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004597 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4598 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 }
4600 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004601 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4602 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 }
4604}
4605
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004606void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004607 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4608 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4609 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610}
4611
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004612void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004613 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4614 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615}
4616
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004617void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004618 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619}
4620
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004621void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004622 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623}
4624
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004625void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4626 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004627 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628}
4629
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004630void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004631 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632}
4633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004634void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4635 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004636 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4637 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638}
4639
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004640void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4641 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004642 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643}
4644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004645void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004646 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647}
4648
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 prepareButtons();
4651 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004652 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004654 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655}
4656
4657TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004658 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4659 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 prepareButtons();
4661 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004662 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004664 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665}
4666
4667TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 prepareButtons();
4669 prepareAxes(POSITION);
4670 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004671 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004673 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674}
4675
4676TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677 prepareButtons();
4678 prepareAxes(POSITION);
4679 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004680 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004682 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683}
4684
4685TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 addConfigurationProperty("touch.deviceType", "touchScreen");
4687 prepareDisplay(DISPLAY_ORIENTATION_0);
4688 prepareButtons();
4689 prepareAxes(POSITION);
4690 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004691 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692
4693 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004694 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695
4696 // Virtual key is down.
4697 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4698 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4699 processDown(mapper, x, y);
4700 processSync(mapper);
4701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4702
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004703 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704
4705 // Virtual key is up.
4706 processUp(mapper);
4707 processSync(mapper);
4708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004710 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711}
4712
4713TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 addConfigurationProperty("touch.deviceType", "touchScreen");
4715 prepareDisplay(DISPLAY_ORIENTATION_0);
4716 prepareButtons();
4717 prepareAxes(POSITION);
4718 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004719 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720
4721 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004722 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723
4724 // Virtual key is down.
4725 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4726 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4727 processDown(mapper, x, y);
4728 processSync(mapper);
4729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4730
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004731 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732
4733 // Virtual key is up.
4734 processUp(mapper);
4735 processSync(mapper);
4736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4737
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004738 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739}
4740
4741TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 addConfigurationProperty("touch.deviceType", "touchScreen");
4743 prepareDisplay(DISPLAY_ORIENTATION_0);
4744 prepareButtons();
4745 prepareAxes(POSITION);
4746 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004747 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748
4749 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4750 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004751 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752 ASSERT_TRUE(flags[0]);
4753 ASSERT_FALSE(flags[1]);
4754}
4755
4756TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
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
arthurhungdcef2dc2020-08-11 14:47:50 +08004764 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765
4766 NotifyKeyArgs args;
4767
4768 // Press virtual key.
4769 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4770 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4771 processDown(mapper, x, y);
4772 processSync(mapper);
4773
4774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4775 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4776 ASSERT_EQ(DEVICE_ID, args.deviceId);
4777 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4778 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4779 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4780 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4781 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4782 ASSERT_EQ(KEY_HOME, args.scanCode);
4783 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4784 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4785
4786 // Release virtual key.
4787 processUp(mapper);
4788 processSync(mapper);
4789
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4791 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4792 ASSERT_EQ(DEVICE_ID, args.deviceId);
4793 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4794 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4795 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4796 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4797 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4798 ASSERT_EQ(KEY_HOME, args.scanCode);
4799 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4800 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4801
4802 // Should not have sent any motions.
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4804}
4805
4806TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807 addConfigurationProperty("touch.deviceType", "touchScreen");
4808 prepareDisplay(DISPLAY_ORIENTATION_0);
4809 prepareButtons();
4810 prepareAxes(POSITION);
4811 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004812 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813
arthurhungdcef2dc2020-08-11 14:47:50 +08004814 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815
4816 NotifyKeyArgs keyArgs;
4817
4818 // Press virtual key.
4819 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4820 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4821 processDown(mapper, x, y);
4822 processSync(mapper);
4823
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4825 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4826 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4827 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4828 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4829 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4830 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4831 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4832 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4833 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4834 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4835
4836 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4837 // into the display area.
4838 y -= 100;
4839 processMove(mapper, x, y);
4840 processSync(mapper);
4841
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4843 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4844 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4845 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4846 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4847 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4848 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4849 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4850 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4851 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4852 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4853 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4854
4855 NotifyMotionArgs motionArgs;
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4858 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4862 ASSERT_EQ(0, motionArgs.flags);
4863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4864 ASSERT_EQ(0, motionArgs.buttonState);
4865 ASSERT_EQ(0, motionArgs.edgeFlags);
4866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4870 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4871 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4872 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4874
4875 // Keep moving out of bounds. Should generate a pointer move.
4876 y -= 50;
4877 processMove(mapper, x, y);
4878 processSync(mapper);
4879
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4881 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4882 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4883 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4884 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4886 ASSERT_EQ(0, motionArgs.flags);
4887 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4888 ASSERT_EQ(0, motionArgs.buttonState);
4889 ASSERT_EQ(0, motionArgs.edgeFlags);
4890 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4891 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4894 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4895 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4896 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4897 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4898
4899 // Release out of bounds. Should generate a pointer up.
4900 processUp(mapper);
4901 processSync(mapper);
4902
4903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4904 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4905 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4906 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4907 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4908 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4909 ASSERT_EQ(0, motionArgs.flags);
4910 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4911 ASSERT_EQ(0, motionArgs.buttonState);
4912 ASSERT_EQ(0, motionArgs.edgeFlags);
4913 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4914 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4917 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4918 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4919 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4920 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4921
4922 // Should not have sent any more keys or motions.
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4925}
4926
4927TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928 addConfigurationProperty("touch.deviceType", "touchScreen");
4929 prepareDisplay(DISPLAY_ORIENTATION_0);
4930 prepareButtons();
4931 prepareAxes(POSITION);
4932 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004933 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934
arthurhungdcef2dc2020-08-11 14:47:50 +08004935 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936
4937 NotifyMotionArgs motionArgs;
4938
4939 // Initially go down out of bounds.
4940 int32_t x = -10;
4941 int32_t y = -10;
4942 processDown(mapper, x, y);
4943 processSync(mapper);
4944
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4946
4947 // Move into the display area. Should generate a pointer down.
4948 x = 50;
4949 y = 75;
4950 processMove(mapper, x, y);
4951 processSync(mapper);
4952
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4954 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4955 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4956 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4957 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4958 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4959 ASSERT_EQ(0, motionArgs.flags);
4960 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4961 ASSERT_EQ(0, motionArgs.buttonState);
4962 ASSERT_EQ(0, motionArgs.edgeFlags);
4963 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4964 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4967 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4968 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4969 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4970 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4971
4972 // Release. Should generate a pointer up.
4973 processUp(mapper);
4974 processSync(mapper);
4975
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4977 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4978 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4979 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4980 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4982 ASSERT_EQ(0, motionArgs.flags);
4983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4984 ASSERT_EQ(0, motionArgs.buttonState);
4985 ASSERT_EQ(0, motionArgs.edgeFlags);
4986 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4990 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4991 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4992 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4993 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4994
4995 // Should not have sent any more keys or motions.
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4998}
4999
Santos Cordonfa5cf462017-04-05 10:37:00 -07005000TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005001 addConfigurationProperty("touch.deviceType", "touchScreen");
5002 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5003
5004 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5005 prepareButtons();
5006 prepareAxes(POSITION);
5007 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005008 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005009
arthurhungdcef2dc2020-08-11 14:47:50 +08005010 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005011
5012 NotifyMotionArgs motionArgs;
5013
5014 // Down.
5015 int32_t x = 100;
5016 int32_t y = 125;
5017 processDown(mapper, x, y);
5018 processSync(mapper);
5019
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5021 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5022 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5023 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5024 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5025 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5027 ASSERT_EQ(0, motionArgs.flags);
5028 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5029 ASSERT_EQ(0, motionArgs.buttonState);
5030 ASSERT_EQ(0, motionArgs.edgeFlags);
5031 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5032 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5035 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5036 1, 0, 0, 0, 0, 0, 0, 0));
5037 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5038 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5040
5041 // Move.
5042 x += 50;
5043 y += 75;
5044 processMove(mapper, x, y);
5045 processSync(mapper);
5046
5047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5049 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5050 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5051 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5052 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5054 ASSERT_EQ(0, motionArgs.flags);
5055 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5056 ASSERT_EQ(0, motionArgs.buttonState);
5057 ASSERT_EQ(0, motionArgs.edgeFlags);
5058 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5062 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5063 1, 0, 0, 0, 0, 0, 0, 0));
5064 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5065 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5067
5068 // Up.
5069 processUp(mapper);
5070 processSync(mapper);
5071
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5073 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5074 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5075 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5076 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5077 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5078 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5079 ASSERT_EQ(0, motionArgs.flags);
5080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5081 ASSERT_EQ(0, motionArgs.buttonState);
5082 ASSERT_EQ(0, motionArgs.edgeFlags);
5083 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5084 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5087 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5088 1, 0, 0, 0, 0, 0, 0, 0));
5089 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5090 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5091 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5092
5093 // Should not have sent any more keys or motions.
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5096}
5097
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099 addConfigurationProperty("touch.deviceType", "touchScreen");
5100 prepareDisplay(DISPLAY_ORIENTATION_0);
5101 prepareButtons();
5102 prepareAxes(POSITION);
5103 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005104 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105
arthurhungdcef2dc2020-08-11 14:47:50 +08005106 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107
5108 NotifyMotionArgs motionArgs;
5109
5110 // Down.
5111 int32_t x = 100;
5112 int32_t y = 125;
5113 processDown(mapper, x, y);
5114 processSync(mapper);
5115
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5118 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5119 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5120 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5121 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5122 ASSERT_EQ(0, motionArgs.flags);
5123 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5124 ASSERT_EQ(0, motionArgs.buttonState);
5125 ASSERT_EQ(0, motionArgs.edgeFlags);
5126 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5127 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5130 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5131 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5132 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5134
5135 // Move.
5136 x += 50;
5137 y += 75;
5138 processMove(mapper, x, y);
5139 processSync(mapper);
5140
5141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5142 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5143 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5144 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5145 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5147 ASSERT_EQ(0, motionArgs.flags);
5148 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5149 ASSERT_EQ(0, motionArgs.buttonState);
5150 ASSERT_EQ(0, motionArgs.edgeFlags);
5151 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5152 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5155 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5156 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5157 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5158 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5159
5160 // Up.
5161 processUp(mapper);
5162 processSync(mapper);
5163
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5165 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5166 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5167 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5168 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5169 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5170 ASSERT_EQ(0, motionArgs.flags);
5171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5172 ASSERT_EQ(0, motionArgs.buttonState);
5173 ASSERT_EQ(0, motionArgs.edgeFlags);
5174 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5178 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5179 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5180 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5182
5183 // Should not have sent any more keys or motions.
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5186}
5187
5188TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189 addConfigurationProperty("touch.deviceType", "touchScreen");
5190 prepareButtons();
5191 prepareAxes(POSITION);
5192 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005193 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194
5195 NotifyMotionArgs args;
5196
5197 // Rotation 90.
5198 prepareDisplay(DISPLAY_ORIENTATION_90);
5199 processDown(mapper, toRawX(50), toRawY(75));
5200 processSync(mapper);
5201
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5203 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5204 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5205
5206 processUp(mapper);
5207 processSync(mapper);
5208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5209}
5210
5211TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212 addConfigurationProperty("touch.deviceType", "touchScreen");
5213 prepareButtons();
5214 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005215 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216
5217 NotifyMotionArgs args;
5218
5219 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005220 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221 prepareDisplay(DISPLAY_ORIENTATION_0);
5222 processDown(mapper, toRawX(50), toRawY(75));
5223 processSync(mapper);
5224
5225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5226 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5227 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5228
5229 processUp(mapper);
5230 processSync(mapper);
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5232
5233 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005234 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235 prepareDisplay(DISPLAY_ORIENTATION_90);
5236 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5237 processSync(mapper);
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5240 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5241 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5242
5243 processUp(mapper);
5244 processSync(mapper);
5245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5246
5247 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005248 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249 prepareDisplay(DISPLAY_ORIENTATION_180);
5250 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5251 processSync(mapper);
5252
5253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5254 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5255 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5256
5257 processUp(mapper);
5258 processSync(mapper);
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5260
5261 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005262 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263 prepareDisplay(DISPLAY_ORIENTATION_270);
5264 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5265 processSync(mapper);
5266
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5268 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5269 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5270
5271 processUp(mapper);
5272 processSync(mapper);
5273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5274}
5275
5276TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277 addConfigurationProperty("touch.deviceType", "touchScreen");
5278 prepareDisplay(DISPLAY_ORIENTATION_0);
5279 prepareButtons();
5280 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005281 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282
5283 // These calculations are based on the input device calibration documentation.
5284 int32_t rawX = 100;
5285 int32_t rawY = 200;
5286 int32_t rawPressure = 10;
5287 int32_t rawToolMajor = 12;
5288 int32_t rawDistance = 2;
5289 int32_t rawTiltX = 30;
5290 int32_t rawTiltY = 110;
5291
5292 float x = toDisplayX(rawX);
5293 float y = toDisplayY(rawY);
5294 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5295 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5296 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5297 float distance = float(rawDistance);
5298
5299 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5300 float tiltScale = M_PI / 180;
5301 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5302 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5303 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5304 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5305
5306 processDown(mapper, rawX, rawY);
5307 processPressure(mapper, rawPressure);
5308 processToolMajor(mapper, rawToolMajor);
5309 processDistance(mapper, rawDistance);
5310 processTilt(mapper, rawTiltX, rawTiltY);
5311 processSync(mapper);
5312
5313 NotifyMotionArgs args;
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5316 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5317 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5318}
5319
Jason Gerecke489fda82012-09-07 17:19:40 -07005320TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005321 addConfigurationProperty("touch.deviceType", "touchScreen");
5322 prepareDisplay(DISPLAY_ORIENTATION_0);
5323 prepareLocationCalibration();
5324 prepareButtons();
5325 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005326 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005327
5328 int32_t rawX = 100;
5329 int32_t rawY = 200;
5330
5331 float x = toDisplayX(toCookedX(rawX, rawY));
5332 float y = toDisplayY(toCookedY(rawX, rawY));
5333
5334 processDown(mapper, rawX, rawY);
5335 processSync(mapper);
5336
5337 NotifyMotionArgs args;
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5340 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5341}
5342
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344 addConfigurationProperty("touch.deviceType", "touchScreen");
5345 prepareDisplay(DISPLAY_ORIENTATION_0);
5346 prepareButtons();
5347 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005348 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349
5350 NotifyMotionArgs motionArgs;
5351 NotifyKeyArgs keyArgs;
5352
5353 processDown(mapper, 100, 200);
5354 processSync(mapper);
5355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5357 ASSERT_EQ(0, motionArgs.buttonState);
5358
5359 // press BTN_LEFT, release BTN_LEFT
5360 processKey(mapper, BTN_LEFT, 1);
5361 processSync(mapper);
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5364 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5365
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5368 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5369
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370 processKey(mapper, BTN_LEFT, 0);
5371 processSync(mapper);
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005375
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005378 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379
5380 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5381 processKey(mapper, BTN_RIGHT, 1);
5382 processKey(mapper, BTN_MIDDLE, 1);
5383 processSync(mapper);
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5386 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5387 motionArgs.buttonState);
5388
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5391 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5392
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5395 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5396 motionArgs.buttonState);
5397
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398 processKey(mapper, BTN_RIGHT, 0);
5399 processSync(mapper);
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005402 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005403
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005406 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407
5408 processKey(mapper, BTN_MIDDLE, 0);
5409 processSync(mapper);
5410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005416 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417
5418 // press BTN_BACK, release BTN_BACK
5419 processKey(mapper, BTN_BACK, 1);
5420 processSync(mapper);
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5422 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5423 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005424
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005427 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5428
5429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5431 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432
5433 processKey(mapper, BTN_BACK, 0);
5434 processSync(mapper);
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005441 ASSERT_EQ(0, motionArgs.buttonState);
5442
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5444 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5445 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5446
5447 // press BTN_SIDE, release BTN_SIDE
5448 processKey(mapper, BTN_SIDE, 1);
5449 processSync(mapper);
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5452 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005453
Michael Wrightd02c5b62014-02-10 15:10:22 -08005454 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5460 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
5462 processKey(mapper, BTN_SIDE, 0);
5463 processSync(mapper);
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005467
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005470 ASSERT_EQ(0, motionArgs.buttonState);
5471
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5473 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5474 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5475
5476 // press BTN_FORWARD, release BTN_FORWARD
5477 processKey(mapper, BTN_FORWARD, 1);
5478 processSync(mapper);
5479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5480 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5481 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005482
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5486
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5489 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490
5491 processKey(mapper, BTN_FORWARD, 0);
5492 processSync(mapper);
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005496
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005499 ASSERT_EQ(0, motionArgs.buttonState);
5500
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5502 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5503 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5504
5505 // press BTN_EXTRA, release BTN_EXTRA
5506 processKey(mapper, BTN_EXTRA, 1);
5507 processSync(mapper);
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5509 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5510 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005511
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5515
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5518 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519
5520 processKey(mapper, BTN_EXTRA, 0);
5521 processSync(mapper);
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005525
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005528 ASSERT_EQ(0, motionArgs.buttonState);
5529
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5531 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5532 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5533
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5535
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536 // press BTN_STYLUS, release BTN_STYLUS
5537 processKey(mapper, BTN_STYLUS, 1);
5538 processSync(mapper);
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005541 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5545 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546
5547 processKey(mapper, BTN_STYLUS, 0);
5548 processSync(mapper);
5549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005552
5553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005555 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556
5557 // press BTN_STYLUS2, release BTN_STYLUS2
5558 processKey(mapper, BTN_STYLUS2, 1);
5559 processSync(mapper);
5560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005562 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5563
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5566 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567
5568 processKey(mapper, BTN_STYLUS2, 0);
5569 processSync(mapper);
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005571 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005573
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005576 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577
5578 // release touch
5579 processUp(mapper);
5580 processSync(mapper);
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5583 ASSERT_EQ(0, motionArgs.buttonState);
5584}
5585
5586TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587 addConfigurationProperty("touch.deviceType", "touchScreen");
5588 prepareDisplay(DISPLAY_ORIENTATION_0);
5589 prepareButtons();
5590 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005591 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592
5593 NotifyMotionArgs motionArgs;
5594
5595 // default tool type is finger
5596 processDown(mapper, 100, 200);
5597 processSync(mapper);
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5601
5602 // eraser
5603 processKey(mapper, BTN_TOOL_RUBBER, 1);
5604 processSync(mapper);
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5608
5609 // stylus
5610 processKey(mapper, BTN_TOOL_RUBBER, 0);
5611 processKey(mapper, BTN_TOOL_PEN, 1);
5612 processSync(mapper);
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5616
5617 // brush
5618 processKey(mapper, BTN_TOOL_PEN, 0);
5619 processKey(mapper, BTN_TOOL_BRUSH, 1);
5620 processSync(mapper);
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5624
5625 // pencil
5626 processKey(mapper, BTN_TOOL_BRUSH, 0);
5627 processKey(mapper, BTN_TOOL_PENCIL, 1);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5632
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005633 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 processKey(mapper, BTN_TOOL_PENCIL, 0);
5635 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5636 processSync(mapper);
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5640
5641 // mouse
5642 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5643 processKey(mapper, BTN_TOOL_MOUSE, 1);
5644 processSync(mapper);
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5648
5649 // lens
5650 processKey(mapper, BTN_TOOL_MOUSE, 0);
5651 processKey(mapper, BTN_TOOL_LENS, 1);
5652 processSync(mapper);
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5656
5657 // double-tap
5658 processKey(mapper, BTN_TOOL_LENS, 0);
5659 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5660 processSync(mapper);
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5664
5665 // triple-tap
5666 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5667 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5672
5673 // quad-tap
5674 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5675 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5676 processSync(mapper);
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5680
5681 // finger
5682 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5683 processKey(mapper, BTN_TOOL_FINGER, 1);
5684 processSync(mapper);
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5688
5689 // stylus trumps finger
5690 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
5695
5696 // eraser trumps stylus
5697 processKey(mapper, BTN_TOOL_RUBBER, 1);
5698 processSync(mapper);
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5702
5703 // mouse trumps eraser
5704 processKey(mapper, BTN_TOOL_MOUSE, 1);
5705 processSync(mapper);
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5709
5710 // back to default tool type
5711 processKey(mapper, BTN_TOOL_MOUSE, 0);
5712 processKey(mapper, BTN_TOOL_RUBBER, 0);
5713 processKey(mapper, BTN_TOOL_PEN, 0);
5714 processKey(mapper, BTN_TOOL_FINGER, 0);
5715 processSync(mapper);
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5719}
5720
5721TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 addConfigurationProperty("touch.deviceType", "touchScreen");
5723 prepareDisplay(DISPLAY_ORIENTATION_0);
5724 prepareButtons();
5725 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005726 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005727 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728
5729 NotifyMotionArgs motionArgs;
5730
5731 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5732 processKey(mapper, BTN_TOOL_FINGER, 1);
5733 processMove(mapper, 100, 200);
5734 processSync(mapper);
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5736 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5738 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5739
5740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5743 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5744
5745 // move a little
5746 processMove(mapper, 150, 250);
5747 processSync(mapper);
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5749 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5751 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5752
5753 // down when BTN_TOUCH is pressed, pressure defaults to 1
5754 processKey(mapper, BTN_TOUCH, 1);
5755 processSync(mapper);
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5759 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5760
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5762 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5764 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5765
5766 // up when BTN_TOUCH is released, hover restored
5767 processKey(mapper, BTN_TOUCH, 0);
5768 processSync(mapper);
5769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5772 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5773
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5777 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5778
5779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5782 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5783
5784 // exit hover when pointer goes away
5785 processKey(mapper, BTN_TOOL_FINGER, 0);
5786 processSync(mapper);
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5790 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5791}
5792
5793TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794 addConfigurationProperty("touch.deviceType", "touchScreen");
5795 prepareDisplay(DISPLAY_ORIENTATION_0);
5796 prepareButtons();
5797 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005798 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799
5800 NotifyMotionArgs motionArgs;
5801
5802 // initially hovering because pressure is 0
5803 processDown(mapper, 100, 200);
5804 processPressure(mapper, 0);
5805 processSync(mapper);
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5809 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5810
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5814 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5815
5816 // move a little
5817 processMove(mapper, 150, 250);
5818 processSync(mapper);
5819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5820 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5822 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5823
5824 // down when pressure is non-zero
5825 processPressure(mapper, RAW_PRESSURE_MAX);
5826 processSync(mapper);
5827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5830 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5831
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5835 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5836
5837 // up when pressure becomes 0, hover restored
5838 processPressure(mapper, 0);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5841 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5844
5845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5846 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5848 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5849
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5853 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5854
5855 // exit hover when pointer goes away
5856 processUp(mapper);
5857 processSync(mapper);
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5859 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5861 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5862}
5863
Michael Wrightd02c5b62014-02-10 15:10:22 -08005864// --- MultiTouchInputMapperTest ---
5865
5866class MultiTouchInputMapperTest : public TouchInputMapperTest {
5867protected:
5868 void prepareAxes(int axes);
5869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005870 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5871 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5872 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5873 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5874 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5875 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5876 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5877 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5878 void processId(MultiTouchInputMapper& mapper, int32_t id);
5879 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5880 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5881 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5882 void processMTSync(MultiTouchInputMapper& mapper);
5883 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884};
5885
5886void MultiTouchInputMapperTest::prepareAxes(int axes) {
5887 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005888 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5889 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 }
5891 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005892 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5893 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005895 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5896 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897 }
5898 }
5899 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005900 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5901 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5904 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 }
5906 }
5907 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5909 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 }
5911 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005912 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5913 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 }
5915 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5917 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 }
5919 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005920 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5921 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 }
5923 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5925 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 }
5927 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005928 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929 }
5930}
5931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005932void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5933 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005934 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5935 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936}
5937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005938void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5939 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005940 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941}
5942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005943void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5944 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005945 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946}
5947
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005948void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005949 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950}
5951
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005952void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005953 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954}
5955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005956void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5957 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005958 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959}
5960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005961void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005962 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963}
5964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005965void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005966 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967}
5968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005969void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005970 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005971}
5972
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005973void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005974 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975}
5976
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005977void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005978 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979}
5980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005981void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5982 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005983 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984}
5985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005986void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005987 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988}
5989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005990void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005991 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992}
5993
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 addConfigurationProperty("touch.deviceType", "touchScreen");
5996 prepareDisplay(DISPLAY_ORIENTATION_0);
5997 prepareAxes(POSITION);
5998 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005999 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000
arthurhungdcef2dc2020-08-11 14:47:50 +08006001 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002
6003 NotifyMotionArgs motionArgs;
6004
6005 // Two fingers down at once.
6006 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6007 processPosition(mapper, x1, y1);
6008 processMTSync(mapper);
6009 processPosition(mapper, x2, y2);
6010 processMTSync(mapper);
6011 processSync(mapper);
6012
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6015 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6016 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6017 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6019 ASSERT_EQ(0, motionArgs.flags);
6020 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6021 ASSERT_EQ(0, motionArgs.buttonState);
6022 ASSERT_EQ(0, motionArgs.edgeFlags);
6023 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6024 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6025 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6027 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6028 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6029 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6034 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6035 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6036 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6037 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6038 motionArgs.action);
6039 ASSERT_EQ(0, motionArgs.flags);
6040 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6041 ASSERT_EQ(0, motionArgs.buttonState);
6042 ASSERT_EQ(0, motionArgs.edgeFlags);
6043 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6044 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6046 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6049 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6051 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6052 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6053 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6055
6056 // Move.
6057 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6058 processPosition(mapper, x1, y1);
6059 processMTSync(mapper);
6060 processPosition(mapper, x2, y2);
6061 processMTSync(mapper);
6062 processSync(mapper);
6063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6065 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6066 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6067 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6068 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6070 ASSERT_EQ(0, motionArgs.flags);
6071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6072 ASSERT_EQ(0, motionArgs.buttonState);
6073 ASSERT_EQ(0, motionArgs.edgeFlags);
6074 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6077 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6080 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6082 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6083 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6084 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6085 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6086
6087 // First finger up.
6088 x2 += 15; y2 -= 20;
6089 processPosition(mapper, x2, y2);
6090 processMTSync(mapper);
6091 processSync(mapper);
6092
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6094 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6095 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6096 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6097 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6099 motionArgs.action);
6100 ASSERT_EQ(0, motionArgs.flags);
6101 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6102 ASSERT_EQ(0, motionArgs.buttonState);
6103 ASSERT_EQ(0, motionArgs.edgeFlags);
6104 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6105 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6107 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6110 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6112 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6113 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6114 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6115 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6116
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6119 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6120 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6121 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6123 ASSERT_EQ(0, motionArgs.flags);
6124 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6125 ASSERT_EQ(0, motionArgs.buttonState);
6126 ASSERT_EQ(0, motionArgs.edgeFlags);
6127 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6128 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6131 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6132 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6133 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6134 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6135
6136 // Move.
6137 x2 += 20; y2 -= 25;
6138 processPosition(mapper, x2, y2);
6139 processMTSync(mapper);
6140 processSync(mapper);
6141
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6144 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6145 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6146 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6148 ASSERT_EQ(0, motionArgs.flags);
6149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6150 ASSERT_EQ(0, motionArgs.buttonState);
6151 ASSERT_EQ(0, motionArgs.edgeFlags);
6152 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6153 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6156 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6157 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6158 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6160
6161 // New finger down.
6162 int32_t x3 = 700, y3 = 300;
6163 processPosition(mapper, x2, y2);
6164 processMTSync(mapper);
6165 processPosition(mapper, x3, y3);
6166 processMTSync(mapper);
6167 processSync(mapper);
6168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6171 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6172 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6173 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6175 motionArgs.action);
6176 ASSERT_EQ(0, motionArgs.flags);
6177 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6178 ASSERT_EQ(0, motionArgs.buttonState);
6179 ASSERT_EQ(0, motionArgs.edgeFlags);
6180 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6181 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6183 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6186 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6188 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6189 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6190 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6191 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6192
6193 // Second finger up.
6194 x3 += 30; y3 -= 20;
6195 processPosition(mapper, x3, y3);
6196 processMTSync(mapper);
6197 processSync(mapper);
6198
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6201 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6202 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6203 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6205 motionArgs.action);
6206 ASSERT_EQ(0, motionArgs.flags);
6207 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6208 ASSERT_EQ(0, motionArgs.buttonState);
6209 ASSERT_EQ(0, motionArgs.edgeFlags);
6210 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6211 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6213 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6216 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6218 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6219 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6220 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6221 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6222
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6225 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6226 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6227 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6229 ASSERT_EQ(0, motionArgs.flags);
6230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6231 ASSERT_EQ(0, motionArgs.buttonState);
6232 ASSERT_EQ(0, motionArgs.edgeFlags);
6233 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6234 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6237 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6238 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6239 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6241
6242 // Last finger up.
6243 processMTSync(mapper);
6244 processSync(mapper);
6245
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6248 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6249 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6250 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6252 ASSERT_EQ(0, motionArgs.flags);
6253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6254 ASSERT_EQ(0, motionArgs.buttonState);
6255 ASSERT_EQ(0, motionArgs.edgeFlags);
6256 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6257 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6260 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6261 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6262 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6264
6265 // Should not have sent any more keys or motions.
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6268}
6269
6270TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 addConfigurationProperty("touch.deviceType", "touchScreen");
6272 prepareDisplay(DISPLAY_ORIENTATION_0);
6273 prepareAxes(POSITION | ID);
6274 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006275 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276
arthurhungdcef2dc2020-08-11 14:47:50 +08006277 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278
6279 NotifyMotionArgs motionArgs;
6280
6281 // Two fingers down at once.
6282 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6283 processPosition(mapper, x1, y1);
6284 processId(mapper, 1);
6285 processMTSync(mapper);
6286 processPosition(mapper, x2, y2);
6287 processId(mapper, 2);
6288 processMTSync(mapper);
6289 processSync(mapper);
6290
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6293 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6297 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6301 motionArgs.action);
6302 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6305 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6308 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6311
6312 // Move.
6313 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6314 processPosition(mapper, x1, y1);
6315 processId(mapper, 1);
6316 processMTSync(mapper);
6317 processPosition(mapper, x2, y2);
6318 processId(mapper, 2);
6319 processMTSync(mapper);
6320 processSync(mapper);
6321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6324 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6325 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6327 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6330 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6332 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6333
6334 // First finger up.
6335 x2 += 15; y2 -= 20;
6336 processPosition(mapper, x2, y2);
6337 processId(mapper, 2);
6338 processMTSync(mapper);
6339 processSync(mapper);
6340
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6343 motionArgs.action);
6344 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6345 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6347 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6352 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6353
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6356 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6357 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6360 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6361
6362 // Move.
6363 x2 += 20; y2 -= 25;
6364 processPosition(mapper, x2, y2);
6365 processId(mapper, 2);
6366 processMTSync(mapper);
6367 processSync(mapper);
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 // New finger down.
6378 int32_t x3 = 700, y3 = 300;
6379 processPosition(mapper, x2, y2);
6380 processId(mapper, 2);
6381 processMTSync(mapper);
6382 processPosition(mapper, x3, y3);
6383 processId(mapper, 3);
6384 processMTSync(mapper);
6385 processSync(mapper);
6386
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6389 motionArgs.action);
6390 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6391 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6393 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6396 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6398 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6399
6400 // Second finger up.
6401 x3 += 30; y3 -= 20;
6402 processPosition(mapper, x3, y3);
6403 processId(mapper, 3);
6404 processMTSync(mapper);
6405 processSync(mapper);
6406
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6409 motionArgs.action);
6410 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6411 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6413 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6418 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6419
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6422 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6423 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6426 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6427
6428 // Last finger up.
6429 processMTSync(mapper);
6430 processSync(mapper);
6431
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6434 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6435 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6438 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6439
6440 // Should not have sent any more keys or motions.
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6443}
6444
6445TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006446 addConfigurationProperty("touch.deviceType", "touchScreen");
6447 prepareDisplay(DISPLAY_ORIENTATION_0);
6448 prepareAxes(POSITION | ID | SLOT);
6449 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006450 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451
arthurhungdcef2dc2020-08-11 14:47:50 +08006452 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453
6454 NotifyMotionArgs motionArgs;
6455
6456 // Two fingers down at once.
6457 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6458 processPosition(mapper, x1, y1);
6459 processId(mapper, 1);
6460 processSlot(mapper, 1);
6461 processPosition(mapper, x2, y2);
6462 processId(mapper, 2);
6463 processSync(mapper);
6464
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6467 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6471 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6475 motionArgs.action);
6476 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6477 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6479 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6482 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6484 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6485
6486 // Move.
6487 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6488 processSlot(mapper, 0);
6489 processPosition(mapper, x1, y1);
6490 processSlot(mapper, 1);
6491 processPosition(mapper, x2, y2);
6492 processSync(mapper);
6493
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6496 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6497 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6499 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6502 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6504 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6505
6506 // First finger up.
6507 x2 += 15; y2 -= 20;
6508 processSlot(mapper, 0);
6509 processId(mapper, -1);
6510 processSlot(mapper, 1);
6511 processPosition(mapper, x2, y2);
6512 processSync(mapper);
6513
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6516 motionArgs.action);
6517 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6518 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6520 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6523 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6525 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6529 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6530 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6533 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6534
6535 // Move.
6536 x2 += 20; y2 -= 25;
6537 processPosition(mapper, x2, y2);
6538 processSync(mapper);
6539
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6542 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6543 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6546 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6547
6548 // New finger down.
6549 int32_t x3 = 700, y3 = 300;
6550 processPosition(mapper, x2, y2);
6551 processSlot(mapper, 0);
6552 processId(mapper, 3);
6553 processPosition(mapper, x3, y3);
6554 processSync(mapper);
6555
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6558 motionArgs.action);
6559 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6560 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6562 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6565 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6567 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6568
6569 // Second finger up.
6570 x3 += 30; y3 -= 20;
6571 processSlot(mapper, 1);
6572 processId(mapper, -1);
6573 processSlot(mapper, 0);
6574 processPosition(mapper, x3, y3);
6575 processSync(mapper);
6576
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6579 motionArgs.action);
6580 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6583 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6586 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6588 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6589
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6592 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6597
6598 // Last finger up.
6599 processId(mapper, -1);
6600 processSync(mapper);
6601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6604 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6608 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6609
6610 // Should not have sent any more keys or motions.
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6613}
6614
6615TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006616 addConfigurationProperty("touch.deviceType", "touchScreen");
6617 prepareDisplay(DISPLAY_ORIENTATION_0);
6618 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006620
6621 // These calculations are based on the input device calibration documentation.
6622 int32_t rawX = 100;
6623 int32_t rawY = 200;
6624 int32_t rawTouchMajor = 7;
6625 int32_t rawTouchMinor = 6;
6626 int32_t rawToolMajor = 9;
6627 int32_t rawToolMinor = 8;
6628 int32_t rawPressure = 11;
6629 int32_t rawDistance = 0;
6630 int32_t rawOrientation = 3;
6631 int32_t id = 5;
6632
6633 float x = toDisplayX(rawX);
6634 float y = toDisplayY(rawY);
6635 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6636 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6637 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6638 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6639 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6640 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6641 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6642 float distance = float(rawDistance);
6643
6644 processPosition(mapper, rawX, rawY);
6645 processTouchMajor(mapper, rawTouchMajor);
6646 processTouchMinor(mapper, rawTouchMinor);
6647 processToolMajor(mapper, rawToolMajor);
6648 processToolMinor(mapper, rawToolMinor);
6649 processPressure(mapper, rawPressure);
6650 processOrientation(mapper, rawOrientation);
6651 processDistance(mapper, rawDistance);
6652 processId(mapper, id);
6653 processMTSync(mapper);
6654 processSync(mapper);
6655
6656 NotifyMotionArgs args;
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6658 ASSERT_EQ(0, args.pointerProperties[0].id);
6659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6660 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6661 orientation, distance));
6662}
6663
6664TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006665 addConfigurationProperty("touch.deviceType", "touchScreen");
6666 prepareDisplay(DISPLAY_ORIENTATION_0);
6667 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6668 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006669 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006670
6671 // These calculations are based on the input device calibration documentation.
6672 int32_t rawX = 100;
6673 int32_t rawY = 200;
6674 int32_t rawTouchMajor = 140;
6675 int32_t rawTouchMinor = 120;
6676 int32_t rawToolMajor = 180;
6677 int32_t rawToolMinor = 160;
6678
6679 float x = toDisplayX(rawX);
6680 float y = toDisplayY(rawY);
6681 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6682 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6683 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6684 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6685 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6686
6687 processPosition(mapper, rawX, rawY);
6688 processTouchMajor(mapper, rawTouchMajor);
6689 processTouchMinor(mapper, rawTouchMinor);
6690 processToolMajor(mapper, rawToolMajor);
6691 processToolMinor(mapper, rawToolMinor);
6692 processMTSync(mapper);
6693 processSync(mapper);
6694
6695 NotifyMotionArgs args;
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6698 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6699}
6700
6701TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006702 addConfigurationProperty("touch.deviceType", "touchScreen");
6703 prepareDisplay(DISPLAY_ORIENTATION_0);
6704 prepareAxes(POSITION | TOUCH | TOOL);
6705 addConfigurationProperty("touch.size.calibration", "diameter");
6706 addConfigurationProperty("touch.size.scale", "10");
6707 addConfigurationProperty("touch.size.bias", "160");
6708 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006709 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710
6711 // These calculations are based on the input device calibration documentation.
6712 // Note: We only provide a single common touch/tool value because the device is assumed
6713 // not to emit separate values for each pointer (isSummed = 1).
6714 int32_t rawX = 100;
6715 int32_t rawY = 200;
6716 int32_t rawX2 = 150;
6717 int32_t rawY2 = 250;
6718 int32_t rawTouchMajor = 5;
6719 int32_t rawToolMajor = 8;
6720
6721 float x = toDisplayX(rawX);
6722 float y = toDisplayY(rawY);
6723 float x2 = toDisplayX(rawX2);
6724 float y2 = toDisplayY(rawY2);
6725 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6726 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6727 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6728
6729 processPosition(mapper, rawX, rawY);
6730 processTouchMajor(mapper, rawTouchMajor);
6731 processToolMajor(mapper, rawToolMajor);
6732 processMTSync(mapper);
6733 processPosition(mapper, rawX2, rawY2);
6734 processTouchMajor(mapper, rawTouchMajor);
6735 processToolMajor(mapper, rawToolMajor);
6736 processMTSync(mapper);
6737 processSync(mapper);
6738
6739 NotifyMotionArgs args;
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6741 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6742
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6744 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6745 args.action);
6746 ASSERT_EQ(size_t(2), args.pointerCount);
6747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6748 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6750 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6751}
6752
6753TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006754 addConfigurationProperty("touch.deviceType", "touchScreen");
6755 prepareDisplay(DISPLAY_ORIENTATION_0);
6756 prepareAxes(POSITION | TOUCH | TOOL);
6757 addConfigurationProperty("touch.size.calibration", "area");
6758 addConfigurationProperty("touch.size.scale", "43");
6759 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006760 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006761
6762 // These calculations are based on the input device calibration documentation.
6763 int32_t rawX = 100;
6764 int32_t rawY = 200;
6765 int32_t rawTouchMajor = 5;
6766 int32_t rawToolMajor = 8;
6767
6768 float x = toDisplayX(rawX);
6769 float y = toDisplayY(rawY);
6770 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6771 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6772 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6773
6774 processPosition(mapper, rawX, rawY);
6775 processTouchMajor(mapper, rawTouchMajor);
6776 processToolMajor(mapper, rawToolMajor);
6777 processMTSync(mapper);
6778 processSync(mapper);
6779
6780 NotifyMotionArgs args;
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6783 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6784}
6785
6786TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006787 addConfigurationProperty("touch.deviceType", "touchScreen");
6788 prepareDisplay(DISPLAY_ORIENTATION_0);
6789 prepareAxes(POSITION | PRESSURE);
6790 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6791 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006792 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006793
Michael Wrightaa449c92017-12-13 21:21:43 +00006794 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006795 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006796 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6797 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6798 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6799
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 // These calculations are based on the input device calibration documentation.
6801 int32_t rawX = 100;
6802 int32_t rawY = 200;
6803 int32_t rawPressure = 60;
6804
6805 float x = toDisplayX(rawX);
6806 float y = toDisplayY(rawY);
6807 float pressure = float(rawPressure) * 0.01f;
6808
6809 processPosition(mapper, rawX, rawY);
6810 processPressure(mapper, rawPressure);
6811 processMTSync(mapper);
6812 processSync(mapper);
6813
6814 NotifyMotionArgs args;
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6817 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6818}
6819
6820TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821 addConfigurationProperty("touch.deviceType", "touchScreen");
6822 prepareDisplay(DISPLAY_ORIENTATION_0);
6823 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006824 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006825
6826 NotifyMotionArgs motionArgs;
6827 NotifyKeyArgs keyArgs;
6828
6829 processId(mapper, 1);
6830 processPosition(mapper, 100, 200);
6831 processSync(mapper);
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6834 ASSERT_EQ(0, motionArgs.buttonState);
6835
6836 // press BTN_LEFT, release BTN_LEFT
6837 processKey(mapper, BTN_LEFT, 1);
6838 processSync(mapper);
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6841 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6842
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6844 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6845 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6846
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847 processKey(mapper, BTN_LEFT, 0);
6848 processSync(mapper);
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006850 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006851 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006852
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006855 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006856
6857 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6858 processKey(mapper, BTN_RIGHT, 1);
6859 processKey(mapper, BTN_MIDDLE, 1);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6863 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6864 motionArgs.buttonState);
6865
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6867 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6868 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6869
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6872 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6873 motionArgs.buttonState);
6874
Michael Wrightd02c5b62014-02-10 15:10:22 -08006875 processKey(mapper, BTN_RIGHT, 0);
6876 processSync(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006879 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006880
6881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006883 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006884
6885 processKey(mapper, BTN_MIDDLE, 0);
6886 processSync(mapper);
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006889 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006890
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006893 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006894
6895 // press BTN_BACK, release BTN_BACK
6896 processKey(mapper, BTN_BACK, 1);
6897 processSync(mapper);
6898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6899 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6900 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006901
Michael Wrightd02c5b62014-02-10 15:10:22 -08006902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006904 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6905
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6908 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006909
6910 processKey(mapper, BTN_BACK, 0);
6911 processSync(mapper);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006914 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006915
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006918 ASSERT_EQ(0, motionArgs.buttonState);
6919
Michael Wrightd02c5b62014-02-10 15:10:22 -08006920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6921 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6922 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6923
6924 // press BTN_SIDE, release BTN_SIDE
6925 processKey(mapper, BTN_SIDE, 1);
6926 processSync(mapper);
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6928 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6929 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006930
Michael Wrightd02c5b62014-02-10 15:10:22 -08006931 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6934
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6937 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006938
6939 processKey(mapper, BTN_SIDE, 0);
6940 processSync(mapper);
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006943 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006944
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006947 ASSERT_EQ(0, motionArgs.buttonState);
6948
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6950 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6951 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6952
6953 // press BTN_FORWARD, release BTN_FORWARD
6954 processKey(mapper, BTN_FORWARD, 1);
6955 processSync(mapper);
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6957 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6958 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006959
Michael Wrightd02c5b62014-02-10 15:10:22 -08006960 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6963
6964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6966 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006967
6968 processKey(mapper, BTN_FORWARD, 0);
6969 processSync(mapper);
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006973
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006976 ASSERT_EQ(0, motionArgs.buttonState);
6977
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6979 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6980 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6981
6982 // press BTN_EXTRA, release BTN_EXTRA
6983 processKey(mapper, BTN_EXTRA, 1);
6984 processSync(mapper);
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6986 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6987 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006988
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6992
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6995 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006996
6997 processKey(mapper, BTN_EXTRA, 0);
6998 processSync(mapper);
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007002
7003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007005 ASSERT_EQ(0, motionArgs.buttonState);
7006
Michael Wrightd02c5b62014-02-10 15:10:22 -08007007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7008 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7009 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7010
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7012
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013 // press BTN_STYLUS, release BTN_STYLUS
7014 processKey(mapper, BTN_STYLUS, 1);
7015 processSync(mapper);
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007018 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7019
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7022 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023
7024 processKey(mapper, BTN_STYLUS, 0);
7025 processSync(mapper);
7026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007028 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007029
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007032 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033
7034 // press BTN_STYLUS2, release BTN_STYLUS2
7035 processKey(mapper, BTN_STYLUS2, 1);
7036 processSync(mapper);
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007039 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7040
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7043 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044
7045 processKey(mapper, BTN_STYLUS2, 0);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007050
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007053 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007054
7055 // release touch
7056 processId(mapper, -1);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7059 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7060 ASSERT_EQ(0, motionArgs.buttonState);
7061}
7062
7063TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064 addConfigurationProperty("touch.deviceType", "touchScreen");
7065 prepareDisplay(DISPLAY_ORIENTATION_0);
7066 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007067 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068
7069 NotifyMotionArgs motionArgs;
7070
7071 // default tool type is finger
7072 processId(mapper, 1);
7073 processPosition(mapper, 100, 200);
7074 processSync(mapper);
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7076 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7078
7079 // eraser
7080 processKey(mapper, BTN_TOOL_RUBBER, 1);
7081 processSync(mapper);
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7085
7086 // stylus
7087 processKey(mapper, BTN_TOOL_RUBBER, 0);
7088 processKey(mapper, BTN_TOOL_PEN, 1);
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7093
7094 // brush
7095 processKey(mapper, BTN_TOOL_PEN, 0);
7096 processKey(mapper, BTN_TOOL_BRUSH, 1);
7097 processSync(mapper);
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7101
7102 // pencil
7103 processKey(mapper, BTN_TOOL_BRUSH, 0);
7104 processKey(mapper, BTN_TOOL_PENCIL, 1);
7105 processSync(mapper);
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7109
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007110 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 processKey(mapper, BTN_TOOL_PENCIL, 0);
7112 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7113 processSync(mapper);
7114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7117
7118 // mouse
7119 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7120 processKey(mapper, BTN_TOOL_MOUSE, 1);
7121 processSync(mapper);
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7125
7126 // lens
7127 processKey(mapper, BTN_TOOL_MOUSE, 0);
7128 processKey(mapper, BTN_TOOL_LENS, 1);
7129 processSync(mapper);
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7133
7134 // double-tap
7135 processKey(mapper, BTN_TOOL_LENS, 0);
7136 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7137 processSync(mapper);
7138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7141
7142 // triple-tap
7143 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7144 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7149
7150 // quad-tap
7151 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7152 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7153 processSync(mapper);
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7157
7158 // finger
7159 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7160 processKey(mapper, BTN_TOOL_FINGER, 1);
7161 processSync(mapper);
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7165
7166 // stylus trumps finger
7167 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
7172
7173 // eraser trumps stylus
7174 processKey(mapper, BTN_TOOL_RUBBER, 1);
7175 processSync(mapper);
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7179
7180 // mouse trumps eraser
7181 processKey(mapper, BTN_TOOL_MOUSE, 1);
7182 processSync(mapper);
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7186
7187 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7188 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7193
7194 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7195 processToolType(mapper, MT_TOOL_PEN);
7196 processSync(mapper);
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7200
7201 // back to default tool type
7202 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7203 processKey(mapper, BTN_TOOL_MOUSE, 0);
7204 processKey(mapper, BTN_TOOL_RUBBER, 0);
7205 processKey(mapper, BTN_TOOL_PEN, 0);
7206 processKey(mapper, BTN_TOOL_FINGER, 0);
7207 processSync(mapper);
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7211}
7212
7213TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007214 addConfigurationProperty("touch.deviceType", "touchScreen");
7215 prepareDisplay(DISPLAY_ORIENTATION_0);
7216 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007217 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007218 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219
7220 NotifyMotionArgs motionArgs;
7221
7222 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7223 processId(mapper, 1);
7224 processPosition(mapper, 100, 200);
7225 processSync(mapper);
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7229 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7230
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7232 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7234 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7235
7236 // move a little
7237 processPosition(mapper, 150, 250);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7242 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7243
7244 // down when BTN_TOUCH is pressed, pressure defaults to 1
7245 processKey(mapper, BTN_TOUCH, 1);
7246 processSync(mapper);
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7250 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7251
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7255 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7256
7257 // up when BTN_TOUCH is released, hover restored
7258 processKey(mapper, BTN_TOUCH, 0);
7259 processSync(mapper);
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7263 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7264
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7268 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7269
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7273 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7274
7275 // exit hover when pointer goes away
7276 processId(mapper, -1);
7277 processSync(mapper);
7278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7281 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7282}
7283
7284TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285 addConfigurationProperty("touch.deviceType", "touchScreen");
7286 prepareDisplay(DISPLAY_ORIENTATION_0);
7287 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007288 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007289
7290 NotifyMotionArgs motionArgs;
7291
7292 // initially hovering because pressure is 0
7293 processId(mapper, 1);
7294 processPosition(mapper, 100, 200);
7295 processPressure(mapper, 0);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7300 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7301
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7305 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7306
7307 // move a little
7308 processPosition(mapper, 150, 250);
7309 processSync(mapper);
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7313 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7314
7315 // down when pressure becomes non-zero
7316 processPressure(mapper, RAW_PRESSURE_MAX);
7317 processSync(mapper);
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7321 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7322
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7326 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7327
7328 // up when pressure becomes 0, hover restored
7329 processPressure(mapper, 0);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7334 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7335
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7339 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7340
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7344 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7345
7346 // exit hover when pointer goes away
7347 processId(mapper, -1);
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7352 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7353}
7354
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007355/**
7356 * Set the input device port <--> display port associations, and check that the
7357 * events are routed to the display that matches the display port.
7358 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7359 */
7360TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007361 const std::string usb2 = "USB2";
7362 const uint8_t hdmi1 = 0;
7363 const uint8_t hdmi2 = 1;
7364 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007365 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007366
7367 addConfigurationProperty("touch.deviceType", "touchScreen");
7368 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007369 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007370
7371 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7372 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7373
7374 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7375 // for this input device is specified, and the matching viewport is not present,
7376 // the input device should be disabled (at the mapper level).
7377
7378 // Add viewport for display 2 on hdmi2
7379 prepareSecondaryDisplay(type, hdmi2);
7380 // Send a touch event
7381 processPosition(mapper, 100, 100);
7382 processSync(mapper);
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7384
7385 // Add viewport for display 1 on hdmi1
7386 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7387 // Send a touch event again
7388 processPosition(mapper, 100, 100);
7389 processSync(mapper);
7390
7391 NotifyMotionArgs args;
7392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7393 ASSERT_EQ(DISPLAY_ID, args.displayId);
7394}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007395
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007396TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007397 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007398 std::shared_ptr<FakePointerController> fakePointerController =
7399 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007400 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007401 fakePointerController->setPosition(100, 200);
7402 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007403 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7404
Garfield Tan888a6a42020-01-09 11:39:16 -08007405 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007406 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007407
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007408 prepareDisplay(DISPLAY_ORIENTATION_0);
7409 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007410 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007411
7412 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007413 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007414
7415 NotifyMotionArgs motionArgs;
7416 processPosition(mapper, 100, 100);
7417 processSync(mapper);
7418
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7421 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7422}
7423
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007424/**
7425 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7426 * events should not be delivered to the listener.
7427 */
7428TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7429 addConfigurationProperty("touch.deviceType", "touchScreen");
7430 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7431 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7432 ViewportType::INTERNAL);
7433 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7434 prepareAxes(POSITION);
7435 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7436
7437 NotifyMotionArgs motionArgs;
7438 processPosition(mapper, 100, 100);
7439 processSync(mapper);
7440
7441 mFakeListener->assertNotifyMotionWasNotCalled();
7442}
7443
Garfield Tanc734e4f2021-01-15 20:01:39 -08007444TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7445 addConfigurationProperty("touch.deviceType", "touchScreen");
7446 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7447 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7448 ViewportType::INTERNAL);
7449 std::optional<DisplayViewport> optionalDisplayViewport =
7450 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7451 ASSERT_TRUE(optionalDisplayViewport.has_value());
7452 DisplayViewport displayViewport = *optionalDisplayViewport;
7453
7454 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7455 prepareAxes(POSITION);
7456 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7457
7458 // Finger down
7459 int32_t x = 100, y = 100;
7460 processPosition(mapper, x, y);
7461 processSync(mapper);
7462
7463 NotifyMotionArgs motionArgs;
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7465 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7466
7467 // Deactivate display viewport
7468 displayViewport.isActive = false;
7469 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7470 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7471
7472 // Finger move
7473 x += 10, y += 10;
7474 processPosition(mapper, x, y);
7475 processSync(mapper);
7476
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7479
7480 // Reactivate display viewport
7481 displayViewport.isActive = true;
7482 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7483 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7484
7485 // Finger move again
7486 x += 10, y += 10;
7487 processPosition(mapper, x, y);
7488 processSync(mapper);
7489
7490 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7491 // no pointer on the touch device.
7492 mFakeListener->assertNotifyMotionWasNotCalled();
7493}
7494
Arthur Hung7c645402019-01-25 17:45:42 +08007495TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7496 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007497 prepareAxes(POSITION | ID | SLOT);
7498 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007499 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007500
7501 // Create the second touch screen device, and enable multi fingers.
7502 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007503 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007504 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007505 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007506 std::shared_ptr<InputDevice> device2 =
7507 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7508 Flags<InputDeviceClass>(0));
7509
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007510 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7511 0 /*flat*/, 0 /*fuzz*/);
7512 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7513 0 /*flat*/, 0 /*fuzz*/);
7514 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7515 0 /*flat*/, 0 /*fuzz*/);
7516 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7517 0 /*flat*/, 0 /*fuzz*/);
7518 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7519 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7520 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007521
7522 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007523 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007524 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7525 device2->reset(ARBITRARY_TIME);
7526
7527 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007528 std::shared_ptr<FakePointerController> fakePointerController =
7529 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007530 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7531 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7532
7533 // Setup policy for associated displays and show touches.
7534 const uint8_t hdmi1 = 0;
7535 const uint8_t hdmi2 = 1;
7536 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7537 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7538 mFakePolicy->setShowTouches(true);
7539
7540 // Create displays.
7541 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007542 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007543
7544 // Default device will reconfigure above, need additional reconfiguration for another device.
7545 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007546 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007547
7548 // Two fingers down at default display.
7549 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7550 processPosition(mapper, x1, y1);
7551 processId(mapper, 1);
7552 processSlot(mapper, 1);
7553 processPosition(mapper, x2, y2);
7554 processId(mapper, 2);
7555 processSync(mapper);
7556
7557 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7558 fakePointerController->getSpots().find(DISPLAY_ID);
7559 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7560 ASSERT_EQ(size_t(2), iter->second.size());
7561
7562 // Two fingers down at second display.
7563 processPosition(mapper2, x1, y1);
7564 processId(mapper2, 1);
7565 processSlot(mapper2, 1);
7566 processPosition(mapper2, x2, y2);
7567 processId(mapper2, 2);
7568 processSync(mapper2);
7569
7570 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7571 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7572 ASSERT_EQ(size_t(2), iter->second.size());
7573}
7574
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007575TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007576 prepareAxes(POSITION);
7577 addConfigurationProperty("touch.deviceType", "touchScreen");
7578 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007579 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007580
7581 NotifyMotionArgs motionArgs;
7582 // Unrotated video frame
7583 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7584 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007585 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007586 processPosition(mapper, 100, 200);
7587 processSync(mapper);
7588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7589 ASSERT_EQ(frames, motionArgs.videoFrames);
7590
7591 // Subsequent touch events should not have any videoframes
7592 // This is implemented separately in FakeEventHub,
7593 // but that should match the behaviour of TouchVideoDevice.
7594 processPosition(mapper, 200, 200);
7595 processSync(mapper);
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7597 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7598}
7599
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007600TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007601 prepareAxes(POSITION);
7602 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007603 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007604 // Unrotated video frame
7605 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7606 NotifyMotionArgs motionArgs;
7607
7608 // Test all 4 orientations
7609 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7610 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7611 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7612 clearViewports();
7613 prepareDisplay(orientation);
7614 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007615 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007616 processPosition(mapper, 100, 200);
7617 processSync(mapper);
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7619 frames[0].rotate(orientation);
7620 ASSERT_EQ(frames, motionArgs.videoFrames);
7621 }
7622}
7623
7624TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007625 prepareAxes(POSITION);
7626 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007627 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007628 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7629 // so mix these.
7630 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7631 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7632 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7633 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7634 NotifyMotionArgs motionArgs;
7635
7636 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007637 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007638 processPosition(mapper, 100, 200);
7639 processSync(mapper);
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7641 std::for_each(frames.begin(), frames.end(),
7642 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7643 ASSERT_EQ(frames, motionArgs.videoFrames);
7644}
7645
Arthur Hung9da14732019-09-02 16:16:58 +08007646/**
7647 * If we had defined port associations, but the viewport is not ready, the touch device would be
7648 * expected to be disabled, and it should be enabled after the viewport has found.
7649 */
7650TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007651 constexpr uint8_t hdmi2 = 1;
7652 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007653 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007654
7655 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7656
7657 addConfigurationProperty("touch.deviceType", "touchScreen");
7658 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007659 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007660
7661 ASSERT_EQ(mDevice->isEnabled(), false);
7662
7663 // Add display on hdmi2, the device should be enabled and can receive touch event.
7664 prepareSecondaryDisplay(type, hdmi2);
7665 ASSERT_EQ(mDevice->isEnabled(), true);
7666
7667 // Send a touch event.
7668 processPosition(mapper, 100, 100);
7669 processSync(mapper);
7670
7671 NotifyMotionArgs args;
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7673 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7674}
7675
Arthur Hung6cd19a42019-08-30 19:04:12 +08007676
Arthur Hung6cd19a42019-08-30 19:04:12 +08007677
Arthur Hung421eb1c2020-01-16 00:09:42 +08007678TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007679 addConfigurationProperty("touch.deviceType", "touchScreen");
7680 prepareDisplay(DISPLAY_ORIENTATION_0);
7681 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007682 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007683
7684 NotifyMotionArgs motionArgs;
7685
7686 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7687 // finger down
7688 processId(mapper, 1);
7689 processPosition(mapper, x1, y1);
7690 processSync(mapper);
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7692 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7694
7695 // finger move
7696 processId(mapper, 1);
7697 processPosition(mapper, x2, y2);
7698 processSync(mapper);
7699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7702
7703 // finger up.
7704 processId(mapper, -1);
7705 processSync(mapper);
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7707 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7709
7710 // new finger down
7711 processId(mapper, 1);
7712 processPosition(mapper, x3, y3);
7713 processSync(mapper);
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7715 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7717}
7718
7719/**
arthurhungcc7f9802020-04-30 17:55:40 +08007720 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7721 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007722 */
arthurhungcc7f9802020-04-30 17:55:40 +08007723TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007724 addConfigurationProperty("touch.deviceType", "touchScreen");
7725 prepareDisplay(DISPLAY_ORIENTATION_0);
7726 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007727 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007728
7729 NotifyMotionArgs motionArgs;
7730
7731 // default tool type is finger
7732 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007733 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007734 processPosition(mapper, x1, y1);
7735 processSync(mapper);
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7737 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7739
7740 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7741 processToolType(mapper, MT_TOOL_PALM);
7742 processSync(mapper);
7743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7744 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7745
7746 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007747 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007748 processPosition(mapper, x2, y2);
7749 processSync(mapper);
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7751
7752 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007753 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007754 processSync(mapper);
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7756
7757 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007758 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007759 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007760 processPosition(mapper, x3, y3);
7761 processSync(mapper);
7762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7763 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7765}
7766
arthurhungbf89a482020-04-17 17:37:55 +08007767/**
arthurhungcc7f9802020-04-30 17:55:40 +08007768 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7769 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007770 */
arthurhungcc7f9802020-04-30 17:55:40 +08007771TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007772 addConfigurationProperty("touch.deviceType", "touchScreen");
7773 prepareDisplay(DISPLAY_ORIENTATION_0);
7774 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7775 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7776
7777 NotifyMotionArgs motionArgs;
7778
7779 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007780 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7781 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007782 processPosition(mapper, x1, y1);
7783 processSync(mapper);
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7785 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7787
7788 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007789 processSlot(mapper, SECOND_SLOT);
7790 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007791 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007792 processSync(mapper);
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7794 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7795 motionArgs.action);
7796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7797
7798 // If the tool type of the first finger changes to MT_TOOL_PALM,
7799 // we expect to receive ACTION_POINTER_UP with cancel flag.
7800 processSlot(mapper, FIRST_SLOT);
7801 processId(mapper, FIRST_TRACKING_ID);
7802 processToolType(mapper, MT_TOOL_PALM);
7803 processSync(mapper);
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7805 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7806 motionArgs.action);
7807 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7808
7809 // The following MOVE events of second finger should be processed.
7810 processSlot(mapper, SECOND_SLOT);
7811 processId(mapper, SECOND_TRACKING_ID);
7812 processPosition(mapper, x2 + 1, y2 + 1);
7813 processSync(mapper);
7814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7816 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7817
7818 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7819 // it. Second finger receive move.
7820 processSlot(mapper, FIRST_SLOT);
7821 processId(mapper, INVALID_TRACKING_ID);
7822 processSync(mapper);
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7825 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7826
7827 // Second finger keeps moving.
7828 processSlot(mapper, SECOND_SLOT);
7829 processId(mapper, SECOND_TRACKING_ID);
7830 processPosition(mapper, x2 + 2, y2 + 2);
7831 processSync(mapper);
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7834 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7835
7836 // Second finger up.
7837 processId(mapper, INVALID_TRACKING_ID);
7838 processSync(mapper);
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7841 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7842}
7843
7844/**
7845 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7846 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7847 */
7848TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7849 addConfigurationProperty("touch.deviceType", "touchScreen");
7850 prepareDisplay(DISPLAY_ORIENTATION_0);
7851 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7852 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7853
7854 NotifyMotionArgs motionArgs;
7855
7856 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7857 // First finger down.
7858 processId(mapper, FIRST_TRACKING_ID);
7859 processPosition(mapper, x1, y1);
7860 processSync(mapper);
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7862 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7864
7865 // Second finger down.
7866 processSlot(mapper, SECOND_SLOT);
7867 processId(mapper, SECOND_TRACKING_ID);
7868 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007869 processSync(mapper);
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7871 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7872 motionArgs.action);
7873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7874
arthurhungcc7f9802020-04-30 17:55:40 +08007875 // If the tool type of the first finger changes to MT_TOOL_PALM,
7876 // we expect to receive ACTION_POINTER_UP with cancel flag.
7877 processSlot(mapper, FIRST_SLOT);
7878 processId(mapper, FIRST_TRACKING_ID);
7879 processToolType(mapper, MT_TOOL_PALM);
7880 processSync(mapper);
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7882 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7883 motionArgs.action);
7884 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7885
7886 // Second finger keeps moving.
7887 processSlot(mapper, SECOND_SLOT);
7888 processId(mapper, SECOND_TRACKING_ID);
7889 processPosition(mapper, x2 + 1, y2 + 1);
7890 processSync(mapper);
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7893
7894 // second finger becomes palm, receive cancel due to only 1 finger is active.
7895 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007896 processToolType(mapper, MT_TOOL_PALM);
7897 processSync(mapper);
7898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7899 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7900
arthurhungcc7f9802020-04-30 17:55:40 +08007901 // third finger down.
7902 processSlot(mapper, THIRD_SLOT);
7903 processId(mapper, THIRD_TRACKING_ID);
7904 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007905 processPosition(mapper, x3, y3);
7906 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7908 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007910 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7911
7912 // third finger move
7913 processId(mapper, THIRD_TRACKING_ID);
7914 processPosition(mapper, x3 + 1, y3 + 1);
7915 processSync(mapper);
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7918
7919 // first finger up, third finger receive move.
7920 processSlot(mapper, FIRST_SLOT);
7921 processId(mapper, INVALID_TRACKING_ID);
7922 processSync(mapper);
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7925 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7926
7927 // second finger up, third finger receive move.
7928 processSlot(mapper, SECOND_SLOT);
7929 processId(mapper, INVALID_TRACKING_ID);
7930 processSync(mapper);
7931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7933 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7934
7935 // third finger up.
7936 processSlot(mapper, THIRD_SLOT);
7937 processId(mapper, INVALID_TRACKING_ID);
7938 processSync(mapper);
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7941 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7942}
7943
7944/**
7945 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7946 * and the active finger could still be allowed to receive the events
7947 */
7948TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7949 addConfigurationProperty("touch.deviceType", "touchScreen");
7950 prepareDisplay(DISPLAY_ORIENTATION_0);
7951 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7952 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7953
7954 NotifyMotionArgs motionArgs;
7955
7956 // default tool type is finger
7957 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7958 processId(mapper, FIRST_TRACKING_ID);
7959 processPosition(mapper, x1, y1);
7960 processSync(mapper);
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7962 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7964
7965 // Second finger down.
7966 processSlot(mapper, SECOND_SLOT);
7967 processId(mapper, SECOND_TRACKING_ID);
7968 processPosition(mapper, x2, y2);
7969 processSync(mapper);
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7972 motionArgs.action);
7973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7974
7975 // If the tool type of the second finger changes to MT_TOOL_PALM,
7976 // we expect to receive ACTION_POINTER_UP with cancel flag.
7977 processId(mapper, SECOND_TRACKING_ID);
7978 processToolType(mapper, MT_TOOL_PALM);
7979 processSync(mapper);
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7982 motionArgs.action);
7983 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7984
7985 // The following MOVE event should be processed.
7986 processSlot(mapper, FIRST_SLOT);
7987 processId(mapper, FIRST_TRACKING_ID);
7988 processPosition(mapper, x1 + 1, y1 + 1);
7989 processSync(mapper);
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7992 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7993
7994 // second finger up.
7995 processSlot(mapper, SECOND_SLOT);
7996 processId(mapper, INVALID_TRACKING_ID);
7997 processSync(mapper);
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8000
8001 // first finger keep moving
8002 processSlot(mapper, FIRST_SLOT);
8003 processId(mapper, FIRST_TRACKING_ID);
8004 processPosition(mapper, x1 + 2, y1 + 2);
8005 processSync(mapper);
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8008
8009 // first finger up.
8010 processId(mapper, INVALID_TRACKING_ID);
8011 processSync(mapper);
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8013 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8014 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008015}
8016
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008017// --- MultiTouchInputMapperTest_ExternalDevice ---
8018
8019class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8020protected:
Chris Yea52ade12020-08-27 16:49:20 -07008021 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008022};
8023
8024/**
8025 * Expect fallback to internal viewport if device is external and external viewport is not present.
8026 */
8027TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8028 prepareAxes(POSITION);
8029 addConfigurationProperty("touch.deviceType", "touchScreen");
8030 prepareDisplay(DISPLAY_ORIENTATION_0);
8031 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8032
8033 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8034
8035 NotifyMotionArgs motionArgs;
8036
8037 // Expect the event to be sent to the internal viewport,
8038 // because an external viewport is not present.
8039 processPosition(mapper, 100, 100);
8040 processSync(mapper);
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8042 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8043
8044 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008045 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008046 processPosition(mapper, 100, 100);
8047 processSync(mapper);
8048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8049 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8050}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008051
8052/**
8053 * Test touch should not work if outside of surface.
8054 */
8055class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8056protected:
8057 void halfDisplayToCenterHorizontal(int32_t orientation) {
8058 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008059 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008060
8061 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8062 internalViewport->orientation = orientation;
8063 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8064 internalViewport->logicalLeft = 0;
8065 internalViewport->logicalTop = 0;
8066 internalViewport->logicalRight = DISPLAY_HEIGHT;
8067 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8068
8069 internalViewport->physicalLeft = 0;
8070 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8071 internalViewport->physicalRight = DISPLAY_HEIGHT;
8072 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8073
8074 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8075 internalViewport->deviceHeight = DISPLAY_WIDTH;
8076 } else {
8077 internalViewport->logicalLeft = 0;
8078 internalViewport->logicalTop = 0;
8079 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8080 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8081
8082 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8083 internalViewport->physicalTop = 0;
8084 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8085 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8086
8087 internalViewport->deviceWidth = DISPLAY_WIDTH;
8088 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8089 }
8090
8091 mFakePolicy->updateViewport(internalViewport.value());
8092 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8093 }
8094
arthurhung5d547942020-12-14 17:04:45 +08008095 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8096 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008097 int32_t yExpected) {
8098 // touch on outside area should not work.
8099 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8100 processSync(mapper);
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8102
8103 // touch on inside area should receive the event.
8104 NotifyMotionArgs args;
8105 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8106 processSync(mapper);
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8108 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8109 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8110
8111 // Reset.
8112 mapper.reset(ARBITRARY_TIME);
8113 }
8114};
8115
arthurhung5d547942020-12-14 17:04:45 +08008116TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008117 addConfigurationProperty("touch.deviceType", "touchScreen");
8118 prepareDisplay(DISPLAY_ORIENTATION_0);
8119 prepareAxes(POSITION);
8120 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8121
8122 // Touch on center of normal display should work.
8123 const int32_t x = DISPLAY_WIDTH / 4;
8124 const int32_t y = DISPLAY_HEIGHT / 2;
8125 processPosition(mapper, toRawX(x), toRawY(y));
8126 processSync(mapper);
8127 NotifyMotionArgs args;
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8130 0.0f, 0.0f, 0.0f, 0.0f));
8131 // Reset.
8132 mapper.reset(ARBITRARY_TIME);
8133
8134 // Let physical display be different to device, and make surface and physical could be 1:1.
8135 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8136
8137 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8138 const int32_t yExpected = y;
8139 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8140}
8141
arthurhung5d547942020-12-14 17:04:45 +08008142TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008143 addConfigurationProperty("touch.deviceType", "touchScreen");
8144 prepareDisplay(DISPLAY_ORIENTATION_0);
8145 prepareAxes(POSITION);
8146 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8147
8148 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8149 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8150
8151 const int32_t x = DISPLAY_WIDTH / 4;
8152 const int32_t y = DISPLAY_HEIGHT / 2;
8153
8154 // expect x/y = swap x/y then reverse y.
8155 const int32_t xExpected = y;
8156 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8157 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8158}
8159
arthurhung5d547942020-12-14 17:04:45 +08008160TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008161 addConfigurationProperty("touch.deviceType", "touchScreen");
8162 prepareDisplay(DISPLAY_ORIENTATION_0);
8163 prepareAxes(POSITION);
8164 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8165
8166 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8167 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8168
8169 const int32_t x = DISPLAY_WIDTH / 4;
8170 const int32_t y = DISPLAY_HEIGHT / 2;
8171
8172 // expect x/y = swap x/y then reverse x.
8173 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8174 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8175 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8176}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008177
arthurhunga36b28e2020-12-29 20:28:15 +08008178TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8179 addConfigurationProperty("touch.deviceType", "touchScreen");
8180 prepareDisplay(DISPLAY_ORIENTATION_0);
8181 prepareAxes(POSITION);
8182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8183
8184 const int32_t x = 0;
8185 const int32_t y = 0;
8186
8187 const int32_t xExpected = x;
8188 const int32_t yExpected = y;
8189 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8190
8191 clearViewports();
8192 prepareDisplay(DISPLAY_ORIENTATION_90);
8193 // expect x/y = swap x/y then reverse y.
8194 const int32_t xExpected90 = y;
8195 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8196 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8197
8198 clearViewports();
8199 prepareDisplay(DISPLAY_ORIENTATION_270);
8200 // expect x/y = swap x/y then reverse x.
8201 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8202 const int32_t yExpected270 = x;
8203 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8204}
8205
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008206TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8207 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8208 std::shared_ptr<FakePointerController> fakePointerController =
8209 std::make_shared<FakePointerController>();
8210 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8211 fakePointerController->setPosition(0, 0);
8212 fakePointerController->setButtonState(0);
8213
8214 // prepare device and capture
8215 prepareDisplay(DISPLAY_ORIENTATION_0);
8216 prepareAxes(POSITION | ID | SLOT);
8217 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8218 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8219 mFakePolicy->setPointerCapture(true);
8220 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8221 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8222
8223 // captured touchpad should be a touchpad source
8224 NotifyDeviceResetArgs resetArgs;
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8226 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8227
Chris Yef74dc422020-09-02 22:41:50 -07008228 InputDeviceInfo deviceInfo;
8229 mDevice->getDeviceInfo(&deviceInfo);
8230
8231 const InputDeviceInfo::MotionRange* relRangeX =
8232 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8233 ASSERT_NE(relRangeX, nullptr);
8234 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8235 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8236 const InputDeviceInfo::MotionRange* relRangeY =
8237 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8238 ASSERT_NE(relRangeY, nullptr);
8239 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8240 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8241
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008242 // run captured pointer tests - note that this is unscaled, so input listener events should be
8243 // identical to what the hardware sends (accounting for any
8244 // calibration).
8245 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008246 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008247 processId(mapper, 1);
8248 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8249 processKey(mapper, BTN_TOUCH, 1);
8250 processSync(mapper);
8251
8252 // expect coord[0] to contain initial location of touch 0
8253 NotifyMotionArgs args;
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8256 ASSERT_EQ(1U, args.pointerCount);
8257 ASSERT_EQ(0, args.pointerProperties[0].id);
8258 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8259 ASSERT_NO_FATAL_FAILURE(
8260 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8261
8262 // FINGER 1 DOWN
8263 processSlot(mapper, 1);
8264 processId(mapper, 2);
8265 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8266 processSync(mapper);
8267
8268 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008270 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8271 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008272 ASSERT_EQ(2U, args.pointerCount);
8273 ASSERT_EQ(0, args.pointerProperties[0].id);
8274 ASSERT_EQ(1, args.pointerProperties[1].id);
8275 ASSERT_NO_FATAL_FAILURE(
8276 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8277 ASSERT_NO_FATAL_FAILURE(
8278 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8279
8280 // FINGER 1 MOVE
8281 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8282 processSync(mapper);
8283
8284 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8285 // from move
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8288 ASSERT_NO_FATAL_FAILURE(
8289 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8290 ASSERT_NO_FATAL_FAILURE(
8291 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8292
8293 // FINGER 0 MOVE
8294 processSlot(mapper, 0);
8295 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8296 processSync(mapper);
8297
8298 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8301 ASSERT_NO_FATAL_FAILURE(
8302 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8303 ASSERT_NO_FATAL_FAILURE(
8304 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8305
8306 // BUTTON DOWN
8307 processKey(mapper, BTN_LEFT, 1);
8308 processSync(mapper);
8309
8310 // touchinputmapper design sends a move before button press
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8315
8316 // BUTTON UP
8317 processKey(mapper, BTN_LEFT, 0);
8318 processSync(mapper);
8319
8320 // touchinputmapper design sends a move after button release
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8325
8326 // FINGER 0 UP
8327 processId(mapper, -1);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8331
8332 // FINGER 1 MOVE
8333 processSlot(mapper, 1);
8334 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8335 processSync(mapper);
8336
8337 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8340 ASSERT_EQ(1U, args.pointerCount);
8341 ASSERT_EQ(1, args.pointerProperties[0].id);
8342 ASSERT_NO_FATAL_FAILURE(
8343 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8344
8345 // FINGER 1 UP
8346 processId(mapper, -1);
8347 processKey(mapper, BTN_TOUCH, 0);
8348 processSync(mapper);
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8350 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8351
8352 // non captured touchpad should be a mouse source
8353 mFakePolicy->setPointerCapture(false);
8354 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8356 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8357}
8358
8359TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8360 std::shared_ptr<FakePointerController> fakePointerController =
8361 std::make_shared<FakePointerController>();
8362 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8363 fakePointerController->setPosition(0, 0);
8364 fakePointerController->setButtonState(0);
8365
8366 // prepare device and capture
8367 prepareDisplay(DISPLAY_ORIENTATION_0);
8368 prepareAxes(POSITION | ID | SLOT);
8369 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8370 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8371 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8372 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8373 // run uncaptured pointer tests - pushes out generic events
8374 // FINGER 0 DOWN
8375 processId(mapper, 3);
8376 processPosition(mapper, 100, 100);
8377 processKey(mapper, BTN_TOUCH, 1);
8378 processSync(mapper);
8379
8380 // start at (100,100), cursor should be at (0,0) * scale
8381 NotifyMotionArgs args;
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8384 ASSERT_NO_FATAL_FAILURE(
8385 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8386
8387 // FINGER 0 MOVE
8388 processPosition(mapper, 200, 200);
8389 processSync(mapper);
8390
8391 // compute scaling to help with touch position checking
8392 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8393 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8394 float scale =
8395 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8396
8397 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8401 0, 0, 0, 0, 0, 0, 0));
8402}
8403
8404TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8405 std::shared_ptr<FakePointerController> fakePointerController =
8406 std::make_shared<FakePointerController>();
8407
8408 prepareDisplay(DISPLAY_ORIENTATION_0);
8409 prepareAxes(POSITION | ID | SLOT);
8410 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8411 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8412 mFakePolicy->setPointerCapture(false);
8413 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8414
8415 // uncaptured touchpad should be a pointer device
8416 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8417
8418 // captured touchpad should be a touchpad device
8419 mFakePolicy->setPointerCapture(true);
8420 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8421 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8422}
8423
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424} // namespace android