blob: f6265638b726c2f1eba3039134308e32c5cd6780 [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.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000051static constexpr nsecs_t ARBITRARY_TIME = 1234;
52static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
54// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080055static constexpr int32_t DISPLAY_ID = 0;
56static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
57static constexpr int32_t DISPLAY_WIDTH = 480;
58static constexpr int32_t DISPLAY_HEIGHT = 800;
59static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
60static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
61static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070062static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070063static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080064
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t FIRST_SLOT = 0;
66static constexpr int32_t SECOND_SLOT = 1;
67static constexpr int32_t THIRD_SLOT = 2;
68static constexpr int32_t INVALID_TRACKING_ID = -1;
69static constexpr int32_t FIRST_TRACKING_ID = 0;
70static constexpr int32_t SECOND_TRACKING_ID = 1;
71static constexpr int32_t THIRD_TRACKING_ID = 2;
Kim Low03ea0352020-11-06 12:45:07 -080072static constexpr int32_t BATTERY_STATUS = 4;
73static constexpr int32_t BATTERY_CAPACITY = 66;
arthurhungcc7f9802020-04-30 17:55:40 +080074
Michael Wrightd02c5b62014-02-10 15:10:22 -080075// Error tolerance for floating point assertions.
76static const float EPSILON = 0.001f;
77
78template<typename T>
79static inline T min(T a, T b) {
80 return a < b ? a : b;
81}
82
83static inline float avg(float x, float y) {
84 return (x + y) / 2;
85}
86
87
88// --- FakePointerController ---
89
90class FakePointerController : public PointerControllerInterface {
91 bool mHaveBounds;
92 float mMinX, mMinY, mMaxX, mMaxY;
93 float mX, mY;
94 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080095 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
Michael Wrightd02c5b62014-02-10 15:10:22 -080097public:
98 FakePointerController() :
99 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800100 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101 }
102
Michael Wright17db18e2020-06-26 20:51:44 +0100103 virtual ~FakePointerController() {}
104
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105 void setBounds(float minX, float minY, float maxX, float maxY) {
106 mHaveBounds = true;
107 mMinX = minX;
108 mMinY = minY;
109 mMaxX = maxX;
110 mMaxY = maxY;
111 }
112
Chris Yea52ade12020-08-27 16:49:20 -0700113 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114 mX = x;
115 mY = y;
116 }
117
Chris Yea52ade12020-08-27 16:49:20 -0700118 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119
Chris Yea52ade12020-08-27 16:49:20 -0700120 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121
Chris Yea52ade12020-08-27 16:49:20 -0700122 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 *outX = mX;
124 *outY = mY;
125 }
126
Chris Yea52ade12020-08-27 16:49:20 -0700127 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800128
Chris Yea52ade12020-08-27 16:49:20 -0700129 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800130 mDisplayId = viewport.displayId;
131 }
132
Arthur Hung7c645402019-01-25 17:45:42 +0800133 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
134 return mSpotsByDisplay;
135 }
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137private:
Chris Yea52ade12020-08-27 16:49:20 -0700138 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 *outMinX = mMinX;
140 *outMinY = mMinY;
141 *outMaxX = mMaxX;
142 *outMaxY = mMaxY;
143 return mHaveBounds;
144 }
145
Chris Yea52ade12020-08-27 16:49:20 -0700146 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147 mX += deltaX;
148 if (mX < mMinX) mX = mMinX;
149 if (mX > mMaxX) mX = mMaxX;
150 mY += deltaY;
151 if (mY < mMinY) mY = mMinY;
152 if (mY > mMaxY) mY = mMaxY;
153 }
154
Chris Yea52ade12020-08-27 16:49:20 -0700155 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156
Chris Yea52ade12020-08-27 16:49:20 -0700157 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158
Chris Yea52ade12020-08-27 16:49:20 -0700159 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160
Chris Yea52ade12020-08-27 16:49:20 -0700161 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
162 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800163 std::vector<int32_t> newSpots;
164 // Add spots for fingers that are down.
165 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
166 uint32_t id = idBits.clearFirstMarkedBit();
167 newSpots.push_back(id);
168 }
169
170 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 }
172
Chris Yea52ade12020-08-27 16:49:20 -0700173 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800174
175 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176};
177
178
179// --- FakeInputReaderPolicy ---
180
181class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700182 std::mutex mLock;
183 std::condition_variable mDevicesChangedCondition;
184
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100186 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700187 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
188 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100189 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700190 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191
192protected:
Chris Yea52ade12020-08-27 16:49:20 -0700193 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194
195public:
196 FakeInputReaderPolicy() {
197 }
198
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700199 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800200 waitForInputDevices([](bool devicesChanged) {
201 if (!devicesChanged) {
202 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
203 }
204 });
205 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700206
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800207 void assertInputDevicesNotChanged() {
208 waitForInputDevices([](bool devicesChanged) {
209 if (devicesChanged) {
210 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
211 }
212 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700213 }
214
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700215 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100216 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100217 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700218 }
219
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700220 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
221 return mConfig.getDisplayViewportByUniqueId(uniqueId);
222 }
223 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
224 return mConfig.getDisplayViewportByType(type);
225 }
226
227 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
228 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700229 }
230
231 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000232 bool isActive, const std::string& uniqueId,
233 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
234 const DisplayViewport viewport =
235 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
236 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 }
240
Arthur Hung6cd19a42019-08-30 19:04:12 +0800241 bool updateViewport(const DisplayViewport& viewport) {
242 size_t count = mViewports.size();
243 for (size_t i = 0; i < count; i++) {
244 const DisplayViewport& currentViewport = mViewports[i];
245 if (currentViewport.displayId == viewport.displayId) {
246 mViewports[i] = viewport;
247 mConfig.setDisplayViewports(mViewports);
248 return true;
249 }
250 }
251 // no viewport found.
252 return false;
253 }
254
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100255 void addExcludedDeviceName(const std::string& deviceName) {
256 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 }
258
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700259 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
260 mConfig.portAssociations.insert({inputPort, displayPort});
261 }
262
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000263 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700264
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000265 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700266
Michael Wright17db18e2020-06-26 20:51:44 +0100267 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
268 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 }
270
271 const InputReaderConfiguration* getReaderConfiguration() const {
272 return &mConfig;
273 }
274
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800275 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 return mInputDevices;
277 }
278
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100279 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700280 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700281 return transform;
282 }
283
284 void setTouchAffineTransformation(const TouchAffineTransformation t) {
285 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800286 }
287
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800288 void setPointerCapture(bool enabled) {
289 mConfig.pointerCapture = enabled;
290 }
291
Arthur Hung7c645402019-01-25 17:45:42 +0800292 void setShowTouches(bool enabled) {
293 mConfig.showTouches = enabled;
294 }
295
Garfield Tan888a6a42020-01-09 11:39:16 -0800296 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
297 mConfig.defaultPointerDisplayId = pointerDisplayId;
298 }
299
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800300 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
301
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700303 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000304 int32_t orientation, bool isActive,
305 const std::string& uniqueId,
306 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700307 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
308 || orientation == DISPLAY_ORIENTATION_270);
309 DisplayViewport v;
310 v.displayId = displayId;
311 v.orientation = orientation;
312 v.logicalLeft = 0;
313 v.logicalTop = 0;
314 v.logicalRight = isRotated ? height : width;
315 v.logicalBottom = isRotated ? width : height;
316 v.physicalLeft = 0;
317 v.physicalTop = 0;
318 v.physicalRight = isRotated ? height : width;
319 v.physicalBottom = isRotated ? width : height;
320 v.deviceWidth = isRotated ? height : width;
321 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000322 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700323 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700324 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100325 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700326 return v;
327 }
328
Chris Yea52ade12020-08-27 16:49:20 -0700329 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 *outConfig = mConfig;
331 }
332
Chris Yea52ade12020-08-27 16:49:20 -0700333 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100334 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 }
336
Chris Yea52ade12020-08-27 16:49:20 -0700337 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700338 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700340 mInputDevicesChanged = true;
341 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342 }
343
Chris Yea52ade12020-08-27 16:49:20 -0700344 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
345 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700346 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
Chris Yea52ade12020-08-27 16:49:20 -0700349 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800350
351 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
352 std::unique_lock<std::mutex> lock(mLock);
353 base::ScopedLockAssertion assumeLocked(mLock);
354
355 const bool devicesChanged =
356 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
357 return mInputDevicesChanged;
358 });
359 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
360 mInputDevicesChanged = false;
361 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362};
363
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364// --- FakeEventHub ---
365
366class FakeEventHub : public EventHubInterface {
367 struct KeyInfo {
368 int32_t keyCode;
369 uint32_t flags;
370 };
371
Chris Yef59a2f42020-10-16 12:55:26 -0700372 struct SensorInfo {
373 InputDeviceSensorType sensorType;
374 int32_t sensorDataIndex;
375 };
376
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377 struct Device {
378 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700379 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380 PropertyMap configuration;
381 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
382 KeyedVector<int, bool> relativeAxes;
383 KeyedVector<int32_t, int32_t> keyCodeStates;
384 KeyedVector<int32_t, int32_t> scanCodeStates;
385 KeyedVector<int32_t, int32_t> switchStates;
386 KeyedVector<int32_t, int32_t> absoluteAxisValue;
387 KeyedVector<int32_t, KeyInfo> keysByScanCode;
388 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
389 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700390 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
391 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800392 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700393 bool enabled;
394
395 status_t enable() {
396 enabled = true;
397 return OK;
398 }
399
400 status_t disable() {
401 enabled = false;
402 return OK;
403 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404
Chris Ye1b0c7342020-07-28 21:57:03 -0700405 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800406 };
407
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700408 std::mutex mLock;
409 std::condition_variable mEventsCondition;
410
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100412 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000413 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600414 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000415 std::vector<int32_t> mVibrators = {0, 1};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700417public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418 virtual ~FakeEventHub() {
419 for (size_t i = 0; i < mDevices.size(); i++) {
420 delete mDevices.valueAt(i);
421 }
422 }
423
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424 FakeEventHub() { }
425
Chris Ye1b0c7342020-07-28 21:57:03 -0700426 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 Device* device = new Device(classes);
428 device->identifier.name = name;
429 mDevices.add(deviceId, device);
430
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000431 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 }
433
434 void removeDevice(int32_t deviceId) {
435 delete mDevices.valueFor(deviceId);
436 mDevices.removeItem(deviceId);
437
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000438 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 }
440
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700441 bool isDeviceEnabled(int32_t deviceId) {
442 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700443 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700444 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
445 return false;
446 }
447 return device->enabled;
448 }
449
450 status_t enableDevice(int32_t deviceId) {
451 status_t result;
452 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700453 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700454 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
455 return BAD_VALUE;
456 }
457 if (device->enabled) {
458 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
459 return OK;
460 }
461 result = device->enable();
462 return result;
463 }
464
465 status_t disableDevice(int32_t deviceId) {
466 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700467 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700468 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
469 return BAD_VALUE;
470 }
471 if (!device->enabled) {
472 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
473 return OK;
474 }
475 return device->disable();
476 }
477
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000479 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 }
481
482 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
483 Device* device = getDevice(deviceId);
484 device->configuration.addProperty(key, value);
485 }
486
487 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
488 Device* device = getDevice(deviceId);
489 device->configuration.addAll(configuration);
490 }
491
492 void addAbsoluteAxis(int32_t deviceId, int axis,
493 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
494 Device* device = getDevice(deviceId);
495
496 RawAbsoluteAxisInfo info;
497 info.valid = true;
498 info.minValue = minValue;
499 info.maxValue = maxValue;
500 info.flat = flat;
501 info.fuzz = fuzz;
502 info.resolution = resolution;
503 device->absoluteAxes.add(axis, info);
504 }
505
506 void addRelativeAxis(int32_t deviceId, int32_t axis) {
507 Device* device = getDevice(deviceId);
508 device->relativeAxes.add(axis, true);
509 }
510
511 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
512 Device* device = getDevice(deviceId);
513 device->keyCodeStates.replaceValueFor(keyCode, state);
514 }
515
516 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
517 Device* device = getDevice(deviceId);
518 device->scanCodeStates.replaceValueFor(scanCode, state);
519 }
520
521 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
522 Device* device = getDevice(deviceId);
523 device->switchStates.replaceValueFor(switchCode, state);
524 }
525
526 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
527 Device* device = getDevice(deviceId);
528 device->absoluteAxisValue.replaceValueFor(axis, value);
529 }
530
531 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
532 int32_t keyCode, uint32_t flags) {
533 Device* device = getDevice(deviceId);
534 KeyInfo info;
535 info.keyCode = keyCode;
536 info.flags = flags;
537 if (scanCode) {
538 device->keysByScanCode.add(scanCode, info);
539 }
540 if (usageCode) {
541 device->keysByUsageCode.add(usageCode, info);
542 }
543 }
544
545 void addLed(int32_t deviceId, int32_t led, bool initialState) {
546 Device* device = getDevice(deviceId);
547 device->leds.add(led, initialState);
548 }
549
Chris Yef59a2f42020-10-16 12:55:26 -0700550 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
551 int32_t sensorDataIndex) {
552 Device* device = getDevice(deviceId);
553 SensorInfo info;
554 info.sensorType = sensorType;
555 info.sensorDataIndex = sensorDataIndex;
556 device->sensorsByAbsCode.emplace(absCode, info);
557 }
558
559 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
560 Device* device = getDevice(deviceId);
561 typename BitArray<MSC_MAX>::Buffer buffer;
562 buffer[mscEvent / 32] = 1 << mscEvent % 32;
563 device->mscBitmask.loadFromBuffer(buffer);
564 }
565
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 bool getLedState(int32_t deviceId, int32_t led) {
567 Device* device = getDevice(deviceId);
568 return device->leds.valueFor(led);
569 }
570
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100571 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572 return mExcludedDevices;
573 }
574
575 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
576 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800577 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 }
579
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000580 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
581 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700582 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 RawEvent event;
584 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000585 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 event.deviceId = deviceId;
587 event.type = type;
588 event.code = code;
589 event.value = value;
590 mEvents.push_back(event);
591
592 if (type == EV_ABS) {
593 setAbsoluteAxisValue(deviceId, code, value);
594 }
595 }
596
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600597 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
598 std::vector<TouchVideoFrame>> videoFrames) {
599 mVideoFrames = std::move(videoFrames);
600 }
601
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700603 std::unique_lock<std::mutex> lock(mLock);
604 base::ScopedLockAssertion assumeLocked(mLock);
605 const bool queueIsEmpty =
606 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
607 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
608 if (!queueIsEmpty) {
609 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
610 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 }
612
613private:
614 Device* getDevice(int32_t deviceId) const {
615 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100616 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Chris Yea52ade12020-08-27 16:49:20 -0700619 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700621 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 }
623
Chris Yea52ade12020-08-27 16:49:20 -0700624 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 Device* device = getDevice(deviceId);
626 return device ? device->identifier : InputDeviceIdentifier();
627 }
628
Chris Yea52ade12020-08-27 16:49:20 -0700629 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630
Chris Yea52ade12020-08-27 16:49:20 -0700631 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 Device* device = getDevice(deviceId);
633 if (device) {
634 *outConfiguration = device->configuration;
635 }
636 }
637
Chris Yea52ade12020-08-27 16:49:20 -0700638 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
639 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800641 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 ssize_t index = device->absoluteAxes.indexOfKey(axis);
643 if (index >= 0) {
644 *outAxisInfo = device->absoluteAxes.valueAt(index);
645 return OK;
646 }
647 }
648 outAxisInfo->clear();
649 return -1;
650 }
651
Chris Yea52ade12020-08-27 16:49:20 -0700652 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 Device* device = getDevice(deviceId);
654 if (device) {
655 return device->relativeAxes.indexOfKey(axis) >= 0;
656 }
657 return false;
658 }
659
Chris Yea52ade12020-08-27 16:49:20 -0700660 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661
Chris Yef59a2f42020-10-16 12:55:26 -0700662 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
663 Device* device = getDevice(deviceId);
664 if (device) {
665 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
666 }
667 return false;
668 }
669
Chris Yea52ade12020-08-27 16:49:20 -0700670 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
671 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 Device* device = getDevice(deviceId);
673 if (device) {
674 const KeyInfo* key = getKey(device, scanCode, usageCode);
675 if (key) {
676 if (outKeycode) {
677 *outKeycode = key->keyCode;
678 }
679 if (outFlags) {
680 *outFlags = key->flags;
681 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700682 if (outMetaState) {
683 *outMetaState = metaState;
684 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 return OK;
686 }
687 }
688 return NAME_NOT_FOUND;
689 }
690
691 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
692 if (usageCode) {
693 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
694 if (index >= 0) {
695 return &device->keysByUsageCode.valueAt(index);
696 }
697 }
698 if (scanCode) {
699 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
700 if (index >= 0) {
701 return &device->keysByScanCode.valueAt(index);
702 }
703 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700704 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 }
706
Chris Yea52ade12020-08-27 16:49:20 -0700707 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708
Chris Yef59a2f42020-10-16 12:55:26 -0700709 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
710 int32_t absCode) {
711 Device* device = getDevice(deviceId);
712 if (!device) {
713 return Errorf("Sensor device not found.");
714 }
715 auto it = device->sensorsByAbsCode.find(absCode);
716 if (it == device->sensorsByAbsCode.end()) {
717 return Errorf("Sensor map not found.");
718 }
719 const SensorInfo& info = it->second;
720 return std::make_pair(info.sensorType, info.sensorDataIndex);
721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 mExcludedDevices = devices;
725 }
726
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000727 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
728 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000730 const size_t filledSize = std::min(mEvents.size(), bufferSize);
731 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
732
733 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700734 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000735 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736 }
737
Chris Yea52ade12020-08-27 16:49:20 -0700738 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600739 auto it = mVideoFrames.find(deviceId);
740 if (it != mVideoFrames.end()) {
741 std::vector<TouchVideoFrame> frames = std::move(it->second);
742 mVideoFrames.erase(deviceId);
743 return frames;
744 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800745 return {};
746 }
747
Chris Yea52ade12020-08-27 16:49:20 -0700748 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 Device* device = getDevice(deviceId);
750 if (device) {
751 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
752 if (index >= 0) {
753 return device->scanCodeStates.valueAt(index);
754 }
755 }
756 return AKEY_STATE_UNKNOWN;
757 }
758
Chris Yea52ade12020-08-27 16:49:20 -0700759 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760 Device* device = getDevice(deviceId);
761 if (device) {
762 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
763 if (index >= 0) {
764 return device->keyCodeStates.valueAt(index);
765 }
766 }
767 return AKEY_STATE_UNKNOWN;
768 }
769
Chris Yea52ade12020-08-27 16:49:20 -0700770 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 Device* device = getDevice(deviceId);
772 if (device) {
773 ssize_t index = device->switchStates.indexOfKey(sw);
774 if (index >= 0) {
775 return device->switchStates.valueAt(index);
776 }
777 }
778 return AKEY_STATE_UNKNOWN;
779 }
780
Chris Yea52ade12020-08-27 16:49:20 -0700781 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
782 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 Device* device = getDevice(deviceId);
784 if (device) {
785 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
786 if (index >= 0) {
787 *outValue = device->absoluteAxisValue.valueAt(index);
788 return OK;
789 }
790 }
791 *outValue = 0;
792 return -1;
793 }
794
Chris Yea52ade12020-08-27 16:49:20 -0700795 // Return true if the device has non-empty key layout.
796 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
797 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 bool result = false;
799 Device* device = getDevice(deviceId);
800 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700801 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 for (size_t i = 0; i < numCodes; i++) {
803 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
804 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
805 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 }
807 }
808 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
809 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
810 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811 }
812 }
813 }
814 }
815 return result;
816 }
817
Chris Yea52ade12020-08-27 16:49:20 -0700818 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800819 Device* device = getDevice(deviceId);
820 if (device) {
821 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
822 return index >= 0;
823 }
824 return false;
825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 Device* device = getDevice(deviceId);
829 return device && device->leds.indexOfKey(led) >= 0;
830 }
831
Chris Yea52ade12020-08-27 16:49:20 -0700832 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833 Device* device = getDevice(deviceId);
834 if (device) {
835 ssize_t index = device->leds.indexOfKey(led);
836 if (index >= 0) {
837 device->leds.replaceValueAt(led, on);
838 } else {
839 ADD_FAILURE()
840 << "Attempted to set the state of an LED that the EventHub declared "
841 "was not present. led=" << led;
842 }
843 }
844 }
845
Chris Yea52ade12020-08-27 16:49:20 -0700846 void getVirtualKeyDefinitions(
847 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 outVirtualKeys.clear();
849
850 Device* device = getDevice(deviceId);
851 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800852 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853 }
854 }
855
Chris Yea52ade12020-08-27 16:49:20 -0700856 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700857 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 return false;
862 }
863
Chris Yea52ade12020-08-27 16:49:20 -0700864 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865
Chris Yea52ade12020-08-27 16:49:20 -0700866 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867
Chris Ye87143712020-11-10 05:05:58 +0000868 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
869
Kim Low03ea0352020-11-06 12:45:07 -0800870 std::optional<int32_t> getBatteryCapacity(int32_t) const override { return BATTERY_CAPACITY; }
871
872 std::optional<int32_t> getBatteryStatus(int32_t) const override { return BATTERY_STATUS; }
873
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100874 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 return false;
876 }
877
Chris Yea52ade12020-08-27 16:49:20 -0700878 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879
Chris Yea52ade12020-08-27 16:49:20 -0700880 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881
Chris Yea52ade12020-08-27 16:49:20 -0700882 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883
Chris Yea52ade12020-08-27 16:49:20 -0700884 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885};
886
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887// --- FakeInputMapper ---
888
889class FakeInputMapper : public InputMapper {
890 uint32_t mSources;
891 int32_t mKeyboardType;
892 int32_t mMetaState;
893 KeyedVector<int32_t, int32_t> mKeyCodeStates;
894 KeyedVector<int32_t, int32_t> mScanCodeStates;
895 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800896 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700898 std::mutex mLock;
899 std::condition_variable mStateChangedCondition;
900 bool mConfigureWasCalled GUARDED_BY(mLock);
901 bool mResetWasCalled GUARDED_BY(mLock);
902 bool mProcessWasCalled GUARDED_BY(mLock);
903 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Arthur Hungc23540e2018-11-29 20:42:11 +0800905 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800907 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
908 : InputMapper(deviceContext),
909 mSources(sources),
910 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800912 mConfigureWasCalled(false),
913 mResetWasCalled(false),
914 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
Chris Yea52ade12020-08-27 16:49:20 -0700916 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917
918 void setKeyboardType(int32_t keyboardType) {
919 mKeyboardType = keyboardType;
920 }
921
922 void setMetaState(int32_t metaState) {
923 mMetaState = metaState;
924 }
925
926 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700927 std::unique_lock<std::mutex> lock(mLock);
928 base::ScopedLockAssertion assumeLocked(mLock);
929 const bool configureCalled =
930 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
931 return mConfigureWasCalled;
932 });
933 if (!configureCalled) {
934 FAIL() << "Expected configure() to have been called.";
935 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 mConfigureWasCalled = false;
937 }
938
939 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700940 std::unique_lock<std::mutex> lock(mLock);
941 base::ScopedLockAssertion assumeLocked(mLock);
942 const bool resetCalled =
943 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
944 return mResetWasCalled;
945 });
946 if (!resetCalled) {
947 FAIL() << "Expected reset() to have been called.";
948 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 mResetWasCalled = false;
950 }
951
Yi Kong9b14ac62018-07-17 13:48:38 -0700952 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700953 std::unique_lock<std::mutex> lock(mLock);
954 base::ScopedLockAssertion assumeLocked(mLock);
955 const bool processCalled =
956 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
957 return mProcessWasCalled;
958 });
959 if (!processCalled) {
960 FAIL() << "Expected process() to have been called.";
961 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962 if (outLastEvent) {
963 *outLastEvent = mLastEvent;
964 }
965 mProcessWasCalled = false;
966 }
967
968 void setKeyCodeState(int32_t keyCode, int32_t state) {
969 mKeyCodeStates.replaceValueFor(keyCode, state);
970 }
971
972 void setScanCodeState(int32_t scanCode, int32_t state) {
973 mScanCodeStates.replaceValueFor(scanCode, state);
974 }
975
976 void setSwitchState(int32_t switchCode, int32_t state) {
977 mSwitchStates.replaceValueFor(switchCode, state);
978 }
979
980 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800981 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 }
983
984private:
Chris Yea52ade12020-08-27 16:49:20 -0700985 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986
Chris Yea52ade12020-08-27 16:49:20 -0700987 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988 InputMapper::populateDeviceInfo(deviceInfo);
989
990 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
991 deviceInfo->setKeyboardType(mKeyboardType);
992 }
993 }
994
Chris Yea52ade12020-08-27 16:49:20 -0700995 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800998
999 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001000 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001001 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1002 mViewport = config->getDisplayViewportByPort(*displayPort);
1003 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001004
1005 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001006 }
1007
Chris Yea52ade12020-08-27 16:49:20 -07001008 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001009 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001011 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012 }
1013
Chris Yea52ade12020-08-27 16:49:20 -07001014 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001015 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016 mLastEvent = *rawEvent;
1017 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001018 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 }
1020
Chris Yea52ade12020-08-27 16:49:20 -07001021 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1023 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1024 }
1025
Chris Yea52ade12020-08-27 16:49:20 -07001026 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001027 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1028 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1029 }
1030
Chris Yea52ade12020-08-27 16:49:20 -07001031 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1033 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1034 }
1035
Chris Yea52ade12020-08-27 16:49:20 -07001036 // Return true if the device has non-empty key layout.
1037 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1038 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 for (size_t i = 0; i < numCodes; i++) {
1040 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1041 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1042 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 }
1044 }
1045 }
Chris Yea52ade12020-08-27 16:49:20 -07001046 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 return result;
1048 }
1049
1050 virtual int32_t getMetaState() {
1051 return mMetaState;
1052 }
1053
1054 virtual void fadePointer() {
1055 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
1057 virtual std::optional<int32_t> getAssociatedDisplay() {
1058 if (mViewport) {
1059 return std::make_optional(mViewport->displayId);
1060 }
1061 return std::nullopt;
1062 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063};
1064
1065
1066// --- InstrumentedInputReader ---
1067
1068class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001069 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070
1071public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001072 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1073 const sp<InputReaderPolicyInterface>& policy,
1074 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001075 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001077 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001079 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001081 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001082 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083 InputDeviceIdentifier identifier;
1084 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001085 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001087 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088 }
1089
Prabir Pradhan28efc192019-11-05 01:10:04 +00001090 // Make the protected loopOnce method accessible to tests.
1091 using InputReader::loopOnce;
1092
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001094 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1095 const InputDeviceIdentifier& identifier)
1096 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001097 if (!mNextDevices.empty()) {
1098 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1099 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 return device;
1101 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001102 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 }
1104
arthurhungdcef2dc2020-08-11 14:47:50 +08001105 // --- FakeInputReaderContext ---
1106 class FakeInputReaderContext : public ContextImpl {
1107 int32_t mGlobalMetaState;
1108 bool mUpdateGlobalMetaStateWasCalled;
1109 int32_t mGeneration;
1110
1111 public:
1112 FakeInputReaderContext(InputReader* reader)
1113 : ContextImpl(reader),
1114 mGlobalMetaState(0),
1115 mUpdateGlobalMetaStateWasCalled(false),
1116 mGeneration(1) {}
1117
1118 virtual ~FakeInputReaderContext() {}
1119
1120 void assertUpdateGlobalMetaStateWasCalled() {
1121 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1122 << "Expected updateGlobalMetaState() to have been called.";
1123 mUpdateGlobalMetaStateWasCalled = false;
1124 }
1125
1126 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1127
1128 uint32_t getGeneration() { return mGeneration; }
1129
1130 void updateGlobalMetaState() override {
1131 mUpdateGlobalMetaStateWasCalled = true;
1132 ContextImpl::updateGlobalMetaState();
1133 }
1134
1135 int32_t getGlobalMetaState() override {
1136 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1137 }
1138
1139 int32_t bumpGeneration() override {
1140 mGeneration = ContextImpl::bumpGeneration();
1141 return mGeneration;
1142 }
1143 } mFakeContext;
1144
Michael Wrightd02c5b62014-02-10 15:10:22 -08001145 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001146
1147public:
1148 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149};
1150
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001151// --- InputReaderPolicyTest ---
1152class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001153protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001154 sp<FakeInputReaderPolicy> mFakePolicy;
1155
Chris Yea52ade12020-08-27 16:49:20 -07001156 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1157 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001158};
1159
1160/**
1161 * Check that empty set of viewports is an acceptable configuration.
1162 * Also try to get internal viewport two different ways - by type and by uniqueId.
1163 *
1164 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1165 * Such configuration is not currently allowed.
1166 */
1167TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001168 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001169
1170 // We didn't add any viewports yet, so there shouldn't be any.
1171 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001172 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173 ASSERT_FALSE(internalViewport);
1174
1175 // Add an internal viewport, then clear it
1176 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001177 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001178 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179
1180 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001181 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001182 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001183 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001184
1185 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001186 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001188 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001189
1190 mFakePolicy->clearViewports();
1191 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001192 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001193 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001194 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001195 ASSERT_FALSE(internalViewport);
1196}
1197
1198TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1199 const std::string internalUniqueId = "local:0";
1200 const std::string externalUniqueId = "local:1";
1201 const std::string virtualUniqueId1 = "virtual:2";
1202 const std::string virtualUniqueId2 = "virtual:3";
1203 constexpr int32_t virtualDisplayId1 = 2;
1204 constexpr int32_t virtualDisplayId2 = 3;
1205
1206 // Add an internal viewport
1207 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001208 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1209 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 // Add an external viewport
1211 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001212 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1213 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 // Add an virtual viewport
1215 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001216 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1217 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001218 // Add another virtual viewport
1219 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001220 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1221 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222
1223 // Check matching by type for internal
1224 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001225 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001226 ASSERT_TRUE(internalViewport);
1227 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1228
1229 // Check matching by type for external
1230 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001231 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001232 ASSERT_TRUE(externalViewport);
1233 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1234
1235 // Check matching by uniqueId for virtual viewport #1
1236 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001237 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001239 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1241 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1242
1243 // Check matching by uniqueId for virtual viewport #2
1244 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001245 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001247 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001248 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1249 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1250}
1251
1252
1253/**
1254 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1255 * that lookup works by checking display id.
1256 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1257 */
1258TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1259 const std::string uniqueId1 = "uniqueId1";
1260 const std::string uniqueId2 = "uniqueId2";
1261 constexpr int32_t displayId1 = 2;
1262 constexpr int32_t displayId2 = 3;
1263
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001264 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1265 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001266 for (const ViewportType& type : types) {
1267 mFakePolicy->clearViewports();
1268 // Add a viewport
1269 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001270 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1271 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272 // Add another viewport
1273 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001274 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1275 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001276
1277 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001278 std::optional<DisplayViewport> viewport1 =
1279 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001280 ASSERT_TRUE(viewport1);
1281 ASSERT_EQ(displayId1, viewport1->displayId);
1282 ASSERT_EQ(type, viewport1->type);
1283
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001284 std::optional<DisplayViewport> viewport2 =
1285 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001286 ASSERT_TRUE(viewport2);
1287 ASSERT_EQ(displayId2, viewport2->displayId);
1288 ASSERT_EQ(type, viewport2->type);
1289
1290 // When there are multiple viewports of the same kind, and uniqueId is not specified
1291 // in the call to getDisplayViewport, then that situation is not supported.
1292 // The viewports can be stored in any order, so we cannot rely on the order, since that
1293 // is just implementation detail.
1294 // However, we can check that it still returns *a* viewport, we just cannot assert
1295 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001296 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001297 ASSERT_TRUE(someViewport);
1298 }
1299}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001300
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001301/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001302 * When we have multiple internal displays make sure we always return the default display when
1303 * querying by type.
1304 */
1305TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1306 const std::string uniqueId1 = "uniqueId1";
1307 const std::string uniqueId2 = "uniqueId2";
1308 constexpr int32_t nonDefaultDisplayId = 2;
1309 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1310 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1311
1312 // Add the default display first and ensure it gets returned.
1313 mFakePolicy->clearViewports();
1314 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001315 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001316 ViewportType::INTERNAL);
1317 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001318 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001319 ViewportType::INTERNAL);
1320
1321 std::optional<DisplayViewport> viewport =
1322 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1323 ASSERT_TRUE(viewport);
1324 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1325 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1326
1327 // Add the default display second to make sure order doesn't matter.
1328 mFakePolicy->clearViewports();
1329 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001330 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001331 ViewportType::INTERNAL);
1332 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001333 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001334 ViewportType::INTERNAL);
1335
1336 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1337 ASSERT_TRUE(viewport);
1338 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1339 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1340}
1341
1342/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001343 * Check getDisplayViewportByPort
1344 */
1345TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001346 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001347 const std::string uniqueId1 = "uniqueId1";
1348 const std::string uniqueId2 = "uniqueId2";
1349 constexpr int32_t displayId1 = 1;
1350 constexpr int32_t displayId2 = 2;
1351 const uint8_t hdmi1 = 0;
1352 const uint8_t hdmi2 = 1;
1353 const uint8_t hdmi3 = 2;
1354
1355 mFakePolicy->clearViewports();
1356 // Add a viewport that's associated with some display port that's not of interest.
1357 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001358 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1359 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001360 // Add another viewport, connected to HDMI1 port
1361 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001362 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1363 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001364
1365 // Check that correct display viewport was returned by comparing the display ports.
1366 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1367 ASSERT_TRUE(hdmi1Viewport);
1368 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1369 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1370
1371 // Check that we can still get the same viewport using the uniqueId
1372 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1373 ASSERT_TRUE(hdmi1Viewport);
1374 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1375 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1376 ASSERT_EQ(type, hdmi1Viewport->type);
1377
1378 // Check that we cannot find a port with "HDMI2", because we never added one
1379 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1380 ASSERT_FALSE(hdmi2Viewport);
1381}
1382
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383// --- InputReaderTest ---
1384
1385class InputReaderTest : public testing::Test {
1386protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001387 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001388 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001389 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001390 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391
Chris Yea52ade12020-08-27 16:49:20 -07001392 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001393 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001395 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001396
Prabir Pradhan28efc192019-11-05 01:10:04 +00001397 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1398 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399 }
1400
Chris Yea52ade12020-08-27 16:49:20 -07001401 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001402 mFakeListener.clear();
1403 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001404 }
1405
Chris Ye1b0c7342020-07-28 21:57:03 -07001406 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001407 const PropertyMap* configuration) {
1408 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001409
1410 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001411 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001412 }
1413 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001414 mReader->loopOnce();
1415 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001416 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1417 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 }
1419
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001420 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001421 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001422 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001423 }
1424
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001425 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001426 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001427 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001428 }
1429
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001430 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001431 const std::string& name,
1432 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001433 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001434 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1435 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001436 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001437 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001438 return mapper;
1439 }
1440};
1441
Chris Ye98d3f532020-10-01 21:48:59 -07001442TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001443 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1444 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1445 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001446
Chris Ye98d3f532020-10-01 21:48:59 -07001447 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001448 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001449 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001450 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001451 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1453 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001454}
1455
1456TEST_F(InputReaderTest, PolicyGetInputDevices) {
1457 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1458 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1459 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001460
1461 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001462 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001463 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001464 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001465 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001466 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1467 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1468 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1469}
1470
Chris Yee7310032020-09-22 15:36:28 -07001471TEST_F(InputReaderTest, GetMergedInputDevices) {
1472 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1473 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1474 // Add two subdevices to device
1475 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1476 // Must add at least one mapper or the device will be ignored!
1477 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1478 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1479
1480 // Push same device instance for next device to be added, so they'll have same identifier.
1481 mReader->pushNextDevice(device);
1482 mReader->pushNextDevice(device);
1483 ASSERT_NO_FATAL_FAILURE(
1484 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1485 ASSERT_NO_FATAL_FAILURE(
1486 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1487
1488 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001489 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001490}
1491
Chris Yee14523a2020-12-19 13:46:00 -08001492TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1493 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1494 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1495 // Add two subdevices to device
1496 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1497 // Must add at least one mapper or the device will be ignored!
1498 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1499 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1500
1501 // Push same device instance for next device to be added, so they'll have same identifier.
1502 mReader->pushNextDevice(device);
1503 mReader->pushNextDevice(device);
1504 // Sensor device is initially disabled
1505 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1506 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1507 nullptr));
1508 // Device is disabled because the only sub device is a sensor device and disabled initially.
1509 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1510 ASSERT_FALSE(device->isEnabled());
1511 ASSERT_NO_FATAL_FAILURE(
1512 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1513 // The merged device is enabled if any sub device is enabled
1514 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1515 ASSERT_TRUE(device->isEnabled());
1516}
1517
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001518TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001520 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001521 constexpr int32_t eventHubId = 1;
1522 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001523 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001525 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001526 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001527
Yi Kong9b14ac62018-07-17 13:48:38 -07001528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001529
1530 NotifyDeviceResetArgs resetArgs;
1531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001532 ASSERT_EQ(deviceId, resetArgs.deviceId);
1533
1534 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001535 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001536 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001537
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001539 ASSERT_EQ(deviceId, resetArgs.deviceId);
1540 ASSERT_EQ(device->isEnabled(), false);
1541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001542 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001543 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001546 ASSERT_EQ(device->isEnabled(), false);
1547
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001548 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001549 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001551 ASSERT_EQ(deviceId, resetArgs.deviceId);
1552 ASSERT_EQ(device->isEnabled(), true);
1553}
1554
Michael Wrightd02c5b62014-02-10 15:10:22 -08001555TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001556 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001557 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001558 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001559 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001560 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001561 AINPUT_SOURCE_KEYBOARD, nullptr);
1562 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563
1564 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1565 AINPUT_SOURCE_ANY, AKEYCODE_A))
1566 << "Should return unknown when the device id is >= 0 but unknown.";
1567
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001568 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1569 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1570 << "Should return unknown when the device id is valid but the sources are not "
1571 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001573 ASSERT_EQ(AKEY_STATE_DOWN,
1574 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1575 AKEYCODE_A))
1576 << "Should return value provided by mapper when device id is valid and the device "
1577 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578
1579 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1580 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1581 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1582
1583 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1584 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1585 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1586}
1587
1588TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001589 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001590 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001592 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001593 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001594 AINPUT_SOURCE_KEYBOARD, nullptr);
1595 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596
1597 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1598 AINPUT_SOURCE_ANY, KEY_A))
1599 << "Should return unknown when the device id is >= 0 but unknown.";
1600
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001601 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1602 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1603 << "Should return unknown when the device id is valid but the sources are not "
1604 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001605
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 ASSERT_EQ(AKEY_STATE_DOWN,
1607 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1608 KEY_A))
1609 << "Should return value provided by mapper when device id is valid and the device "
1610 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001611
1612 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1613 AINPUT_SOURCE_TRACKBALL, KEY_A))
1614 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1615
1616 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1617 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1618 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1619}
1620
1621TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001622 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001623 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001624 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001625 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 AINPUT_SOURCE_KEYBOARD, nullptr);
1628 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629
1630 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1631 AINPUT_SOURCE_ANY, SW_LID))
1632 << "Should return unknown when the device id is >= 0 but unknown.";
1633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1635 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1636 << "Should return unknown when the device id is valid but the sources are not "
1637 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001638
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 ASSERT_EQ(AKEY_STATE_DOWN,
1640 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1641 SW_LID))
1642 << "Should return value provided by mapper when device id is valid and the device "
1643 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644
1645 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1646 AINPUT_SOURCE_TRACKBALL, SW_LID))
1647 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1648
1649 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1650 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1651 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1652}
1653
1654TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001656 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001657 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001658 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001659 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001660 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001661
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 mapper.addSupportedKeyCode(AKEYCODE_A);
1663 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664
1665 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1666 uint8_t flags[4] = { 0, 0, 0, 1 };
1667
1668 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1669 << "Should return false when device id is >= 0 but unknown.";
1670 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1671
1672 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001673 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1674 << "Should return false when device id is valid but the sources are not supported by "
1675 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001676 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1677
1678 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001679 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1680 keyCodes, flags))
1681 << "Should return value provided by mapper when device id is valid and the device "
1682 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1684
1685 flags[3] = 1;
1686 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1687 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1688 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1689
1690 flags[3] = 1;
1691 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1692 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1693 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1694}
1695
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001696TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001697 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001698 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001699
1700 NotifyConfigurationChangedArgs args;
1701
1702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1703 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1704}
1705
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001706TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001708 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001709 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001711 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001712 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001714 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001715
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001716 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001717 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1719
1720 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001721 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001722 ASSERT_EQ(when, event.when);
1723 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001725 ASSERT_EQ(EV_KEY, event.type);
1726 ASSERT_EQ(KEY_A, event.code);
1727 ASSERT_EQ(1, event.value);
1728}
1729
Garfield Tan1c7bc862020-01-28 13:24:04 -08001730TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001732 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001733 constexpr int32_t eventHubId = 1;
1734 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001735 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001737 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001739
1740 NotifyDeviceResetArgs resetArgs;
1741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001742 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001743
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001744 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001745 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001747 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001748 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001749
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001750 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001751 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001753 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001754 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001755
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001756 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001757 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001759 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001760 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001761}
1762
Garfield Tan1c7bc862020-01-28 13:24:04 -08001763TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1764 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001765 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001766 constexpr int32_t eventHubId = 1;
1767 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1768 // Must add at least one mapper or the device will be ignored!
1769 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001770 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001771 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1772
1773 NotifyDeviceResetArgs resetArgs;
1774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1775 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1776}
1777
Arthur Hungc23540e2018-11-29 20:42:11 +08001778TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001780 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001782 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001783 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1784 FakeInputMapper& mapper =
1785 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001786 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001787
1788 const uint8_t hdmi1 = 1;
1789
1790 // Associated touch screen with second display.
1791 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1792
1793 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001794 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001795 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001796 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001797 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001798 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001799 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001800 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001801 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001802 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001803
1804 // Add the device, and make sure all of the callbacks are triggered.
1805 // The device is added after the input port associations are processed since
1806 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001807 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001810 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001811
Arthur Hung2c9a3342019-07-23 14:18:59 +08001812 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001813 ASSERT_EQ(deviceId, device->getId());
1814 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1815 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001816
1817 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001818 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001819 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001820 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001821}
1822
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001823TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1824 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1825 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1826 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1827 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1828 // Must add at least one mapper or the device will be ignored!
1829 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1830 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1831 mReader->pushNextDevice(device);
1832 mReader->pushNextDevice(device);
1833 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1834 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1835
1836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1837
1838 NotifyDeviceResetArgs resetArgs;
1839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1840 ASSERT_EQ(deviceId, resetArgs.deviceId);
1841 ASSERT_TRUE(device->isEnabled());
1842 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1843 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1844
1845 disableDevice(deviceId);
1846 mReader->loopOnce();
1847
1848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1849 ASSERT_EQ(deviceId, resetArgs.deviceId);
1850 ASSERT_FALSE(device->isEnabled());
1851 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1852 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1853
1854 enableDevice(deviceId);
1855 mReader->loopOnce();
1856
1857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1858 ASSERT_EQ(deviceId, resetArgs.deviceId);
1859 ASSERT_TRUE(device->isEnabled());
1860 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1861 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1862}
1863
1864TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1865 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1866 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1867 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1868 // Add two subdevices to device
1869 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1870 FakeInputMapper& mapperDevice1 =
1871 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1872 FakeInputMapper& mapperDevice2 =
1873 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1874 mReader->pushNextDevice(device);
1875 mReader->pushNextDevice(device);
1876 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1877 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1878
1879 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1880 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1881
1882 ASSERT_EQ(AKEY_STATE_DOWN,
1883 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1884 ASSERT_EQ(AKEY_STATE_DOWN,
1885 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1886 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1887 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1888}
1889
Prabir Pradhan7e186182020-11-10 13:56:45 -08001890TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1891 NotifyPointerCaptureChangedArgs args;
1892
1893 mFakePolicy->setPointerCapture(true);
1894 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1895 mReader->loopOnce();
1896 mFakeListener->assertNotifyCaptureWasCalled(&args);
1897 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1898
1899 mFakePolicy->setPointerCapture(false);
1900 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1901 mReader->loopOnce();
1902 mFakeListener->assertNotifyCaptureWasCalled(&args);
1903 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1904
1905 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1906 // does not change.
1907 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1908 mReader->loopOnce();
1909 mFakeListener->assertNotifyCaptureWasCalled(&args);
1910 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1911}
1912
Chris Ye87143712020-11-10 05:05:58 +00001913class FakeVibratorInputMapper : public FakeInputMapper {
1914public:
1915 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1916 : FakeInputMapper(deviceContext, sources) {}
1917
1918 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1919};
1920
1921TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1922 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1923 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1924 constexpr int32_t eventHubId = 1;
1925 const char* DEVICE_LOCATION = "BLUETOOTH";
1926 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1927 FakeVibratorInputMapper& mapper =
1928 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1929 mReader->pushNextDevice(device);
1930
1931 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1932 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1933
1934 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1935 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1936}
1937
Kim Low03ea0352020-11-06 12:45:07 -08001938class FakeBatteryInputMapper : public FakeInputMapper {
1939public:
1940 FakeBatteryInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1941 : FakeInputMapper(deviceContext, sources) {}
1942
1943 std::optional<int32_t> getBatteryCapacity() override {
1944 return getDeviceContext().getBatteryCapacity();
1945 }
1946
1947 std::optional<int32_t> getBatteryStatus() override {
1948 return getDeviceContext().getBatteryStatus();
1949 }
1950};
1951
1952TEST_F(InputReaderTest, BatteryGetCapacity) {
1953 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1954 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1955 constexpr int32_t eventHubId = 1;
1956 const char* DEVICE_LOCATION = "BLUETOOTH";
1957 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1958 FakeBatteryInputMapper& mapper =
1959 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1960 mReader->pushNextDevice(device);
1961
1962 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1963 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1964
1965 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
1966}
1967
1968TEST_F(InputReaderTest, BatteryGetStatus) {
1969 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1970 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1971 constexpr int32_t eventHubId = 1;
1972 const char* DEVICE_LOCATION = "BLUETOOTH";
1973 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1974 FakeBatteryInputMapper& mapper =
1975 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1976 mReader->pushNextDevice(device);
1977
1978 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1979 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1980
1981 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
1982}
1983
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001984// --- InputReaderIntegrationTest ---
1985
1986// These tests create and interact with the InputReader only through its interface.
1987// The InputReader is started during SetUp(), which starts its processing in its own
1988// thread. The tests use linux uinput to emulate input devices.
1989// NOTE: Interacting with the physical device while these tests are running may cause
1990// the tests to fail.
1991class InputReaderIntegrationTest : public testing::Test {
1992protected:
1993 sp<TestInputListener> mTestListener;
1994 sp<FakeInputReaderPolicy> mFakePolicy;
1995 sp<InputReaderInterface> mReader;
1996
Chris Yea52ade12020-08-27 16:49:20 -07001997 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001998 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001999 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2000 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002001
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002002 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002003 ASSERT_EQ(mReader->start(), OK);
2004
2005 // Since this test is run on a real device, all the input devices connected
2006 // to the test device will show up in mReader. We wait for those input devices to
2007 // show up before beginning the tests.
2008 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2009 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2010 }
2011
Chris Yea52ade12020-08-27 16:49:20 -07002012 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002013 ASSERT_EQ(mReader->stop(), OK);
2014 mTestListener.clear();
2015 mFakePolicy.clear();
2016 }
2017};
2018
2019TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2020 // An invalid input device that is only used for this test.
2021 class InvalidUinputDevice : public UinputDevice {
2022 public:
2023 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2024
2025 private:
2026 void configureDevice(int fd, uinput_user_dev* device) override {}
2027 };
2028
2029 const size_t numDevices = mFakePolicy->getInputDevices().size();
2030
2031 // UinputDevice does not set any event or key bits, so InputReader should not
2032 // consider it as a valid device.
2033 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2034 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2035 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2036 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2037
2038 invalidDevice.reset();
2039 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2040 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2041 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2042}
2043
2044TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2045 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2046
2047 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2048 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2049 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2050 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2051
2052 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002053 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2054 const auto& it =
2055 std::find_if(inputDevices.begin(), inputDevices.end(),
2056 [&keyboard](const InputDeviceInfo& info) {
2057 return info.getIdentifier().name == keyboard->getName();
2058 });
2059
2060 ASSERT_NE(it, inputDevices.end());
2061 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2062 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2063 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002064
2065 keyboard.reset();
2066 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2067 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2068 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2069}
2070
2071TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2072 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2073 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2074
2075 NotifyConfigurationChangedArgs configChangedArgs;
2076 ASSERT_NO_FATAL_FAILURE(
2077 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002078 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002079 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2080
2081 NotifyKeyArgs keyArgs;
2082 keyboard->pressAndReleaseHomeKey();
2083 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2084 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002085 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002086 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002087 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002088 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002089 prevTimestamp = keyArgs.eventTime;
2090
2091 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2092 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002093 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002094 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002095 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002096}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002097
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002098/**
2099 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2100 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2101 * are passed to the listener.
2102 */
2103static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2104TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2105 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2106 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2107 NotifyKeyArgs keyArgs;
2108
2109 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2110 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2111 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2112 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2113
2114 controller->pressAndReleaseKey(BTN_GEAR_UP);
2115 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2116 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2117 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2118}
2119
Arthur Hungaab25622020-01-16 11:22:11 +08002120// --- TouchProcessTest ---
2121class TouchIntegrationTest : public InputReaderIntegrationTest {
2122protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002123 const std::string UNIQUE_ID = "local:0";
2124
Chris Yea52ade12020-08-27 16:49:20 -07002125 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002126 InputReaderIntegrationTest::SetUp();
2127 // At least add an internal display.
2128 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2129 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002130 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002131
2132 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2133 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2134 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2135 }
2136
2137 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2138 int32_t orientation, const std::string& uniqueId,
2139 std::optional<uint8_t> physicalPort,
2140 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002141 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2142 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002143 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2144 }
2145
2146 std::unique_ptr<UinputTouchScreen> mDevice;
2147};
2148
2149TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2150 NotifyMotionArgs args;
2151 const Point centerPoint = mDevice->getCenterPoint();
2152
2153 // ACTION_DOWN
2154 mDevice->sendDown(centerPoint);
2155 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2156 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2157
2158 // ACTION_MOVE
2159 mDevice->sendMove(centerPoint + Point(1, 1));
2160 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2162
2163 // ACTION_UP
2164 mDevice->sendUp();
2165 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2166 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2167}
2168
2169TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2170 NotifyMotionArgs args;
2171 const Point centerPoint = mDevice->getCenterPoint();
2172
2173 // ACTION_DOWN
2174 mDevice->sendDown(centerPoint);
2175 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2177
2178 // ACTION_POINTER_DOWN (Second slot)
2179 const Point secondPoint = centerPoint + Point(100, 100);
2180 mDevice->sendSlot(SECOND_SLOT);
2181 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2182 mDevice->sendDown(secondPoint + Point(1, 1));
2183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2184 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2185 args.action);
2186
2187 // ACTION_MOVE (Second slot)
2188 mDevice->sendMove(secondPoint);
2189 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2191
2192 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002193 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002195 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002196 args.action);
2197
2198 // ACTION_UP
2199 mDevice->sendSlot(FIRST_SLOT);
2200 mDevice->sendUp();
2201 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2202 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2203}
2204
2205TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2206 NotifyMotionArgs args;
2207 const Point centerPoint = mDevice->getCenterPoint();
2208
2209 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002210 mDevice->sendSlot(FIRST_SLOT);
2211 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002212 mDevice->sendDown(centerPoint);
2213 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2214 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2215
arthurhungcc7f9802020-04-30 17:55:40 +08002216 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002217 const Point secondPoint = centerPoint + Point(100, 100);
2218 mDevice->sendSlot(SECOND_SLOT);
2219 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2220 mDevice->sendDown(secondPoint);
2221 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2222 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2223 args.action);
2224
arthurhungcc7f9802020-04-30 17:55:40 +08002225 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002226 mDevice->sendMove(secondPoint + Point(1, 1));
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2229
arthurhungcc7f9802020-04-30 17:55:40 +08002230 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2231 // a palm event.
2232 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002233 mDevice->sendToolType(MT_TOOL_PALM);
2234 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002235 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2236 args.action);
2237 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002238
arthurhungcc7f9802020-04-30 17:55:40 +08002239 // Send up to second slot, expect first slot send moving.
2240 mDevice->sendPointerUp();
2241 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002243
arthurhungcc7f9802020-04-30 17:55:40 +08002244 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002245 mDevice->sendSlot(FIRST_SLOT);
2246 mDevice->sendUp();
2247
arthurhungcc7f9802020-04-30 17:55:40 +08002248 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2249 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002250}
2251
Michael Wrightd02c5b62014-02-10 15:10:22 -08002252// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002253class InputDeviceTest : public testing::Test {
2254protected:
2255 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002256 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257 static const int32_t DEVICE_ID;
2258 static const int32_t DEVICE_GENERATION;
2259 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002260 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002261 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002263 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002265 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002266 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002267 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268
Chris Yea52ade12020-08-27 16:49:20 -07002269 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002270 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002271 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002272 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002273 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2274 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002275 InputDeviceIdentifier identifier;
2276 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002277 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002278 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002279 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002280 mReader->pushNextDevice(mDevice);
2281 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2282 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283 }
2284
Chris Yea52ade12020-08-27 16:49:20 -07002285 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 mFakeListener.clear();
2287 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 }
2289};
2290
2291const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002292const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002293const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002294const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2295const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002296const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2297 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002298const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002299
2300TEST_F(InputDeviceTest, ImmutableProperties) {
2301 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002302 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002303 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002304}
2305
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002306TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2307 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002308}
2309
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2311 // Configuration.
2312 InputReaderConfiguration config;
2313 mDevice->configure(ARBITRARY_TIME, &config, 0);
2314
2315 // Reset.
2316 mDevice->reset(ARBITRARY_TIME);
2317
2318 NotifyDeviceResetArgs resetArgs;
2319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2320 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2321 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2322
2323 // Metadata.
2324 ASSERT_TRUE(mDevice->isIgnored());
2325 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2326
2327 InputDeviceInfo info;
2328 mDevice->getDeviceInfo(&info);
2329 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002330 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002331 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2332 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2333
2334 // State queries.
2335 ASSERT_EQ(0, mDevice->getMetaState());
2336
2337 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2338 << "Ignored device should return unknown key code state.";
2339 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2340 << "Ignored device should return unknown scan code state.";
2341 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2342 << "Ignored device should return unknown switch state.";
2343
2344 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2345 uint8_t flags[2] = { 0, 1 };
2346 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2347 << "Ignored device should never mark any key codes.";
2348 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2349 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2350}
2351
2352TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2353 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002354 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002356 FakeInputMapper& mapper1 =
2357 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002358 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2359 mapper1.setMetaState(AMETA_ALT_ON);
2360 mapper1.addSupportedKeyCode(AKEYCODE_A);
2361 mapper1.addSupportedKeyCode(AKEYCODE_B);
2362 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2363 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2364 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2365 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2366 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002367
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002368 FakeInputMapper& mapper2 =
2369 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002370 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371
2372 InputReaderConfiguration config;
2373 mDevice->configure(ARBITRARY_TIME, &config, 0);
2374
2375 String8 propertyValue;
2376 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2377 << "Device should have read configuration during configuration phase.";
2378 ASSERT_STREQ("value", propertyValue.string());
2379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002380 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2381 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382
2383 // Reset
2384 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002385 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2386 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387
2388 NotifyDeviceResetArgs resetArgs;
2389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2390 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2391 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2392
2393 // Metadata.
2394 ASSERT_FALSE(mDevice->isIgnored());
2395 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2396
2397 InputDeviceInfo info;
2398 mDevice->getDeviceInfo(&info);
2399 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002400 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2402 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2403
2404 // State queries.
2405 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2406 << "Should query mappers and combine meta states.";
2407
2408 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2409 << "Should return unknown key code state when source not supported.";
2410 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2411 << "Should return unknown scan code state when source not supported.";
2412 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2413 << "Should return unknown switch state when source not supported.";
2414
2415 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2416 << "Should query mapper when source is supported.";
2417 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2418 << "Should query mapper when source is supported.";
2419 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2420 << "Should query mapper when source is supported.";
2421
2422 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2423 uint8_t flags[4] = { 0, 0, 0, 1 };
2424 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2425 << "Should do nothing when source is unsupported.";
2426 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2427 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2428 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2429 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2430
2431 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2432 << "Should query mapper when source is supported.";
2433 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2434 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2435 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2436 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2437
2438 // Event handling.
2439 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002440 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441 mDevice->process(&event, 1);
2442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002443 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2444 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445}
2446
Arthur Hung2c9a3342019-07-23 14:18:59 +08002447// A single input device is associated with a specific display. Check that:
2448// 1. Device is disabled if the viewport corresponding to the associated display is not found
2449// 2. Device is disabled when setEnabled API is called
2450TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002451 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002452
2453 // First Configuration.
2454 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2455
2456 // Device should be enabled by default.
2457 ASSERT_TRUE(mDevice->isEnabled());
2458
2459 // Prepare associated info.
2460 constexpr uint8_t hdmi = 1;
2461 const std::string UNIQUE_ID = "local:1";
2462
2463 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2464 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2465 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2466 // Device should be disabled because it is associated with a specific display via
2467 // input port <-> display port association, but the corresponding display is not found
2468 ASSERT_FALSE(mDevice->isEnabled());
2469
2470 // Prepare displays.
2471 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002472 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2473 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002474 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2475 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2476 ASSERT_TRUE(mDevice->isEnabled());
2477
2478 // Device should be disabled after set disable.
2479 mFakePolicy->addDisabledDevice(mDevice->getId());
2480 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2481 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2482 ASSERT_FALSE(mDevice->isEnabled());
2483
2484 // Device should still be disabled even found the associated display.
2485 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2486 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2487 ASSERT_FALSE(mDevice->isEnabled());
2488}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489
2490// --- InputMapperTest ---
2491
2492class InputMapperTest : public testing::Test {
2493protected:
2494 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002495 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496 static const int32_t DEVICE_ID;
2497 static const int32_t DEVICE_GENERATION;
2498 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002499 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002500 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002501
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002502 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002503 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002504 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002505 std::unique_ptr<InstrumentedInputReader> mReader;
2506 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507
Chris Ye1b0c7342020-07-28 21:57:03 -07002508 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002509 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002510 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002511 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002512 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2513 mFakeListener);
2514 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 }
2516
Chris Yea52ade12020-08-27 16:49:20 -07002517 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002518
Chris Yea52ade12020-08-27 16:49:20 -07002519 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002520 mFakeListener.clear();
2521 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522 }
2523
2524 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002525 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526 }
2527
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002528 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002529 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002530 mReader->requestRefreshConfiguration(changes);
2531 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002532 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002533 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2534 }
2535
arthurhungdcef2dc2020-08-11 14:47:50 +08002536 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2537 const std::string& location, int32_t eventHubId,
2538 Flags<InputDeviceClass> classes) {
2539 InputDeviceIdentifier identifier;
2540 identifier.name = name;
2541 identifier.location = location;
2542 std::shared_ptr<InputDevice> device =
2543 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2544 identifier);
2545 mReader->pushNextDevice(device);
2546 mFakeEventHub->addDevice(eventHubId, name, classes);
2547 mReader->loopOnce();
2548 return device;
2549 }
2550
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002551 template <class T, typename... Args>
2552 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002553 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002554 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002556 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002557 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558 }
2559
2560 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002561 int32_t orientation, const std::string& uniqueId,
2562 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002563 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2564 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002565 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2566 }
2567
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002568 void clearViewports() {
2569 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570 }
2571
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002572 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2573 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574 RawEvent event;
2575 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002576 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002577 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002578 event.type = type;
2579 event.code = code;
2580 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002581 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002582 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 }
2584
2585 static void assertMotionRange(const InputDeviceInfo& info,
2586 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2587 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002588 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2590 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2591 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2592 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2593 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2594 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2595 }
2596
2597 static void assertPointerCoords(const PointerCoords& coords,
2598 float x, float y, float pressure, float size,
2599 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2600 float orientation, float distance) {
2601 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2602 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2603 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2604 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2605 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2606 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2607 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2608 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2609 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2610 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2611 }
2612
Michael Wright17db18e2020-06-26 20:51:44 +01002613 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002615 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 ASSERT_NEAR(x, actualX, 1);
2617 ASSERT_NEAR(y, actualY, 1);
2618 }
2619};
2620
2621const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002622const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002623const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2625const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002626const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2627 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002628const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002629
2630// --- SwitchInputMapperTest ---
2631
2632class SwitchInputMapperTest : public InputMapperTest {
2633protected:
2634};
2635
2636TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002637 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002639 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640}
2641
2642TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002643 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002645 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002646 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002648 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002649 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650}
2651
2652TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002653 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659
2660 NotifySwitchArgs args;
2661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2662 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002663 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2664 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 args.switchMask);
2666 ASSERT_EQ(uint32_t(0), args.policyFlags);
2667}
2668
Chris Ye87143712020-11-10 05:05:58 +00002669// --- VibratorInputMapperTest ---
2670class VibratorInputMapperTest : public InputMapperTest {
2671protected:
2672 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2673};
2674
2675TEST_F(VibratorInputMapperTest, GetSources) {
2676 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2677
2678 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2679}
2680
2681TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2682 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2683
2684 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2685}
2686
2687TEST_F(VibratorInputMapperTest, Vibrate) {
2688 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002689 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002690 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2691
2692 VibrationElement pattern(2);
2693 VibrationSequence sequence(2);
2694 pattern.duration = std::chrono::milliseconds(200);
2695 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2696 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2697 sequence.addElement(pattern);
2698 pattern.duration = std::chrono::milliseconds(500);
2699 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2700 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2701 sequence.addElement(pattern);
2702
2703 std::vector<int64_t> timings = {0, 1};
2704 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2705
2706 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002707 // Start vibrating
2708 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002709 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002710 // Verify vibrator state listener was notified.
2711 mReader->loopOnce();
2712 NotifyVibratorStateArgs args;
2713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2714 ASSERT_EQ(DEVICE_ID, args.deviceId);
2715 ASSERT_TRUE(args.isOn);
2716 // Stop vibrating
2717 mapper.cancelVibrate(VIBRATION_TOKEN);
2718 ASSERT_FALSE(mapper.isVibrating());
2719 // Verify vibrator state listener was notified.
2720 mReader->loopOnce();
2721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2722 ASSERT_EQ(DEVICE_ID, args.deviceId);
2723 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002724}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725
Chris Yef59a2f42020-10-16 12:55:26 -07002726// --- SensorInputMapperTest ---
2727
2728class SensorInputMapperTest : public InputMapperTest {
2729protected:
2730 static const int32_t ACCEL_RAW_MIN;
2731 static const int32_t ACCEL_RAW_MAX;
2732 static const int32_t ACCEL_RAW_FUZZ;
2733 static const int32_t ACCEL_RAW_FLAT;
2734 static const int32_t ACCEL_RAW_RESOLUTION;
2735
2736 static const int32_t GYRO_RAW_MIN;
2737 static const int32_t GYRO_RAW_MAX;
2738 static const int32_t GYRO_RAW_FUZZ;
2739 static const int32_t GYRO_RAW_FLAT;
2740 static const int32_t GYRO_RAW_RESOLUTION;
2741
2742 static const float GRAVITY_MS2_UNIT;
2743 static const float DEGREE_RADIAN_UNIT;
2744
2745 void prepareAccelAxes();
2746 void prepareGyroAxes();
2747 void setAccelProperties();
2748 void setGyroProperties();
2749 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2750};
2751
2752const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2753const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2754const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2755const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2756const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2757
2758const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2759const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2760const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2761const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2762const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2763
2764const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2765const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2766
2767void SensorInputMapperTest::prepareAccelAxes() {
2768 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2769 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2770 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2771 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2772 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2773 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2774}
2775
2776void SensorInputMapperTest::prepareGyroAxes() {
2777 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2778 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2779 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2780 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2781 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2782 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2783}
2784
2785void SensorInputMapperTest::setAccelProperties() {
2786 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2787 /* sensorDataIndex */ 0);
2788 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2789 /* sensorDataIndex */ 1);
2790 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2791 /* sensorDataIndex */ 2);
2792 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2793 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2794 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2795 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2796 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2797}
2798
2799void SensorInputMapperTest::setGyroProperties() {
2800 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2801 /* sensorDataIndex */ 0);
2802 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2803 /* sensorDataIndex */ 1);
2804 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2805 /* sensorDataIndex */ 2);
2806 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2807 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2808 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2809 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2810 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2811}
2812
2813TEST_F(SensorInputMapperTest, GetSources) {
2814 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2815
2816 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2817}
2818
2819TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2820 setAccelProperties();
2821 prepareAccelAxes();
2822 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2823
2824 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2825 std::chrono::microseconds(10000),
2826 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002827 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002833
2834 NotifySensorArgs args;
2835 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2836 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2837 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2838
2839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2840 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2841 ASSERT_EQ(args.deviceId, DEVICE_ID);
2842 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2843 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2844 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2845 ASSERT_EQ(args.values, values);
2846 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2847}
2848
2849TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2850 setGyroProperties();
2851 prepareGyroAxes();
2852 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2853
2854 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2855 std::chrono::microseconds(10000),
2856 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002857 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002863
2864 NotifySensorArgs args;
2865 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2866 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2867 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2868
2869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2870 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2871 ASSERT_EQ(args.deviceId, DEVICE_ID);
2872 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2873 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2874 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2875 ASSERT_EQ(args.values, values);
2876 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2877}
2878
Kim Low03ea0352020-11-06 12:45:07 -08002879// --- BatteryInputMapperTest ---
2880class BatteryInputMapperTest : public InputMapperTest {
2881protected:
2882 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY); }
2883};
2884
2885TEST_F(BatteryInputMapperTest, GetSources) {
2886 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2887
2888 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2889}
2890
2891TEST_F(BatteryInputMapperTest, GetBatteryCapacity) {
2892 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2893
2894 ASSERT_TRUE(mapper.getBatteryCapacity());
2895 ASSERT_EQ(*mapper.getBatteryCapacity(), BATTERY_CAPACITY);
2896}
2897
2898TEST_F(BatteryInputMapperTest, GetBatteryStatus) {
2899 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2900
2901 ASSERT_TRUE(mapper.getBatteryStatus());
2902 ASSERT_EQ(*mapper.getBatteryStatus(), BATTERY_STATUS);
2903}
2904
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905// --- KeyboardInputMapperTest ---
2906
2907class KeyboardInputMapperTest : public InputMapperTest {
2908protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002909 const std::string UNIQUE_ID = "local:0";
2910
2911 void prepareDisplay(int32_t orientation);
2912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002913 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002914 int32_t originalKeyCode, int32_t rotatedKeyCode,
2915 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916};
2917
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002918/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2919 * orientation.
2920 */
2921void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002922 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2923 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002924}
2925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002926void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002927 int32_t originalScanCode, int32_t originalKeyCode,
2928 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 NotifyKeyArgs args;
2930
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2933 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2934 ASSERT_EQ(originalScanCode, args.scanCode);
2935 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002936 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2940 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2941 ASSERT_EQ(originalScanCode, args.scanCode);
2942 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002943 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944}
2945
Michael Wrightd02c5b62014-02-10 15:10:22 -08002946TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002947 KeyboardInputMapper& mapper =
2948 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2949 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002951 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952}
2953
2954TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2955 const int32_t USAGE_A = 0x070004;
2956 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002957 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2958 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002959 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2960 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2961 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002963 KeyboardInputMapper& mapper =
2964 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2965 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002966 // Initial metastate to AMETA_NONE.
2967 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2968 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969
2970 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 NotifyKeyArgs args;
2973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2974 ASSERT_EQ(DEVICE_ID, args.deviceId);
2975 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2976 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2977 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2978 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2979 ASSERT_EQ(KEY_HOME, args.scanCode);
2980 ASSERT_EQ(AMETA_NONE, args.metaState);
2981 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2982 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2983 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2984
2985 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002986 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2988 ASSERT_EQ(DEVICE_ID, args.deviceId);
2989 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2990 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2991 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2992 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2993 ASSERT_EQ(KEY_HOME, args.scanCode);
2994 ASSERT_EQ(AMETA_NONE, args.metaState);
2995 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2996 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2997 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2998
2999 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3003 ASSERT_EQ(DEVICE_ID, args.deviceId);
3004 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3005 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3006 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3007 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3008 ASSERT_EQ(0, args.scanCode);
3009 ASSERT_EQ(AMETA_NONE, args.metaState);
3010 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3011 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3012 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3013
3014 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3016 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3018 ASSERT_EQ(DEVICE_ID, args.deviceId);
3019 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3020 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3021 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3022 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3023 ASSERT_EQ(0, args.scanCode);
3024 ASSERT_EQ(AMETA_NONE, args.metaState);
3025 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3026 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3027 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3028
3029 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3033 ASSERT_EQ(DEVICE_ID, args.deviceId);
3034 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3035 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3036 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3037 ASSERT_EQ(0, args.keyCode);
3038 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3039 ASSERT_EQ(AMETA_NONE, args.metaState);
3040 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3041 ASSERT_EQ(0U, args.policyFlags);
3042 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3043
3044 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3046 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3048 ASSERT_EQ(DEVICE_ID, args.deviceId);
3049 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3050 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3051 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3052 ASSERT_EQ(0, args.keyCode);
3053 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3054 ASSERT_EQ(AMETA_NONE, args.metaState);
3055 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3056 ASSERT_EQ(0U, args.policyFlags);
3057 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3058}
3059
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003060/**
3061 * Ensure that the readTime is set to the time when the EV_KEY is received.
3062 */
3063TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3064 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3065
3066 KeyboardInputMapper& mapper =
3067 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3068 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3069 NotifyKeyArgs args;
3070
3071 // Key down
3072 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3074 ASSERT_EQ(12, args.readTime);
3075
3076 // Key up
3077 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3079 ASSERT_EQ(15, args.readTime);
3080}
3081
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003085 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3086 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3087 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 KeyboardInputMapper& mapper =
3090 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092
arthurhungc903df12020-08-11 15:08:42 +08003093 // Initial metastate to AMETA_NONE.
3094 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3095 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
3097 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 NotifyKeyArgs args;
3100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3101 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003103 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104
3105 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003106 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3108 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003109 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
3111 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003112 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003115 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116
3117 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003118 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3120 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003122 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123}
3124
3125TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003126 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3127 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3128 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3129 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003131 KeyboardInputMapper& mapper =
3132 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3133 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003135 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3137 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3138 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3139 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3140 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3141 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3142 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3143 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3144}
3145
3146TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003147 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3149 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3150 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003153 KeyboardInputMapper& mapper =
3154 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3155 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003157 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003158 ASSERT_NO_FATAL_FAILURE(
3159 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3160 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3161 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3162 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3163 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3164 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3165 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003167 clearViewports();
3168 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003169 ASSERT_NO_FATAL_FAILURE(
3170 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3171 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3172 AKEYCODE_DPAD_UP, DISPLAY_ID));
3173 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3174 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3175 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3176 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003178 clearViewports();
3179 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003180 ASSERT_NO_FATAL_FAILURE(
3181 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3182 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3183 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3184 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3185 AKEYCODE_DPAD_UP, DISPLAY_ID));
3186 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3187 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003189 clearViewports();
3190 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003191 ASSERT_NO_FATAL_FAILURE(
3192 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3193 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3194 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3195 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3196 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3197 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3198 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199
3200 // Special case: if orientation changes while key is down, we still emit the same keycode
3201 // in the key up as we did in the key down.
3202 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003203 clearViewports();
3204 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3207 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3208 ASSERT_EQ(KEY_UP, args.scanCode);
3209 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3210
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003211 clearViewports();
3212 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3215 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3216 ASSERT_EQ(KEY_UP, args.scanCode);
3217 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3218}
3219
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003220TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3221 // If the keyboard is not orientation aware,
3222 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003223 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003225 KeyboardInputMapper& mapper =
3226 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3227 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003228 NotifyKeyArgs args;
3229
3230 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3235 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3236
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003237 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3243}
3244
3245TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3246 // If the keyboard is orientation aware,
3247 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003248 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003249
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003250 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 KeyboardInputMapper& mapper =
3252 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3253 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003254 NotifyKeyArgs args;
3255
3256 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3257 // ^--- already checked by the previous test
3258
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003259 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003260 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3265 ASSERT_EQ(DISPLAY_ID, args.displayId);
3266
3267 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003268 clearViewports();
3269 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003270 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3275 ASSERT_EQ(newDisplayId, args.displayId);
3276}
3277
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 KeyboardInputMapper& mapper =
3280 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3281 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003283 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003284 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003287 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288}
3289
3290TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003291 KeyboardInputMapper& mapper =
3292 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3293 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003295 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003298 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003299 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300}
3301
3302TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003303 KeyboardInputMapper& mapper =
3304 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3305 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003307 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308
3309 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3310 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003311 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 ASSERT_TRUE(flags[0]);
3313 ASSERT_FALSE(flags[1]);
3314}
3315
3316TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3318 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3319 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3320 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3321 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3322 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003323
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003324 KeyboardInputMapper& mapper =
3325 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3326 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003327 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003328 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3329 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330
3331 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3333 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3334 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335
3336 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003339 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3340 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3341 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343
3344 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003347 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3348 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3349 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003350 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
3352 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003355 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3356 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3357 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003358 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359
3360 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003363 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3364 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3365 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003366 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367
3368 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003371 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3372 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3373 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375
3376 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003379 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3380 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3381 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003382 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383}
3384
Chris Yea52ade12020-08-27 16:49:20 -07003385TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3386 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3387 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3388 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3389 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3390
3391 KeyboardInputMapper& mapper =
3392 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3393 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3394
3395 // Initial metastate should be AMETA_NONE as no meta keys added.
3396 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3397 // Meta state should be AMETA_NONE after reset
3398 mapper.reset(ARBITRARY_TIME);
3399 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3400 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3401 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3402 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3403
3404 NotifyKeyArgs args;
3405 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3408 ASSERT_EQ(AMETA_NONE, args.metaState);
3409 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3411 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3412
3413 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003414 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3416 ASSERT_EQ(AMETA_NONE, args.metaState);
3417 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3418 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3419 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3420}
3421
Arthur Hung2c9a3342019-07-23 14:18:59 +08003422TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3423 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003424 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3425 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3426 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003428
3429 // keyboard 2.
3430 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003431 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003432 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003433 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003434 std::shared_ptr<InputDevice> device2 =
3435 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3436 Flags<InputDeviceClass>(0));
3437
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003438 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3439 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3440 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3441 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003443 KeyboardInputMapper& mapper =
3444 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3445 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003446
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003448 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003449 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003450 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3451 device2->reset(ARBITRARY_TIME);
3452
3453 // Prepared displays and associated info.
3454 constexpr uint8_t hdmi1 = 0;
3455 constexpr uint8_t hdmi2 = 1;
3456 const std::string SECONDARY_UNIQUE_ID = "local:1";
3457
3458 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3459 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3460
3461 // No associated display viewport found, should disable the device.
3462 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3463 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3464 ASSERT_FALSE(device2->isEnabled());
3465
3466 // Prepare second display.
3467 constexpr int32_t newDisplayId = 2;
3468 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003469 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003470 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003471 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003472 // Default device will reconfigure above, need additional reconfiguration for another device.
3473 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3474 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3475
3476 // Device should be enabled after the associated display is found.
3477 ASSERT_TRUE(mDevice->isEnabled());
3478 ASSERT_TRUE(device2->isEnabled());
3479
3480 // Test pad key events
3481 ASSERT_NO_FATAL_FAILURE(
3482 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3483 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3484 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3485 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3486 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3487 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3488 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3489
3490 ASSERT_NO_FATAL_FAILURE(
3491 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3492 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3493 AKEYCODE_DPAD_RIGHT, newDisplayId));
3494 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3495 AKEYCODE_DPAD_DOWN, newDisplayId));
3496 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3497 AKEYCODE_DPAD_LEFT, newDisplayId));
3498}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
arthurhungc903df12020-08-11 15:08:42 +08003500TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3501 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3502 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3503 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3504 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3505 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3507
3508 KeyboardInputMapper& mapper =
3509 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3510 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3511 // Initial metastate to AMETA_NONE.
3512 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3513 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3514
3515 // Initialization should have turned all of the lights off.
3516 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3517 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3519
3520 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003523 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3524 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3525
3526 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003529 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3530 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3531
3532 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003535 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3536 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3537
3538 mFakeEventHub->removeDevice(EVENTHUB_ID);
3539 mReader->loopOnce();
3540
3541 // keyboard 2 should default toggle keys.
3542 const std::string USB2 = "USB2";
3543 const std::string DEVICE_NAME2 = "KEYBOARD2";
3544 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3545 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3546 std::shared_ptr<InputDevice> device2 =
3547 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3548 Flags<InputDeviceClass>(0));
3549 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3550 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3551 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3552 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3553 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3554 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3555
arthurhung6fe95782020-10-05 22:41:16 +08003556 KeyboardInputMapper& mapper2 =
3557 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3558 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003559 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3560 device2->reset(ARBITRARY_TIME);
3561
3562 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3563 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3564 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003565 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3566 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003567}
3568
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003569// --- KeyboardInputMapperTest_ExternalDevice ---
3570
3571class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3572protected:
Chris Yea52ade12020-08-27 16:49:20 -07003573 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003574};
3575
3576TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003577 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3578 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003579
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003580 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3581 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3582 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3583 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 KeyboardInputMapper& mapper =
3586 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3587 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003588
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003590 NotifyKeyArgs args;
3591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3592 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3593
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003594 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3596 ASSERT_EQ(uint32_t(0), args.policyFlags);
3597
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3600 ASSERT_EQ(uint32_t(0), args.policyFlags);
3601
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003602 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3604 ASSERT_EQ(uint32_t(0), args.policyFlags);
3605
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3608 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3609
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003610 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3612 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3613}
3614
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003615TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003616 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003617
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003618 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3619 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3620 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003621
Powei Fengd041c5d2019-05-03 17:11:33 -07003622 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003623 KeyboardInputMapper& mapper =
3624 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3625 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003626
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003628 NotifyKeyArgs args;
3629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3630 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3631
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003632 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3634 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3635
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3638 ASSERT_EQ(uint32_t(0), args.policyFlags);
3639
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003640 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3642 ASSERT_EQ(uint32_t(0), args.policyFlags);
3643
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3646 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3647
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003648 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3650 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3651}
3652
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653// --- CursorInputMapperTest ---
3654
3655class CursorInputMapperTest : public InputMapperTest {
3656protected:
3657 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3658
Michael Wright17db18e2020-06-26 20:51:44 +01003659 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660
Chris Yea52ade12020-08-27 16:49:20 -07003661 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662 InputMapperTest::SetUp();
3663
Michael Wright17db18e2020-06-26 20:51:44 +01003664 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003665 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 }
3667
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003668 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3669 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003670
3671 void prepareDisplay(int32_t orientation) {
3672 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003673 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003674 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3675 orientation, uniqueId, NO_PORT, viewportType);
3676 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677};
3678
3679const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3680
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003681void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3682 int32_t originalY, int32_t rotatedX,
3683 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684 NotifyMotionArgs args;
3685
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3692 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3693 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3694 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3695}
3696
3697TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003699 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003701 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702}
3703
3704TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003706 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709}
3710
3711TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
3715 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
3718 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003719 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3720 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3722 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3723
3724 // When the bounds are set, then there should be a valid motion range.
3725 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3726
3727 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729
3730 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3731 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3732 1, 800 - 1, 0.0f, 0.0f));
3733 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3734 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3735 2, 480 - 1, 0.0f, 0.0f));
3736 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3737 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3738 0.0f, 1.0f, 0.0f, 0.0f));
3739}
3740
3741TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003743 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
3745 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747
3748 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3749 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3750 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3751 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3752 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3753 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3754 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3755 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3756 0.0f, 1.0f, 0.0f, 0.0f));
3757}
3758
3759TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
arthurhungdcef2dc2020-08-11 14:47:50 +08003763 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
3765 NotifyMotionArgs args;
3766
3767 // Button press.
3768 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3772 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3773 ASSERT_EQ(DEVICE_ID, args.deviceId);
3774 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3775 ASSERT_EQ(uint32_t(0), args.policyFlags);
3776 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3777 ASSERT_EQ(0, args.flags);
3778 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3779 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3780 ASSERT_EQ(0, args.edgeFlags);
3781 ASSERT_EQ(uint32_t(1), args.pointerCount);
3782 ASSERT_EQ(0, args.pointerProperties[0].id);
3783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3785 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3786 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3787 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3788 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3789
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3791 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3792 ASSERT_EQ(DEVICE_ID, args.deviceId);
3793 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3794 ASSERT_EQ(uint32_t(0), args.policyFlags);
3795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3796 ASSERT_EQ(0, args.flags);
3797 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3798 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3799 ASSERT_EQ(0, args.edgeFlags);
3800 ASSERT_EQ(uint32_t(1), args.pointerCount);
3801 ASSERT_EQ(0, args.pointerProperties[0].id);
3802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3804 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3805 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3806 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3807 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3808
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003810 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3811 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3813 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3814 ASSERT_EQ(DEVICE_ID, args.deviceId);
3815 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3816 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3818 ASSERT_EQ(0, args.flags);
3819 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3820 ASSERT_EQ(0, args.buttonState);
3821 ASSERT_EQ(0, args.edgeFlags);
3822 ASSERT_EQ(uint32_t(1), args.pointerCount);
3823 ASSERT_EQ(0, args.pointerProperties[0].id);
3824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3826 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3827 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3828 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3829 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3830
3831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3832 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3833 ASSERT_EQ(DEVICE_ID, args.deviceId);
3834 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3835 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3837 ASSERT_EQ(0, args.flags);
3838 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3839 ASSERT_EQ(0, args.buttonState);
3840 ASSERT_EQ(0, args.edgeFlags);
3841 ASSERT_EQ(uint32_t(1), args.pointerCount);
3842 ASSERT_EQ(0, args.pointerProperties[0].id);
3843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3845 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3846 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3847 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3848 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3849}
3850
3851TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003853 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854
3855 NotifyMotionArgs args;
3856
3857 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3863 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3864
3865 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3871 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3872}
3873
3874TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877
3878 NotifyMotionArgs args;
3879
3880 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3884 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3886 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3887
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3891 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3892
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3899 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3900
3901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3904 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3905}
3906
3907TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003909 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910
3911 NotifyMotionArgs args;
3912
3913 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3921 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3922 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3923
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3925 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3927 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3928 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3929
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
3932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
3933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3937 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3938 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3939
3940 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3946 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3947
3948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3951 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3952}
3953
3954TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003956 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003958 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3960 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3961 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3962 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3963 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3964 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3965 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3966 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3967}
3968
3969TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970 addConfigurationProperty("cursor.mode", "navigation");
3971 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003972 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003974 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3976 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3977 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3978 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3979 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3983
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003984 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3986 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3987 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3988 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3989 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3990 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3991 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3992 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3993
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003994 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3996 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3997 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3998 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3999 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4000 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4001 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4003
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004004 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4007 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4008 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4009 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4010 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4011 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4012 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4013}
4014
4015TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004017 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018
4019 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4020 mFakePointerController->setPosition(100, 200);
4021 mFakePointerController->setButtonState(0);
4022
4023 NotifyMotionArgs motionArgs;
4024 NotifyKeyArgs keyArgs;
4025
4026 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4030 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4031 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4032 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4034 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4035
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4038 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4039 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4041 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4042
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 ASSERT_EQ(0, motionArgs.buttonState);
4048 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4050 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4051
4052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004053 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_EQ(0, motionArgs.buttonState);
4055 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4057 100.0f, 200.0f, 0.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_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061 ASSERT_EQ(0, motionArgs.buttonState);
4062 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4064 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4065
4066 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4071 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4072 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4073 motionArgs.buttonState);
4074 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4075 mFakePointerController->getButtonState());
4076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4077 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4078
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4080 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4081 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4082 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4083 mFakePointerController->getButtonState());
4084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4085 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4086
4087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4089 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4090 motionArgs.buttonState);
4091 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4092 mFakePointerController->getButtonState());
4093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4094 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4095
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
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_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4101 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4103 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4104
4105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004107 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4108 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4110 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4111
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4113 process(mapper, ARBITRARY_TIME, READ_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);
4116 ASSERT_EQ(0, motionArgs.buttonState);
4117 ASSERT_EQ(0, mFakePointerController->getButtonState());
4118 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));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004122
4123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_EQ(0, motionArgs.buttonState);
4125 ASSERT_EQ(0, mFakePointerController->getButtonState());
4126 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4128 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 -08004129
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4131 ASSERT_EQ(0, motionArgs.buttonState);
4132 ASSERT_EQ(0, mFakePointerController->getButtonState());
4133 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4135 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4136
4137 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4141 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4142 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004143
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004145 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4147 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4149 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4150
4151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4152 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4153 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4154 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4156 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_EQ(0, motionArgs.buttonState);
4163 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4165 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4166
4167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004169 ASSERT_EQ(0, motionArgs.buttonState);
4170 ASSERT_EQ(0, mFakePointerController->getButtonState());
4171
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4173 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4175 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4176 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4177
4178 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4182 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4183 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004184
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4188 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4190 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4191
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4194 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4195 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4197 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4198
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 ASSERT_EQ(0, motionArgs.buttonState);
4204 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4206 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 -08004207
4208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4210 ASSERT_EQ(0, motionArgs.buttonState);
4211 ASSERT_EQ(0, mFakePointerController->getButtonState());
4212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4213 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4214
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4216 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4217 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4218
4219 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4223 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4224 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4229 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4231 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4232
4233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4234 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4235 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4236 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4238 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4239
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 ASSERT_EQ(0, motionArgs.buttonState);
4245 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4247 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 -08004248
4249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4250 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4251 ASSERT_EQ(0, motionArgs.buttonState);
4252 ASSERT_EQ(0, mFakePointerController->getButtonState());
4253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4254 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4255
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4257 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4258 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4259
4260 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4264 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4265 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4270 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4272 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4273
4274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4276 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4277 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4279 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4280
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_EQ(0, motionArgs.buttonState);
4286 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4288 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 -08004289
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4292 ASSERT_EQ(0, motionArgs.buttonState);
4293 ASSERT_EQ(0, mFakePointerController->getButtonState());
4294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4295 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4296
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4298 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4299 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4300}
4301
4302TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004304 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305
4306 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4307 mFakePointerController->setPosition(100, 200);
4308 mFakePointerController->setButtonState(0);
4309
4310 NotifyMotionArgs args;
4311
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004316 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4319 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 +01004320 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004321}
4322
4323TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004324 addConfigurationProperty("cursor.mode", "pointer");
4325 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004326 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004327
4328 NotifyDeviceResetArgs resetArgs;
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4330 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4331 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4332
4333 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4334 mFakePointerController->setPosition(100, 200);
4335 mFakePointerController->setButtonState(0);
4336
4337 NotifyMotionArgs args;
4338
4339 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4344 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4347 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 +01004348 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004349
4350 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4354 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4357 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4359 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4362 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4363
4364 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004365 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4366 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4368 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4371 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4373 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4374 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4376 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4377
4378 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4383 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4386 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 +01004387 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004388
4389 // Disable pointer capture and check that the device generation got bumped
4390 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004391 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004392 mFakePolicy->setPointerCapture(false);
4393 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004394 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004395
4396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4397 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4398 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4399
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4404 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4407 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 +01004408 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409}
4410
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004411TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004412 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004413
Garfield Tan888a6a42020-01-09 11:39:16 -08004414 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004415 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004416 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4417 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004418 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4419 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004420 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4421 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4422
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004423 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4424 mFakePointerController->setPosition(100, 200);
4425 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004426
4427 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4432 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4433 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4435 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 +01004436 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004437 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4438}
4439
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440// --- TouchInputMapperTest ---
4441
4442class TouchInputMapperTest : public InputMapperTest {
4443protected:
4444 static const int32_t RAW_X_MIN;
4445 static const int32_t RAW_X_MAX;
4446 static const int32_t RAW_Y_MIN;
4447 static const int32_t RAW_Y_MAX;
4448 static const int32_t RAW_TOUCH_MIN;
4449 static const int32_t RAW_TOUCH_MAX;
4450 static const int32_t RAW_TOOL_MIN;
4451 static const int32_t RAW_TOOL_MAX;
4452 static const int32_t RAW_PRESSURE_MIN;
4453 static const int32_t RAW_PRESSURE_MAX;
4454 static const int32_t RAW_ORIENTATION_MIN;
4455 static const int32_t RAW_ORIENTATION_MAX;
4456 static const int32_t RAW_DISTANCE_MIN;
4457 static const int32_t RAW_DISTANCE_MAX;
4458 static const int32_t RAW_TILT_MIN;
4459 static const int32_t RAW_TILT_MAX;
4460 static const int32_t RAW_ID_MIN;
4461 static const int32_t RAW_ID_MAX;
4462 static const int32_t RAW_SLOT_MIN;
4463 static const int32_t RAW_SLOT_MAX;
4464 static const float X_PRECISION;
4465 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004466 static const float X_PRECISION_VIRTUAL;
4467 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468
4469 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004470 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471
4472 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4473
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004474 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004475 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004476
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 enum Axes {
4478 POSITION = 1 << 0,
4479 TOUCH = 1 << 1,
4480 TOOL = 1 << 2,
4481 PRESSURE = 1 << 3,
4482 ORIENTATION = 1 << 4,
4483 MINOR = 1 << 5,
4484 ID = 1 << 6,
4485 DISTANCE = 1 << 7,
4486 TILT = 1 << 8,
4487 SLOT = 1 << 9,
4488 TOOL_TYPE = 1 << 10,
4489 };
4490
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004491 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4492 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004493 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004495 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496 int32_t toRawX(float displayX);
4497 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004498 float toCookedX(float rawX, float rawY);
4499 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004501 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004503 float toDisplayY(int32_t rawY, int32_t displayHeight);
4504
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505};
4506
4507const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4508const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4509const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4510const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4511const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4512const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4513const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4514const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004515const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4516const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4518const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4519const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4520const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4521const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4522const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4523const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4524const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4525const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4526const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4527const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4528const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004529const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4530 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4531const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4532 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004533const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4534 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535
4536const float TouchInputMapperTest::GEOMETRIC_SCALE =
4537 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4538 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4539
4540const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4541 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4542 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4543};
4544
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004545void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004546 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4547 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004548}
4549
4550void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4551 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4552 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553}
4554
Santos Cordonfa5cf462017-04-05 10:37:00 -07004555void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004556 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4557 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4558 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004559}
4560
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004562 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4563 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4565 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566}
4567
Jason Gerecke489fda82012-09-07 17:19:40 -07004568void TouchInputMapperTest::prepareLocationCalibration() {
4569 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4570}
4571
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572int32_t TouchInputMapperTest::toRawX(float displayX) {
4573 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4574}
4575
4576int32_t TouchInputMapperTest::toRawY(float displayY) {
4577 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4578}
4579
Jason Gerecke489fda82012-09-07 17:19:40 -07004580float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4581 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4582 return rawX;
4583}
4584
4585float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4586 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4587 return rawY;
4588}
4589
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004591 return toDisplayX(rawX, DISPLAY_WIDTH);
4592}
4593
4594float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4595 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596}
4597
4598float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004599 return toDisplayY(rawY, DISPLAY_HEIGHT);
4600}
4601
4602float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4603 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604}
4605
4606
4607// --- SingleTouchInputMapperTest ---
4608
4609class SingleTouchInputMapperTest : public TouchInputMapperTest {
4610protected:
4611 void prepareButtons();
4612 void prepareAxes(int axes);
4613
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004614 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4615 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4616 void processUp(SingleTouchInputMapper& mappery);
4617 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4618 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4619 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4620 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4621 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4622 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623};
4624
4625void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004626 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627}
4628
4629void SingleTouchInputMapperTest::prepareAxes(int axes) {
4630 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004631 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4632 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 }
4634 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004635 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4636 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 }
4638 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004639 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4640 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 }
4642 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004643 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4644 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 }
4646 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004647 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4648 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 }
4650}
4651
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004652void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656}
4657
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004658void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661}
4662
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004663void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665}
4666
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004667void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669}
4670
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004671void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4672 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674}
4675
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004676void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678}
4679
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004680void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4681 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684}
4685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004686void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4687 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689}
4690
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004691void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693}
4694
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 prepareButtons();
4697 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004698 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004700 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701}
4702
4703TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004704 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4705 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 prepareButtons();
4707 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004708 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004710 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711}
4712
4713TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 prepareButtons();
4715 prepareAxes(POSITION);
4716 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004717 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004719 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720}
4721
4722TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723 prepareButtons();
4724 prepareAxes(POSITION);
4725 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004726 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004728 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729}
4730
4731TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 addConfigurationProperty("touch.deviceType", "touchScreen");
4733 prepareDisplay(DISPLAY_ORIENTATION_0);
4734 prepareButtons();
4735 prepareAxes(POSITION);
4736 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004737 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738
4739 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004740 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741
4742 // Virtual key is down.
4743 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4744 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4745 processDown(mapper, x, y);
4746 processSync(mapper);
4747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004749 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750
4751 // Virtual key is up.
4752 processUp(mapper);
4753 processSync(mapper);
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4755
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004756 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757}
4758
4759TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 addConfigurationProperty("touch.deviceType", "touchScreen");
4761 prepareDisplay(DISPLAY_ORIENTATION_0);
4762 prepareButtons();
4763 prepareAxes(POSITION);
4764 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004765 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766
4767 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004768 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769
4770 // Virtual key is down.
4771 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4772 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4773 processDown(mapper, x, y);
4774 processSync(mapper);
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4776
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004777 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778
4779 // Virtual key is up.
4780 processUp(mapper);
4781 processSync(mapper);
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4783
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004784 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785}
4786
4787TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 addConfigurationProperty("touch.deviceType", "touchScreen");
4789 prepareDisplay(DISPLAY_ORIENTATION_0);
4790 prepareButtons();
4791 prepareAxes(POSITION);
4792 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004793 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794
4795 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4796 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004797 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_TRUE(flags[0]);
4799 ASSERT_FALSE(flags[1]);
4800}
4801
4802TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 addConfigurationProperty("touch.deviceType", "touchScreen");
4804 prepareDisplay(DISPLAY_ORIENTATION_0);
4805 prepareButtons();
4806 prepareAxes(POSITION);
4807 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004808 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809
arthurhungdcef2dc2020-08-11 14:47:50 +08004810 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811
4812 NotifyKeyArgs args;
4813
4814 // Press virtual key.
4815 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4816 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4817 processDown(mapper, x, y);
4818 processSync(mapper);
4819
4820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4821 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4822 ASSERT_EQ(DEVICE_ID, args.deviceId);
4823 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4824 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4825 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4826 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4827 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4828 ASSERT_EQ(KEY_HOME, args.scanCode);
4829 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4830 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4831
4832 // Release virtual key.
4833 processUp(mapper);
4834 processSync(mapper);
4835
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4837 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4838 ASSERT_EQ(DEVICE_ID, args.deviceId);
4839 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4840 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4841 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4842 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4843 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4844 ASSERT_EQ(KEY_HOME, args.scanCode);
4845 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4846 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4847
4848 // Should not have sent any motions.
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4850}
4851
4852TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 addConfigurationProperty("touch.deviceType", "touchScreen");
4854 prepareDisplay(DISPLAY_ORIENTATION_0);
4855 prepareButtons();
4856 prepareAxes(POSITION);
4857 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004858 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859
arthurhungdcef2dc2020-08-11 14:47:50 +08004860 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861
4862 NotifyKeyArgs keyArgs;
4863
4864 // Press virtual key.
4865 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4866 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4867 processDown(mapper, x, y);
4868 processSync(mapper);
4869
4870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4871 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4872 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4873 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4874 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4875 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4876 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4877 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4878 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4879 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4880 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4881
4882 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4883 // into the display area.
4884 y -= 100;
4885 processMove(mapper, x, y);
4886 processSync(mapper);
4887
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4889 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4890 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4891 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4892 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4893 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4894 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4895 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4896 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4897 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4898 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4899 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4900
4901 NotifyMotionArgs motionArgs;
4902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4903 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4904 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4905 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4906 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4907 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4908 ASSERT_EQ(0, motionArgs.flags);
4909 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4910 ASSERT_EQ(0, motionArgs.buttonState);
4911 ASSERT_EQ(0, motionArgs.edgeFlags);
4912 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4913 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4916 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4917 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4918 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4920
4921 // Keep moving out of bounds. Should generate a pointer move.
4922 y -= 50;
4923 processMove(mapper, x, y);
4924 processSync(mapper);
4925
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4927 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4928 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4929 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4930 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4932 ASSERT_EQ(0, motionArgs.flags);
4933 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4934 ASSERT_EQ(0, motionArgs.buttonState);
4935 ASSERT_EQ(0, motionArgs.edgeFlags);
4936 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4937 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4940 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4941 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4942 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4943 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4944
4945 // Release out of bounds. Should generate a pointer up.
4946 processUp(mapper);
4947 processSync(mapper);
4948
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4950 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4951 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4952 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4953 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4954 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4955 ASSERT_EQ(0, motionArgs.flags);
4956 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4957 ASSERT_EQ(0, motionArgs.buttonState);
4958 ASSERT_EQ(0, motionArgs.edgeFlags);
4959 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4960 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4963 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4964 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4965 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4966 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4967
4968 // Should not have sent any more keys or motions.
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4971}
4972
4973TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 addConfigurationProperty("touch.deviceType", "touchScreen");
4975 prepareDisplay(DISPLAY_ORIENTATION_0);
4976 prepareButtons();
4977 prepareAxes(POSITION);
4978 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004979 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980
arthurhungdcef2dc2020-08-11 14:47:50 +08004981 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982
4983 NotifyMotionArgs motionArgs;
4984
4985 // Initially go down out of bounds.
4986 int32_t x = -10;
4987 int32_t y = -10;
4988 processDown(mapper, x, y);
4989 processSync(mapper);
4990
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4992
4993 // Move into the display area. Should generate a pointer down.
4994 x = 50;
4995 y = 75;
4996 processMove(mapper, x, y);
4997 processSync(mapper);
4998
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5000 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5001 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5002 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5003 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5004 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5005 ASSERT_EQ(0, motionArgs.flags);
5006 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5007 ASSERT_EQ(0, motionArgs.buttonState);
5008 ASSERT_EQ(0, motionArgs.edgeFlags);
5009 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5010 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5013 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5014 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5015 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5016 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5017
5018 // Release. Should generate a pointer up.
5019 processUp(mapper);
5020 processSync(mapper);
5021
5022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5023 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5024 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5025 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5026 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5027 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5028 ASSERT_EQ(0, motionArgs.flags);
5029 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5030 ASSERT_EQ(0, motionArgs.buttonState);
5031 ASSERT_EQ(0, motionArgs.edgeFlags);
5032 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5033 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5036 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5037 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5038 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5040
5041 // Should not have sent any more keys or motions.
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5044}
5045
Santos Cordonfa5cf462017-04-05 10:37:00 -07005046TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005047 addConfigurationProperty("touch.deviceType", "touchScreen");
5048 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5049
5050 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5051 prepareButtons();
5052 prepareAxes(POSITION);
5053 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005054 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005055
arthurhungdcef2dc2020-08-11 14:47:50 +08005056 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005057
5058 NotifyMotionArgs motionArgs;
5059
5060 // Down.
5061 int32_t x = 100;
5062 int32_t y = 125;
5063 processDown(mapper, x, y);
5064 processSync(mapper);
5065
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5068 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5069 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5070 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5071 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5072 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5073 ASSERT_EQ(0, motionArgs.flags);
5074 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5075 ASSERT_EQ(0, motionArgs.buttonState);
5076 ASSERT_EQ(0, motionArgs.edgeFlags);
5077 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5078 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5081 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5082 1, 0, 0, 0, 0, 0, 0, 0));
5083 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5084 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5085 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5086
5087 // Move.
5088 x += 50;
5089 y += 75;
5090 processMove(mapper, x, y);
5091 processSync(mapper);
5092
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5094 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5095 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5096 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5097 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5098 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5100 ASSERT_EQ(0, motionArgs.flags);
5101 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5102 ASSERT_EQ(0, motionArgs.buttonState);
5103 ASSERT_EQ(0, motionArgs.edgeFlags);
5104 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5105 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5108 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5109 1, 0, 0, 0, 0, 0, 0, 0));
5110 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5111 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5112 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5113
5114 // Up.
5115 processUp(mapper);
5116 processSync(mapper);
5117
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5120 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5121 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5122 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5123 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5124 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5125 ASSERT_EQ(0, motionArgs.flags);
5126 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5127 ASSERT_EQ(0, motionArgs.buttonState);
5128 ASSERT_EQ(0, motionArgs.edgeFlags);
5129 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5130 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5133 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5134 1, 0, 0, 0, 0, 0, 0, 0));
5135 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5136 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5138
5139 // Should not have sent any more keys or motions.
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5142}
5143
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 addConfigurationProperty("touch.deviceType", "touchScreen");
5146 prepareDisplay(DISPLAY_ORIENTATION_0);
5147 prepareButtons();
5148 prepareAxes(POSITION);
5149 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005150 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151
arthurhungdcef2dc2020-08-11 14:47:50 +08005152 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153
5154 NotifyMotionArgs motionArgs;
5155
5156 // Down.
5157 int32_t x = 100;
5158 int32_t y = 125;
5159 processDown(mapper, x, y);
5160 processSync(mapper);
5161
5162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5164 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5165 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5166 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5168 ASSERT_EQ(0, motionArgs.flags);
5169 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5170 ASSERT_EQ(0, motionArgs.buttonState);
5171 ASSERT_EQ(0, motionArgs.edgeFlags);
5172 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5173 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5176 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5177 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5178 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5179 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5180
5181 // Move.
5182 x += 50;
5183 y += 75;
5184 processMove(mapper, x, y);
5185 processSync(mapper);
5186
5187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5188 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5189 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5190 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5191 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5193 ASSERT_EQ(0, motionArgs.flags);
5194 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5195 ASSERT_EQ(0, motionArgs.buttonState);
5196 ASSERT_EQ(0, motionArgs.edgeFlags);
5197 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5198 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5201 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5202 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5203 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5204 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5205
5206 // Up.
5207 processUp(mapper);
5208 processSync(mapper);
5209
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5211 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5212 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5213 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5214 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5215 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5216 ASSERT_EQ(0, motionArgs.flags);
5217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5218 ASSERT_EQ(0, motionArgs.buttonState);
5219 ASSERT_EQ(0, motionArgs.edgeFlags);
5220 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5221 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5224 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5225 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5226 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5227 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5228
5229 // Should not have sent any more keys or motions.
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5232}
5233
5234TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235 addConfigurationProperty("touch.deviceType", "touchScreen");
5236 prepareButtons();
5237 prepareAxes(POSITION);
5238 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005239 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240
5241 NotifyMotionArgs args;
5242
5243 // Rotation 90.
5244 prepareDisplay(DISPLAY_ORIENTATION_90);
5245 processDown(mapper, toRawX(50), toRawY(75));
5246 processSync(mapper);
5247
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5249 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5250 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5251
5252 processUp(mapper);
5253 processSync(mapper);
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5255}
5256
5257TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258 addConfigurationProperty("touch.deviceType", "touchScreen");
5259 prepareButtons();
5260 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005261 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262
5263 NotifyMotionArgs args;
5264
5265 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005266 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 prepareDisplay(DISPLAY_ORIENTATION_0);
5268 processDown(mapper, toRawX(50), toRawY(75));
5269 processSync(mapper);
5270
5271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5272 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5273 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5274
5275 processUp(mapper);
5276 processSync(mapper);
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5278
5279 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005280 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281 prepareDisplay(DISPLAY_ORIENTATION_90);
5282 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5283 processSync(mapper);
5284
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5286 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5287 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5288
5289 processUp(mapper);
5290 processSync(mapper);
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5292
5293 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005294 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295 prepareDisplay(DISPLAY_ORIENTATION_180);
5296 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5297 processSync(mapper);
5298
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5300 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5301 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5302
5303 processUp(mapper);
5304 processSync(mapper);
5305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5306
5307 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005308 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309 prepareDisplay(DISPLAY_ORIENTATION_270);
5310 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5311 processSync(mapper);
5312
5313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5314 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5315 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5316
5317 processUp(mapper);
5318 processSync(mapper);
5319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5320}
5321
5322TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 addConfigurationProperty("touch.deviceType", "touchScreen");
5324 prepareDisplay(DISPLAY_ORIENTATION_0);
5325 prepareButtons();
5326 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005327 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328
5329 // These calculations are based on the input device calibration documentation.
5330 int32_t rawX = 100;
5331 int32_t rawY = 200;
5332 int32_t rawPressure = 10;
5333 int32_t rawToolMajor = 12;
5334 int32_t rawDistance = 2;
5335 int32_t rawTiltX = 30;
5336 int32_t rawTiltY = 110;
5337
5338 float x = toDisplayX(rawX);
5339 float y = toDisplayY(rawY);
5340 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5341 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5342 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5343 float distance = float(rawDistance);
5344
5345 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5346 float tiltScale = M_PI / 180;
5347 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5348 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5349 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5350 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5351
5352 processDown(mapper, rawX, rawY);
5353 processPressure(mapper, rawPressure);
5354 processToolMajor(mapper, rawToolMajor);
5355 processDistance(mapper, rawDistance);
5356 processTilt(mapper, rawTiltX, rawTiltY);
5357 processSync(mapper);
5358
5359 NotifyMotionArgs args;
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5362 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5363 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5364}
5365
Jason Gerecke489fda82012-09-07 17:19:40 -07005366TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005367 addConfigurationProperty("touch.deviceType", "touchScreen");
5368 prepareDisplay(DISPLAY_ORIENTATION_0);
5369 prepareLocationCalibration();
5370 prepareButtons();
5371 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005372 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005373
5374 int32_t rawX = 100;
5375 int32_t rawY = 200;
5376
5377 float x = toDisplayX(toCookedX(rawX, rawY));
5378 float y = toDisplayY(toCookedY(rawX, rawY));
5379
5380 processDown(mapper, rawX, rawY);
5381 processSync(mapper);
5382
5383 NotifyMotionArgs args;
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5386 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5387}
5388
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390 addConfigurationProperty("touch.deviceType", "touchScreen");
5391 prepareDisplay(DISPLAY_ORIENTATION_0);
5392 prepareButtons();
5393 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005394 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395
5396 NotifyMotionArgs motionArgs;
5397 NotifyKeyArgs keyArgs;
5398
5399 processDown(mapper, 100, 200);
5400 processSync(mapper);
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5402 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5403 ASSERT_EQ(0, motionArgs.buttonState);
5404
5405 // press BTN_LEFT, release BTN_LEFT
5406 processKey(mapper, BTN_LEFT, 1);
5407 processSync(mapper);
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5410 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5411
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5414 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5415
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 processKey(mapper, BTN_LEFT, 0);
5417 processSync(mapper);
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005419 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005424 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425
5426 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5427 processKey(mapper, BTN_RIGHT, 1);
5428 processKey(mapper, BTN_MIDDLE, 1);
5429 processSync(mapper);
5430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5432 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5433 motionArgs.buttonState);
5434
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5437 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5441 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5442 motionArgs.buttonState);
5443
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 processKey(mapper, BTN_RIGHT, 0);
5445 processSync(mapper);
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005449
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005452 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453
5454 processKey(mapper, BTN_MIDDLE, 0);
5455 processSync(mapper);
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005459
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005462 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463
5464 // press BTN_BACK, release BTN_BACK
5465 processKey(mapper, BTN_BACK, 1);
5466 processSync(mapper);
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5468 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5469 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005470
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005473 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5474
5475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5477 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478
5479 processKey(mapper, BTN_BACK, 0);
5480 processSync(mapper);
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005484
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005487 ASSERT_EQ(0, motionArgs.buttonState);
5488
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5490 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5491 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5492
5493 // press BTN_SIDE, release BTN_SIDE
5494 processKey(mapper, BTN_SIDE, 1);
5495 processSync(mapper);
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5497 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5498 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005499
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005502 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5506 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507
5508 processKey(mapper, BTN_SIDE, 0);
5509 processSync(mapper);
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005511 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005513
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005516 ASSERT_EQ(0, motionArgs.buttonState);
5517
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5519 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5520 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5521
5522 // press BTN_FORWARD, release BTN_FORWARD
5523 processKey(mapper, BTN_FORWARD, 1);
5524 processSync(mapper);
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5526 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5527 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005528
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005531 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5535 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536
5537 processKey(mapper, BTN_FORWARD, 0);
5538 processSync(mapper);
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005545 ASSERT_EQ(0, motionArgs.buttonState);
5546
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5548 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5549 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5550
5551 // press BTN_EXTRA, release BTN_EXTRA
5552 processKey(mapper, BTN_EXTRA, 1);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5555 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5556 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5564 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565
5566 processKey(mapper, BTN_EXTRA, 0);
5567 processSync(mapper);
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005571
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005574 ASSERT_EQ(0, motionArgs.buttonState);
5575
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5577 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5578 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5579
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5581
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 // press BTN_STYLUS, release BTN_STYLUS
5583 processKey(mapper, BTN_STYLUS, 1);
5584 processSync(mapper);
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005587 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5588
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5590 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5591 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592
5593 processKey(mapper, BTN_STYLUS, 0);
5594 processSync(mapper);
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005598
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005601 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602
5603 // press BTN_STYLUS2, release BTN_STYLUS2
5604 processKey(mapper, BTN_STYLUS2, 1);
5605 processSync(mapper);
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005608 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5612 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613
5614 processKey(mapper, BTN_STYLUS2, 0);
5615 processSync(mapper);
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005619
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005622 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623
5624 // release touch
5625 processUp(mapper);
5626 processSync(mapper);
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5628 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5629 ASSERT_EQ(0, motionArgs.buttonState);
5630}
5631
5632TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633 addConfigurationProperty("touch.deviceType", "touchScreen");
5634 prepareDisplay(DISPLAY_ORIENTATION_0);
5635 prepareButtons();
5636 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005637 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638
5639 NotifyMotionArgs motionArgs;
5640
5641 // default tool type is finger
5642 processDown(mapper, 100, 200);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5645 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5647
5648 // eraser
5649 processKey(mapper, BTN_TOOL_RUBBER, 1);
5650 processSync(mapper);
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5654
5655 // stylus
5656 processKey(mapper, BTN_TOOL_RUBBER, 0);
5657 processKey(mapper, BTN_TOOL_PEN, 1);
5658 processSync(mapper);
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5662
5663 // brush
5664 processKey(mapper, BTN_TOOL_PEN, 0);
5665 processKey(mapper, BTN_TOOL_BRUSH, 1);
5666 processSync(mapper);
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5670
5671 // pencil
5672 processKey(mapper, BTN_TOOL_BRUSH, 0);
5673 processKey(mapper, BTN_TOOL_PENCIL, 1);
5674 processSync(mapper);
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5678
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005679 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680 processKey(mapper, BTN_TOOL_PENCIL, 0);
5681 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5682 processSync(mapper);
5683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5686
5687 // mouse
5688 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5689 processKey(mapper, BTN_TOOL_MOUSE, 1);
5690 processSync(mapper);
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5694
5695 // lens
5696 processKey(mapper, BTN_TOOL_MOUSE, 0);
5697 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
5702
5703 // double-tap
5704 processKey(mapper, BTN_TOOL_LENS, 0);
5705 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5706 processSync(mapper);
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5710
5711 // triple-tap
5712 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5713 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5714 processSync(mapper);
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5718
5719 // quad-tap
5720 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5721 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5722 processSync(mapper);
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5726
5727 // finger
5728 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5729 processKey(mapper, BTN_TOOL_FINGER, 1);
5730 processSync(mapper);
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5734
5735 // stylus trumps finger
5736 processKey(mapper, BTN_TOOL_PEN, 1);
5737 processSync(mapper);
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5741
5742 // eraser trumps stylus
5743 processKey(mapper, BTN_TOOL_RUBBER, 1);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5748
5749 // mouse trumps eraser
5750 processKey(mapper, BTN_TOOL_MOUSE, 1);
5751 processSync(mapper);
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5755
5756 // back to default tool type
5757 processKey(mapper, BTN_TOOL_MOUSE, 0);
5758 processKey(mapper, BTN_TOOL_RUBBER, 0);
5759 processKey(mapper, BTN_TOOL_PEN, 0);
5760 processKey(mapper, BTN_TOOL_FINGER, 0);
5761 processSync(mapper);
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5765}
5766
5767TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768 addConfigurationProperty("touch.deviceType", "touchScreen");
5769 prepareDisplay(DISPLAY_ORIENTATION_0);
5770 prepareButtons();
5771 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005772 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005773 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774
5775 NotifyMotionArgs motionArgs;
5776
5777 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5778 processKey(mapper, BTN_TOOL_FINGER, 1);
5779 processMove(mapper, 100, 200);
5780 processSync(mapper);
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5784 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5785
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5789 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5790
5791 // move a little
5792 processMove(mapper, 150, 250);
5793 processSync(mapper);
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5795 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5797 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5798
5799 // down when BTN_TOUCH is pressed, pressure defaults to 1
5800 processKey(mapper, BTN_TOUCH, 1);
5801 processSync(mapper);
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5803 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5805 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5806
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5808 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5810 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5811
5812 // up when BTN_TOUCH is released, hover restored
5813 processKey(mapper, BTN_TOUCH, 0);
5814 processSync(mapper);
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5818 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5819
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5823 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5824
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5826 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5828 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5829
5830 // exit hover when pointer goes away
5831 processKey(mapper, BTN_TOOL_FINGER, 0);
5832 processSync(mapper);
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5836 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5837}
5838
5839TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005840 addConfigurationProperty("touch.deviceType", "touchScreen");
5841 prepareDisplay(DISPLAY_ORIENTATION_0);
5842 prepareButtons();
5843 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005844 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845
5846 NotifyMotionArgs motionArgs;
5847
5848 // initially hovering because pressure is 0
5849 processDown(mapper, 100, 200);
5850 processPressure(mapper, 0);
5851 processSync(mapper);
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5856
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5858 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5860 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5861
5862 // move a little
5863 processMove(mapper, 150, 250);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5868 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5869
5870 // down when pressure is non-zero
5871 processPressure(mapper, RAW_PRESSURE_MAX);
5872 processSync(mapper);
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5874 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5876 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5877
5878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5881 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5882
5883 // up when pressure becomes 0, hover restored
5884 processPressure(mapper, 0);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5887 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5889 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5890
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5894 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5895
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5899 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5900
5901 // exit hover when pointer goes away
5902 processUp(mapper);
5903 processSync(mapper);
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5907 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5908}
5909
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910// --- MultiTouchInputMapperTest ---
5911
5912class MultiTouchInputMapperTest : public TouchInputMapperTest {
5913protected:
5914 void prepareAxes(int axes);
5915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005916 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5917 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5918 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5919 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5920 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5921 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5922 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5923 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5924 void processId(MultiTouchInputMapper& mapper, int32_t id);
5925 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5926 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5927 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5928 void processMTSync(MultiTouchInputMapper& mapper);
5929 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930};
5931
5932void MultiTouchInputMapperTest::prepareAxes(int axes) {
5933 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005934 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5935 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 }
5937 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5939 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005941 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5942 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 }
5944 }
5945 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005946 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5947 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005948 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005949 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5950 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951 }
5952 }
5953 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005954 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5955 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956 }
5957 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005958 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5959 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960 }
5961 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005962 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5963 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964 }
5965 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005966 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5967 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 }
5969 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5971 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972 }
5973 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975 }
5976}
5977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005978void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5979 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982}
5983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005984void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5985 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987}
5988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005989void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5990 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992}
5993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005994void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005996}
5997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005998void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000}
6001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006002void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6003 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005}
6006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006007void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009}
6010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006011void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013}
6014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006015void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017}
6018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006019void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021}
6022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006023void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025}
6026
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006027void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6028 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030}
6031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006032void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034}
6035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006036void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038}
6039
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 addConfigurationProperty("touch.deviceType", "touchScreen");
6042 prepareDisplay(DISPLAY_ORIENTATION_0);
6043 prepareAxes(POSITION);
6044 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006045 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046
arthurhungdcef2dc2020-08-11 14:47:50 +08006047 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048
6049 NotifyMotionArgs motionArgs;
6050
6051 // Two fingers down at once.
6052 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6053 processPosition(mapper, x1, y1);
6054 processMTSync(mapper);
6055 processPosition(mapper, x2, y2);
6056 processMTSync(mapper);
6057 processSync(mapper);
6058
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6061 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6062 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6063 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6065 ASSERT_EQ(0, motionArgs.flags);
6066 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6067 ASSERT_EQ(0, motionArgs.buttonState);
6068 ASSERT_EQ(0, motionArgs.edgeFlags);
6069 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6073 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6074 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6075 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6077
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6080 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6081 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6082 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6084 motionArgs.action);
6085 ASSERT_EQ(0, motionArgs.flags);
6086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6087 ASSERT_EQ(0, motionArgs.buttonState);
6088 ASSERT_EQ(0, motionArgs.edgeFlags);
6089 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6090 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6092 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6095 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6097 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6098 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6099 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6100 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6101
6102 // Move.
6103 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6104 processPosition(mapper, x1, y1);
6105 processMTSync(mapper);
6106 processPosition(mapper, x2, y2);
6107 processMTSync(mapper);
6108 processSync(mapper);
6109
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6111 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6112 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6113 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6114 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6116 ASSERT_EQ(0, motionArgs.flags);
6117 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6118 ASSERT_EQ(0, motionArgs.buttonState);
6119 ASSERT_EQ(0, motionArgs.edgeFlags);
6120 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6121 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6123 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6126 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6128 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6129 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6130 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6131 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6132
6133 // First finger up.
6134 x2 += 15; y2 -= 20;
6135 processPosition(mapper, x2, y2);
6136 processMTSync(mapper);
6137 processSync(mapper);
6138
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6141 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6142 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6143 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6145 motionArgs.action);
6146 ASSERT_EQ(0, motionArgs.flags);
6147 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6148 ASSERT_EQ(0, motionArgs.buttonState);
6149 ASSERT_EQ(0, motionArgs.edgeFlags);
6150 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6151 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6153 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6156 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6158 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6159 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6160 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6162
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6164 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6165 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6166 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6167 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6169 ASSERT_EQ(0, motionArgs.flags);
6170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6171 ASSERT_EQ(0, motionArgs.buttonState);
6172 ASSERT_EQ(0, motionArgs.edgeFlags);
6173 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6174 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6177 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6178 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6179 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6180 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6181
6182 // Move.
6183 x2 += 20; y2 -= 25;
6184 processPosition(mapper, x2, y2);
6185 processMTSync(mapper);
6186 processSync(mapper);
6187
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6190 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6191 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6192 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6194 ASSERT_EQ(0, motionArgs.flags);
6195 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6196 ASSERT_EQ(0, motionArgs.buttonState);
6197 ASSERT_EQ(0, motionArgs.edgeFlags);
6198 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6199 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6206
6207 // New finger down.
6208 int32_t x3 = 700, y3 = 300;
6209 processPosition(mapper, x2, y2);
6210 processMTSync(mapper);
6211 processPosition(mapper, x3, y3);
6212 processMTSync(mapper);
6213 processSync(mapper);
6214
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6221 motionArgs.action);
6222 ASSERT_EQ(0, motionArgs.flags);
6223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6224 ASSERT_EQ(0, motionArgs.buttonState);
6225 ASSERT_EQ(0, motionArgs.edgeFlags);
6226 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6232 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6238
6239 // Second finger up.
6240 x3 += 30; y3 -= 20;
6241 processPosition(mapper, x3, y3);
6242 processMTSync(mapper);
6243 processSync(mapper);
6244
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6247 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6248 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6249 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6251 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(2), motionArgs.pointerCount);
6257 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6259 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6262 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6264 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6265 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6266 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6268
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6275 ASSERT_EQ(0, motionArgs.flags);
6276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6277 ASSERT_EQ(0, motionArgs.buttonState);
6278 ASSERT_EQ(0, motionArgs.edgeFlags);
6279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6283 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6284 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6285 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6287
6288 // Last finger up.
6289 processMTSync(mapper);
6290 processSync(mapper);
6291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6298 ASSERT_EQ(0, motionArgs.flags);
6299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6300 ASSERT_EQ(0, motionArgs.buttonState);
6301 ASSERT_EQ(0, motionArgs.edgeFlags);
6302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6306 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6310
6311 // Should not have sent any more keys or motions.
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6314}
6315
6316TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317 addConfigurationProperty("touch.deviceType", "touchScreen");
6318 prepareDisplay(DISPLAY_ORIENTATION_0);
6319 prepareAxes(POSITION | ID);
6320 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006321 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322
arthurhungdcef2dc2020-08-11 14:47:50 +08006323 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324
6325 NotifyMotionArgs motionArgs;
6326
6327 // Two fingers down at once.
6328 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6329 processPosition(mapper, x1, y1);
6330 processId(mapper, 1);
6331 processMTSync(mapper);
6332 processPosition(mapper, x2, y2);
6333 processId(mapper, 2);
6334 processMTSync(mapper);
6335 processSync(mapper);
6336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6339 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6343 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6346 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6347 motionArgs.action);
6348 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6349 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6351 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6354 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6356 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6357
6358 // Move.
6359 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6360 processPosition(mapper, x1, y1);
6361 processId(mapper, 1);
6362 processMTSync(mapper);
6363 processPosition(mapper, x2, y2);
6364 processId(mapper, 2);
6365 processMTSync(mapper);
6366 processSync(mapper);
6367
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6370 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6373 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6376 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6378 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6379
6380 // First finger up.
6381 x2 += 15; y2 -= 20;
6382 processPosition(mapper, x2, y2);
6383 processId(mapper, 2);
6384 processMTSync(mapper);
6385 processSync(mapper);
6386
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (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(x1), toDisplayY(y1), 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6402 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6403 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6406 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6407
6408 // Move.
6409 x2 += 20; y2 -= 25;
6410 processPosition(mapper, x2, y2);
6411 processId(mapper, 2);
6412 processMTSync(mapper);
6413 processSync(mapper);
6414
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6417 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6418 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6421 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6422
6423 // New finger down.
6424 int32_t x3 = 700, y3 = 300;
6425 processPosition(mapper, x2, y2);
6426 processId(mapper, 2);
6427 processMTSync(mapper);
6428 processPosition(mapper, x3, y3);
6429 processId(mapper, 3);
6430 processMTSync(mapper);
6431 processSync(mapper);
6432
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6435 motionArgs.action);
6436 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6439 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6444 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6445
6446 // Second finger up.
6447 x3 += 30; y3 -= 20;
6448 processPosition(mapper, x3, y3);
6449 processId(mapper, 3);
6450 processMTSync(mapper);
6451 processSync(mapper);
6452
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6455 motionArgs.action);
6456 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6457 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6459 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6462 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6464 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6465
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6468 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6469 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6472 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6473
6474 // Last finger up.
6475 processMTSync(mapper);
6476 processSync(mapper);
6477
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6480 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6484 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6485
6486 // Should not have sent any more keys or motions.
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6489}
6490
6491TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492 addConfigurationProperty("touch.deviceType", "touchScreen");
6493 prepareDisplay(DISPLAY_ORIENTATION_0);
6494 prepareAxes(POSITION | ID | SLOT);
6495 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006496 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006497
arthurhungdcef2dc2020-08-11 14:47:50 +08006498 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006499
6500 NotifyMotionArgs motionArgs;
6501
6502 // Two fingers down at once.
6503 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6504 processPosition(mapper, x1, y1);
6505 processId(mapper, 1);
6506 processSlot(mapper, 1);
6507 processPosition(mapper, x2, y2);
6508 processId(mapper, 2);
6509 processSync(mapper);
6510
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6513 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6517 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6518
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6521 motionArgs.action);
6522 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6523 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6525 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6528 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6530 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6531
6532 // Move.
6533 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6534 processSlot(mapper, 0);
6535 processPosition(mapper, x1, y1);
6536 processSlot(mapper, 1);
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(2), motionArgs.pointerCount);
6543 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6545 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6548 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6550 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6551
6552 // First finger up.
6553 x2 += 15; y2 -= 20;
6554 processSlot(mapper, 0);
6555 processId(mapper, -1);
6556 processSlot(mapper, 1);
6557 processPosition(mapper, x2, y2);
6558 processSync(mapper);
6559
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6561 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6562 motionArgs.action);
6563 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6564 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6566 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6569 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6571 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6572
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6576 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6579 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6580
6581 // Move.
6582 x2 += 20; y2 -= 25;
6583 processPosition(mapper, x2, y2);
6584 processSync(mapper);
6585
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6588 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6589 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6592 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6593
6594 // New finger down.
6595 int32_t x3 = 700, y3 = 300;
6596 processPosition(mapper, x2, y2);
6597 processSlot(mapper, 0);
6598 processId(mapper, 3);
6599 processPosition(mapper, x3, y3);
6600 processSync(mapper);
6601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6604 motionArgs.action);
6605 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6608 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6614
6615 // Second finger up.
6616 x3 += 30; y3 -= 20;
6617 processSlot(mapper, 1);
6618 processId(mapper, -1);
6619 processSlot(mapper, 0);
6620 processPosition(mapper, x3, y3);
6621 processSync(mapper);
6622
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6625 motionArgs.action);
6626 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6629 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6632 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6634 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6635
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6638 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6639 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6642 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6643
6644 // Last finger up.
6645 processId(mapper, -1);
6646 processSync(mapper);
6647
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6649 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6650 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6654 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6655
6656 // Should not have sent any more keys or motions.
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6659}
6660
6661TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006662 addConfigurationProperty("touch.deviceType", "touchScreen");
6663 prepareDisplay(DISPLAY_ORIENTATION_0);
6664 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006665 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006666
6667 // These calculations are based on the input device calibration documentation.
6668 int32_t rawX = 100;
6669 int32_t rawY = 200;
6670 int32_t rawTouchMajor = 7;
6671 int32_t rawTouchMinor = 6;
6672 int32_t rawToolMajor = 9;
6673 int32_t rawToolMinor = 8;
6674 int32_t rawPressure = 11;
6675 int32_t rawDistance = 0;
6676 int32_t rawOrientation = 3;
6677 int32_t id = 5;
6678
6679 float x = toDisplayX(rawX);
6680 float y = toDisplayY(rawY);
6681 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6682 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6683 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6684 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6685 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6686 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6687 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6688 float distance = float(rawDistance);
6689
6690 processPosition(mapper, rawX, rawY);
6691 processTouchMajor(mapper, rawTouchMajor);
6692 processTouchMinor(mapper, rawTouchMinor);
6693 processToolMajor(mapper, rawToolMajor);
6694 processToolMinor(mapper, rawToolMinor);
6695 processPressure(mapper, rawPressure);
6696 processOrientation(mapper, rawOrientation);
6697 processDistance(mapper, rawDistance);
6698 processId(mapper, id);
6699 processMTSync(mapper);
6700 processSync(mapper);
6701
6702 NotifyMotionArgs args;
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6704 ASSERT_EQ(0, args.pointerProperties[0].id);
6705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6706 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6707 orientation, distance));
6708}
6709
6710TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006711 addConfigurationProperty("touch.deviceType", "touchScreen");
6712 prepareDisplay(DISPLAY_ORIENTATION_0);
6713 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6714 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006715 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006716
6717 // These calculations are based on the input device calibration documentation.
6718 int32_t rawX = 100;
6719 int32_t rawY = 200;
6720 int32_t rawTouchMajor = 140;
6721 int32_t rawTouchMinor = 120;
6722 int32_t rawToolMajor = 180;
6723 int32_t rawToolMinor = 160;
6724
6725 float x = toDisplayX(rawX);
6726 float y = toDisplayY(rawY);
6727 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6728 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6729 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6730 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6731 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6732
6733 processPosition(mapper, rawX, rawY);
6734 processTouchMajor(mapper, rawTouchMajor);
6735 processTouchMinor(mapper, rawTouchMinor);
6736 processToolMajor(mapper, rawToolMajor);
6737 processToolMinor(mapper, rawToolMinor);
6738 processMTSync(mapper);
6739 processSync(mapper);
6740
6741 NotifyMotionArgs args;
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6744 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6745}
6746
6747TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006748 addConfigurationProperty("touch.deviceType", "touchScreen");
6749 prepareDisplay(DISPLAY_ORIENTATION_0);
6750 prepareAxes(POSITION | TOUCH | TOOL);
6751 addConfigurationProperty("touch.size.calibration", "diameter");
6752 addConfigurationProperty("touch.size.scale", "10");
6753 addConfigurationProperty("touch.size.bias", "160");
6754 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006755 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006756
6757 // These calculations are based on the input device calibration documentation.
6758 // Note: We only provide a single common touch/tool value because the device is assumed
6759 // not to emit separate values for each pointer (isSummed = 1).
6760 int32_t rawX = 100;
6761 int32_t rawY = 200;
6762 int32_t rawX2 = 150;
6763 int32_t rawY2 = 250;
6764 int32_t rawTouchMajor = 5;
6765 int32_t rawToolMajor = 8;
6766
6767 float x = toDisplayX(rawX);
6768 float y = toDisplayY(rawY);
6769 float x2 = toDisplayX(rawX2);
6770 float y2 = toDisplayY(rawY2);
6771 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6772 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6773 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6774
6775 processPosition(mapper, rawX, rawY);
6776 processTouchMajor(mapper, rawTouchMajor);
6777 processToolMajor(mapper, rawToolMajor);
6778 processMTSync(mapper);
6779 processPosition(mapper, rawX2, rawY2);
6780 processTouchMajor(mapper, rawTouchMajor);
6781 processToolMajor(mapper, rawToolMajor);
6782 processMTSync(mapper);
6783 processSync(mapper);
6784
6785 NotifyMotionArgs args;
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6788
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6790 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6791 args.action);
6792 ASSERT_EQ(size_t(2), args.pointerCount);
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6794 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6796 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6797}
6798
6799TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 addConfigurationProperty("touch.deviceType", "touchScreen");
6801 prepareDisplay(DISPLAY_ORIENTATION_0);
6802 prepareAxes(POSITION | TOUCH | TOOL);
6803 addConfigurationProperty("touch.size.calibration", "area");
6804 addConfigurationProperty("touch.size.scale", "43");
6805 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006806 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006807
6808 // These calculations are based on the input device calibration documentation.
6809 int32_t rawX = 100;
6810 int32_t rawY = 200;
6811 int32_t rawTouchMajor = 5;
6812 int32_t rawToolMajor = 8;
6813
6814 float x = toDisplayX(rawX);
6815 float y = toDisplayY(rawY);
6816 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6817 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6818 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6819
6820 processPosition(mapper, rawX, rawY);
6821 processTouchMajor(mapper, rawTouchMajor);
6822 processToolMajor(mapper, rawToolMajor);
6823 processMTSync(mapper);
6824 processSync(mapper);
6825
6826 NotifyMotionArgs args;
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6829 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6830}
6831
6832TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006833 addConfigurationProperty("touch.deviceType", "touchScreen");
6834 prepareDisplay(DISPLAY_ORIENTATION_0);
6835 prepareAxes(POSITION | PRESSURE);
6836 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6837 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006839
Michael Wrightaa449c92017-12-13 21:21:43 +00006840 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006841 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006842 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6843 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6844 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6845
Michael Wrightd02c5b62014-02-10 15:10:22 -08006846 // These calculations are based on the input device calibration documentation.
6847 int32_t rawX = 100;
6848 int32_t rawY = 200;
6849 int32_t rawPressure = 60;
6850
6851 float x = toDisplayX(rawX);
6852 float y = toDisplayY(rawY);
6853 float pressure = float(rawPressure) * 0.01f;
6854
6855 processPosition(mapper, rawX, rawY);
6856 processPressure(mapper, rawPressure);
6857 processMTSync(mapper);
6858 processSync(mapper);
6859
6860 NotifyMotionArgs args;
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6863 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6864}
6865
6866TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867 addConfigurationProperty("touch.deviceType", "touchScreen");
6868 prepareDisplay(DISPLAY_ORIENTATION_0);
6869 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006871
6872 NotifyMotionArgs motionArgs;
6873 NotifyKeyArgs keyArgs;
6874
6875 processId(mapper, 1);
6876 processPosition(mapper, 100, 200);
6877 processSync(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6880 ASSERT_EQ(0, motionArgs.buttonState);
6881
6882 // press BTN_LEFT, release BTN_LEFT
6883 processKey(mapper, BTN_LEFT, 1);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6887 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6888
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6891 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6892
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 processKey(mapper, BTN_LEFT, 0);
6894 processSync(mapper);
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006896 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006898
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006901 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006902
6903 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6904 processKey(mapper, BTN_RIGHT, 1);
6905 processKey(mapper, BTN_MIDDLE, 1);
6906 processSync(mapper);
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6909 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6910 motionArgs.buttonState);
6911
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6914 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6915
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6918 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6919 motionArgs.buttonState);
6920
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921 processKey(mapper, BTN_RIGHT, 0);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006926
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006929 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006930
6931 processKey(mapper, BTN_MIDDLE, 0);
6932 processSync(mapper);
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006935 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006936
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006939 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940
6941 // press BTN_BACK, release BTN_BACK
6942 processKey(mapper, BTN_BACK, 1);
6943 processSync(mapper);
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6945 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6946 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006947
Michael Wrightd02c5b62014-02-10 15:10:22 -08006948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006950 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6951
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6954 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955
6956 processKey(mapper, BTN_BACK, 0);
6957 processSync(mapper);
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006960 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006961
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006964 ASSERT_EQ(0, motionArgs.buttonState);
6965
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6968 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6969
6970 // press BTN_SIDE, release BTN_SIDE
6971 processKey(mapper, BTN_SIDE, 1);
6972 processSync(mapper);
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6974 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6975 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006976
Michael Wrightd02c5b62014-02-10 15:10:22 -08006977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006979 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6980
6981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6982 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6983 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006984
6985 processKey(mapper, BTN_SIDE, 0);
6986 processSync(mapper);
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006990
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006993 ASSERT_EQ(0, motionArgs.buttonState);
6994
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6996 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6997 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6998
6999 // press BTN_FORWARD, release BTN_FORWARD
7000 processKey(mapper, BTN_FORWARD, 1);
7001 processSync(mapper);
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7003 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7004 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007005
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007008 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7009
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7012 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013
7014 processKey(mapper, BTN_FORWARD, 0);
7015 processSync(mapper);
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007017 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007018 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007019
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007022 ASSERT_EQ(0, motionArgs.buttonState);
7023
Michael Wrightd02c5b62014-02-10 15:10:22 -08007024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7025 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7026 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7027
7028 // press BTN_EXTRA, release BTN_EXTRA
7029 processKey(mapper, BTN_EXTRA, 1);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7032 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7033 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007034
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007037 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7038
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7041 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042
7043 processKey(mapper, BTN_EXTRA, 0);
7044 processSync(mapper);
7045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007048
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007051 ASSERT_EQ(0, motionArgs.buttonState);
7052
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7054 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7055 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7056
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7058
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059 // press BTN_STYLUS, release BTN_STYLUS
7060 processKey(mapper, BTN_STYLUS, 1);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007064 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7065
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7068 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069
7070 processKey(mapper, BTN_STYLUS, 0);
7071 processSync(mapper);
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007075
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007078 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079
7080 // press BTN_STYLUS2, release BTN_STYLUS2
7081 processKey(mapper, BTN_STYLUS2, 1);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007085 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7086
7087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7089 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090
7091 processKey(mapper, BTN_STYLUS2, 0);
7092 processSync(mapper);
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007096
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007099 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100
7101 // release touch
7102 processId(mapper, -1);
7103 processSync(mapper);
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7105 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7106 ASSERT_EQ(0, motionArgs.buttonState);
7107}
7108
7109TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110 addConfigurationProperty("touch.deviceType", "touchScreen");
7111 prepareDisplay(DISPLAY_ORIENTATION_0);
7112 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007113 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114
7115 NotifyMotionArgs motionArgs;
7116
7117 // default tool type is finger
7118 processId(mapper, 1);
7119 processPosition(mapper, 100, 200);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7124
7125 // eraser
7126 processKey(mapper, BTN_TOOL_RUBBER, 1);
7127 processSync(mapper);
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7131
7132 // stylus
7133 processKey(mapper, BTN_TOOL_RUBBER, 0);
7134 processKey(mapper, BTN_TOOL_PEN, 1);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7139
7140 // brush
7141 processKey(mapper, BTN_TOOL_PEN, 0);
7142 processKey(mapper, BTN_TOOL_BRUSH, 1);
7143 processSync(mapper);
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7147
7148 // pencil
7149 processKey(mapper, BTN_TOOL_BRUSH, 0);
7150 processKey(mapper, BTN_TOOL_PENCIL, 1);
7151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7155
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007156 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 processKey(mapper, BTN_TOOL_PENCIL, 0);
7158 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7159 processSync(mapper);
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7163
7164 // mouse
7165 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7166 processKey(mapper, BTN_TOOL_MOUSE, 1);
7167 processSync(mapper);
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7171
7172 // lens
7173 processKey(mapper, BTN_TOOL_MOUSE, 0);
7174 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
7179
7180 // double-tap
7181 processKey(mapper, BTN_TOOL_LENS, 0);
7182 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7183 processSync(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7187
7188 // triple-tap
7189 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7190 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7195
7196 // quad-tap
7197 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7198 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7199 processSync(mapper);
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7203
7204 // finger
7205 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7206 processKey(mapper, BTN_TOOL_FINGER, 1);
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 // stylus trumps finger
7213 processKey(mapper, BTN_TOOL_PEN, 1);
7214 processSync(mapper);
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7218
7219 // eraser trumps stylus
7220 processKey(mapper, BTN_TOOL_RUBBER, 1);
7221 processSync(mapper);
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7225
7226 // mouse trumps eraser
7227 processKey(mapper, BTN_TOOL_MOUSE, 1);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7232
7233 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7234 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7235 processSync(mapper);
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7239
7240 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7241 processToolType(mapper, MT_TOOL_PEN);
7242 processSync(mapper);
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7246
7247 // back to default tool type
7248 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7249 processKey(mapper, BTN_TOOL_MOUSE, 0);
7250 processKey(mapper, BTN_TOOL_RUBBER, 0);
7251 processKey(mapper, BTN_TOOL_PEN, 0);
7252 processKey(mapper, BTN_TOOL_FINGER, 0);
7253 processSync(mapper);
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7257}
7258
7259TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007260 addConfigurationProperty("touch.deviceType", "touchScreen");
7261 prepareDisplay(DISPLAY_ORIENTATION_0);
7262 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007263 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007264 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007265
7266 NotifyMotionArgs motionArgs;
7267
7268 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7269 processId(mapper, 1);
7270 processPosition(mapper, 100, 200);
7271 processSync(mapper);
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7275 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7276
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7278 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7280 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7281
7282 // move a little
7283 processPosition(mapper, 150, 250);
7284 processSync(mapper);
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7288 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7289
7290 // down when BTN_TOUCH is pressed, pressure defaults to 1
7291 processKey(mapper, BTN_TOUCH, 1);
7292 processSync(mapper);
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7294 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7296 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7297
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7301 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7302
7303 // up when BTN_TOUCH is released, hover restored
7304 processKey(mapper, BTN_TOUCH, 0);
7305 processSync(mapper);
7306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7307 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7309 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7310
7311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7312 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7314 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7315
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7319 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7320
7321 // exit hover when pointer goes away
7322 processId(mapper, -1);
7323 processSync(mapper);
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7325 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7327 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7328}
7329
7330TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007331 addConfigurationProperty("touch.deviceType", "touchScreen");
7332 prepareDisplay(DISPLAY_ORIENTATION_0);
7333 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335
7336 NotifyMotionArgs motionArgs;
7337
7338 // initially hovering because pressure is 0
7339 processId(mapper, 1);
7340 processPosition(mapper, 100, 200);
7341 processPressure(mapper, 0);
7342 processSync(mapper);
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7346 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7347
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7351 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7352
7353 // move a little
7354 processPosition(mapper, 150, 250);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7359 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7360
7361 // down when pressure becomes non-zero
7362 processPressure(mapper, RAW_PRESSURE_MAX);
7363 processSync(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7367 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7368
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7372 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7373
7374 // up when pressure becomes 0, hover restored
7375 processPressure(mapper, 0);
7376 processSync(mapper);
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7378 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7380 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7381
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7383 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7385 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7386
7387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7388 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7390 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7391
7392 // exit hover when pointer goes away
7393 processId(mapper, -1);
7394 processSync(mapper);
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7398 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7399}
7400
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007401/**
7402 * Set the input device port <--> display port associations, and check that the
7403 * events are routed to the display that matches the display port.
7404 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7405 */
7406TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007407 const std::string usb2 = "USB2";
7408 const uint8_t hdmi1 = 0;
7409 const uint8_t hdmi2 = 1;
7410 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007411 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007412
7413 addConfigurationProperty("touch.deviceType", "touchScreen");
7414 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007415 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007416
7417 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7418 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7419
7420 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7421 // for this input device is specified, and the matching viewport is not present,
7422 // the input device should be disabled (at the mapper level).
7423
7424 // Add viewport for display 2 on hdmi2
7425 prepareSecondaryDisplay(type, hdmi2);
7426 // Send a touch event
7427 processPosition(mapper, 100, 100);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7430
7431 // Add viewport for display 1 on hdmi1
7432 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7433 // Send a touch event again
7434 processPosition(mapper, 100, 100);
7435 processSync(mapper);
7436
7437 NotifyMotionArgs args;
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7439 ASSERT_EQ(DISPLAY_ID, args.displayId);
7440}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007441
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007442TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007443 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007444 std::shared_ptr<FakePointerController> fakePointerController =
7445 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007446 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007447 fakePointerController->setPosition(100, 200);
7448 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007449 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7450
Garfield Tan888a6a42020-01-09 11:39:16 -08007451 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007452 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007453
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007454 prepareDisplay(DISPLAY_ORIENTATION_0);
7455 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007456 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007457
7458 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007459 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007460
7461 NotifyMotionArgs motionArgs;
7462 processPosition(mapper, 100, 100);
7463 processSync(mapper);
7464
7465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7466 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7467 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7468}
7469
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007470/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007471 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7472 */
7473TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7474 addConfigurationProperty("touch.deviceType", "touchScreen");
7475 prepareAxes(POSITION);
7476 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7477
7478 prepareDisplay(DISPLAY_ORIENTATION_0);
7479 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7480 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7481 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7482 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7483
7484 NotifyMotionArgs args;
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7486 ASSERT_EQ(26, args.readTime);
7487
7488 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7489 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7490 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7491
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7493 ASSERT_EQ(33, args.readTime);
7494}
7495
7496/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007497 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7498 * events should not be delivered to the listener.
7499 */
7500TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7501 addConfigurationProperty("touch.deviceType", "touchScreen");
7502 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7503 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7504 ViewportType::INTERNAL);
7505 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7506 prepareAxes(POSITION);
7507 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7508
7509 NotifyMotionArgs motionArgs;
7510 processPosition(mapper, 100, 100);
7511 processSync(mapper);
7512
7513 mFakeListener->assertNotifyMotionWasNotCalled();
7514}
7515
Garfield Tanc734e4f2021-01-15 20:01:39 -08007516TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7517 addConfigurationProperty("touch.deviceType", "touchScreen");
7518 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7519 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7520 ViewportType::INTERNAL);
7521 std::optional<DisplayViewport> optionalDisplayViewport =
7522 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7523 ASSERT_TRUE(optionalDisplayViewport.has_value());
7524 DisplayViewport displayViewport = *optionalDisplayViewport;
7525
7526 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7527 prepareAxes(POSITION);
7528 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7529
7530 // Finger down
7531 int32_t x = 100, y = 100;
7532 processPosition(mapper, x, y);
7533 processSync(mapper);
7534
7535 NotifyMotionArgs motionArgs;
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7538
7539 // Deactivate display viewport
7540 displayViewport.isActive = false;
7541 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7542 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7543
7544 // Finger move
7545 x += 10, y += 10;
7546 processPosition(mapper, x, y);
7547 processSync(mapper);
7548
7549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7550 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7551
7552 // Reactivate display viewport
7553 displayViewport.isActive = true;
7554 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7555 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7556
7557 // Finger move again
7558 x += 10, y += 10;
7559 processPosition(mapper, x, y);
7560 processSync(mapper);
7561
7562 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7563 // no pointer on the touch device.
7564 mFakeListener->assertNotifyMotionWasNotCalled();
7565}
7566
Arthur Hung7c645402019-01-25 17:45:42 +08007567TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7568 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007569 prepareAxes(POSITION | ID | SLOT);
7570 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007571 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007572
7573 // Create the second touch screen device, and enable multi fingers.
7574 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007575 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007576 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007577 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007578 std::shared_ptr<InputDevice> device2 =
7579 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7580 Flags<InputDeviceClass>(0));
7581
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007582 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7583 0 /*flat*/, 0 /*fuzz*/);
7584 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7585 0 /*flat*/, 0 /*fuzz*/);
7586 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7587 0 /*flat*/, 0 /*fuzz*/);
7588 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7589 0 /*flat*/, 0 /*fuzz*/);
7590 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7591 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7592 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007593
7594 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007595 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007596 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7597 device2->reset(ARBITRARY_TIME);
7598
7599 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007600 std::shared_ptr<FakePointerController> fakePointerController =
7601 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007602 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7603 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7604
7605 // Setup policy for associated displays and show touches.
7606 const uint8_t hdmi1 = 0;
7607 const uint8_t hdmi2 = 1;
7608 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7609 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7610 mFakePolicy->setShowTouches(true);
7611
7612 // Create displays.
7613 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007614 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007615
7616 // Default device will reconfigure above, need additional reconfiguration for another device.
7617 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007618 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007619
7620 // Two fingers down at default display.
7621 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7622 processPosition(mapper, x1, y1);
7623 processId(mapper, 1);
7624 processSlot(mapper, 1);
7625 processPosition(mapper, x2, y2);
7626 processId(mapper, 2);
7627 processSync(mapper);
7628
7629 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7630 fakePointerController->getSpots().find(DISPLAY_ID);
7631 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7632 ASSERT_EQ(size_t(2), iter->second.size());
7633
7634 // Two fingers down at second display.
7635 processPosition(mapper2, x1, y1);
7636 processId(mapper2, 1);
7637 processSlot(mapper2, 1);
7638 processPosition(mapper2, x2, y2);
7639 processId(mapper2, 2);
7640 processSync(mapper2);
7641
7642 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7643 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7644 ASSERT_EQ(size_t(2), iter->second.size());
7645}
7646
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007647TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007648 prepareAxes(POSITION);
7649 addConfigurationProperty("touch.deviceType", "touchScreen");
7650 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007651 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007652
7653 NotifyMotionArgs motionArgs;
7654 // Unrotated video frame
7655 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7656 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007657 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007658 processPosition(mapper, 100, 200);
7659 processSync(mapper);
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7661 ASSERT_EQ(frames, motionArgs.videoFrames);
7662
7663 // Subsequent touch events should not have any videoframes
7664 // This is implemented separately in FakeEventHub,
7665 // but that should match the behaviour of TouchVideoDevice.
7666 processPosition(mapper, 200, 200);
7667 processSync(mapper);
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7669 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7670}
7671
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007672TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007673 prepareAxes(POSITION);
7674 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007675 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007676 // Unrotated video frame
7677 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7678 NotifyMotionArgs motionArgs;
7679
7680 // Test all 4 orientations
7681 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7682 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7683 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7684 clearViewports();
7685 prepareDisplay(orientation);
7686 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007687 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007688 processPosition(mapper, 100, 200);
7689 processSync(mapper);
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7691 frames[0].rotate(orientation);
7692 ASSERT_EQ(frames, motionArgs.videoFrames);
7693 }
7694}
7695
7696TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007697 prepareAxes(POSITION);
7698 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007700 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7701 // so mix these.
7702 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7703 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7704 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7705 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7706 NotifyMotionArgs motionArgs;
7707
7708 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007709 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007710 processPosition(mapper, 100, 200);
7711 processSync(mapper);
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7713 std::for_each(frames.begin(), frames.end(),
7714 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7715 ASSERT_EQ(frames, motionArgs.videoFrames);
7716}
7717
Arthur Hung9da14732019-09-02 16:16:58 +08007718/**
7719 * If we had defined port associations, but the viewport is not ready, the touch device would be
7720 * expected to be disabled, and it should be enabled after the viewport has found.
7721 */
7722TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007723 constexpr uint8_t hdmi2 = 1;
7724 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007725 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007726
7727 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7728
7729 addConfigurationProperty("touch.deviceType", "touchScreen");
7730 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007731 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007732
7733 ASSERT_EQ(mDevice->isEnabled(), false);
7734
7735 // Add display on hdmi2, the device should be enabled and can receive touch event.
7736 prepareSecondaryDisplay(type, hdmi2);
7737 ASSERT_EQ(mDevice->isEnabled(), true);
7738
7739 // Send a touch event.
7740 processPosition(mapper, 100, 100);
7741 processSync(mapper);
7742
7743 NotifyMotionArgs args;
7744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7745 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7746}
7747
Arthur Hung6cd19a42019-08-30 19:04:12 +08007748
Arthur Hung6cd19a42019-08-30 19:04:12 +08007749
Arthur Hung421eb1c2020-01-16 00:09:42 +08007750TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007751 addConfigurationProperty("touch.deviceType", "touchScreen");
7752 prepareDisplay(DISPLAY_ORIENTATION_0);
7753 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007754 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007755
7756 NotifyMotionArgs motionArgs;
7757
7758 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7759 // finger down
7760 processId(mapper, 1);
7761 processPosition(mapper, x1, y1);
7762 processSync(mapper);
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7766
7767 // finger move
7768 processId(mapper, 1);
7769 processPosition(mapper, x2, y2);
7770 processSync(mapper);
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7774
7775 // finger up.
7776 processId(mapper, -1);
7777 processSync(mapper);
7778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7779 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7781
7782 // new finger down
7783 processId(mapper, 1);
7784 processPosition(mapper, x3, y3);
7785 processSync(mapper);
7786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7789}
7790
7791/**
arthurhungcc7f9802020-04-30 17:55:40 +08007792 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7793 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007794 */
arthurhungcc7f9802020-04-30 17:55:40 +08007795TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007796 addConfigurationProperty("touch.deviceType", "touchScreen");
7797 prepareDisplay(DISPLAY_ORIENTATION_0);
7798 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007800
7801 NotifyMotionArgs motionArgs;
7802
7803 // default tool type is finger
7804 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007805 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007806 processPosition(mapper, x1, y1);
7807 processSync(mapper);
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7811
7812 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7813 processToolType(mapper, MT_TOOL_PALM);
7814 processSync(mapper);
7815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7816 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7817
7818 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007819 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007820 processPosition(mapper, x2, y2);
7821 processSync(mapper);
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7823
7824 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007825 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007826 processSync(mapper);
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7828
7829 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007830 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007831 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007832 processPosition(mapper, x3, y3);
7833 processSync(mapper);
7834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7835 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7837}
7838
arthurhungbf89a482020-04-17 17:37:55 +08007839/**
arthurhungcc7f9802020-04-30 17:55:40 +08007840 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7841 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007842 */
arthurhungcc7f9802020-04-30 17:55:40 +08007843TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007844 addConfigurationProperty("touch.deviceType", "touchScreen");
7845 prepareDisplay(DISPLAY_ORIENTATION_0);
7846 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7847 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7848
7849 NotifyMotionArgs motionArgs;
7850
7851 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007852 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7853 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007854 processPosition(mapper, x1, y1);
7855 processSync(mapper);
7856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7859
7860 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007861 processSlot(mapper, SECOND_SLOT);
7862 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007863 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007864 processSync(mapper);
7865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7866 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7867 motionArgs.action);
7868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7869
7870 // If the tool type of the first finger changes to MT_TOOL_PALM,
7871 // we expect to receive ACTION_POINTER_UP with cancel flag.
7872 processSlot(mapper, FIRST_SLOT);
7873 processId(mapper, FIRST_TRACKING_ID);
7874 processToolType(mapper, MT_TOOL_PALM);
7875 processSync(mapper);
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7877 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7878 motionArgs.action);
7879 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7880
7881 // The following MOVE events of second finger should be processed.
7882 processSlot(mapper, SECOND_SLOT);
7883 processId(mapper, SECOND_TRACKING_ID);
7884 processPosition(mapper, x2 + 1, y2 + 1);
7885 processSync(mapper);
7886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7888 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7889
7890 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7891 // it. Second finger receive move.
7892 processSlot(mapper, FIRST_SLOT);
7893 processId(mapper, INVALID_TRACKING_ID);
7894 processSync(mapper);
7895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7897 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7898
7899 // Second finger keeps moving.
7900 processSlot(mapper, SECOND_SLOT);
7901 processId(mapper, SECOND_TRACKING_ID);
7902 processPosition(mapper, x2 + 2, y2 + 2);
7903 processSync(mapper);
7904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7906 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7907
7908 // Second finger up.
7909 processId(mapper, INVALID_TRACKING_ID);
7910 processSync(mapper);
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7912 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7913 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7914}
7915
7916/**
7917 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7918 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7919 */
7920TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7921 addConfigurationProperty("touch.deviceType", "touchScreen");
7922 prepareDisplay(DISPLAY_ORIENTATION_0);
7923 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7924 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7925
7926 NotifyMotionArgs motionArgs;
7927
7928 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7929 // First finger down.
7930 processId(mapper, FIRST_TRACKING_ID);
7931 processPosition(mapper, x1, y1);
7932 processSync(mapper);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7936
7937 // Second finger down.
7938 processSlot(mapper, SECOND_SLOT);
7939 processId(mapper, SECOND_TRACKING_ID);
7940 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007941 processSync(mapper);
7942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7943 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7944 motionArgs.action);
7945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7946
arthurhungcc7f9802020-04-30 17:55:40 +08007947 // If the tool type of the first finger changes to MT_TOOL_PALM,
7948 // we expect to receive ACTION_POINTER_UP with cancel flag.
7949 processSlot(mapper, FIRST_SLOT);
7950 processId(mapper, FIRST_TRACKING_ID);
7951 processToolType(mapper, MT_TOOL_PALM);
7952 processSync(mapper);
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7954 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7955 motionArgs.action);
7956 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7957
7958 // Second finger keeps moving.
7959 processSlot(mapper, SECOND_SLOT);
7960 processId(mapper, SECOND_TRACKING_ID);
7961 processPosition(mapper, x2 + 1, y2 + 1);
7962 processSync(mapper);
7963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7965
7966 // second finger becomes palm, receive cancel due to only 1 finger is active.
7967 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007968 processToolType(mapper, MT_TOOL_PALM);
7969 processSync(mapper);
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7972
arthurhungcc7f9802020-04-30 17:55:40 +08007973 // third finger down.
7974 processSlot(mapper, THIRD_SLOT);
7975 processId(mapper, THIRD_TRACKING_ID);
7976 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007977 processPosition(mapper, x3, y3);
7978 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7980 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007982 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7983
7984 // third finger move
7985 processId(mapper, THIRD_TRACKING_ID);
7986 processPosition(mapper, x3 + 1, y3 + 1);
7987 processSync(mapper);
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7990
7991 // first finger up, third finger receive move.
7992 processSlot(mapper, FIRST_SLOT);
7993 processId(mapper, INVALID_TRACKING_ID);
7994 processSync(mapper);
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7997 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7998
7999 // second finger up, third finger receive move.
8000 processSlot(mapper, SECOND_SLOT);
8001 processId(mapper, INVALID_TRACKING_ID);
8002 processSync(mapper);
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8005 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8006
8007 // third finger up.
8008 processSlot(mapper, THIRD_SLOT);
8009 processId(mapper, INVALID_TRACKING_ID);
8010 processSync(mapper);
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8012 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8013 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8014}
8015
8016/**
8017 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8018 * and the active finger could still be allowed to receive the events
8019 */
8020TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8021 addConfigurationProperty("touch.deviceType", "touchScreen");
8022 prepareDisplay(DISPLAY_ORIENTATION_0);
8023 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8025
8026 NotifyMotionArgs motionArgs;
8027
8028 // default tool type is finger
8029 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8030 processId(mapper, FIRST_TRACKING_ID);
8031 processPosition(mapper, x1, y1);
8032 processSync(mapper);
8033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8036
8037 // Second finger down.
8038 processSlot(mapper, SECOND_SLOT);
8039 processId(mapper, SECOND_TRACKING_ID);
8040 processPosition(mapper, x2, y2);
8041 processSync(mapper);
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8043 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8044 motionArgs.action);
8045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8046
8047 // If the tool type of the second finger changes to MT_TOOL_PALM,
8048 // we expect to receive ACTION_POINTER_UP with cancel flag.
8049 processId(mapper, SECOND_TRACKING_ID);
8050 processToolType(mapper, MT_TOOL_PALM);
8051 processSync(mapper);
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8053 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8054 motionArgs.action);
8055 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8056
8057 // The following MOVE event should be processed.
8058 processSlot(mapper, FIRST_SLOT);
8059 processId(mapper, FIRST_TRACKING_ID);
8060 processPosition(mapper, x1 + 1, y1 + 1);
8061 processSync(mapper);
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8064 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8065
8066 // second finger up.
8067 processSlot(mapper, SECOND_SLOT);
8068 processId(mapper, INVALID_TRACKING_ID);
8069 processSync(mapper);
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8072
8073 // first finger keep moving
8074 processSlot(mapper, FIRST_SLOT);
8075 processId(mapper, FIRST_TRACKING_ID);
8076 processPosition(mapper, x1 + 2, y1 + 2);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8080
8081 // first finger up.
8082 processId(mapper, INVALID_TRACKING_ID);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8086 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008087}
8088
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008089// --- MultiTouchInputMapperTest_ExternalDevice ---
8090
8091class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8092protected:
Chris Yea52ade12020-08-27 16:49:20 -07008093 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008094};
8095
8096/**
8097 * Expect fallback to internal viewport if device is external and external viewport is not present.
8098 */
8099TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8100 prepareAxes(POSITION);
8101 addConfigurationProperty("touch.deviceType", "touchScreen");
8102 prepareDisplay(DISPLAY_ORIENTATION_0);
8103 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8104
8105 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8106
8107 NotifyMotionArgs motionArgs;
8108
8109 // Expect the event to be sent to the internal viewport,
8110 // because an external viewport is not present.
8111 processPosition(mapper, 100, 100);
8112 processSync(mapper);
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8115
8116 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008117 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008118 processPosition(mapper, 100, 100);
8119 processSync(mapper);
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8121 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8122}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008123
8124/**
8125 * Test touch should not work if outside of surface.
8126 */
8127class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8128protected:
8129 void halfDisplayToCenterHorizontal(int32_t orientation) {
8130 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008131 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008132
8133 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8134 internalViewport->orientation = orientation;
8135 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8136 internalViewport->logicalLeft = 0;
8137 internalViewport->logicalTop = 0;
8138 internalViewport->logicalRight = DISPLAY_HEIGHT;
8139 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8140
8141 internalViewport->physicalLeft = 0;
8142 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8143 internalViewport->physicalRight = DISPLAY_HEIGHT;
8144 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8145
8146 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8147 internalViewport->deviceHeight = DISPLAY_WIDTH;
8148 } else {
8149 internalViewport->logicalLeft = 0;
8150 internalViewport->logicalTop = 0;
8151 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8152 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8153
8154 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8155 internalViewport->physicalTop = 0;
8156 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8157 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8158
8159 internalViewport->deviceWidth = DISPLAY_WIDTH;
8160 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8161 }
8162
8163 mFakePolicy->updateViewport(internalViewport.value());
8164 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8165 }
8166
arthurhung5d547942020-12-14 17:04:45 +08008167 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8168 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008169 int32_t yExpected) {
8170 // touch on outside area should not work.
8171 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8174
8175 // touch on inside area should receive the event.
8176 NotifyMotionArgs args;
8177 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8180 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8181 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8182
8183 // Reset.
8184 mapper.reset(ARBITRARY_TIME);
8185 }
8186};
8187
arthurhung5d547942020-12-14 17:04:45 +08008188TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008189 addConfigurationProperty("touch.deviceType", "touchScreen");
8190 prepareDisplay(DISPLAY_ORIENTATION_0);
8191 prepareAxes(POSITION);
8192 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8193
8194 // Touch on center of normal display should work.
8195 const int32_t x = DISPLAY_WIDTH / 4;
8196 const int32_t y = DISPLAY_HEIGHT / 2;
8197 processPosition(mapper, toRawX(x), toRawY(y));
8198 processSync(mapper);
8199 NotifyMotionArgs args;
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8202 0.0f, 0.0f, 0.0f, 0.0f));
8203 // Reset.
8204 mapper.reset(ARBITRARY_TIME);
8205
8206 // Let physical display be different to device, and make surface and physical could be 1:1.
8207 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8208
8209 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8210 const int32_t yExpected = y;
8211 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8212}
8213
arthurhung5d547942020-12-14 17:04:45 +08008214TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008215 addConfigurationProperty("touch.deviceType", "touchScreen");
8216 prepareDisplay(DISPLAY_ORIENTATION_0);
8217 prepareAxes(POSITION);
8218 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8219
8220 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8221 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8222
8223 const int32_t x = DISPLAY_WIDTH / 4;
8224 const int32_t y = DISPLAY_HEIGHT / 2;
8225
8226 // expect x/y = swap x/y then reverse y.
8227 const int32_t xExpected = y;
8228 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8229 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8230}
8231
arthurhung5d547942020-12-14 17:04:45 +08008232TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008233 addConfigurationProperty("touch.deviceType", "touchScreen");
8234 prepareDisplay(DISPLAY_ORIENTATION_0);
8235 prepareAxes(POSITION);
8236 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8237
8238 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8239 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8240
8241 const int32_t x = DISPLAY_WIDTH / 4;
8242 const int32_t y = DISPLAY_HEIGHT / 2;
8243
8244 // expect x/y = swap x/y then reverse x.
8245 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8246 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8247 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8248}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008249
arthurhunga36b28e2020-12-29 20:28:15 +08008250TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8251 addConfigurationProperty("touch.deviceType", "touchScreen");
8252 prepareDisplay(DISPLAY_ORIENTATION_0);
8253 prepareAxes(POSITION);
8254 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8255
8256 const int32_t x = 0;
8257 const int32_t y = 0;
8258
8259 const int32_t xExpected = x;
8260 const int32_t yExpected = y;
8261 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8262
8263 clearViewports();
8264 prepareDisplay(DISPLAY_ORIENTATION_90);
8265 // expect x/y = swap x/y then reverse y.
8266 const int32_t xExpected90 = y;
8267 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8268 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8269
8270 clearViewports();
8271 prepareDisplay(DISPLAY_ORIENTATION_270);
8272 // expect x/y = swap x/y then reverse x.
8273 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8274 const int32_t yExpected270 = x;
8275 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8276}
8277
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008278TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8279 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8280 std::shared_ptr<FakePointerController> fakePointerController =
8281 std::make_shared<FakePointerController>();
8282 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8283 fakePointerController->setPosition(0, 0);
8284 fakePointerController->setButtonState(0);
8285
8286 // prepare device and capture
8287 prepareDisplay(DISPLAY_ORIENTATION_0);
8288 prepareAxes(POSITION | ID | SLOT);
8289 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8290 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8291 mFakePolicy->setPointerCapture(true);
8292 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8293 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8294
8295 // captured touchpad should be a touchpad source
8296 NotifyDeviceResetArgs resetArgs;
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8298 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8299
Chris Yef74dc422020-09-02 22:41:50 -07008300 InputDeviceInfo deviceInfo;
8301 mDevice->getDeviceInfo(&deviceInfo);
8302
8303 const InputDeviceInfo::MotionRange* relRangeX =
8304 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8305 ASSERT_NE(relRangeX, nullptr);
8306 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8307 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8308 const InputDeviceInfo::MotionRange* relRangeY =
8309 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8310 ASSERT_NE(relRangeY, nullptr);
8311 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8312 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8313
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008314 // run captured pointer tests - note that this is unscaled, so input listener events should be
8315 // identical to what the hardware sends (accounting for any
8316 // calibration).
8317 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008318 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008319 processId(mapper, 1);
8320 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8321 processKey(mapper, BTN_TOUCH, 1);
8322 processSync(mapper);
8323
8324 // expect coord[0] to contain initial location of touch 0
8325 NotifyMotionArgs args;
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8327 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8328 ASSERT_EQ(1U, args.pointerCount);
8329 ASSERT_EQ(0, args.pointerProperties[0].id);
8330 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8331 ASSERT_NO_FATAL_FAILURE(
8332 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8333
8334 // FINGER 1 DOWN
8335 processSlot(mapper, 1);
8336 processId(mapper, 2);
8337 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8338 processSync(mapper);
8339
8340 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008342 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8343 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008344 ASSERT_EQ(2U, args.pointerCount);
8345 ASSERT_EQ(0, args.pointerProperties[0].id);
8346 ASSERT_EQ(1, args.pointerProperties[1].id);
8347 ASSERT_NO_FATAL_FAILURE(
8348 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8349 ASSERT_NO_FATAL_FAILURE(
8350 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8351
8352 // FINGER 1 MOVE
8353 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8354 processSync(mapper);
8355
8356 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8357 // from move
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8360 ASSERT_NO_FATAL_FAILURE(
8361 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8362 ASSERT_NO_FATAL_FAILURE(
8363 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8364
8365 // FINGER 0 MOVE
8366 processSlot(mapper, 0);
8367 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8368 processSync(mapper);
8369
8370 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8373 ASSERT_NO_FATAL_FAILURE(
8374 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8375 ASSERT_NO_FATAL_FAILURE(
8376 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8377
8378 // BUTTON DOWN
8379 processKey(mapper, BTN_LEFT, 1);
8380 processSync(mapper);
8381
8382 // touchinputmapper design sends a move before button press
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8387
8388 // BUTTON UP
8389 processKey(mapper, BTN_LEFT, 0);
8390 processSync(mapper);
8391
8392 // touchinputmapper design sends a move after button release
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8397
8398 // FINGER 0 UP
8399 processId(mapper, -1);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8403
8404 // FINGER 1 MOVE
8405 processSlot(mapper, 1);
8406 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8407 processSync(mapper);
8408
8409 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8412 ASSERT_EQ(1U, args.pointerCount);
8413 ASSERT_EQ(1, args.pointerProperties[0].id);
8414 ASSERT_NO_FATAL_FAILURE(
8415 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8416
8417 // FINGER 1 UP
8418 processId(mapper, -1);
8419 processKey(mapper, BTN_TOUCH, 0);
8420 processSync(mapper);
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8422 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8423
8424 // non captured touchpad should be a mouse source
8425 mFakePolicy->setPointerCapture(false);
8426 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8428 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8429}
8430
8431TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8432 std::shared_ptr<FakePointerController> fakePointerController =
8433 std::make_shared<FakePointerController>();
8434 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8435 fakePointerController->setPosition(0, 0);
8436 fakePointerController->setButtonState(0);
8437
8438 // prepare device and capture
8439 prepareDisplay(DISPLAY_ORIENTATION_0);
8440 prepareAxes(POSITION | ID | SLOT);
8441 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8442 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8443 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8444 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8445 // run uncaptured pointer tests - pushes out generic events
8446 // FINGER 0 DOWN
8447 processId(mapper, 3);
8448 processPosition(mapper, 100, 100);
8449 processKey(mapper, BTN_TOUCH, 1);
8450 processSync(mapper);
8451
8452 // start at (100,100), cursor should be at (0,0) * scale
8453 NotifyMotionArgs args;
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8456 ASSERT_NO_FATAL_FAILURE(
8457 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8458
8459 // FINGER 0 MOVE
8460 processPosition(mapper, 200, 200);
8461 processSync(mapper);
8462
8463 // compute scaling to help with touch position checking
8464 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8465 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8466 float scale =
8467 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8468
8469 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8473 0, 0, 0, 0, 0, 0, 0));
8474}
8475
8476TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8477 std::shared_ptr<FakePointerController> fakePointerController =
8478 std::make_shared<FakePointerController>();
8479
8480 prepareDisplay(DISPLAY_ORIENTATION_0);
8481 prepareAxes(POSITION | ID | SLOT);
8482 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8483 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8484 mFakePolicy->setPointerCapture(false);
8485 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8486
8487 // uncaptured touchpad should be a pointer device
8488 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8489
8490 // captured touchpad should be a touchpad device
8491 mFakePolicy->setPointerCapture(true);
8492 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8493 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8494}
8495
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496} // namespace android