blob: acbccea705be05572e024e44336421f2e9d15983 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
25#include <SingleTouchInputMapper.h>
26#include <SwitchInputMapper.h>
27#include <TestInputListener.h>
28#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080029#include <UinputDevice.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070030#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080032#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <math.h>
34
Michael Wright17db18e2020-06-26 20:51:44 +010035#include <memory>
36
Michael Wrightd02c5b62014-02-10 15:10:22 -080037namespace android {
38
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070040using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070041
42// Timeout for waiting for an expected event
43static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
44
Michael Wrightd02c5b62014-02-10 15:10:22 -080045// An arbitrary time value.
46static const nsecs_t ARBITRARY_TIME = 1234;
47
48// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080049static constexpr int32_t DISPLAY_ID = 0;
50static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
51static constexpr int32_t DISPLAY_WIDTH = 480;
52static constexpr int32_t DISPLAY_HEIGHT = 800;
53static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
54static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
55static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070056static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070057static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
arthurhungcc7f9802020-04-30 17:55:40 +080059static constexpr int32_t FIRST_SLOT = 0;
60static constexpr int32_t SECOND_SLOT = 1;
61static constexpr int32_t THIRD_SLOT = 2;
62static constexpr int32_t INVALID_TRACKING_ID = -1;
63static constexpr int32_t FIRST_TRACKING_ID = 0;
64static constexpr int32_t SECOND_TRACKING_ID = 1;
65static constexpr int32_t THIRD_TRACKING_ID = 2;
66
Michael Wrightd02c5b62014-02-10 15:10:22 -080067// Error tolerance for floating point assertions.
68static const float EPSILON = 0.001f;
69
70template<typename T>
71static inline T min(T a, T b) {
72 return a < b ? a : b;
73}
74
75static inline float avg(float x, float y) {
76 return (x + y) / 2;
77}
78
79
80// --- FakePointerController ---
81
82class FakePointerController : public PointerControllerInterface {
83 bool mHaveBounds;
84 float mMinX, mMinY, mMaxX, mMaxY;
85 float mX, mY;
86 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080087 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
Michael Wrightd02c5b62014-02-10 15:10:22 -080089public:
90 FakePointerController() :
91 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080092 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080093 }
94
Michael Wright17db18e2020-06-26 20:51:44 +010095 virtual ~FakePointerController() {}
96
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 void setBounds(float minX, float minY, float maxX, float maxY) {
98 mHaveBounds = true;
99 mMinX = minX;
100 mMinY = minY;
101 mMaxX = maxX;
102 mMaxY = maxY;
103 }
104
Chris Yea52ade12020-08-27 16:49:20 -0700105 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106 mX = x;
107 mY = y;
108 }
109
Chris Yea52ade12020-08-27 16:49:20 -0700110 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
Chris Yea52ade12020-08-27 16:49:20 -0700112 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
Chris Yea52ade12020-08-27 16:49:20 -0700114 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115 *outX = mX;
116 *outY = mY;
117 }
118
Chris Yea52ade12020-08-27 16:49:20 -0700119 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800120
Chris Yea52ade12020-08-27 16:49:20 -0700121 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800122 mDisplayId = viewport.displayId;
123 }
124
Arthur Hung7c645402019-01-25 17:45:42 +0800125 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
126 return mSpotsByDisplay;
127 }
128
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129private:
Chris Yea52ade12020-08-27 16:49:20 -0700130 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 *outMinX = mMinX;
132 *outMinY = mMinY;
133 *outMaxX = mMaxX;
134 *outMaxY = mMaxY;
135 return mHaveBounds;
136 }
137
Chris Yea52ade12020-08-27 16:49:20 -0700138 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 mX += deltaX;
140 if (mX < mMinX) mX = mMinX;
141 if (mX > mMaxX) mX = mMaxX;
142 mY += deltaY;
143 if (mY < mMinY) mY = mMinY;
144 if (mY > mMaxY) mY = mMaxY;
145 }
146
Chris Yea52ade12020-08-27 16:49:20 -0700147 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148
Chris Yea52ade12020-08-27 16:49:20 -0700149 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Chris Yea52ade12020-08-27 16:49:20 -0700153 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
154 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800155 std::vector<int32_t> newSpots;
156 // Add spots for fingers that are down.
157 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
158 uint32_t id = idBits.clearFirstMarkedBit();
159 newSpots.push_back(id);
160 }
161
162 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 }
164
Chris Yea52ade12020-08-27 16:49:20 -0700165 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800166
167 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168};
169
170
171// --- FakeInputReaderPolicy ---
172
173class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700174 std::mutex mLock;
175 std::condition_variable mDevicesChangedCondition;
176
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100178 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700179 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
180 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100181 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700182 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
184protected:
Chris Yea52ade12020-08-27 16:49:20 -0700185 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
187public:
188 FakeInputReaderPolicy() {
189 }
190
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700191 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800192 waitForInputDevices([](bool devicesChanged) {
193 if (!devicesChanged) {
194 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
195 }
196 });
197 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700198
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800199 void assertInputDevicesNotChanged() {
200 waitForInputDevices([](bool devicesChanged) {
201 if (devicesChanged) {
202 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
203 }
204 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700205 }
206
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700207 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100208 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100209 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700210 }
211
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700212 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
213 return mConfig.getDisplayViewportByUniqueId(uniqueId);
214 }
215 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
216 return mConfig.getDisplayViewportByType(type);
217 }
218
219 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
220 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700221 }
222
223 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700224 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
225 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700226 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700227 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700228 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100229 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 }
231
Arthur Hung6cd19a42019-08-30 19:04:12 +0800232 bool updateViewport(const DisplayViewport& viewport) {
233 size_t count = mViewports.size();
234 for (size_t i = 0; i < count; i++) {
235 const DisplayViewport& currentViewport = mViewports[i];
236 if (currentViewport.displayId == viewport.displayId) {
237 mViewports[i] = viewport;
238 mConfig.setDisplayViewports(mViewports);
239 return true;
240 }
241 }
242 // no viewport found.
243 return false;
244 }
245
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100246 void addExcludedDeviceName(const std::string& deviceName) {
247 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 }
249
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700250 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
251 mConfig.portAssociations.insert({inputPort, displayPort});
252 }
253
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000254 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700255
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000256 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700257
Michael Wright17db18e2020-06-26 20:51:44 +0100258 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
259 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 }
261
262 const InputReaderConfiguration* getReaderConfiguration() const {
263 return &mConfig;
264 }
265
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800266 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 return mInputDevices;
268 }
269
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100270 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700271 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700272 return transform;
273 }
274
275 void setTouchAffineTransformation(const TouchAffineTransformation t) {
276 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800277 }
278
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800279 void setPointerCapture(bool enabled) {
280 mConfig.pointerCapture = enabled;
281 }
282
Arthur Hung7c645402019-01-25 17:45:42 +0800283 void setShowTouches(bool enabled) {
284 mConfig.showTouches = enabled;
285 }
286
Garfield Tan888a6a42020-01-09 11:39:16 -0800287 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
288 mConfig.defaultPointerDisplayId = pointerDisplayId;
289 }
290
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800291 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
292
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700294 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700295 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
296 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700297 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
298 || orientation == DISPLAY_ORIENTATION_270);
299 DisplayViewport v;
300 v.displayId = displayId;
301 v.orientation = orientation;
302 v.logicalLeft = 0;
303 v.logicalTop = 0;
304 v.logicalRight = isRotated ? height : width;
305 v.logicalBottom = isRotated ? width : height;
306 v.physicalLeft = 0;
307 v.physicalTop = 0;
308 v.physicalRight = isRotated ? height : width;
309 v.physicalBottom = isRotated ? width : height;
310 v.deviceWidth = isRotated ? height : width;
311 v.deviceHeight = isRotated ? width : height;
312 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700313 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100314 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700315 return v;
316 }
317
Chris Yea52ade12020-08-27 16:49:20 -0700318 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 *outConfig = mConfig;
320 }
321
Chris Yea52ade12020-08-27 16:49:20 -0700322 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100323 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 }
325
Chris Yea52ade12020-08-27 16:49:20 -0700326 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700327 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700329 mInputDevicesChanged = true;
330 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 }
332
Chris Yea52ade12020-08-27 16:49:20 -0700333 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
334 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700335 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 }
337
Chris Yea52ade12020-08-27 16:49:20 -0700338 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800339
340 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
341 std::unique_lock<std::mutex> lock(mLock);
342 base::ScopedLockAssertion assumeLocked(mLock);
343
344 const bool devicesChanged =
345 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
346 return mInputDevicesChanged;
347 });
348 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
349 mInputDevicesChanged = false;
350 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351};
352
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353// --- FakeEventHub ---
354
355class FakeEventHub : public EventHubInterface {
356 struct KeyInfo {
357 int32_t keyCode;
358 uint32_t flags;
359 };
360
361 struct Device {
362 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700363 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 PropertyMap configuration;
365 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
366 KeyedVector<int, bool> relativeAxes;
367 KeyedVector<int32_t, int32_t> keyCodeStates;
368 KeyedVector<int32_t, int32_t> scanCodeStates;
369 KeyedVector<int32_t, int32_t> switchStates;
370 KeyedVector<int32_t, int32_t> absoluteAxisValue;
371 KeyedVector<int32_t, KeyInfo> keysByScanCode;
372 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
373 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800374 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700375 bool enabled;
376
377 status_t enable() {
378 enabled = true;
379 return OK;
380 }
381
382 status_t disable() {
383 enabled = false;
384 return OK;
385 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386
Chris Ye1b0c7342020-07-28 21:57:03 -0700387 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388 };
389
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700390 std::mutex mLock;
391 std::condition_variable mEventsCondition;
392
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100394 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700395 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600396 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700398public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 virtual ~FakeEventHub() {
400 for (size_t i = 0; i < mDevices.size(); i++) {
401 delete mDevices.valueAt(i);
402 }
403 }
404
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 FakeEventHub() { }
406
Chris Ye1b0c7342020-07-28 21:57:03 -0700407 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 Device* device = new Device(classes);
409 device->identifier.name = name;
410 mDevices.add(deviceId, device);
411
412 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
413 }
414
415 void removeDevice(int32_t deviceId) {
416 delete mDevices.valueFor(deviceId);
417 mDevices.removeItem(deviceId);
418
419 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
420 }
421
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700422 bool isDeviceEnabled(int32_t deviceId) {
423 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700424 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700425 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
426 return false;
427 }
428 return device->enabled;
429 }
430
431 status_t enableDevice(int32_t deviceId) {
432 status_t result;
433 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700434 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700435 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
436 return BAD_VALUE;
437 }
438 if (device->enabled) {
439 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
440 return OK;
441 }
442 result = device->enable();
443 return result;
444 }
445
446 status_t disableDevice(int32_t deviceId) {
447 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700448 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700449 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
450 return BAD_VALUE;
451 }
452 if (!device->enabled) {
453 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
454 return OK;
455 }
456 return device->disable();
457 }
458
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459 void finishDeviceScan() {
460 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
461 }
462
463 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
464 Device* device = getDevice(deviceId);
465 device->configuration.addProperty(key, value);
466 }
467
468 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
469 Device* device = getDevice(deviceId);
470 device->configuration.addAll(configuration);
471 }
472
473 void addAbsoluteAxis(int32_t deviceId, int axis,
474 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
475 Device* device = getDevice(deviceId);
476
477 RawAbsoluteAxisInfo info;
478 info.valid = true;
479 info.minValue = minValue;
480 info.maxValue = maxValue;
481 info.flat = flat;
482 info.fuzz = fuzz;
483 info.resolution = resolution;
484 device->absoluteAxes.add(axis, info);
485 }
486
487 void addRelativeAxis(int32_t deviceId, int32_t axis) {
488 Device* device = getDevice(deviceId);
489 device->relativeAxes.add(axis, true);
490 }
491
492 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
493 Device* device = getDevice(deviceId);
494 device->keyCodeStates.replaceValueFor(keyCode, state);
495 }
496
497 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
498 Device* device = getDevice(deviceId);
499 device->scanCodeStates.replaceValueFor(scanCode, state);
500 }
501
502 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
503 Device* device = getDevice(deviceId);
504 device->switchStates.replaceValueFor(switchCode, state);
505 }
506
507 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
508 Device* device = getDevice(deviceId);
509 device->absoluteAxisValue.replaceValueFor(axis, value);
510 }
511
512 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
513 int32_t keyCode, uint32_t flags) {
514 Device* device = getDevice(deviceId);
515 KeyInfo info;
516 info.keyCode = keyCode;
517 info.flags = flags;
518 if (scanCode) {
519 device->keysByScanCode.add(scanCode, info);
520 }
521 if (usageCode) {
522 device->keysByUsageCode.add(usageCode, info);
523 }
524 }
525
526 void addLed(int32_t deviceId, int32_t led, bool initialState) {
527 Device* device = getDevice(deviceId);
528 device->leds.add(led, initialState);
529 }
530
531 bool getLedState(int32_t deviceId, int32_t led) {
532 Device* device = getDevice(deviceId);
533 return device->leds.valueFor(led);
534 }
535
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100536 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537 return mExcludedDevices;
538 }
539
540 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
541 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800542 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543 }
544
545 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
546 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700547 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800548 RawEvent event;
549 event.when = when;
550 event.deviceId = deviceId;
551 event.type = type;
552 event.code = code;
553 event.value = value;
554 mEvents.push_back(event);
555
556 if (type == EV_ABS) {
557 setAbsoluteAxisValue(deviceId, code, value);
558 }
559 }
560
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600561 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
562 std::vector<TouchVideoFrame>> videoFrames) {
563 mVideoFrames = std::move(videoFrames);
564 }
565
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700567 std::unique_lock<std::mutex> lock(mLock);
568 base::ScopedLockAssertion assumeLocked(mLock);
569 const bool queueIsEmpty =
570 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
571 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
572 if (!queueIsEmpty) {
573 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
574 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 }
576
577private:
578 Device* getDevice(int32_t deviceId) const {
579 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100580 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 }
582
Chris Yea52ade12020-08-27 16:49:20 -0700583 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700585 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 }
587
Chris Yea52ade12020-08-27 16:49:20 -0700588 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 Device* device = getDevice(deviceId);
590 return device ? device->identifier : InputDeviceIdentifier();
591 }
592
Chris Yea52ade12020-08-27 16:49:20 -0700593 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594
Chris Yea52ade12020-08-27 16:49:20 -0700595 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 Device* device = getDevice(deviceId);
597 if (device) {
598 *outConfiguration = device->configuration;
599 }
600 }
601
Chris Yea52ade12020-08-27 16:49:20 -0700602 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
603 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800605 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 ssize_t index = device->absoluteAxes.indexOfKey(axis);
607 if (index >= 0) {
608 *outAxisInfo = device->absoluteAxes.valueAt(index);
609 return OK;
610 }
611 }
612 outAxisInfo->clear();
613 return -1;
614 }
615
Chris Yea52ade12020-08-27 16:49:20 -0700616 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 Device* device = getDevice(deviceId);
618 if (device) {
619 return device->relativeAxes.indexOfKey(axis) >= 0;
620 }
621 return false;
622 }
623
Chris Yea52ade12020-08-27 16:49:20 -0700624 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625
Chris Yea52ade12020-08-27 16:49:20 -0700626 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
627 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 Device* device = getDevice(deviceId);
629 if (device) {
630 const KeyInfo* key = getKey(device, scanCode, usageCode);
631 if (key) {
632 if (outKeycode) {
633 *outKeycode = key->keyCode;
634 }
635 if (outFlags) {
636 *outFlags = key->flags;
637 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700638 if (outMetaState) {
639 *outMetaState = metaState;
640 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 return OK;
642 }
643 }
644 return NAME_NOT_FOUND;
645 }
646
647 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
648 if (usageCode) {
649 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
650 if (index >= 0) {
651 return &device->keysByUsageCode.valueAt(index);
652 }
653 }
654 if (scanCode) {
655 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
656 if (index >= 0) {
657 return &device->keysByScanCode.valueAt(index);
658 }
659 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700660 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 }
662
Chris Yea52ade12020-08-27 16:49:20 -0700663 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664
Chris Yea52ade12020-08-27 16:49:20 -0700665 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 mExcludedDevices = devices;
667 }
668
Chris Yea52ade12020-08-27 16:49:20 -0700669 size_t getEvents(int, RawEvent* buffer, size_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700670 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 if (mEvents.empty()) {
672 return 0;
673 }
674
675 *buffer = *mEvents.begin();
676 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700677 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 return 1;
679 }
680
Chris Yea52ade12020-08-27 16:49:20 -0700681 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600682 auto it = mVideoFrames.find(deviceId);
683 if (it != mVideoFrames.end()) {
684 std::vector<TouchVideoFrame> frames = std::move(it->second);
685 mVideoFrames.erase(deviceId);
686 return frames;
687 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800688 return {};
689 }
690
Chris Yea52ade12020-08-27 16:49:20 -0700691 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 Device* device = getDevice(deviceId);
693 if (device) {
694 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
695 if (index >= 0) {
696 return device->scanCodeStates.valueAt(index);
697 }
698 }
699 return AKEY_STATE_UNKNOWN;
700 }
701
Chris Yea52ade12020-08-27 16:49:20 -0700702 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 Device* device = getDevice(deviceId);
704 if (device) {
705 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
706 if (index >= 0) {
707 return device->keyCodeStates.valueAt(index);
708 }
709 }
710 return AKEY_STATE_UNKNOWN;
711 }
712
Chris Yea52ade12020-08-27 16:49:20 -0700713 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 Device* device = getDevice(deviceId);
715 if (device) {
716 ssize_t index = device->switchStates.indexOfKey(sw);
717 if (index >= 0) {
718 return device->switchStates.valueAt(index);
719 }
720 }
721 return AKEY_STATE_UNKNOWN;
722 }
723
Chris Yea52ade12020-08-27 16:49:20 -0700724 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
725 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800726 Device* device = getDevice(deviceId);
727 if (device) {
728 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
729 if (index >= 0) {
730 *outValue = device->absoluteAxisValue.valueAt(index);
731 return OK;
732 }
733 }
734 *outValue = 0;
735 return -1;
736 }
737
Chris Yea52ade12020-08-27 16:49:20 -0700738 // Return true if the device has non-empty key layout.
739 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
740 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741 bool result = false;
742 Device* device = getDevice(deviceId);
743 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700744 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 for (size_t i = 0; i < numCodes; i++) {
746 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
747 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
748 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 }
750 }
751 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
752 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
753 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 }
755 }
756 }
757 }
758 return result;
759 }
760
Chris Yea52ade12020-08-27 16:49:20 -0700761 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 Device* device = getDevice(deviceId);
763 if (device) {
764 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
765 return index >= 0;
766 }
767 return false;
768 }
769
Chris Yea52ade12020-08-27 16:49:20 -0700770 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 Device* device = getDevice(deviceId);
772 return device && device->leds.indexOfKey(led) >= 0;
773 }
774
Chris Yea52ade12020-08-27 16:49:20 -0700775 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776 Device* device = getDevice(deviceId);
777 if (device) {
778 ssize_t index = device->leds.indexOfKey(led);
779 if (index >= 0) {
780 device->leds.replaceValueAt(led, on);
781 } else {
782 ADD_FAILURE()
783 << "Attempted to set the state of an LED that the EventHub declared "
784 "was not present. led=" << led;
785 }
786 }
787 }
788
Chris Yea52ade12020-08-27 16:49:20 -0700789 void getVirtualKeyDefinitions(
790 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800791 outVirtualKeys.clear();
792
793 Device* device = getDevice(deviceId);
794 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800795 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796 }
797 }
798
Chris Yea52ade12020-08-27 16:49:20 -0700799 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700800 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801 }
802
Chris Yea52ade12020-08-27 16:49:20 -0700803 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 return false;
805 }
806
Chris Yea52ade12020-08-27 16:49:20 -0700807 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808
Chris Yea52ade12020-08-27 16:49:20 -0700809 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100811 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812 return false;
813 }
814
Chris Yea52ade12020-08-27 16:49:20 -0700815 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816
Chris Yea52ade12020-08-27 16:49:20 -0700817 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
Chris Yea52ade12020-08-27 16:49:20 -0700819 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820
Chris Yea52ade12020-08-27 16:49:20 -0700821 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822};
823
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824// --- FakeInputMapper ---
825
826class FakeInputMapper : public InputMapper {
827 uint32_t mSources;
828 int32_t mKeyboardType;
829 int32_t mMetaState;
830 KeyedVector<int32_t, int32_t> mKeyCodeStates;
831 KeyedVector<int32_t, int32_t> mScanCodeStates;
832 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800833 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700835 std::mutex mLock;
836 std::condition_variable mStateChangedCondition;
837 bool mConfigureWasCalled GUARDED_BY(mLock);
838 bool mResetWasCalled GUARDED_BY(mLock);
839 bool mProcessWasCalled GUARDED_BY(mLock);
840 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800841
Arthur Hungc23540e2018-11-29 20:42:11 +0800842 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800844 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
845 : InputMapper(deviceContext),
846 mSources(sources),
847 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800849 mConfigureWasCalled(false),
850 mResetWasCalled(false),
851 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852
Chris Yea52ade12020-08-27 16:49:20 -0700853 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854
855 void setKeyboardType(int32_t keyboardType) {
856 mKeyboardType = keyboardType;
857 }
858
859 void setMetaState(int32_t metaState) {
860 mMetaState = metaState;
861 }
862
863 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700864 std::unique_lock<std::mutex> lock(mLock);
865 base::ScopedLockAssertion assumeLocked(mLock);
866 const bool configureCalled =
867 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
868 return mConfigureWasCalled;
869 });
870 if (!configureCalled) {
871 FAIL() << "Expected configure() to have been called.";
872 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 mConfigureWasCalled = false;
874 }
875
876 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700877 std::unique_lock<std::mutex> lock(mLock);
878 base::ScopedLockAssertion assumeLocked(mLock);
879 const bool resetCalled =
880 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
881 return mResetWasCalled;
882 });
883 if (!resetCalled) {
884 FAIL() << "Expected reset() to have been called.";
885 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800886 mResetWasCalled = false;
887 }
888
Yi Kong9b14ac62018-07-17 13:48:38 -0700889 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700890 std::unique_lock<std::mutex> lock(mLock);
891 base::ScopedLockAssertion assumeLocked(mLock);
892 const bool processCalled =
893 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
894 return mProcessWasCalled;
895 });
896 if (!processCalled) {
897 FAIL() << "Expected process() to have been called.";
898 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 if (outLastEvent) {
900 *outLastEvent = mLastEvent;
901 }
902 mProcessWasCalled = false;
903 }
904
905 void setKeyCodeState(int32_t keyCode, int32_t state) {
906 mKeyCodeStates.replaceValueFor(keyCode, state);
907 }
908
909 void setScanCodeState(int32_t scanCode, int32_t state) {
910 mScanCodeStates.replaceValueFor(scanCode, state);
911 }
912
913 void setSwitchState(int32_t switchCode, int32_t state) {
914 mSwitchStates.replaceValueFor(switchCode, state);
915 }
916
917 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800918 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919 }
920
921private:
Chris Yea52ade12020-08-27 16:49:20 -0700922 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923
Chris Yea52ade12020-08-27 16:49:20 -0700924 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 InputMapper::populateDeviceInfo(deviceInfo);
926
927 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
928 deviceInfo->setKeyboardType(mKeyboardType);
929 }
930 }
931
Chris Yea52ade12020-08-27 16:49:20 -0700932 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700933 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800935
936 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800937 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800938 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
939 mViewport = config->getDisplayViewportByPort(*displayPort);
940 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700941
942 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 }
944
Chris Yea52ade12020-08-27 16:49:20 -0700945 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700946 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700948 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 }
950
Chris Yea52ade12020-08-27 16:49:20 -0700951 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700952 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 mLastEvent = *rawEvent;
954 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700955 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956 }
957
Chris Yea52ade12020-08-27 16:49:20 -0700958 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
960 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
961 }
962
Chris Yea52ade12020-08-27 16:49:20 -0700963 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
965 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 ssize_t index = mSwitchStates.indexOfKey(switchCode);
970 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
971 }
972
Chris Yea52ade12020-08-27 16:49:20 -0700973 // Return true if the device has non-empty key layout.
974 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
975 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976 for (size_t i = 0; i < numCodes; i++) {
977 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
978 if (keyCodes[i] == mSupportedKeyCodes[j]) {
979 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 }
981 }
982 }
Chris Yea52ade12020-08-27 16:49:20 -0700983 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 return result;
985 }
986
987 virtual int32_t getMetaState() {
988 return mMetaState;
989 }
990
991 virtual void fadePointer() {
992 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800993
994 virtual std::optional<int32_t> getAssociatedDisplay() {
995 if (mViewport) {
996 return std::make_optional(mViewport->displayId);
997 }
998 return std::nullopt;
999 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000};
1001
1002
1003// --- InstrumentedInputReader ---
1004
1005class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001006 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007
1008public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001009 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1010 const sp<InputReaderPolicyInterface>& policy,
1011 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001012 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001014 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001016 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001018 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001019 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020 InputDeviceIdentifier identifier;
1021 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001022 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001024 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 }
1026
Prabir Pradhan28efc192019-11-05 01:10:04 +00001027 // Make the protected loopOnce method accessible to tests.
1028 using InputReader::loopOnce;
1029
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001031 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1032 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001033 if (!mNextDevices.empty()) {
1034 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1035 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036 return device;
1037 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001038 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 }
1040
arthurhungdcef2dc2020-08-11 14:47:50 +08001041 // --- FakeInputReaderContext ---
1042 class FakeInputReaderContext : public ContextImpl {
1043 int32_t mGlobalMetaState;
1044 bool mUpdateGlobalMetaStateWasCalled;
1045 int32_t mGeneration;
1046
1047 public:
1048 FakeInputReaderContext(InputReader* reader)
1049 : ContextImpl(reader),
1050 mGlobalMetaState(0),
1051 mUpdateGlobalMetaStateWasCalled(false),
1052 mGeneration(1) {}
1053
1054 virtual ~FakeInputReaderContext() {}
1055
1056 void assertUpdateGlobalMetaStateWasCalled() {
1057 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1058 << "Expected updateGlobalMetaState() to have been called.";
1059 mUpdateGlobalMetaStateWasCalled = false;
1060 }
1061
1062 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1063
1064 uint32_t getGeneration() { return mGeneration; }
1065
1066 void updateGlobalMetaState() override {
1067 mUpdateGlobalMetaStateWasCalled = true;
1068 ContextImpl::updateGlobalMetaState();
1069 }
1070
1071 int32_t getGlobalMetaState() override {
1072 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1073 }
1074
1075 int32_t bumpGeneration() override {
1076 mGeneration = ContextImpl::bumpGeneration();
1077 return mGeneration;
1078 }
1079 } mFakeContext;
1080
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001082
1083public:
1084 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085};
1086
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001087// --- InputReaderPolicyTest ---
1088class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001089protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001090 sp<FakeInputReaderPolicy> mFakePolicy;
1091
Chris Yea52ade12020-08-27 16:49:20 -07001092 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1093 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001094};
1095
1096/**
1097 * Check that empty set of viewports is an acceptable configuration.
1098 * Also try to get internal viewport two different ways - by type and by uniqueId.
1099 *
1100 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1101 * Such configuration is not currently allowed.
1102 */
1103TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001104 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001105
1106 // We didn't add any viewports yet, so there shouldn't be any.
1107 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001108 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001109 ASSERT_FALSE(internalViewport);
1110
1111 // Add an internal viewport, then clear it
1112 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001113 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1114 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001115
1116 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001117 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001118 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001119 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001120
1121 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001122 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001123 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001124 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001125
1126 mFakePolicy->clearViewports();
1127 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001128 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001129 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001130 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001131 ASSERT_FALSE(internalViewport);
1132}
1133
1134TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1135 const std::string internalUniqueId = "local:0";
1136 const std::string externalUniqueId = "local:1";
1137 const std::string virtualUniqueId1 = "virtual:2";
1138 const std::string virtualUniqueId2 = "virtual:3";
1139 constexpr int32_t virtualDisplayId1 = 2;
1140 constexpr int32_t virtualDisplayId2 = 3;
1141
1142 // Add an internal viewport
1143 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001144 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1145 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001146 // Add an external viewport
1147 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001148 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1149 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150 // Add an virtual viewport
1151 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001152 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1153 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001154 // Add another virtual viewport
1155 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001156 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1157 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001158
1159 // Check matching by type for internal
1160 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001161 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001162 ASSERT_TRUE(internalViewport);
1163 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1164
1165 // Check matching by type for external
1166 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001167 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168 ASSERT_TRUE(externalViewport);
1169 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1170
1171 // Check matching by uniqueId for virtual viewport #1
1172 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001173 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001174 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001175 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001176 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1177 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1178
1179 // Check matching by uniqueId for virtual viewport #2
1180 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001181 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001182 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001183 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001184 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1185 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1186}
1187
1188
1189/**
1190 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1191 * that lookup works by checking display id.
1192 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1193 */
1194TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1195 const std::string uniqueId1 = "uniqueId1";
1196 const std::string uniqueId2 = "uniqueId2";
1197 constexpr int32_t displayId1 = 2;
1198 constexpr int32_t displayId2 = 3;
1199
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001200 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1201 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202 for (const ViewportType& type : types) {
1203 mFakePolicy->clearViewports();
1204 // Add a viewport
1205 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207 // Add another viewport
1208 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001209 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210
1211 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 std::optional<DisplayViewport> viewport1 =
1213 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 ASSERT_TRUE(viewport1);
1215 ASSERT_EQ(displayId1, viewport1->displayId);
1216 ASSERT_EQ(type, viewport1->type);
1217
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001218 std::optional<DisplayViewport> viewport2 =
1219 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001220 ASSERT_TRUE(viewport2);
1221 ASSERT_EQ(displayId2, viewport2->displayId);
1222 ASSERT_EQ(type, viewport2->type);
1223
1224 // When there are multiple viewports of the same kind, and uniqueId is not specified
1225 // in the call to getDisplayViewport, then that situation is not supported.
1226 // The viewports can be stored in any order, so we cannot rely on the order, since that
1227 // is just implementation detail.
1228 // However, we can check that it still returns *a* viewport, we just cannot assert
1229 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001230 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001231 ASSERT_TRUE(someViewport);
1232 }
1233}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001235/**
1236 * Check getDisplayViewportByPort
1237 */
1238TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001239 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001240 const std::string uniqueId1 = "uniqueId1";
1241 const std::string uniqueId2 = "uniqueId2";
1242 constexpr int32_t displayId1 = 1;
1243 constexpr int32_t displayId2 = 2;
1244 const uint8_t hdmi1 = 0;
1245 const uint8_t hdmi2 = 1;
1246 const uint8_t hdmi3 = 2;
1247
1248 mFakePolicy->clearViewports();
1249 // Add a viewport that's associated with some display port that's not of interest.
1250 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1251 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1252 // Add another viewport, connected to HDMI1 port
1253 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1254 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1255
1256 // Check that correct display viewport was returned by comparing the display ports.
1257 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1258 ASSERT_TRUE(hdmi1Viewport);
1259 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1260 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1261
1262 // Check that we can still get the same viewport using the uniqueId
1263 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1264 ASSERT_TRUE(hdmi1Viewport);
1265 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1266 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1267 ASSERT_EQ(type, hdmi1Viewport->type);
1268
1269 // Check that we cannot find a port with "HDMI2", because we never added one
1270 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1271 ASSERT_FALSE(hdmi2Viewport);
1272}
1273
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274// --- InputReaderTest ---
1275
1276class InputReaderTest : public testing::Test {
1277protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001278 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001280 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001281 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282
Chris Yea52ade12020-08-27 16:49:20 -07001283 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001284 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001286 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287
Prabir Pradhan28efc192019-11-05 01:10:04 +00001288 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1289 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290 }
1291
Chris Yea52ade12020-08-27 16:49:20 -07001292 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001293 mFakeListener.clear();
1294 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 }
1296
Chris Ye1b0c7342020-07-28 21:57:03 -07001297 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001298 const PropertyMap* configuration) {
1299 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001300
1301 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001302 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001303 }
1304 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001305 mReader->loopOnce();
1306 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001307 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1308 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001309 }
1310
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001311 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001312 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001313 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001314 }
1315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001316 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001317 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001318 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001319 }
1320
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001321 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001322 const std::string& name,
1323 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001324 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001325 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1326 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001327 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001328 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329 return mapper;
1330 }
1331};
1332
1333TEST_F(InputReaderTest, GetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001334 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1335 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1336 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001337
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001338 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001341 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001342 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001343 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1344 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1345 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1346
1347 // Should also have received a notification describing the new input devices.
1348 inputDevices = mFakePolicy->getInputDevices();
1349 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001350 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001351 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1353 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1354 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1355}
1356
Chris Yee7310032020-09-22 15:36:28 -07001357TEST_F(InputReaderTest, GetMergedInputDevices) {
1358 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1359 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1360 // Add two subdevices to device
1361 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1362 // Must add at least one mapper or the device will be ignored!
1363 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1364 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1365
1366 // Push same device instance for next device to be added, so they'll have same identifier.
1367 mReader->pushNextDevice(device);
1368 mReader->pushNextDevice(device);
1369 ASSERT_NO_FATAL_FAILURE(
1370 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1371 ASSERT_NO_FATAL_FAILURE(
1372 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1373
1374 // Two devices will be merged to one input device as they have same identifier
1375 std::vector<InputDeviceInfo> inputDevices;
1376 mReader->getInputDevices(inputDevices);
1377 ASSERT_EQ(1U, inputDevices.size());
1378}
1379
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001380TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001381 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001382 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001383 constexpr int32_t eventHubId = 1;
1384 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001385 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001386 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001387 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001388 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001389
Yi Kong9b14ac62018-07-17 13:48:38 -07001390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001391
1392 NotifyDeviceResetArgs resetArgs;
1393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001394 ASSERT_EQ(deviceId, resetArgs.deviceId);
1395
1396 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001397 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001398 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001399
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001401 ASSERT_EQ(deviceId, resetArgs.deviceId);
1402 ASSERT_EQ(device->isEnabled(), false);
1403
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001404 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001405 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001408 ASSERT_EQ(device->isEnabled(), false);
1409
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001410 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001411 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001413 ASSERT_EQ(deviceId, resetArgs.deviceId);
1414 ASSERT_EQ(device->isEnabled(), true);
1415}
1416
Michael Wrightd02c5b62014-02-10 15:10:22 -08001417TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001418 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001419 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001420 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001421 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001422 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001423 AINPUT_SOURCE_KEYBOARD, nullptr);
1424 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001425
1426 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1427 AINPUT_SOURCE_ANY, AKEYCODE_A))
1428 << "Should return unknown when the device id is >= 0 but unknown.";
1429
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001430 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1431 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1432 << "Should return unknown when the device id is valid but the sources are not "
1433 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001434
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001435 ASSERT_EQ(AKEY_STATE_DOWN,
1436 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1437 AKEYCODE_A))
1438 << "Should return value provided by mapper when device id is valid and the device "
1439 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001440
1441 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1442 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1443 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1444
1445 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1446 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1447 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1448}
1449
1450TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001451 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001452 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001453 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001454 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001455 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001456 AINPUT_SOURCE_KEYBOARD, nullptr);
1457 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458
1459 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1460 AINPUT_SOURCE_ANY, KEY_A))
1461 << "Should return unknown when the device id is >= 0 but unknown.";
1462
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001463 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1464 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1465 << "Should return unknown when the device id is valid but the sources are not "
1466 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001467
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001468 ASSERT_EQ(AKEY_STATE_DOWN,
1469 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1470 KEY_A))
1471 << "Should return value provided by mapper when device id is valid and the device "
1472 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001473
1474 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1475 AINPUT_SOURCE_TRACKBALL, KEY_A))
1476 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1477
1478 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1479 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1480 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1481}
1482
1483TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001484 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001485 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001487 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001488 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001489 AINPUT_SOURCE_KEYBOARD, nullptr);
1490 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001491
1492 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1493 AINPUT_SOURCE_ANY, SW_LID))
1494 << "Should return unknown when the device id is >= 0 but unknown.";
1495
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001496 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1497 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1498 << "Should return unknown when the device id is valid but the sources are not "
1499 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001500
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001501 ASSERT_EQ(AKEY_STATE_DOWN,
1502 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1503 SW_LID))
1504 << "Should return value provided by mapper when device id is valid and the device "
1505 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001506
1507 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1508 AINPUT_SOURCE_TRACKBALL, SW_LID))
1509 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1510
1511 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1512 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1513 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1514}
1515
1516TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001517 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001518 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001520 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001521 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001522 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001523
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001524 mapper.addSupportedKeyCode(AKEYCODE_A);
1525 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526
1527 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1528 uint8_t flags[4] = { 0, 0, 0, 1 };
1529
1530 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1531 << "Should return false when device id is >= 0 but unknown.";
1532 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1533
1534 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001535 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1536 << "Should return false when device id is valid but the sources are not supported by "
1537 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001538 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1539
1540 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001541 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1542 keyCodes, flags))
1543 << "Should return value provided by mapper when device id is valid and the device "
1544 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001545 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1546
1547 flags[3] = 1;
1548 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1549 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1550 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1551
1552 flags[3] = 1;
1553 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1554 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1555 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1556}
1557
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001558TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001559 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001560 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
1562 NotifyConfigurationChangedArgs args;
1563
1564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1565 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1566}
1567
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001568TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001569 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001570 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001571 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001572 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001573 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001574 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001576 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001577 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1579
1580 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001581 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001583 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584 ASSERT_EQ(EV_KEY, event.type);
1585 ASSERT_EQ(KEY_A, event.code);
1586 ASSERT_EQ(1, event.value);
1587}
1588
Garfield Tan1c7bc862020-01-28 13:24:04 -08001589TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001591 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001592 constexpr int32_t eventHubId = 1;
1593 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001594 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001595 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001596 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001597 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001598
1599 NotifyDeviceResetArgs resetArgs;
1600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001601 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001603 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001604 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001606 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001607 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001609 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001610 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001612 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001613 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001615 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001616 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001618 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001619 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001620}
1621
Garfield Tan1c7bc862020-01-28 13:24:04 -08001622TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1623 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001624 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001625 constexpr int32_t eventHubId = 1;
1626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1627 // Must add at least one mapper or the device will be ignored!
1628 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001629 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001630 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1631
1632 NotifyDeviceResetArgs resetArgs;
1633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1634 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1635}
1636
Arthur Hungc23540e2018-11-29 20:42:11 +08001637TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001639 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001640 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001641 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1643 FakeInputMapper& mapper =
1644 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001645 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001646
1647 const uint8_t hdmi1 = 1;
1648
1649 // Associated touch screen with second display.
1650 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1651
1652 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001653 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001654 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001655 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1656 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001657 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001658 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1659 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001660 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001661 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001662
1663 // Add the device, and make sure all of the callbacks are triggered.
1664 // The device is added after the input port associations are processed since
1665 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001666 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001669 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001670
Arthur Hung2c9a3342019-07-23 14:18:59 +08001671 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001672 ASSERT_EQ(deviceId, device->getId());
1673 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1674 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001675
1676 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001677 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001678 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001679 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001680}
1681
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001682TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1683 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1684 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1685 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1686 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1687 // Must add at least one mapper or the device will be ignored!
1688 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1689 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1690 mReader->pushNextDevice(device);
1691 mReader->pushNextDevice(device);
1692 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1693 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1694
1695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1696
1697 NotifyDeviceResetArgs resetArgs;
1698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1699 ASSERT_EQ(deviceId, resetArgs.deviceId);
1700 ASSERT_TRUE(device->isEnabled());
1701 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1702 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1703
1704 disableDevice(deviceId);
1705 mReader->loopOnce();
1706
1707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1708 ASSERT_EQ(deviceId, resetArgs.deviceId);
1709 ASSERT_FALSE(device->isEnabled());
1710 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1711 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1712
1713 enableDevice(deviceId);
1714 mReader->loopOnce();
1715
1716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1717 ASSERT_EQ(deviceId, resetArgs.deviceId);
1718 ASSERT_TRUE(device->isEnabled());
1719 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1720 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1721}
1722
1723TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1724 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1725 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1726 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1727 // Add two subdevices to device
1728 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1729 FakeInputMapper& mapperDevice1 =
1730 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1731 FakeInputMapper& mapperDevice2 =
1732 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1733 mReader->pushNextDevice(device);
1734 mReader->pushNextDevice(device);
1735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1737
1738 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1739 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1740
1741 ASSERT_EQ(AKEY_STATE_DOWN,
1742 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1743 ASSERT_EQ(AKEY_STATE_DOWN,
1744 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1746 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1747}
1748
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001749// --- InputReaderIntegrationTest ---
1750
1751// These tests create and interact with the InputReader only through its interface.
1752// The InputReader is started during SetUp(), which starts its processing in its own
1753// thread. The tests use linux uinput to emulate input devices.
1754// NOTE: Interacting with the physical device while these tests are running may cause
1755// the tests to fail.
1756class InputReaderIntegrationTest : public testing::Test {
1757protected:
1758 sp<TestInputListener> mTestListener;
1759 sp<FakeInputReaderPolicy> mFakePolicy;
1760 sp<InputReaderInterface> mReader;
1761
Chris Yea52ade12020-08-27 16:49:20 -07001762 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001763 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001764 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1765 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001766
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001767 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001768 ASSERT_EQ(mReader->start(), OK);
1769
1770 // Since this test is run on a real device, all the input devices connected
1771 // to the test device will show up in mReader. We wait for those input devices to
1772 // show up before beginning the tests.
1773 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1774 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1775 }
1776
Chris Yea52ade12020-08-27 16:49:20 -07001777 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001778 ASSERT_EQ(mReader->stop(), OK);
1779 mTestListener.clear();
1780 mFakePolicy.clear();
1781 }
1782};
1783
1784TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1785 // An invalid input device that is only used for this test.
1786 class InvalidUinputDevice : public UinputDevice {
1787 public:
1788 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1789
1790 private:
1791 void configureDevice(int fd, uinput_user_dev* device) override {}
1792 };
1793
1794 const size_t numDevices = mFakePolicy->getInputDevices().size();
1795
1796 // UinputDevice does not set any event or key bits, so InputReader should not
1797 // consider it as a valid device.
1798 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1799 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1800 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1801 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1802
1803 invalidDevice.reset();
1804 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1805 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1806 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1807}
1808
1809TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1810 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1811
1812 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1813 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1814 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1815 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1816
1817 // Find the test device by its name.
1818 std::vector<InputDeviceInfo> inputDevices;
1819 mReader->getInputDevices(inputDevices);
1820 InputDeviceInfo* keyboardInfo = nullptr;
1821 const char* keyboardName = keyboard->getName();
1822 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1823 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1824 keyboardInfo = &inputDevices[i];
1825 break;
1826 }
1827 }
1828 ASSERT_NE(keyboardInfo, nullptr);
1829 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1830 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1831 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1832
1833 keyboard.reset();
1834 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1835 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1836 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1837}
1838
1839TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1840 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1841 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1842
1843 NotifyConfigurationChangedArgs configChangedArgs;
1844 ASSERT_NO_FATAL_FAILURE(
1845 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001846 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001847 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1848
1849 NotifyKeyArgs keyArgs;
1850 keyboard->pressAndReleaseHomeKey();
1851 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1852 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001853 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001854 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001855 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1856 prevTimestamp = keyArgs.eventTime;
1857
1858 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1859 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001860 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001861 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1862}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001864/**
1865 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1866 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1867 * are passed to the listener.
1868 */
1869static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1870TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1871 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1872 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1873 NotifyKeyArgs keyArgs;
1874
1875 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1876 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1877 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1878 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1879
1880 controller->pressAndReleaseKey(BTN_GEAR_UP);
1881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1882 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1883 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1884}
1885
Arthur Hungaab25622020-01-16 11:22:11 +08001886// --- TouchProcessTest ---
1887class TouchIntegrationTest : public InputReaderIntegrationTest {
1888protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001889 const std::string UNIQUE_ID = "local:0";
1890
Chris Yea52ade12020-08-27 16:49:20 -07001891 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08001892 InputReaderIntegrationTest::SetUp();
1893 // At least add an internal display.
1894 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1895 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001896 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001897
1898 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1899 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1900 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1901 }
1902
1903 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1904 int32_t orientation, const std::string& uniqueId,
1905 std::optional<uint8_t> physicalPort,
1906 ViewportType viewportType) {
1907 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1908 physicalPort, viewportType);
1909 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1910 }
1911
1912 std::unique_ptr<UinputTouchScreen> mDevice;
1913};
1914
1915TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1916 NotifyMotionArgs args;
1917 const Point centerPoint = mDevice->getCenterPoint();
1918
1919 // ACTION_DOWN
1920 mDevice->sendDown(centerPoint);
1921 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1923
1924 // ACTION_MOVE
1925 mDevice->sendMove(centerPoint + Point(1, 1));
1926 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1928
1929 // ACTION_UP
1930 mDevice->sendUp();
1931 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1932 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1933}
1934
1935TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1936 NotifyMotionArgs args;
1937 const Point centerPoint = mDevice->getCenterPoint();
1938
1939 // ACTION_DOWN
1940 mDevice->sendDown(centerPoint);
1941 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1943
1944 // ACTION_POINTER_DOWN (Second slot)
1945 const Point secondPoint = centerPoint + Point(100, 100);
1946 mDevice->sendSlot(SECOND_SLOT);
1947 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1948 mDevice->sendDown(secondPoint + Point(1, 1));
1949 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1950 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1951 args.action);
1952
1953 // ACTION_MOVE (Second slot)
1954 mDevice->sendMove(secondPoint);
1955 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1957
1958 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001959 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08001960 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08001961 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08001962 args.action);
1963
1964 // ACTION_UP
1965 mDevice->sendSlot(FIRST_SLOT);
1966 mDevice->sendUp();
1967 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1968 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1969}
1970
1971TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1972 NotifyMotionArgs args;
1973 const Point centerPoint = mDevice->getCenterPoint();
1974
1975 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001976 mDevice->sendSlot(FIRST_SLOT);
1977 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001978 mDevice->sendDown(centerPoint);
1979 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1980 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1981
arthurhungcc7f9802020-04-30 17:55:40 +08001982 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001983 const Point secondPoint = centerPoint + Point(100, 100);
1984 mDevice->sendSlot(SECOND_SLOT);
1985 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1986 mDevice->sendDown(secondPoint);
1987 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1988 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1989 args.action);
1990
arthurhungcc7f9802020-04-30 17:55:40 +08001991 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001992 mDevice->sendMove(secondPoint + Point(1, 1));
1993 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1995
arthurhungcc7f9802020-04-30 17:55:40 +08001996 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1997 // a palm event.
1998 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001999 mDevice->sendToolType(MT_TOOL_PALM);
2000 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002001 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2002 args.action);
2003 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002004
arthurhungcc7f9802020-04-30 17:55:40 +08002005 // Send up to second slot, expect first slot send moving.
2006 mDevice->sendPointerUp();
2007 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002009
arthurhungcc7f9802020-04-30 17:55:40 +08002010 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002011 mDevice->sendSlot(FIRST_SLOT);
2012 mDevice->sendUp();
2013
arthurhungcc7f9802020-04-30 17:55:40 +08002014 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2015 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002016}
2017
Michael Wrightd02c5b62014-02-10 15:10:22 -08002018// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002019class InputDeviceTest : public testing::Test {
2020protected:
2021 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002022 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002023 static const int32_t DEVICE_ID;
2024 static const int32_t DEVICE_GENERATION;
2025 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002026 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002027 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002028
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002029 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002030 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002031 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002032 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002033 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002034
Chris Yea52ade12020-08-27 16:49:20 -07002035 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002036 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002037 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002038 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002039 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2040 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002041 InputDeviceIdentifier identifier;
2042 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002043 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002044 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002045 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002046 mReader->pushNextDevice(mDevice);
2047 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2048 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002049 }
2050
Chris Yea52ade12020-08-27 16:49:20 -07002051 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002052 mFakeListener.clear();
2053 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002054 }
2055};
2056
2057const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002058const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002059const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002060const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2061const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002062const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2063 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002064const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002065
2066TEST_F(InputDeviceTest, ImmutableProperties) {
2067 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002068 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002069 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002070}
2071
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002072TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2073 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002074}
2075
Michael Wrightd02c5b62014-02-10 15:10:22 -08002076TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2077 // Configuration.
2078 InputReaderConfiguration config;
2079 mDevice->configure(ARBITRARY_TIME, &config, 0);
2080
2081 // Reset.
2082 mDevice->reset(ARBITRARY_TIME);
2083
2084 NotifyDeviceResetArgs resetArgs;
2085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2086 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2087 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2088
2089 // Metadata.
2090 ASSERT_TRUE(mDevice->isIgnored());
2091 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2092
2093 InputDeviceInfo info;
2094 mDevice->getDeviceInfo(&info);
2095 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002096 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002097 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2098 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2099
2100 // State queries.
2101 ASSERT_EQ(0, mDevice->getMetaState());
2102
2103 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2104 << "Ignored device should return unknown key code state.";
2105 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2106 << "Ignored device should return unknown scan code state.";
2107 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2108 << "Ignored device should return unknown switch state.";
2109
2110 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2111 uint8_t flags[2] = { 0, 1 };
2112 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2113 << "Ignored device should never mark any key codes.";
2114 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2115 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2116}
2117
2118TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2119 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002120 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002121
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002122 FakeInputMapper& mapper1 =
2123 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002124 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2125 mapper1.setMetaState(AMETA_ALT_ON);
2126 mapper1.addSupportedKeyCode(AKEYCODE_A);
2127 mapper1.addSupportedKeyCode(AKEYCODE_B);
2128 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2129 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2130 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2131 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2132 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002134 FakeInputMapper& mapper2 =
2135 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002136 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002137
2138 InputReaderConfiguration config;
2139 mDevice->configure(ARBITRARY_TIME, &config, 0);
2140
2141 String8 propertyValue;
2142 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2143 << "Device should have read configuration during configuration phase.";
2144 ASSERT_STREQ("value", propertyValue.string());
2145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002146 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2147 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002148
2149 // Reset
2150 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002151 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2152 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002153
2154 NotifyDeviceResetArgs resetArgs;
2155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2156 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2157 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2158
2159 // Metadata.
2160 ASSERT_FALSE(mDevice->isIgnored());
2161 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2162
2163 InputDeviceInfo info;
2164 mDevice->getDeviceInfo(&info);
2165 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002166 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002167 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2168 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2169
2170 // State queries.
2171 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2172 << "Should query mappers and combine meta states.";
2173
2174 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2175 << "Should return unknown key code state when source not supported.";
2176 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2177 << "Should return unknown scan code state when source not supported.";
2178 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2179 << "Should return unknown switch state when source not supported.";
2180
2181 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2182 << "Should query mapper when source is supported.";
2183 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2184 << "Should query mapper when source is supported.";
2185 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2186 << "Should query mapper when source is supported.";
2187
2188 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2189 uint8_t flags[4] = { 0, 0, 0, 1 };
2190 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2191 << "Should do nothing when source is unsupported.";
2192 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2193 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2194 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2195 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2196
2197 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2198 << "Should query mapper when source is supported.";
2199 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2200 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2201 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2202 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2203
2204 // Event handling.
2205 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002206 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002207 mDevice->process(&event, 1);
2208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002209 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2210 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211}
2212
Arthur Hung2c9a3342019-07-23 14:18:59 +08002213// A single input device is associated with a specific display. Check that:
2214// 1. Device is disabled if the viewport corresponding to the associated display is not found
2215// 2. Device is disabled when setEnabled API is called
2216TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002217 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002218
2219 // First Configuration.
2220 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2221
2222 // Device should be enabled by default.
2223 ASSERT_TRUE(mDevice->isEnabled());
2224
2225 // Prepare associated info.
2226 constexpr uint8_t hdmi = 1;
2227 const std::string UNIQUE_ID = "local:1";
2228
2229 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2230 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2231 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2232 // Device should be disabled because it is associated with a specific display via
2233 // input port <-> display port association, but the corresponding display is not found
2234 ASSERT_FALSE(mDevice->isEnabled());
2235
2236 // Prepare displays.
2237 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002238 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002239 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2240 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2241 ASSERT_TRUE(mDevice->isEnabled());
2242
2243 // Device should be disabled after set disable.
2244 mFakePolicy->addDisabledDevice(mDevice->getId());
2245 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2246 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2247 ASSERT_FALSE(mDevice->isEnabled());
2248
2249 // Device should still be disabled even found the associated display.
2250 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2251 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2252 ASSERT_FALSE(mDevice->isEnabled());
2253}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002254
2255// --- InputMapperTest ---
2256
2257class InputMapperTest : public testing::Test {
2258protected:
2259 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002260 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002261 static const int32_t DEVICE_ID;
2262 static const int32_t DEVICE_GENERATION;
2263 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002264 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002265 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002266
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002267 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002269 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002270 std::unique_ptr<InstrumentedInputReader> mReader;
2271 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002272
Chris Ye1b0c7342020-07-28 21:57:03 -07002273 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002274 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002275 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002276 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002277 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2278 mFakeListener);
2279 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280 }
2281
Chris Yea52ade12020-08-27 16:49:20 -07002282 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002283
Chris Yea52ade12020-08-27 16:49:20 -07002284 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285 mFakeListener.clear();
2286 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287 }
2288
2289 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002290 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291 }
2292
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002293 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002294 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002295 mReader->requestRefreshConfiguration(changes);
2296 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002297 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002298 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2299 }
2300
arthurhungdcef2dc2020-08-11 14:47:50 +08002301 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2302 const std::string& location, int32_t eventHubId,
2303 Flags<InputDeviceClass> classes) {
2304 InputDeviceIdentifier identifier;
2305 identifier.name = name;
2306 identifier.location = location;
2307 std::shared_ptr<InputDevice> device =
2308 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2309 identifier);
2310 mReader->pushNextDevice(device);
2311 mFakeEventHub->addDevice(eventHubId, name, classes);
2312 mReader->loopOnce();
2313 return device;
2314 }
2315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002316 template <class T, typename... Args>
2317 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002318 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002319 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002320 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002321 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002322 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 }
2324
2325 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002326 int32_t orientation, const std::string& uniqueId,
2327 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002328 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002329 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002330 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2331 }
2332
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002333 void clearViewports() {
2334 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335 }
2336
arthurhungdcef2dc2020-08-11 14:47:50 +08002337 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338 RawEvent event;
2339 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002340 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341 event.type = type;
2342 event.code = code;
2343 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002344 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002345 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346 }
2347
2348 static void assertMotionRange(const InputDeviceInfo& info,
2349 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2350 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002351 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2353 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2354 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2355 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2356 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2357 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2358 }
2359
2360 static void assertPointerCoords(const PointerCoords& coords,
2361 float x, float y, float pressure, float size,
2362 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2363 float orientation, float distance) {
2364 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2365 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2366 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2367 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2368 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2369 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2370 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2371 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2372 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2373 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2374 }
2375
Michael Wright17db18e2020-06-26 20:51:44 +01002376 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002378 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379 ASSERT_NEAR(x, actualX, 1);
2380 ASSERT_NEAR(y, actualY, 1);
2381 }
2382};
2383
2384const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002385const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002386const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2388const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002389const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2390 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002391const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392
2393// --- SwitchInputMapperTest ---
2394
2395class SwitchInputMapperTest : public InputMapperTest {
2396protected:
2397};
2398
2399TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002400 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002402 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403}
2404
2405TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002406 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002408 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002409 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002411 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413}
2414
2415TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002416 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002418 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2419 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2420 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2421 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422
2423 NotifySwitchArgs args;
2424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2425 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002426 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2427 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428 args.switchMask);
2429 ASSERT_EQ(uint32_t(0), args.policyFlags);
2430}
2431
2432
2433// --- KeyboardInputMapperTest ---
2434
2435class KeyboardInputMapperTest : public InputMapperTest {
2436protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002437 const std::string UNIQUE_ID = "local:0";
2438
2439 void prepareDisplay(int32_t orientation);
2440
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002441 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002442 int32_t originalKeyCode, int32_t rotatedKeyCode,
2443 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444};
2445
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002446/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2447 * orientation.
2448 */
2449void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002450 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2451 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002452}
2453
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002454void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002455 int32_t originalScanCode, int32_t originalKeyCode,
2456 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457 NotifyKeyArgs args;
2458
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002459 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2461 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2462 ASSERT_EQ(originalScanCode, args.scanCode);
2463 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002464 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002466 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2468 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2469 ASSERT_EQ(originalScanCode, args.scanCode);
2470 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002471 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472}
2473
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002475 KeyboardInputMapper& mapper =
2476 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2477 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002479 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480}
2481
2482TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2483 const int32_t USAGE_A = 0x070004;
2484 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2486 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002487 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2488 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2489 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002491 KeyboardInputMapper& mapper =
2492 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2493 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002494 // Initial metastate to AMETA_NONE.
2495 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2496 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497
2498 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002499 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500 NotifyKeyArgs args;
2501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2502 ASSERT_EQ(DEVICE_ID, args.deviceId);
2503 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2504 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2505 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2506 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2507 ASSERT_EQ(KEY_HOME, args.scanCode);
2508 ASSERT_EQ(AMETA_NONE, args.metaState);
2509 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2510 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2511 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2512
2513 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002514 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2516 ASSERT_EQ(DEVICE_ID, args.deviceId);
2517 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2518 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2519 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2520 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2521 ASSERT_EQ(KEY_HOME, args.scanCode);
2522 ASSERT_EQ(AMETA_NONE, args.metaState);
2523 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2524 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2525 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2526
2527 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002528 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2529 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2531 ASSERT_EQ(DEVICE_ID, args.deviceId);
2532 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2533 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2534 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2535 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2536 ASSERT_EQ(0, args.scanCode);
2537 ASSERT_EQ(AMETA_NONE, args.metaState);
2538 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2539 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2540 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2541
2542 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002543 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2544 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2546 ASSERT_EQ(DEVICE_ID, args.deviceId);
2547 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2548 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2549 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2550 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2551 ASSERT_EQ(0, args.scanCode);
2552 ASSERT_EQ(AMETA_NONE, args.metaState);
2553 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2554 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2555 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2556
2557 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002558 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2559 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2561 ASSERT_EQ(DEVICE_ID, args.deviceId);
2562 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2563 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2564 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2565 ASSERT_EQ(0, args.keyCode);
2566 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2567 ASSERT_EQ(AMETA_NONE, args.metaState);
2568 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2569 ASSERT_EQ(0U, args.policyFlags);
2570 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2571
2572 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002573 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2574 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2576 ASSERT_EQ(DEVICE_ID, args.deviceId);
2577 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2578 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2579 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2580 ASSERT_EQ(0, args.keyCode);
2581 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2582 ASSERT_EQ(AMETA_NONE, args.metaState);
2583 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2584 ASSERT_EQ(0U, args.policyFlags);
2585 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2586}
2587
2588TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2590 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002591 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2592 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2593 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002595 KeyboardInputMapper& mapper =
2596 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598
arthurhungc903df12020-08-11 15:08:42 +08002599 // Initial metastate to AMETA_NONE.
2600 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2601 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602
2603 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002604 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605 NotifyKeyArgs args;
2606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2607 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002608 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610
2611 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002612 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2614 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002615 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616
2617 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002618 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2620 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002621 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622
2623 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002624 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2626 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002627 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002628 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002629}
2630
2631TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002637 KeyboardInputMapper& mapper =
2638 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002641 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2643 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2644 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2645 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2646 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2647 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2649 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2650}
2651
2652TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002653 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2654 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2656 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002659 KeyboardInputMapper& mapper =
2660 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2661 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002663 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002664 ASSERT_NO_FATAL_FAILURE(
2665 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2666 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2667 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2668 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2669 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2670 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2671 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002673 clearViewports();
2674 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002675 ASSERT_NO_FATAL_FAILURE(
2676 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2677 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2678 AKEYCODE_DPAD_UP, DISPLAY_ID));
2679 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2680 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2681 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2682 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002684 clearViewports();
2685 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002686 ASSERT_NO_FATAL_FAILURE(
2687 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2688 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2689 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2690 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2691 AKEYCODE_DPAD_UP, DISPLAY_ID));
2692 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2693 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002695 clearViewports();
2696 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002697 ASSERT_NO_FATAL_FAILURE(
2698 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2699 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2700 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2701 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2702 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2703 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2704 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705
2706 // Special case: if orientation changes while key is down, we still emit the same keycode
2707 // in the key up as we did in the key down.
2708 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002709 clearViewports();
2710 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002711 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2713 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2714 ASSERT_EQ(KEY_UP, args.scanCode);
2715 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2716
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002717 clearViewports();
2718 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002719 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2721 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2722 ASSERT_EQ(KEY_UP, args.scanCode);
2723 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2724}
2725
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002726TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2727 // If the keyboard is not orientation aware,
2728 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002730
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002731 KeyboardInputMapper& mapper =
2732 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2733 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002734 NotifyKeyArgs args;
2735
2736 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002737 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002739 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2741 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2742
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002743 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002744 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002746 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2748 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2749}
2750
2751TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2752 // If the keyboard is orientation aware,
2753 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002755
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002756 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 KeyboardInputMapper& mapper =
2758 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2759 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002760 NotifyKeyArgs args;
2761
2762 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2763 // ^--- already checked by the previous test
2764
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002765 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002766 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002767 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002769 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2771 ASSERT_EQ(DISPLAY_ID, args.displayId);
2772
2773 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002774 clearViewports();
2775 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002776 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002777 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002779 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2781 ASSERT_EQ(newDisplayId, args.displayId);
2782}
2783
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 KeyboardInputMapper& mapper =
2786 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2787 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002789 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002790 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002792 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002793 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794}
2795
2796TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002797 KeyboardInputMapper& mapper =
2798 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2799 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002801 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002802 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002804 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806}
2807
2808TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002809 KeyboardInputMapper& mapper =
2810 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2811 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002813 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814
2815 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2816 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002817 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818 ASSERT_TRUE(flags[0]);
2819 ASSERT_FALSE(flags[1]);
2820}
2821
2822TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2824 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2825 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2826 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2827 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2828 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002830 KeyboardInputMapper& mapper =
2831 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2832 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07002833 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08002834 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2835 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
2837 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002838 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2839 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2840 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841
2842 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002843 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2844 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002845 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2846 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2847 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002848 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849
2850 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002851 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2852 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002853 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2854 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002856 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857
2858 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002859 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2860 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002861 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2862 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002864 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865
2866 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002867 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2868 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002869 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2870 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2871 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002872 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002873
2874 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002875 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2876 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002877 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2878 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2879 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002880 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881
2882 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002883 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2884 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002885 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2886 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2887 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002888 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889}
2890
Chris Yea52ade12020-08-27 16:49:20 -07002891TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
2892 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
2893 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
2894 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
2895 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
2896
2897 KeyboardInputMapper& mapper =
2898 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2899 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
2900
2901 // Initial metastate should be AMETA_NONE as no meta keys added.
2902 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2903 // Meta state should be AMETA_NONE after reset
2904 mapper.reset(ARBITRARY_TIME);
2905 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2906 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
2907 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
2908 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2909
2910 NotifyKeyArgs args;
2911 // Press button "A"
2912 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
2913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2914 ASSERT_EQ(AMETA_NONE, args.metaState);
2915 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2916 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2917 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2918
2919 // Button up.
2920 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
2921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2922 ASSERT_EQ(AMETA_NONE, args.metaState);
2923 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2924 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2925 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2926}
2927
Arthur Hung2c9a3342019-07-23 14:18:59 +08002928TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2929 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002930 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2931 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2932 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2933 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002934
2935 // keyboard 2.
2936 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08002937 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002938 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002939 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08002940 std::shared_ptr<InputDevice> device2 =
2941 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
2942 Flags<InputDeviceClass>(0));
2943
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002944 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2945 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2946 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2947 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002949 KeyboardInputMapper& mapper =
2950 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2951 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002953 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002954 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002955 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002956 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2957 device2->reset(ARBITRARY_TIME);
2958
2959 // Prepared displays and associated info.
2960 constexpr uint8_t hdmi1 = 0;
2961 constexpr uint8_t hdmi2 = 1;
2962 const std::string SECONDARY_UNIQUE_ID = "local:1";
2963
2964 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2965 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2966
2967 // No associated display viewport found, should disable the device.
2968 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2969 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2970 ASSERT_FALSE(device2->isEnabled());
2971
2972 // Prepare second display.
2973 constexpr int32_t newDisplayId = 2;
2974 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002975 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002976 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002977 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002978 // Default device will reconfigure above, need additional reconfiguration for another device.
2979 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2980 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2981
2982 // Device should be enabled after the associated display is found.
2983 ASSERT_TRUE(mDevice->isEnabled());
2984 ASSERT_TRUE(device2->isEnabled());
2985
2986 // Test pad key events
2987 ASSERT_NO_FATAL_FAILURE(
2988 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2989 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2990 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2991 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2992 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2993 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2994 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2995
2996 ASSERT_NO_FATAL_FAILURE(
2997 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2998 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2999 AKEYCODE_DPAD_RIGHT, newDisplayId));
3000 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3001 AKEYCODE_DPAD_DOWN, newDisplayId));
3002 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3003 AKEYCODE_DPAD_LEFT, newDisplayId));
3004}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
arthurhungc903df12020-08-11 15:08:42 +08003006TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3007 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3008 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3009 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3010 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3011 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3012 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3013
3014 KeyboardInputMapper& mapper =
3015 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3016 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3017 // Initial metastate to AMETA_NONE.
3018 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3019 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3020
3021 // Initialization should have turned all of the lights off.
3022 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3023 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3024 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3025
3026 // Toggle caps lock on.
3027 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3028 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3029 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3030 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3031
3032 // Toggle num lock on.
3033 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3034 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3035 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3036 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3037
3038 // Toggle scroll lock on.
3039 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3040 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3041 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3042 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3043
3044 mFakeEventHub->removeDevice(EVENTHUB_ID);
3045 mReader->loopOnce();
3046
3047 // keyboard 2 should default toggle keys.
3048 const std::string USB2 = "USB2";
3049 const std::string DEVICE_NAME2 = "KEYBOARD2";
3050 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3051 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3052 std::shared_ptr<InputDevice> device2 =
3053 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3054 Flags<InputDeviceClass>(0));
3055 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3056 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3057 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3058 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3059 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3060 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3061
arthurhung6fe95782020-10-05 22:41:16 +08003062 KeyboardInputMapper& mapper2 =
3063 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3064 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003065 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3066 device2->reset(ARBITRARY_TIME);
3067
3068 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3069 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3070 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003071 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3072 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003073}
3074
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003075// --- KeyboardInputMapperTest_ExternalDevice ---
3076
3077class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3078protected:
Chris Yea52ade12020-08-27 16:49:20 -07003079 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003080};
3081
3082TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003083 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3084 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003085
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003086 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3087 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3088 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3089 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 KeyboardInputMapper& mapper =
3092 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3093 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003094
3095 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3096 NotifyKeyArgs args;
3097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3098 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3099
3100 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3102 ASSERT_EQ(uint32_t(0), args.policyFlags);
3103
3104 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3106 ASSERT_EQ(uint32_t(0), args.policyFlags);
3107
3108 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3110 ASSERT_EQ(uint32_t(0), args.policyFlags);
3111
3112 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3114 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3115
3116 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3118 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3119}
3120
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003121TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003122 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003123
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003124 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3125 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3126 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003127
Powei Fengd041c5d2019-05-03 17:11:33 -07003128 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003129 KeyboardInputMapper& mapper =
3130 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3131 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003132
3133 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3134 NotifyKeyArgs args;
3135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3136 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3137
3138 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3141
3142 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3144 ASSERT_EQ(uint32_t(0), args.policyFlags);
3145
3146 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3148 ASSERT_EQ(uint32_t(0), args.policyFlags);
3149
3150 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3152 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3153
3154 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3156 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3157}
3158
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159// --- CursorInputMapperTest ---
3160
3161class CursorInputMapperTest : public InputMapperTest {
3162protected:
3163 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3164
Michael Wright17db18e2020-06-26 20:51:44 +01003165 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166
Chris Yea52ade12020-08-27 16:49:20 -07003167 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 InputMapperTest::SetUp();
3169
Michael Wright17db18e2020-06-26 20:51:44 +01003170 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003171 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 }
3173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003174 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3175 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003176
3177 void prepareDisplay(int32_t orientation) {
3178 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003179 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003180 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3181 orientation, uniqueId, NO_PORT, viewportType);
3182 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183};
3184
3185const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003187void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3188 int32_t originalY, int32_t rotatedX,
3189 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 NotifyMotionArgs args;
3191
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003192 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3193 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3194 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3198 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3199 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3200 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3201}
3202
3203TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003205 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003207 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208}
3209
3210TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003212 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003214 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215}
3216
3217TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003219 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220
3221 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003222 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223
3224 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003225 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3226 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3228 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3229
3230 // When the bounds are set, then there should be a valid motion range.
3231 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3232
3233 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003234 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235
3236 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3237 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3238 1, 800 - 1, 0.0f, 0.0f));
3239 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3240 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3241 2, 480 - 1, 0.0f, 0.0f));
3242 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3243 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3244 0.0f, 1.0f, 0.0f, 0.0f));
3245}
3246
3247TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250
3251 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3255 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3256 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3257 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3258 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3259 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3260 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3261 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3262 0.0f, 1.0f, 0.0f, 0.0f));
3263}
3264
3265TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003267 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268
arthurhungdcef2dc2020-08-11 14:47:50 +08003269 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
3271 NotifyMotionArgs args;
3272
3273 // Button press.
3274 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003275 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3276 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3278 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3279 ASSERT_EQ(DEVICE_ID, args.deviceId);
3280 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3281 ASSERT_EQ(uint32_t(0), args.policyFlags);
3282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3283 ASSERT_EQ(0, args.flags);
3284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3285 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3286 ASSERT_EQ(0, args.edgeFlags);
3287 ASSERT_EQ(uint32_t(1), args.pointerCount);
3288 ASSERT_EQ(0, args.pointerProperties[0].id);
3289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3291 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3292 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3293 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3294 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3295
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3297 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3298 ASSERT_EQ(DEVICE_ID, args.deviceId);
3299 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3300 ASSERT_EQ(uint32_t(0), args.policyFlags);
3301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3302 ASSERT_EQ(0, args.flags);
3303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3304 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3305 ASSERT_EQ(0, args.edgeFlags);
3306 ASSERT_EQ(uint32_t(1), args.pointerCount);
3307 ASSERT_EQ(0, args.pointerProperties[0].id);
3308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3310 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3311 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3312 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3313 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3314
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003316 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3317 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3319 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3320 ASSERT_EQ(DEVICE_ID, args.deviceId);
3321 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3322 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3324 ASSERT_EQ(0, args.flags);
3325 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3326 ASSERT_EQ(0, args.buttonState);
3327 ASSERT_EQ(0, args.edgeFlags);
3328 ASSERT_EQ(uint32_t(1), args.pointerCount);
3329 ASSERT_EQ(0, args.pointerProperties[0].id);
3330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3332 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3333 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3334 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3335 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3336
3337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3338 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3339 ASSERT_EQ(DEVICE_ID, args.deviceId);
3340 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3341 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3343 ASSERT_EQ(0, args.flags);
3344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3345 ASSERT_EQ(0, args.buttonState);
3346 ASSERT_EQ(0, args.edgeFlags);
3347 ASSERT_EQ(uint32_t(1), args.pointerCount);
3348 ASSERT_EQ(0, args.pointerProperties[0].id);
3349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3351 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3352 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3353 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3354 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3355}
3356
3357TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
3361 NotifyMotionArgs args;
3362
3363 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003364 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3365 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3369 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3370
3371 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003372 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3373 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3377 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3378}
3379
3380TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003382 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383
3384 NotifyMotionArgs args;
3385
3386 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003387 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3388 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3390 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3392 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3393
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3397 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3398
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003400 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3401 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3405 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3406
3407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3410 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3411}
3412
3413TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003415 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416
3417 NotifyMotionArgs args;
3418
3419 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003420 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3421 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3422 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3423 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3425 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3427 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3428 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3429
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3433 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3434 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3435
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003437 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3438 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3439 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3443 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3444 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3445
3446 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003447 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3448 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003450 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3452 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3453
3454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3457 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3458}
3459
3460TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003462 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003464 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3469 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3472 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3473}
3474
3475TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 addConfigurationProperty("cursor.mode", "navigation");
3477 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003480 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3485 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3487 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3489
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003490 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3499
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003500 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3509
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003510 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3519}
3520
3521TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3526 mFakePointerController->setPosition(100, 200);
3527 mFakePointerController->setButtonState(0);
3528
3529 NotifyMotionArgs motionArgs;
3530 NotifyKeyArgs keyArgs;
3531
3532 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003533 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3534 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3537 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3538 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3540 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3541
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3544 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3545 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3547 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3548
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003549 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3550 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553 ASSERT_EQ(0, motionArgs.buttonState);
3554 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3556 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3557
3558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560 ASSERT_EQ(0, motionArgs.buttonState);
3561 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3563 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3564
3565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003567 ASSERT_EQ(0, motionArgs.buttonState);
3568 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3570 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3571
3572 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003573 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3574 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3575 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3578 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3579 motionArgs.buttonState);
3580 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3581 mFakePointerController->getButtonState());
3582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3583 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3584
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3587 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3588 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3589 mFakePointerController->getButtonState());
3590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3591 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3592
3593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3595 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3596 motionArgs.buttonState);
3597 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3598 mFakePointerController->getButtonState());
3599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3600 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3601
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003602 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3603 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003605 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3607 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3609 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3610
3611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003613 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3614 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3616 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3617
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003618 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3619 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3622 ASSERT_EQ(0, motionArgs.buttonState);
3623 ASSERT_EQ(0, mFakePointerController->getButtonState());
3624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3625 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003626 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3627 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003628
3629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003630 ASSERT_EQ(0, motionArgs.buttonState);
3631 ASSERT_EQ(0, mFakePointerController->getButtonState());
3632 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3634 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 -08003635
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3637 ASSERT_EQ(0, motionArgs.buttonState);
3638 ASSERT_EQ(0, mFakePointerController->getButtonState());
3639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3641 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3642
3643 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003644 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3645 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3647 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3648 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003649
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3653 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3655 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3656
3657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3659 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3660 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3662 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3663
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003664 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3665 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_EQ(0, motionArgs.buttonState);
3669 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3671 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3672
3673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003675 ASSERT_EQ(0, motionArgs.buttonState);
3676 ASSERT_EQ(0, mFakePointerController->getButtonState());
3677
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3679 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3681 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3682 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3683
3684 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003685 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3686 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3688 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3689 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003690
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3694 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3696 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3697
3698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3700 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3701 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3703 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3704
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003705 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3706 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709 ASSERT_EQ(0, motionArgs.buttonState);
3710 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3712 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 -08003713
3714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3716 ASSERT_EQ(0, motionArgs.buttonState);
3717 ASSERT_EQ(0, mFakePointerController->getButtonState());
3718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3719 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3720
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3722 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3723 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3724
3725 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003726 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3727 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3729 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3730 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003731
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3735 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3737 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3738
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3742 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3744 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3745
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003746 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3747 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 ASSERT_EQ(0, motionArgs.buttonState);
3751 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3753 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 -08003754
3755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3757 ASSERT_EQ(0, motionArgs.buttonState);
3758 ASSERT_EQ(0, mFakePointerController->getButtonState());
3759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3760 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3761
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3763 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3764 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3765
3766 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003767 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3768 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3770 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3771 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003772
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3776 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3778 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3779
3780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3781 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3782 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3783 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3785 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3786
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003787 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3788 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003790 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791 ASSERT_EQ(0, motionArgs.buttonState);
3792 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3794 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 -08003795
3796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3797 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3798 ASSERT_EQ(0, motionArgs.buttonState);
3799 ASSERT_EQ(0, mFakePointerController->getButtonState());
3800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3801 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3802
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3804 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3805 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3806}
3807
3808TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811
3812 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3813 mFakePointerController->setPosition(100, 200);
3814 mFakePointerController->setButtonState(0);
3815
3816 NotifyMotionArgs args;
3817
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003818 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3819 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3820 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003822 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3825 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 +01003826 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003827}
3828
3829TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003830 addConfigurationProperty("cursor.mode", "pointer");
3831 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003832 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003833
3834 NotifyDeviceResetArgs resetArgs;
3835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3836 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3837 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3838
3839 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3840 mFakePointerController->setPosition(100, 200);
3841 mFakePointerController->setButtonState(0);
3842
3843 NotifyMotionArgs args;
3844
3845 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003846 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3847 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3848 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3850 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3853 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 +01003854 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003855
3856 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003857 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3858 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3860 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3863 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3865 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3868 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3869
3870 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003871 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3872 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3874 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3875 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3877 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3879 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3880 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3882 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3883
3884 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003885 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3886 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3887 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3889 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3892 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 +01003893 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003894
3895 // Disable pointer capture and check that the device generation got bumped
3896 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003897 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003898 mFakePolicy->setPointerCapture(false);
3899 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003900 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003901
3902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3903 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3904 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3905
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003906 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3907 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3908 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3910 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3913 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 +01003914 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915}
3916
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003917TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003918 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003919
Garfield Tan888a6a42020-01-09 11:39:16 -08003920 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003921 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003922 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3923 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003924 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003925 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3926 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3927
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003928 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3929 mFakePointerController->setPosition(100, 200);
3930 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003931
3932 NotifyMotionArgs args;
3933 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3934 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3935 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3937 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3938 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3940 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 +01003941 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003942 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3943}
3944
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945// --- TouchInputMapperTest ---
3946
3947class TouchInputMapperTest : public InputMapperTest {
3948protected:
3949 static const int32_t RAW_X_MIN;
3950 static const int32_t RAW_X_MAX;
3951 static const int32_t RAW_Y_MIN;
3952 static const int32_t RAW_Y_MAX;
3953 static const int32_t RAW_TOUCH_MIN;
3954 static const int32_t RAW_TOUCH_MAX;
3955 static const int32_t RAW_TOOL_MIN;
3956 static const int32_t RAW_TOOL_MAX;
3957 static const int32_t RAW_PRESSURE_MIN;
3958 static const int32_t RAW_PRESSURE_MAX;
3959 static const int32_t RAW_ORIENTATION_MIN;
3960 static const int32_t RAW_ORIENTATION_MAX;
3961 static const int32_t RAW_DISTANCE_MIN;
3962 static const int32_t RAW_DISTANCE_MAX;
3963 static const int32_t RAW_TILT_MIN;
3964 static const int32_t RAW_TILT_MAX;
3965 static const int32_t RAW_ID_MIN;
3966 static const int32_t RAW_ID_MAX;
3967 static const int32_t RAW_SLOT_MIN;
3968 static const int32_t RAW_SLOT_MAX;
3969 static const float X_PRECISION;
3970 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003971 static const float X_PRECISION_VIRTUAL;
3972 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973
3974 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003975 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976
3977 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3978
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003979 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003980 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003981
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 enum Axes {
3983 POSITION = 1 << 0,
3984 TOUCH = 1 << 1,
3985 TOOL = 1 << 2,
3986 PRESSURE = 1 << 3,
3987 ORIENTATION = 1 << 4,
3988 MINOR = 1 << 5,
3989 ID = 1 << 6,
3990 DISTANCE = 1 << 7,
3991 TILT = 1 << 8,
3992 SLOT = 1 << 9,
3993 TOOL_TYPE = 1 << 10,
3994 };
3995
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003996 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3997 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003998 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004000 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 int32_t toRawX(float displayX);
4002 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004003 float toCookedX(float rawX, float rawY);
4004 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004006 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004008 float toDisplayY(int32_t rawY, int32_t displayHeight);
4009
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010};
4011
4012const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4013const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4014const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4015const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4016const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4017const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4018const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4019const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004020const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4021const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4023const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4024const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4025const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4026const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4027const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4028const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4029const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4030const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4031const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4032const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4033const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004034const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4035 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4036const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4037 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004038const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4039 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040
4041const float TouchInputMapperTest::GEOMETRIC_SCALE =
4042 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4043 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4044
4045const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4046 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4047 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4048};
4049
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004050void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004051 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4052 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004053}
4054
4055void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4056 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4057 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058}
4059
Santos Cordonfa5cf462017-04-05 10:37:00 -07004060void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004061 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4062 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4063 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004064}
4065
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004067 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4068 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4069 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4070 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071}
4072
Jason Gerecke489fda82012-09-07 17:19:40 -07004073void TouchInputMapperTest::prepareLocationCalibration() {
4074 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4075}
4076
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077int32_t TouchInputMapperTest::toRawX(float displayX) {
4078 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4079}
4080
4081int32_t TouchInputMapperTest::toRawY(float displayY) {
4082 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4083}
4084
Jason Gerecke489fda82012-09-07 17:19:40 -07004085float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4086 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4087 return rawX;
4088}
4089
4090float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4091 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4092 return rawY;
4093}
4094
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004096 return toDisplayX(rawX, DISPLAY_WIDTH);
4097}
4098
4099float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4100 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101}
4102
4103float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004104 return toDisplayY(rawY, DISPLAY_HEIGHT);
4105}
4106
4107float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4108 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109}
4110
4111
4112// --- SingleTouchInputMapperTest ---
4113
4114class SingleTouchInputMapperTest : public TouchInputMapperTest {
4115protected:
4116 void prepareButtons();
4117 void prepareAxes(int axes);
4118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004119 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4120 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4121 void processUp(SingleTouchInputMapper& mappery);
4122 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4123 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4124 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4125 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4126 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4127 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128};
4129
4130void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004131 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132}
4133
4134void SingleTouchInputMapperTest::prepareAxes(int axes) {
4135 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004136 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 }
4139 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4141 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 }
4143 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4145 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 }
4147 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4149 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 }
4151 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004152 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4153 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 }
4155}
4156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004157void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004158 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4159 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4160 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161}
4162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004163void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004164 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4165 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166}
4167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004168void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004169 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170}
4171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004172void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004173 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174}
4175
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004176void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4177 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004178 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179}
4180
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004181void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004182 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183}
4184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004185void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4186 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004187 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4188 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189}
4190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004191void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4192 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004193 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194}
4195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004196void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004197 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198}
4199
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 prepareButtons();
4202 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004203 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004205 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206}
4207
4208TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004209 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4210 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 prepareButtons();
4212 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004213 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004215 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216}
4217
4218TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 prepareButtons();
4220 prepareAxes(POSITION);
4221 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004222 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225}
4226
4227TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 prepareButtons();
4229 prepareAxes(POSITION);
4230 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004233 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234}
4235
4236TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 addConfigurationProperty("touch.deviceType", "touchScreen");
4238 prepareDisplay(DISPLAY_ORIENTATION_0);
4239 prepareButtons();
4240 prepareAxes(POSITION);
4241 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004242 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243
4244 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004245 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246
4247 // Virtual key is down.
4248 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4249 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4250 processDown(mapper, x, y);
4251 processSync(mapper);
4252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004254 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255
4256 // Virtual key is up.
4257 processUp(mapper);
4258 processSync(mapper);
4259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4260
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004261 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262}
4263
4264TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 addConfigurationProperty("touch.deviceType", "touchScreen");
4266 prepareDisplay(DISPLAY_ORIENTATION_0);
4267 prepareButtons();
4268 prepareAxes(POSITION);
4269 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004270 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
4275 // Virtual key is down.
4276 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4277 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4278 processDown(mapper, x, y);
4279 processSync(mapper);
4280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4281
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004282 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283
4284 // Virtual key is up.
4285 processUp(mapper);
4286 processSync(mapper);
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004289 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290}
4291
4292TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 addConfigurationProperty("touch.deviceType", "touchScreen");
4294 prepareDisplay(DISPLAY_ORIENTATION_0);
4295 prepareButtons();
4296 prepareAxes(POSITION);
4297 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004298 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299
4300 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4301 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004302 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 ASSERT_TRUE(flags[0]);
4304 ASSERT_FALSE(flags[1]);
4305}
4306
4307TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 addConfigurationProperty("touch.deviceType", "touchScreen");
4309 prepareDisplay(DISPLAY_ORIENTATION_0);
4310 prepareButtons();
4311 prepareAxes(POSITION);
4312 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004313 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314
arthurhungdcef2dc2020-08-11 14:47:50 +08004315 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316
4317 NotifyKeyArgs args;
4318
4319 // Press virtual key.
4320 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4321 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4322 processDown(mapper, x, y);
4323 processSync(mapper);
4324
4325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4326 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4327 ASSERT_EQ(DEVICE_ID, args.deviceId);
4328 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4329 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4330 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4331 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4332 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4333 ASSERT_EQ(KEY_HOME, args.scanCode);
4334 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4335 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4336
4337 // Release virtual key.
4338 processUp(mapper);
4339 processSync(mapper);
4340
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4342 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4343 ASSERT_EQ(DEVICE_ID, args.deviceId);
4344 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4345 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4346 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4347 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4348 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4349 ASSERT_EQ(KEY_HOME, args.scanCode);
4350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4351 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4352
4353 // Should not have sent any motions.
4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4355}
4356
4357TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 addConfigurationProperty("touch.deviceType", "touchScreen");
4359 prepareDisplay(DISPLAY_ORIENTATION_0);
4360 prepareButtons();
4361 prepareAxes(POSITION);
4362 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004363 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364
arthurhungdcef2dc2020-08-11 14:47:50 +08004365 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366
4367 NotifyKeyArgs keyArgs;
4368
4369 // Press virtual key.
4370 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4371 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4372 processDown(mapper, x, y);
4373 processSync(mapper);
4374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4376 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4377 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4378 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4379 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4380 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4381 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4382 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4383 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4385 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4386
4387 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4388 // into the display area.
4389 y -= 100;
4390 processMove(mapper, x, y);
4391 processSync(mapper);
4392
4393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4394 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4395 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4396 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4397 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4398 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4399 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4400 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4401 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4402 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4403 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4404 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4405
4406 NotifyMotionArgs motionArgs;
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4408 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4409 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4410 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4411 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4413 ASSERT_EQ(0, motionArgs.flags);
4414 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4415 ASSERT_EQ(0, motionArgs.buttonState);
4416 ASSERT_EQ(0, motionArgs.edgeFlags);
4417 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4421 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4422 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4423 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4424 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4425
4426 // Keep moving out of bounds. Should generate a pointer move.
4427 y -= 50;
4428 processMove(mapper, x, y);
4429 processSync(mapper);
4430
4431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4433 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4434 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4435 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4437 ASSERT_EQ(0, motionArgs.flags);
4438 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4439 ASSERT_EQ(0, motionArgs.buttonState);
4440 ASSERT_EQ(0, motionArgs.edgeFlags);
4441 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4442 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4445 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4446 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4447 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4448 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4449
4450 // Release out of bounds. Should generate a pointer up.
4451 processUp(mapper);
4452 processSync(mapper);
4453
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4456 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4457 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4458 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4460 ASSERT_EQ(0, motionArgs.flags);
4461 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4462 ASSERT_EQ(0, motionArgs.buttonState);
4463 ASSERT_EQ(0, motionArgs.edgeFlags);
4464 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4465 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4468 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4469 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4470 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4471 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4472
4473 // Should not have sent any more keys or motions.
4474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4476}
4477
4478TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 addConfigurationProperty("touch.deviceType", "touchScreen");
4480 prepareDisplay(DISPLAY_ORIENTATION_0);
4481 prepareButtons();
4482 prepareAxes(POSITION);
4483 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004484 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485
arthurhungdcef2dc2020-08-11 14:47:50 +08004486 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487
4488 NotifyMotionArgs motionArgs;
4489
4490 // Initially go down out of bounds.
4491 int32_t x = -10;
4492 int32_t y = -10;
4493 processDown(mapper, x, y);
4494 processSync(mapper);
4495
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4497
4498 // Move into the display area. Should generate a pointer down.
4499 x = 50;
4500 y = 75;
4501 processMove(mapper, x, y);
4502 processSync(mapper);
4503
4504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4506 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4507 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4508 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4510 ASSERT_EQ(0, motionArgs.flags);
4511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4512 ASSERT_EQ(0, motionArgs.buttonState);
4513 ASSERT_EQ(0, motionArgs.edgeFlags);
4514 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4518 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4519 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4520 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4522
4523 // Release. Should generate a pointer up.
4524 processUp(mapper);
4525 processSync(mapper);
4526
4527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4528 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4529 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4530 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4531 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4532 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4533 ASSERT_EQ(0, motionArgs.flags);
4534 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4535 ASSERT_EQ(0, motionArgs.buttonState);
4536 ASSERT_EQ(0, motionArgs.edgeFlags);
4537 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4538 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4541 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4542 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4543 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4545
4546 // Should not have sent any more keys or motions.
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4549}
4550
Santos Cordonfa5cf462017-04-05 10:37:00 -07004551TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004552 addConfigurationProperty("touch.deviceType", "touchScreen");
4553 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4554
4555 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4556 prepareButtons();
4557 prepareAxes(POSITION);
4558 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004559 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004560
arthurhungdcef2dc2020-08-11 14:47:50 +08004561 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004562
4563 NotifyMotionArgs motionArgs;
4564
4565 // Down.
4566 int32_t x = 100;
4567 int32_t y = 125;
4568 processDown(mapper, x, y);
4569 processSync(mapper);
4570
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4572 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4573 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4574 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4575 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4576 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4578 ASSERT_EQ(0, motionArgs.flags);
4579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4580 ASSERT_EQ(0, motionArgs.buttonState);
4581 ASSERT_EQ(0, motionArgs.edgeFlags);
4582 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4583 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4586 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4587 1, 0, 0, 0, 0, 0, 0, 0));
4588 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4589 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4590 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4591
4592 // Move.
4593 x += 50;
4594 y += 75;
4595 processMove(mapper, x, y);
4596 processSync(mapper);
4597
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4600 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4601 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4602 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4603 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4605 ASSERT_EQ(0, motionArgs.flags);
4606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4607 ASSERT_EQ(0, motionArgs.buttonState);
4608 ASSERT_EQ(0, motionArgs.edgeFlags);
4609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4613 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4614 1, 0, 0, 0, 0, 0, 0, 0));
4615 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4616 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4618
4619 // Up.
4620 processUp(mapper);
4621 processSync(mapper);
4622
4623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4624 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4625 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4626 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4627 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4628 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4630 ASSERT_EQ(0, motionArgs.flags);
4631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4632 ASSERT_EQ(0, motionArgs.buttonState);
4633 ASSERT_EQ(0, motionArgs.edgeFlags);
4634 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4638 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4639 1, 0, 0, 0, 0, 0, 0, 0));
4640 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4641 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4643
4644 // Should not have sent any more keys or motions.
4645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4647}
4648
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 addConfigurationProperty("touch.deviceType", "touchScreen");
4651 prepareDisplay(DISPLAY_ORIENTATION_0);
4652 prepareButtons();
4653 prepareAxes(POSITION);
4654 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004655 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656
arthurhungdcef2dc2020-08-11 14:47:50 +08004657 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658
4659 NotifyMotionArgs motionArgs;
4660
4661 // Down.
4662 int32_t x = 100;
4663 int32_t y = 125;
4664 processDown(mapper, x, y);
4665 processSync(mapper);
4666
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4668 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4669 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4670 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4671 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4672 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4673 ASSERT_EQ(0, motionArgs.flags);
4674 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4675 ASSERT_EQ(0, motionArgs.buttonState);
4676 ASSERT_EQ(0, motionArgs.edgeFlags);
4677 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4678 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4681 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4682 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4683 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4684 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4685
4686 // Move.
4687 x += 50;
4688 y += 75;
4689 processMove(mapper, x, y);
4690 processSync(mapper);
4691
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4693 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4694 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4695 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4696 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4698 ASSERT_EQ(0, motionArgs.flags);
4699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4700 ASSERT_EQ(0, motionArgs.buttonState);
4701 ASSERT_EQ(0, motionArgs.edgeFlags);
4702 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4703 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4706 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4707 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4708 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4709 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4710
4711 // Up.
4712 processUp(mapper);
4713 processSync(mapper);
4714
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4716 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4717 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4718 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4719 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4721 ASSERT_EQ(0, motionArgs.flags);
4722 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4723 ASSERT_EQ(0, motionArgs.buttonState);
4724 ASSERT_EQ(0, motionArgs.edgeFlags);
4725 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4726 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4729 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4730 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4731 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4732 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4733
4734 // Should not have sent any more keys or motions.
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4737}
4738
4739TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 addConfigurationProperty("touch.deviceType", "touchScreen");
4741 prepareButtons();
4742 prepareAxes(POSITION);
4743 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004744 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745
4746 NotifyMotionArgs args;
4747
4748 // Rotation 90.
4749 prepareDisplay(DISPLAY_ORIENTATION_90);
4750 processDown(mapper, toRawX(50), toRawY(75));
4751 processSync(mapper);
4752
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4754 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4755 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4756
4757 processUp(mapper);
4758 processSync(mapper);
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4760}
4761
4762TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 addConfigurationProperty("touch.deviceType", "touchScreen");
4764 prepareButtons();
4765 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004766 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767
4768 NotifyMotionArgs args;
4769
4770 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004771 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 prepareDisplay(DISPLAY_ORIENTATION_0);
4773 processDown(mapper, toRawX(50), toRawY(75));
4774 processSync(mapper);
4775
4776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4777 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4778 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4779
4780 processUp(mapper);
4781 processSync(mapper);
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4783
4784 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004785 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 prepareDisplay(DISPLAY_ORIENTATION_90);
4787 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4788 processSync(mapper);
4789
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4791 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4792 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4793
4794 processUp(mapper);
4795 processSync(mapper);
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4797
4798 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004799 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800 prepareDisplay(DISPLAY_ORIENTATION_180);
4801 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4802 processSync(mapper);
4803
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4805 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4806 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4807
4808 processUp(mapper);
4809 processSync(mapper);
4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4811
4812 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004813 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 prepareDisplay(DISPLAY_ORIENTATION_270);
4815 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4816 processSync(mapper);
4817
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4819 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4820 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4821
4822 processUp(mapper);
4823 processSync(mapper);
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4825}
4826
4827TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 addConfigurationProperty("touch.deviceType", "touchScreen");
4829 prepareDisplay(DISPLAY_ORIENTATION_0);
4830 prepareButtons();
4831 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004832 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833
4834 // These calculations are based on the input device calibration documentation.
4835 int32_t rawX = 100;
4836 int32_t rawY = 200;
4837 int32_t rawPressure = 10;
4838 int32_t rawToolMajor = 12;
4839 int32_t rawDistance = 2;
4840 int32_t rawTiltX = 30;
4841 int32_t rawTiltY = 110;
4842
4843 float x = toDisplayX(rawX);
4844 float y = toDisplayY(rawY);
4845 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4846 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4847 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4848 float distance = float(rawDistance);
4849
4850 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4851 float tiltScale = M_PI / 180;
4852 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4853 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4854 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4855 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4856
4857 processDown(mapper, rawX, rawY);
4858 processPressure(mapper, rawPressure);
4859 processToolMajor(mapper, rawToolMajor);
4860 processDistance(mapper, rawDistance);
4861 processTilt(mapper, rawTiltX, rawTiltY);
4862 processSync(mapper);
4863
4864 NotifyMotionArgs args;
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4867 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4868 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4869}
4870
Jason Gerecke489fda82012-09-07 17:19:40 -07004871TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004872 addConfigurationProperty("touch.deviceType", "touchScreen");
4873 prepareDisplay(DISPLAY_ORIENTATION_0);
4874 prepareLocationCalibration();
4875 prepareButtons();
4876 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004878
4879 int32_t rawX = 100;
4880 int32_t rawY = 200;
4881
4882 float x = toDisplayX(toCookedX(rawX, rawY));
4883 float y = toDisplayY(toCookedY(rawX, rawY));
4884
4885 processDown(mapper, rawX, rawY);
4886 processSync(mapper);
4887
4888 NotifyMotionArgs args;
4889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4891 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4892}
4893
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895 addConfigurationProperty("touch.deviceType", "touchScreen");
4896 prepareDisplay(DISPLAY_ORIENTATION_0);
4897 prepareButtons();
4898 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004899 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900
4901 NotifyMotionArgs motionArgs;
4902 NotifyKeyArgs keyArgs;
4903
4904 processDown(mapper, 100, 200);
4905 processSync(mapper);
4906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4907 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4908 ASSERT_EQ(0, motionArgs.buttonState);
4909
4910 // press BTN_LEFT, release BTN_LEFT
4911 processKey(mapper, BTN_LEFT, 1);
4912 processSync(mapper);
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4915 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4916
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4919 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4920
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 processKey(mapper, BTN_LEFT, 0);
4922 processSync(mapper);
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004926
4927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004929 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930
4931 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4932 processKey(mapper, BTN_RIGHT, 1);
4933 processKey(mapper, BTN_MIDDLE, 1);
4934 processSync(mapper);
4935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4937 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4938 motionArgs.buttonState);
4939
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4942 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4943
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4946 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4947 motionArgs.buttonState);
4948
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949 processKey(mapper, BTN_RIGHT, 0);
4950 processSync(mapper);
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004954
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004957 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958
4959 processKey(mapper, BTN_MIDDLE, 0);
4960 processSync(mapper);
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004967 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968
4969 // press BTN_BACK, release BTN_BACK
4970 processKey(mapper, BTN_BACK, 1);
4971 processSync(mapper);
4972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4973 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4974 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004975
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004978 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4979
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4982 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983
4984 processKey(mapper, BTN_BACK, 0);
4985 processSync(mapper);
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004989
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004992 ASSERT_EQ(0, motionArgs.buttonState);
4993
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4995 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4996 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4997
4998 // press BTN_SIDE, release BTN_SIDE
4999 processKey(mapper, BTN_SIDE, 1);
5000 processSync(mapper);
5001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5002 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5003 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005004
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005007 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5008
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5011 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012
5013 processKey(mapper, BTN_SIDE, 0);
5014 processSync(mapper);
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005018
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005021 ASSERT_EQ(0, motionArgs.buttonState);
5022
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5024 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5025 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5026
5027 // press BTN_FORWARD, release BTN_FORWARD
5028 processKey(mapper, BTN_FORWARD, 1);
5029 processSync(mapper);
5030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5031 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5032 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005033
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005036 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5037
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5040 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041
5042 processKey(mapper, BTN_FORWARD, 0);
5043 processSync(mapper);
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005047
5048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005050 ASSERT_EQ(0, motionArgs.buttonState);
5051
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5053 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5054 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5055
5056 // press BTN_EXTRA, release BTN_EXTRA
5057 processKey(mapper, BTN_EXTRA, 1);
5058 processSync(mapper);
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5060 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5061 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005062
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005065 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5066
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5069 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070
5071 processKey(mapper, BTN_EXTRA, 0);
5072 processSync(mapper);
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005074 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005076
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005079 ASSERT_EQ(0, motionArgs.buttonState);
5080
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5082 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5083 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5084
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5086
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 // press BTN_STYLUS, release BTN_STYLUS
5088 processKey(mapper, BTN_STYLUS, 1);
5089 processSync(mapper);
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005092 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5093
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5096 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097
5098 processKey(mapper, BTN_STYLUS, 0);
5099 processSync(mapper);
5100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005103
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005106 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107
5108 // press BTN_STYLUS2, release BTN_STYLUS2
5109 processKey(mapper, BTN_STYLUS2, 1);
5110 processSync(mapper);
5111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005113 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5114
5115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5117 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118
5119 processKey(mapper, BTN_STYLUS2, 0);
5120 processSync(mapper);
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005124
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005127 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128
5129 // release touch
5130 processUp(mapper);
5131 processSync(mapper);
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5134 ASSERT_EQ(0, motionArgs.buttonState);
5135}
5136
5137TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138 addConfigurationProperty("touch.deviceType", "touchScreen");
5139 prepareDisplay(DISPLAY_ORIENTATION_0);
5140 prepareButtons();
5141 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005142 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143
5144 NotifyMotionArgs motionArgs;
5145
5146 // default tool type is finger
5147 processDown(mapper, 100, 200);
5148 processSync(mapper);
5149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5152
5153 // eraser
5154 processKey(mapper, BTN_TOOL_RUBBER, 1);
5155 processSync(mapper);
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5159
5160 // stylus
5161 processKey(mapper, BTN_TOOL_RUBBER, 0);
5162 processKey(mapper, BTN_TOOL_PEN, 1);
5163 processSync(mapper);
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5167
5168 // brush
5169 processKey(mapper, BTN_TOOL_PEN, 0);
5170 processKey(mapper, BTN_TOOL_BRUSH, 1);
5171 processSync(mapper);
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5175
5176 // pencil
5177 processKey(mapper, BTN_TOOL_BRUSH, 0);
5178 processKey(mapper, BTN_TOOL_PENCIL, 1);
5179 processSync(mapper);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5183
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005184 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185 processKey(mapper, BTN_TOOL_PENCIL, 0);
5186 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5187 processSync(mapper);
5188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5191
5192 // mouse
5193 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5194 processKey(mapper, BTN_TOOL_MOUSE, 1);
5195 processSync(mapper);
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5199
5200 // lens
5201 processKey(mapper, BTN_TOOL_MOUSE, 0);
5202 processKey(mapper, BTN_TOOL_LENS, 1);
5203 processSync(mapper);
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5207
5208 // double-tap
5209 processKey(mapper, BTN_TOOL_LENS, 0);
5210 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5211 processSync(mapper);
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5215
5216 // triple-tap
5217 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5218 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5219 processSync(mapper);
5220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5223
5224 // quad-tap
5225 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5226 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5227 processSync(mapper);
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5231
5232 // finger
5233 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5234 processKey(mapper, BTN_TOOL_FINGER, 1);
5235 processSync(mapper);
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5239
5240 // stylus trumps finger
5241 processKey(mapper, BTN_TOOL_PEN, 1);
5242 processSync(mapper);
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5246
5247 // eraser trumps stylus
5248 processKey(mapper, BTN_TOOL_RUBBER, 1);
5249 processSync(mapper);
5250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5253
5254 // mouse trumps eraser
5255 processKey(mapper, BTN_TOOL_MOUSE, 1);
5256 processSync(mapper);
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5260
5261 // back to default tool type
5262 processKey(mapper, BTN_TOOL_MOUSE, 0);
5263 processKey(mapper, BTN_TOOL_RUBBER, 0);
5264 processKey(mapper, BTN_TOOL_PEN, 0);
5265 processKey(mapper, BTN_TOOL_FINGER, 0);
5266 processSync(mapper);
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5270}
5271
5272TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273 addConfigurationProperty("touch.deviceType", "touchScreen");
5274 prepareDisplay(DISPLAY_ORIENTATION_0);
5275 prepareButtons();
5276 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005277 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005278 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279
5280 NotifyMotionArgs motionArgs;
5281
5282 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5283 processKey(mapper, BTN_TOOL_FINGER, 1);
5284 processMove(mapper, 100, 200);
5285 processSync(mapper);
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5289 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5290
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5292 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5294 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5295
5296 // move a little
5297 processMove(mapper, 150, 250);
5298 processSync(mapper);
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5300 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5302 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5303
5304 // down when BTN_TOUCH is pressed, pressure defaults to 1
5305 processKey(mapper, BTN_TOUCH, 1);
5306 processSync(mapper);
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5308 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5310 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5311
5312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5313 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5315 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5316
5317 // up when BTN_TOUCH is released, hover restored
5318 processKey(mapper, BTN_TOUCH, 0);
5319 processSync(mapper);
5320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5321 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5323 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5324
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5326 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5328 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5329
5330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5333 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5334
5335 // exit hover when pointer goes away
5336 processKey(mapper, BTN_TOOL_FINGER, 0);
5337 processSync(mapper);
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5342}
5343
5344TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345 addConfigurationProperty("touch.deviceType", "touchScreen");
5346 prepareDisplay(DISPLAY_ORIENTATION_0);
5347 prepareButtons();
5348 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005349 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350
5351 NotifyMotionArgs motionArgs;
5352
5353 // initially hovering because pressure is 0
5354 processDown(mapper, 100, 200);
5355 processPressure(mapper, 0);
5356 processSync(mapper);
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5360 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5361
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5365 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5366
5367 // move a little
5368 processMove(mapper, 150, 250);
5369 processSync(mapper);
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5373 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5374
5375 // down when pressure is non-zero
5376 processPressure(mapper, RAW_PRESSURE_MAX);
5377 processSync(mapper);
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5381 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5382
5383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5386 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5387
5388 // up when pressure becomes 0, hover restored
5389 processPressure(mapper, 0);
5390 processSync(mapper);
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5392 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5394 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5395
5396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5399 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5400
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5404 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5405
5406 // exit hover when pointer goes away
5407 processUp(mapper);
5408 processSync(mapper);
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5413}
5414
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415// --- MultiTouchInputMapperTest ---
5416
5417class MultiTouchInputMapperTest : public TouchInputMapperTest {
5418protected:
5419 void prepareAxes(int axes);
5420
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005421 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5422 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5423 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5424 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5425 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5426 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5427 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5428 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5429 void processId(MultiTouchInputMapper& mapper, int32_t id);
5430 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5431 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5432 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5433 void processMTSync(MultiTouchInputMapper& mapper);
5434 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435};
5436
5437void MultiTouchInputMapperTest::prepareAxes(int axes) {
5438 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005439 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5440 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441 }
5442 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005443 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5444 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005446 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5447 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 }
5449 }
5450 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5452 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005454 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5455 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456 }
5457 }
5458 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005459 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5460 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461 }
5462 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005463 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5464 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 }
5466 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005467 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5468 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469 }
5470 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005471 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5472 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473 }
5474 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005475 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5476 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 }
5478 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005479 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480 }
5481}
5482
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005483void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5484 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005485 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5486 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487}
5488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005489void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5490 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005491 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005492}
5493
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005494void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5495 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005496 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497}
5498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005499void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005500 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501}
5502
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005503void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005504 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505}
5506
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005507void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5508 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005509 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510}
5511
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005512void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005513 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514}
5515
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005516void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005517 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518}
5519
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005520void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005521 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522}
5523
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005524void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005525 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526}
5527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005528void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005529 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530}
5531
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005532void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5533 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005534 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005535}
5536
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005537void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005538 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539}
5540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005541void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005542 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543}
5544
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 addConfigurationProperty("touch.deviceType", "touchScreen");
5547 prepareDisplay(DISPLAY_ORIENTATION_0);
5548 prepareAxes(POSITION);
5549 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005550 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551
arthurhungdcef2dc2020-08-11 14:47:50 +08005552 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553
5554 NotifyMotionArgs motionArgs;
5555
5556 // Two fingers down at once.
5557 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5558 processPosition(mapper, x1, y1);
5559 processMTSync(mapper);
5560 processPosition(mapper, x2, y2);
5561 processMTSync(mapper);
5562 processSync(mapper);
5563
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5566 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5567 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5568 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5569 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5570 ASSERT_EQ(0, motionArgs.flags);
5571 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5572 ASSERT_EQ(0, motionArgs.buttonState);
5573 ASSERT_EQ(0, motionArgs.edgeFlags);
5574 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5575 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5578 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5579 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5580 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5585 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5586 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5587 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5588 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5589 motionArgs.action);
5590 ASSERT_EQ(0, motionArgs.flags);
5591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5592 ASSERT_EQ(0, motionArgs.buttonState);
5593 ASSERT_EQ(0, motionArgs.edgeFlags);
5594 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5595 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5597 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5600 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5602 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5603 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5604 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5605 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5606
5607 // Move.
5608 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5609 processPosition(mapper, x1, y1);
5610 processMTSync(mapper);
5611 processPosition(mapper, x2, y2);
5612 processMTSync(mapper);
5613 processSync(mapper);
5614
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5616 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5617 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5618 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5619 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5621 ASSERT_EQ(0, motionArgs.flags);
5622 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5623 ASSERT_EQ(0, motionArgs.buttonState);
5624 ASSERT_EQ(0, motionArgs.edgeFlags);
5625 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5626 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5628 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5631 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5633 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5634 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5635 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5636 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5637
5638 // First finger up.
5639 x2 += 15; y2 -= 20;
5640 processPosition(mapper, x2, y2);
5641 processMTSync(mapper);
5642 processSync(mapper);
5643
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5645 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5646 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5647 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5648 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5649 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5650 motionArgs.action);
5651 ASSERT_EQ(0, motionArgs.flags);
5652 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5653 ASSERT_EQ(0, motionArgs.buttonState);
5654 ASSERT_EQ(0, motionArgs.edgeFlags);
5655 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5658 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5661 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5663 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5664 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5665 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5667
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5670 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5672 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5674 ASSERT_EQ(0, motionArgs.flags);
5675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5676 ASSERT_EQ(0, motionArgs.buttonState);
5677 ASSERT_EQ(0, motionArgs.edgeFlags);
5678 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5679 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5682 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5683 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5684 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5686
5687 // Move.
5688 x2 += 20; y2 -= 25;
5689 processPosition(mapper, x2, y2);
5690 processMTSync(mapper);
5691 processSync(mapper);
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5694 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5695 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5696 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5697 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5699 ASSERT_EQ(0, motionArgs.flags);
5700 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5701 ASSERT_EQ(0, motionArgs.buttonState);
5702 ASSERT_EQ(0, motionArgs.edgeFlags);
5703 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5704 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5705 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5707 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5708 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5709 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5711
5712 // New finger down.
5713 int32_t x3 = 700, y3 = 300;
5714 processPosition(mapper, x2, y2);
5715 processMTSync(mapper);
5716 processPosition(mapper, x3, y3);
5717 processMTSync(mapper);
5718 processSync(mapper);
5719
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5722 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5723 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5724 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5725 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5726 motionArgs.action);
5727 ASSERT_EQ(0, motionArgs.flags);
5728 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5729 ASSERT_EQ(0, motionArgs.buttonState);
5730 ASSERT_EQ(0, motionArgs.edgeFlags);
5731 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5734 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5737 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5740 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5741 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5742 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5743
5744 // Second finger up.
5745 x3 += 30; y3 -= 20;
5746 processPosition(mapper, x3, y3);
5747 processMTSync(mapper);
5748 processSync(mapper);
5749
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5751 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5752 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5753 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5754 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5755 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5756 motionArgs.action);
5757 ASSERT_EQ(0, motionArgs.flags);
5758 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5759 ASSERT_EQ(0, motionArgs.buttonState);
5760 ASSERT_EQ(0, motionArgs.edgeFlags);
5761 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5762 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5764 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5767 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5769 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5770 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5771 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5772 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5773
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5775 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5776 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5777 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5778 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5780 ASSERT_EQ(0, motionArgs.flags);
5781 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5782 ASSERT_EQ(0, motionArgs.buttonState);
5783 ASSERT_EQ(0, motionArgs.edgeFlags);
5784 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5785 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5788 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5789 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5790 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5791 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5792
5793 // Last finger up.
5794 processMTSync(mapper);
5795 processSync(mapper);
5796
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5799 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5800 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5801 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5802 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5803 ASSERT_EQ(0, motionArgs.flags);
5804 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5805 ASSERT_EQ(0, motionArgs.buttonState);
5806 ASSERT_EQ(0, motionArgs.edgeFlags);
5807 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5808 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5811 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5812 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5813 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5814 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5815
5816 // Should not have sent any more keys or motions.
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5819}
5820
5821TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822 addConfigurationProperty("touch.deviceType", "touchScreen");
5823 prepareDisplay(DISPLAY_ORIENTATION_0);
5824 prepareAxes(POSITION | ID);
5825 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005826 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827
arthurhungdcef2dc2020-08-11 14:47:50 +08005828 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829
5830 NotifyMotionArgs motionArgs;
5831
5832 // Two fingers down at once.
5833 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5834 processPosition(mapper, x1, y1);
5835 processId(mapper, 1);
5836 processMTSync(mapper);
5837 processPosition(mapper, x2, y2);
5838 processId(mapper, 2);
5839 processMTSync(mapper);
5840 processSync(mapper);
5841
5842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5844 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5845 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5848 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5849
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5852 motionArgs.action);
5853 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5854 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5856 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5859 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5861 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5862
5863 // Move.
5864 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5865 processPosition(mapper, x1, y1);
5866 processId(mapper, 1);
5867 processMTSync(mapper);
5868 processPosition(mapper, x2, y2);
5869 processId(mapper, 2);
5870 processMTSync(mapper);
5871 processSync(mapper);
5872
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5875 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5876 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5878 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5881 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5883 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5884
5885 // First finger up.
5886 x2 += 15; y2 -= 20;
5887 processPosition(mapper, x2, y2);
5888 processId(mapper, 2);
5889 processMTSync(mapper);
5890 processSync(mapper);
5891
5892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5893 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5894 motionArgs.action);
5895 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5896 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5898 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5901 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5903 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5904
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5907 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5908 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5911 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5912
5913 // Move.
5914 x2 += 20; y2 -= 25;
5915 processPosition(mapper, x2, y2);
5916 processId(mapper, 2);
5917 processMTSync(mapper);
5918 processSync(mapper);
5919
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5922 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5923 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5926 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5927
5928 // New finger down.
5929 int32_t x3 = 700, y3 = 300;
5930 processPosition(mapper, x2, y2);
5931 processId(mapper, 2);
5932 processMTSync(mapper);
5933 processPosition(mapper, x3, y3);
5934 processId(mapper, 3);
5935 processMTSync(mapper);
5936 processSync(mapper);
5937
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5940 motionArgs.action);
5941 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5944 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5949 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5950
5951 // Second finger up.
5952 x3 += 30; y3 -= 20;
5953 processPosition(mapper, x3, y3);
5954 processId(mapper, 3);
5955 processMTSync(mapper);
5956 processSync(mapper);
5957
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5960 motionArgs.action);
5961 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5962 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5964 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5967 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5970
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5973 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5974 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5977 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5978
5979 // Last finger up.
5980 processMTSync(mapper);
5981 processSync(mapper);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5985 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5986 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5989 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5990
5991 // Should not have sent any more keys or motions.
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5994}
5995
5996TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 addConfigurationProperty("touch.deviceType", "touchScreen");
5998 prepareDisplay(DISPLAY_ORIENTATION_0);
5999 prepareAxes(POSITION | ID | SLOT);
6000 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002
arthurhungdcef2dc2020-08-11 14:47:50 +08006003 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004
6005 NotifyMotionArgs motionArgs;
6006
6007 // Two fingers down at once.
6008 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6009 processPosition(mapper, x1, y1);
6010 processId(mapper, 1);
6011 processSlot(mapper, 1);
6012 processPosition(mapper, x2, y2);
6013 processId(mapper, 2);
6014 processSync(mapper);
6015
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6017 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6018 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6019 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6022 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6023
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6025 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6026 motionArgs.action);
6027 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6028 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6030 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6033 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6035 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6036
6037 // Move.
6038 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6039 processSlot(mapper, 0);
6040 processPosition(mapper, x1, y1);
6041 processSlot(mapper, 1);
6042 processPosition(mapper, x2, y2);
6043 processSync(mapper);
6044
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6047 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6048 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6050 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6053 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6055 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6056
6057 // First finger up.
6058 x2 += 15; y2 -= 20;
6059 processSlot(mapper, 0);
6060 processId(mapper, -1);
6061 processSlot(mapper, 1);
6062 processPosition(mapper, x2, y2);
6063 processSync(mapper);
6064
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6067 motionArgs.action);
6068 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6071 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6074 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6076 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6077
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6080 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6081 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6084 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6085
6086 // Move.
6087 x2 += 20; y2 -= 25;
6088 processPosition(mapper, x2, y2);
6089 processSync(mapper);
6090
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6093 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6094 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6097 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6098
6099 // New finger down.
6100 int32_t x3 = 700, y3 = 300;
6101 processPosition(mapper, x2, y2);
6102 processSlot(mapper, 0);
6103 processId(mapper, 3);
6104 processPosition(mapper, x3, y3);
6105 processSync(mapper);
6106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6109 motionArgs.action);
6110 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6111 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6113 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6116 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6118 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6119
6120 // Second finger up.
6121 x3 += 30; y3 -= 20;
6122 processSlot(mapper, 1);
6123 processId(mapper, -1);
6124 processSlot(mapper, 0);
6125 processPosition(mapper, x3, y3);
6126 processSync(mapper);
6127
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6130 motionArgs.action);
6131 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6132 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6134 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6137 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6139 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6140
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6143 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6144 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6147 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6148
6149 // Last finger up.
6150 processId(mapper, -1);
6151 processSync(mapper);
6152
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6155 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6156 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6159 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6160
6161 // Should not have sent any more keys or motions.
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6164}
6165
6166TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 addConfigurationProperty("touch.deviceType", "touchScreen");
6168 prepareDisplay(DISPLAY_ORIENTATION_0);
6169 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006170 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171
6172 // These calculations are based on the input device calibration documentation.
6173 int32_t rawX = 100;
6174 int32_t rawY = 200;
6175 int32_t rawTouchMajor = 7;
6176 int32_t rawTouchMinor = 6;
6177 int32_t rawToolMajor = 9;
6178 int32_t rawToolMinor = 8;
6179 int32_t rawPressure = 11;
6180 int32_t rawDistance = 0;
6181 int32_t rawOrientation = 3;
6182 int32_t id = 5;
6183
6184 float x = toDisplayX(rawX);
6185 float y = toDisplayY(rawY);
6186 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6187 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6188 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6189 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6190 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6191 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6192 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6193 float distance = float(rawDistance);
6194
6195 processPosition(mapper, rawX, rawY);
6196 processTouchMajor(mapper, rawTouchMajor);
6197 processTouchMinor(mapper, rawTouchMinor);
6198 processToolMajor(mapper, rawToolMajor);
6199 processToolMinor(mapper, rawToolMinor);
6200 processPressure(mapper, rawPressure);
6201 processOrientation(mapper, rawOrientation);
6202 processDistance(mapper, rawDistance);
6203 processId(mapper, id);
6204 processMTSync(mapper);
6205 processSync(mapper);
6206
6207 NotifyMotionArgs args;
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6209 ASSERT_EQ(0, args.pointerProperties[0].id);
6210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6211 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6212 orientation, distance));
6213}
6214
6215TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 addConfigurationProperty("touch.deviceType", "touchScreen");
6217 prepareDisplay(DISPLAY_ORIENTATION_0);
6218 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6219 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
6222 // These calculations are based on the input device calibration documentation.
6223 int32_t rawX = 100;
6224 int32_t rawY = 200;
6225 int32_t rawTouchMajor = 140;
6226 int32_t rawTouchMinor = 120;
6227 int32_t rawToolMajor = 180;
6228 int32_t rawToolMinor = 160;
6229
6230 float x = toDisplayX(rawX);
6231 float y = toDisplayY(rawY);
6232 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6233 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6234 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6235 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6236 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6237
6238 processPosition(mapper, rawX, rawY);
6239 processTouchMajor(mapper, rawTouchMajor);
6240 processTouchMinor(mapper, rawTouchMinor);
6241 processToolMajor(mapper, rawToolMajor);
6242 processToolMinor(mapper, rawToolMinor);
6243 processMTSync(mapper);
6244 processSync(mapper);
6245
6246 NotifyMotionArgs args;
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6249 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6250}
6251
6252TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 addConfigurationProperty("touch.deviceType", "touchScreen");
6254 prepareDisplay(DISPLAY_ORIENTATION_0);
6255 prepareAxes(POSITION | TOUCH | TOOL);
6256 addConfigurationProperty("touch.size.calibration", "diameter");
6257 addConfigurationProperty("touch.size.scale", "10");
6258 addConfigurationProperty("touch.size.bias", "160");
6259 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006260 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261
6262 // These calculations are based on the input device calibration documentation.
6263 // Note: We only provide a single common touch/tool value because the device is assumed
6264 // not to emit separate values for each pointer (isSummed = 1).
6265 int32_t rawX = 100;
6266 int32_t rawY = 200;
6267 int32_t rawX2 = 150;
6268 int32_t rawY2 = 250;
6269 int32_t rawTouchMajor = 5;
6270 int32_t rawToolMajor = 8;
6271
6272 float x = toDisplayX(rawX);
6273 float y = toDisplayY(rawY);
6274 float x2 = toDisplayX(rawX2);
6275 float y2 = toDisplayY(rawY2);
6276 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6277 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6278 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6279
6280 processPosition(mapper, rawX, rawY);
6281 processTouchMajor(mapper, rawTouchMajor);
6282 processToolMajor(mapper, rawToolMajor);
6283 processMTSync(mapper);
6284 processPosition(mapper, rawX2, rawY2);
6285 processTouchMajor(mapper, rawTouchMajor);
6286 processToolMajor(mapper, rawToolMajor);
6287 processMTSync(mapper);
6288 processSync(mapper);
6289
6290 NotifyMotionArgs args;
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6293
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6296 args.action);
6297 ASSERT_EQ(size_t(2), args.pointerCount);
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6299 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6301 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6302}
6303
6304TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 addConfigurationProperty("touch.deviceType", "touchScreen");
6306 prepareDisplay(DISPLAY_ORIENTATION_0);
6307 prepareAxes(POSITION | TOUCH | TOOL);
6308 addConfigurationProperty("touch.size.calibration", "area");
6309 addConfigurationProperty("touch.size.scale", "43");
6310 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312
6313 // These calculations are based on the input device calibration documentation.
6314 int32_t rawX = 100;
6315 int32_t rawY = 200;
6316 int32_t rawTouchMajor = 5;
6317 int32_t rawToolMajor = 8;
6318
6319 float x = toDisplayX(rawX);
6320 float y = toDisplayY(rawY);
6321 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6322 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6323 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6324
6325 processPosition(mapper, rawX, rawY);
6326 processTouchMajor(mapper, rawTouchMajor);
6327 processToolMajor(mapper, rawToolMajor);
6328 processMTSync(mapper);
6329 processSync(mapper);
6330
6331 NotifyMotionArgs args;
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6334 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6335}
6336
6337TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 addConfigurationProperty("touch.deviceType", "touchScreen");
6339 prepareDisplay(DISPLAY_ORIENTATION_0);
6340 prepareAxes(POSITION | PRESSURE);
6341 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6342 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006343 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344
Michael Wrightaa449c92017-12-13 21:21:43 +00006345 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006346 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006347 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6348 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6349 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6350
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351 // These calculations are based on the input device calibration documentation.
6352 int32_t rawX = 100;
6353 int32_t rawY = 200;
6354 int32_t rawPressure = 60;
6355
6356 float x = toDisplayX(rawX);
6357 float y = toDisplayY(rawY);
6358 float pressure = float(rawPressure) * 0.01f;
6359
6360 processPosition(mapper, rawX, rawY);
6361 processPressure(mapper, rawPressure);
6362 processMTSync(mapper);
6363 processSync(mapper);
6364
6365 NotifyMotionArgs args;
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6368 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6369}
6370
6371TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 addConfigurationProperty("touch.deviceType", "touchScreen");
6373 prepareDisplay(DISPLAY_ORIENTATION_0);
6374 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006375 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376
6377 NotifyMotionArgs motionArgs;
6378 NotifyKeyArgs keyArgs;
6379
6380 processId(mapper, 1);
6381 processPosition(mapper, 100, 200);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6385 ASSERT_EQ(0, motionArgs.buttonState);
6386
6387 // press BTN_LEFT, release BTN_LEFT
6388 processKey(mapper, BTN_LEFT, 1);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6392 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6393
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6396 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6397
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398 processKey(mapper, BTN_LEFT, 0);
6399 processSync(mapper);
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407
6408 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6409 processKey(mapper, BTN_RIGHT, 1);
6410 processKey(mapper, BTN_MIDDLE, 1);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6414 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6415 motionArgs.buttonState);
6416
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6419 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6423 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6424 motionArgs.buttonState);
6425
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 processKey(mapper, BTN_RIGHT, 0);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006431
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006434 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435
6436 processKey(mapper, BTN_MIDDLE, 0);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006441
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445
6446 // press BTN_BACK, release BTN_BACK
6447 processKey(mapper, BTN_BACK, 1);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6450 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6451 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006452
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006455 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6459 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 processKey(mapper, BTN_BACK, 0);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006466
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006469 ASSERT_EQ(0, motionArgs.buttonState);
6470
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6472 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6473 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6474
6475 // press BTN_SIDE, release BTN_SIDE
6476 processKey(mapper, BTN_SIDE, 1);
6477 processSync(mapper);
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6479 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6480 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006481
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6485
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6488 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006489
6490 processKey(mapper, BTN_SIDE, 0);
6491 processSync(mapper);
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006494 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006495
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006498 ASSERT_EQ(0, motionArgs.buttonState);
6499
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6501 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6502 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6503
6504 // press BTN_FORWARD, release BTN_FORWARD
6505 processKey(mapper, BTN_FORWARD, 1);
6506 processSync(mapper);
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6508 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6509 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006510
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006513 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6514
6515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6517 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518
6519 processKey(mapper, BTN_FORWARD, 0);
6520 processSync(mapper);
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006522 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006523 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006524
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006527 ASSERT_EQ(0, motionArgs.buttonState);
6528
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6530 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6531 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6532
6533 // press BTN_EXTRA, release BTN_EXTRA
6534 processKey(mapper, BTN_EXTRA, 1);
6535 processSync(mapper);
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6537 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6538 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006539
Michael Wrightd02c5b62014-02-10 15:10:22 -08006540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006542 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6543
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6546 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547
6548 processKey(mapper, BTN_EXTRA, 0);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006551 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006553
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006556 ASSERT_EQ(0, motionArgs.buttonState);
6557
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6559 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6560 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6561
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6563
Michael Wrightd02c5b62014-02-10 15:10:22 -08006564 // press BTN_STYLUS, release BTN_STYLUS
6565 processKey(mapper, BTN_STYLUS, 1);
6566 processSync(mapper);
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006569 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6570
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6573 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006574
6575 processKey(mapper, BTN_STYLUS, 0);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006583 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584
6585 // press BTN_STYLUS2, release BTN_STYLUS2
6586 processKey(mapper, BTN_STYLUS2, 1);
6587 processSync(mapper);
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006590 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6591
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6594 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006595
6596 processKey(mapper, BTN_STYLUS2, 0);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006600 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006604 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006605
6606 // release touch
6607 processId(mapper, -1);
6608 processSync(mapper);
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6611 ASSERT_EQ(0, motionArgs.buttonState);
6612}
6613
6614TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006615 addConfigurationProperty("touch.deviceType", "touchScreen");
6616 prepareDisplay(DISPLAY_ORIENTATION_0);
6617 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006618 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006619
6620 NotifyMotionArgs motionArgs;
6621
6622 // default tool type is finger
6623 processId(mapper, 1);
6624 processPosition(mapper, 100, 200);
6625 processSync(mapper);
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6629
6630 // eraser
6631 processKey(mapper, BTN_TOOL_RUBBER, 1);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6636
6637 // stylus
6638 processKey(mapper, BTN_TOOL_RUBBER, 0);
6639 processKey(mapper, BTN_TOOL_PEN, 1);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6644
6645 // brush
6646 processKey(mapper, BTN_TOOL_PEN, 0);
6647 processKey(mapper, BTN_TOOL_BRUSH, 1);
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6652
6653 // pencil
6654 processKey(mapper, BTN_TOOL_BRUSH, 0);
6655 processKey(mapper, BTN_TOOL_PENCIL, 1);
6656 processSync(mapper);
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6660
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006661 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006662 processKey(mapper, BTN_TOOL_PENCIL, 0);
6663 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6664 processSync(mapper);
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6668
6669 // mouse
6670 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6671 processKey(mapper, BTN_TOOL_MOUSE, 1);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6676
6677 // lens
6678 processKey(mapper, BTN_TOOL_MOUSE, 0);
6679 processKey(mapper, BTN_TOOL_LENS, 1);
6680 processSync(mapper);
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6683 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6684
6685 // double-tap
6686 processKey(mapper, BTN_TOOL_LENS, 0);
6687 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6688 processSync(mapper);
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6692
6693 // triple-tap
6694 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6695 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6696 processSync(mapper);
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6700
6701 // quad-tap
6702 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6703 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6704 processSync(mapper);
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6707 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6708
6709 // finger
6710 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6711 processKey(mapper, BTN_TOOL_FINGER, 1);
6712 processSync(mapper);
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6716
6717 // stylus trumps finger
6718 processKey(mapper, BTN_TOOL_PEN, 1);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6723
6724 // eraser trumps stylus
6725 processKey(mapper, BTN_TOOL_RUBBER, 1);
6726 processSync(mapper);
6727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6730
6731 // mouse trumps eraser
6732 processKey(mapper, BTN_TOOL_MOUSE, 1);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6737
6738 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6739 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6740 processSync(mapper);
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6744
6745 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6746 processToolType(mapper, MT_TOOL_PEN);
6747 processSync(mapper);
6748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6751
6752 // back to default tool type
6753 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6754 processKey(mapper, BTN_TOOL_MOUSE, 0);
6755 processKey(mapper, BTN_TOOL_RUBBER, 0);
6756 processKey(mapper, BTN_TOOL_PEN, 0);
6757 processKey(mapper, BTN_TOOL_FINGER, 0);
6758 processSync(mapper);
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6762}
6763
6764TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006765 addConfigurationProperty("touch.deviceType", "touchScreen");
6766 prepareDisplay(DISPLAY_ORIENTATION_0);
6767 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006768 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006769 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006770
6771 NotifyMotionArgs motionArgs;
6772
6773 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6774 processId(mapper, 1);
6775 processPosition(mapper, 100, 200);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6780 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6781
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6785 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6786
6787 // move a little
6788 processPosition(mapper, 150, 250);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6791 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6793 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6794
6795 // down when BTN_TOUCH is pressed, pressure defaults to 1
6796 processKey(mapper, BTN_TOUCH, 1);
6797 processSync(mapper);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6799 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6801 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6802
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6806 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6807
6808 // up when BTN_TOUCH is released, hover restored
6809 processKey(mapper, BTN_TOUCH, 0);
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6812 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6814 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6815
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6817 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6819 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6820
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6824 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6825
6826 // exit hover when pointer goes away
6827 processId(mapper, -1);
6828 processSync(mapper);
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6832 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6833}
6834
6835TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836 addConfigurationProperty("touch.deviceType", "touchScreen");
6837 prepareDisplay(DISPLAY_ORIENTATION_0);
6838 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006840
6841 NotifyMotionArgs motionArgs;
6842
6843 // initially hovering because pressure is 0
6844 processId(mapper, 1);
6845 processPosition(mapper, 100, 200);
6846 processPressure(mapper, 0);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6851 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6852
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6854 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6856 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6857
6858 // move a little
6859 processPosition(mapper, 150, 250);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6864 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6865
6866 // down when pressure becomes non-zero
6867 processPressure(mapper, RAW_PRESSURE_MAX);
6868 processSync(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6872 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6873
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6875 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6877 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6878
6879 // up when pressure becomes 0, hover restored
6880 processPressure(mapper, 0);
6881 processSync(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6885 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6886
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6888 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6890 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6891
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6893 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6895 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6896
6897 // exit hover when pointer goes away
6898 processId(mapper, -1);
6899 processSync(mapper);
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6903 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6904}
6905
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006906/**
6907 * Set the input device port <--> display port associations, and check that the
6908 * events are routed to the display that matches the display port.
6909 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6910 */
6911TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006912 const std::string usb2 = "USB2";
6913 const uint8_t hdmi1 = 0;
6914 const uint8_t hdmi2 = 1;
6915 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006916 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006917
6918 addConfigurationProperty("touch.deviceType", "touchScreen");
6919 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006921
6922 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6923 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6924
6925 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6926 // for this input device is specified, and the matching viewport is not present,
6927 // the input device should be disabled (at the mapper level).
6928
6929 // Add viewport for display 2 on hdmi2
6930 prepareSecondaryDisplay(type, hdmi2);
6931 // Send a touch event
6932 processPosition(mapper, 100, 100);
6933 processSync(mapper);
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6935
6936 // Add viewport for display 1 on hdmi1
6937 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6938 // Send a touch event again
6939 processPosition(mapper, 100, 100);
6940 processSync(mapper);
6941
6942 NotifyMotionArgs args;
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6944 ASSERT_EQ(DISPLAY_ID, args.displayId);
6945}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006946
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006947TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006948 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006949 std::shared_ptr<FakePointerController> fakePointerController =
6950 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006951 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006952 fakePointerController->setPosition(100, 200);
6953 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006954 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6955
Garfield Tan888a6a42020-01-09 11:39:16 -08006956 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006957 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006958
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006959 prepareDisplay(DISPLAY_ORIENTATION_0);
6960 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006961 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006962
6963 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006964 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006965
6966 NotifyMotionArgs motionArgs;
6967 processPosition(mapper, 100, 100);
6968 processSync(mapper);
6969
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6972 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6973}
6974
Arthur Hung7c645402019-01-25 17:45:42 +08006975TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6976 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006977 prepareAxes(POSITION | ID | SLOT);
6978 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006979 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006980
6981 // Create the second touch screen device, and enable multi fingers.
6982 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08006983 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006984 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006985 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08006986 std::shared_ptr<InputDevice> device2 =
6987 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
6988 Flags<InputDeviceClass>(0));
6989
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006990 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6991 0 /*flat*/, 0 /*fuzz*/);
6992 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6993 0 /*flat*/, 0 /*fuzz*/);
6994 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6995 0 /*flat*/, 0 /*fuzz*/);
6996 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6997 0 /*flat*/, 0 /*fuzz*/);
6998 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6999 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7000 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007001
7002 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007003 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007004 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7005 device2->reset(ARBITRARY_TIME);
7006
7007 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007008 std::shared_ptr<FakePointerController> fakePointerController =
7009 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007010 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7011 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7012
7013 // Setup policy for associated displays and show touches.
7014 const uint8_t hdmi1 = 0;
7015 const uint8_t hdmi2 = 1;
7016 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7017 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7018 mFakePolicy->setShowTouches(true);
7019
7020 // Create displays.
7021 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007022 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007023
7024 // Default device will reconfigure above, need additional reconfiguration for another device.
7025 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007026 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007027
7028 // Two fingers down at default display.
7029 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7030 processPosition(mapper, x1, y1);
7031 processId(mapper, 1);
7032 processSlot(mapper, 1);
7033 processPosition(mapper, x2, y2);
7034 processId(mapper, 2);
7035 processSync(mapper);
7036
7037 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7038 fakePointerController->getSpots().find(DISPLAY_ID);
7039 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7040 ASSERT_EQ(size_t(2), iter->second.size());
7041
7042 // Two fingers down at second display.
7043 processPosition(mapper2, x1, y1);
7044 processId(mapper2, 1);
7045 processSlot(mapper2, 1);
7046 processPosition(mapper2, x2, y2);
7047 processId(mapper2, 2);
7048 processSync(mapper2);
7049
7050 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7051 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7052 ASSERT_EQ(size_t(2), iter->second.size());
7053}
7054
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007055TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007056 prepareAxes(POSITION);
7057 addConfigurationProperty("touch.deviceType", "touchScreen");
7058 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007059 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007060
7061 NotifyMotionArgs motionArgs;
7062 // Unrotated video frame
7063 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7064 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007065 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007066 processPosition(mapper, 100, 200);
7067 processSync(mapper);
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7069 ASSERT_EQ(frames, motionArgs.videoFrames);
7070
7071 // Subsequent touch events should not have any videoframes
7072 // This is implemented separately in FakeEventHub,
7073 // but that should match the behaviour of TouchVideoDevice.
7074 processPosition(mapper, 200, 200);
7075 processSync(mapper);
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7077 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7078}
7079
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007080TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007081 prepareAxes(POSITION);
7082 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007083 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007084 // Unrotated video frame
7085 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7086 NotifyMotionArgs motionArgs;
7087
7088 // Test all 4 orientations
7089 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7090 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7091 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7092 clearViewports();
7093 prepareDisplay(orientation);
7094 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007095 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007096 processPosition(mapper, 100, 200);
7097 processSync(mapper);
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7099 frames[0].rotate(orientation);
7100 ASSERT_EQ(frames, motionArgs.videoFrames);
7101 }
7102}
7103
7104TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007105 prepareAxes(POSITION);
7106 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007107 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007108 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7109 // so mix these.
7110 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7111 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7112 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7113 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7114 NotifyMotionArgs motionArgs;
7115
7116 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007117 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007118 processPosition(mapper, 100, 200);
7119 processSync(mapper);
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7121 std::for_each(frames.begin(), frames.end(),
7122 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7123 ASSERT_EQ(frames, motionArgs.videoFrames);
7124}
7125
Arthur Hung9da14732019-09-02 16:16:58 +08007126/**
7127 * If we had defined port associations, but the viewport is not ready, the touch device would be
7128 * expected to be disabled, and it should be enabled after the viewport has found.
7129 */
7130TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007131 constexpr uint8_t hdmi2 = 1;
7132 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007133 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007134
7135 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7136
7137 addConfigurationProperty("touch.deviceType", "touchScreen");
7138 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007139 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007140
7141 ASSERT_EQ(mDevice->isEnabled(), false);
7142
7143 // Add display on hdmi2, the device should be enabled and can receive touch event.
7144 prepareSecondaryDisplay(type, hdmi2);
7145 ASSERT_EQ(mDevice->isEnabled(), true);
7146
7147 // Send a touch event.
7148 processPosition(mapper, 100, 100);
7149 processSync(mapper);
7150
7151 NotifyMotionArgs args;
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7153 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7154}
7155
Arthur Hung6cd19a42019-08-30 19:04:12 +08007156
Arthur Hung6cd19a42019-08-30 19:04:12 +08007157
Arthur Hung421eb1c2020-01-16 00:09:42 +08007158TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007159 addConfigurationProperty("touch.deviceType", "touchScreen");
7160 prepareDisplay(DISPLAY_ORIENTATION_0);
7161 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007162 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007163
7164 NotifyMotionArgs motionArgs;
7165
7166 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7167 // finger down
7168 processId(mapper, 1);
7169 processPosition(mapper, x1, y1);
7170 processSync(mapper);
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7172 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7174
7175 // finger move
7176 processId(mapper, 1);
7177 processPosition(mapper, x2, y2);
7178 processSync(mapper);
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7182
7183 // finger up.
7184 processId(mapper, -1);
7185 processSync(mapper);
7186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7187 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7189
7190 // new finger down
7191 processId(mapper, 1);
7192 processPosition(mapper, x3, y3);
7193 processSync(mapper);
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7195 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7197}
7198
7199/**
arthurhungcc7f9802020-04-30 17:55:40 +08007200 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7201 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007202 */
arthurhungcc7f9802020-04-30 17:55:40 +08007203TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007204 addConfigurationProperty("touch.deviceType", "touchScreen");
7205 prepareDisplay(DISPLAY_ORIENTATION_0);
7206 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007208
7209 NotifyMotionArgs motionArgs;
7210
7211 // default tool type is finger
7212 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007213 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007214 processPosition(mapper, x1, y1);
7215 processSync(mapper);
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7219
7220 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7221 processToolType(mapper, MT_TOOL_PALM);
7222 processSync(mapper);
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7225
7226 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007227 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007228 processPosition(mapper, x2, y2);
7229 processSync(mapper);
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7231
7232 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007233 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007234 processSync(mapper);
7235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7236
7237 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007238 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007239 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007240 processPosition(mapper, x3, y3);
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7245}
7246
arthurhungbf89a482020-04-17 17:37:55 +08007247/**
arthurhungcc7f9802020-04-30 17:55:40 +08007248 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7249 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007250 */
arthurhungcc7f9802020-04-30 17:55:40 +08007251TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007252 addConfigurationProperty("touch.deviceType", "touchScreen");
7253 prepareDisplay(DISPLAY_ORIENTATION_0);
7254 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7255 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7256
7257 NotifyMotionArgs motionArgs;
7258
7259 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007260 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7261 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007262 processPosition(mapper, x1, y1);
7263 processSync(mapper);
7264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7267
7268 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007269 processSlot(mapper, SECOND_SLOT);
7270 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007271 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007272 processSync(mapper);
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7274 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7275 motionArgs.action);
7276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7277
7278 // If the tool type of the first finger changes to MT_TOOL_PALM,
7279 // we expect to receive ACTION_POINTER_UP with cancel flag.
7280 processSlot(mapper, FIRST_SLOT);
7281 processId(mapper, FIRST_TRACKING_ID);
7282 processToolType(mapper, MT_TOOL_PALM);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7286 motionArgs.action);
7287 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7288
7289 // The following MOVE events of second finger should be processed.
7290 processSlot(mapper, SECOND_SLOT);
7291 processId(mapper, SECOND_TRACKING_ID);
7292 processPosition(mapper, x2 + 1, y2 + 1);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7296 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7297
7298 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7299 // it. Second finger receive move.
7300 processSlot(mapper, FIRST_SLOT);
7301 processId(mapper, INVALID_TRACKING_ID);
7302 processSync(mapper);
7303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7305 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7306
7307 // Second finger keeps moving.
7308 processSlot(mapper, SECOND_SLOT);
7309 processId(mapper, SECOND_TRACKING_ID);
7310 processPosition(mapper, x2 + 2, y2 + 2);
7311 processSync(mapper);
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7314 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7315
7316 // Second finger up.
7317 processId(mapper, INVALID_TRACKING_ID);
7318 processSync(mapper);
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7320 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7321 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7322}
7323
7324/**
7325 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7326 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7327 */
7328TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7329 addConfigurationProperty("touch.deviceType", "touchScreen");
7330 prepareDisplay(DISPLAY_ORIENTATION_0);
7331 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7332 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7333
7334 NotifyMotionArgs motionArgs;
7335
7336 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7337 // First finger down.
7338 processId(mapper, FIRST_TRACKING_ID);
7339 processPosition(mapper, x1, y1);
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7344
7345 // Second finger down.
7346 processSlot(mapper, SECOND_SLOT);
7347 processId(mapper, SECOND_TRACKING_ID);
7348 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007349 processSync(mapper);
7350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7351 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7352 motionArgs.action);
7353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7354
arthurhungcc7f9802020-04-30 17:55:40 +08007355 // If the tool type of the first finger changes to MT_TOOL_PALM,
7356 // we expect to receive ACTION_POINTER_UP with cancel flag.
7357 processSlot(mapper, FIRST_SLOT);
7358 processId(mapper, FIRST_TRACKING_ID);
7359 processToolType(mapper, MT_TOOL_PALM);
7360 processSync(mapper);
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7362 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7363 motionArgs.action);
7364 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7365
7366 // Second finger keeps moving.
7367 processSlot(mapper, SECOND_SLOT);
7368 processId(mapper, SECOND_TRACKING_ID);
7369 processPosition(mapper, x2 + 1, y2 + 1);
7370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7373
7374 // second finger becomes palm, receive cancel due to only 1 finger is active.
7375 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007376 processToolType(mapper, MT_TOOL_PALM);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7380
arthurhungcc7f9802020-04-30 17:55:40 +08007381 // third finger down.
7382 processSlot(mapper, THIRD_SLOT);
7383 processId(mapper, THIRD_TRACKING_ID);
7384 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007385 processPosition(mapper, x3, y3);
7386 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7388 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007390 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7391
7392 // third finger move
7393 processId(mapper, THIRD_TRACKING_ID);
7394 processPosition(mapper, x3 + 1, y3 + 1);
7395 processSync(mapper);
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7398
7399 // first finger up, third finger receive move.
7400 processSlot(mapper, FIRST_SLOT);
7401 processId(mapper, INVALID_TRACKING_ID);
7402 processSync(mapper);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7405 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7406
7407 // second finger up, third finger receive move.
7408 processSlot(mapper, SECOND_SLOT);
7409 processId(mapper, INVALID_TRACKING_ID);
7410 processSync(mapper);
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7413 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7414
7415 // third finger up.
7416 processSlot(mapper, THIRD_SLOT);
7417 processId(mapper, INVALID_TRACKING_ID);
7418 processSync(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7421 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7422}
7423
7424/**
7425 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7426 * and the active finger could still be allowed to receive the events
7427 */
7428TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7429 addConfigurationProperty("touch.deviceType", "touchScreen");
7430 prepareDisplay(DISPLAY_ORIENTATION_0);
7431 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7432 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7433
7434 NotifyMotionArgs motionArgs;
7435
7436 // default tool type is finger
7437 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7438 processId(mapper, FIRST_TRACKING_ID);
7439 processPosition(mapper, x1, y1);
7440 processSync(mapper);
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7444
7445 // Second finger down.
7446 processSlot(mapper, SECOND_SLOT);
7447 processId(mapper, SECOND_TRACKING_ID);
7448 processPosition(mapper, x2, y2);
7449 processSync(mapper);
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7452 motionArgs.action);
7453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7454
7455 // If the tool type of the second finger changes to MT_TOOL_PALM,
7456 // we expect to receive ACTION_POINTER_UP with cancel flag.
7457 processId(mapper, SECOND_TRACKING_ID);
7458 processToolType(mapper, MT_TOOL_PALM);
7459 processSync(mapper);
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7461 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7462 motionArgs.action);
7463 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7464
7465 // The following MOVE event should be processed.
7466 processSlot(mapper, FIRST_SLOT);
7467 processId(mapper, FIRST_TRACKING_ID);
7468 processPosition(mapper, x1 + 1, y1 + 1);
7469 processSync(mapper);
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7472 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7473
7474 // second finger up.
7475 processSlot(mapper, SECOND_SLOT);
7476 processId(mapper, INVALID_TRACKING_ID);
7477 processSync(mapper);
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7480
7481 // first finger keep moving
7482 processSlot(mapper, FIRST_SLOT);
7483 processId(mapper, FIRST_TRACKING_ID);
7484 processPosition(mapper, x1 + 2, y1 + 2);
7485 processSync(mapper);
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7488
7489 // first finger up.
7490 processId(mapper, INVALID_TRACKING_ID);
7491 processSync(mapper);
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7494 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007495}
7496
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007497// --- MultiTouchInputMapperTest_ExternalDevice ---
7498
7499class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7500protected:
Chris Yea52ade12020-08-27 16:49:20 -07007501 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007502};
7503
7504/**
7505 * Expect fallback to internal viewport if device is external and external viewport is not present.
7506 */
7507TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7508 prepareAxes(POSITION);
7509 addConfigurationProperty("touch.deviceType", "touchScreen");
7510 prepareDisplay(DISPLAY_ORIENTATION_0);
7511 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7512
7513 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7514
7515 NotifyMotionArgs motionArgs;
7516
7517 // Expect the event to be sent to the internal viewport,
7518 // because an external viewport is not present.
7519 processPosition(mapper, 100, 100);
7520 processSync(mapper);
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7523
7524 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007525 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007526 processPosition(mapper, 100, 100);
7527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7529 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7530}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007531
7532/**
7533 * Test touch should not work if outside of surface.
7534 */
7535class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7536protected:
7537 void halfDisplayToCenterHorizontal(int32_t orientation) {
7538 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007539 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007540
7541 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7542 internalViewport->orientation = orientation;
7543 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7544 internalViewport->logicalLeft = 0;
7545 internalViewport->logicalTop = 0;
7546 internalViewport->logicalRight = DISPLAY_HEIGHT;
7547 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7548
7549 internalViewport->physicalLeft = 0;
7550 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7551 internalViewport->physicalRight = DISPLAY_HEIGHT;
7552 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7553
7554 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7555 internalViewport->deviceHeight = DISPLAY_WIDTH;
7556 } else {
7557 internalViewport->logicalLeft = 0;
7558 internalViewport->logicalTop = 0;
7559 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7560 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7561
7562 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7563 internalViewport->physicalTop = 0;
7564 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7565 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7566
7567 internalViewport->deviceWidth = DISPLAY_WIDTH;
7568 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7569 }
7570
7571 mFakePolicy->updateViewport(internalViewport.value());
7572 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7573 }
7574
7575 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7576 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7577 int32_t yExpected) {
7578 // touch on outside area should not work.
7579 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7580 processSync(mapper);
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7582
7583 // touch on inside area should receive the event.
7584 NotifyMotionArgs args;
7585 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7586 processSync(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7588 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7589 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7590
7591 // Reset.
7592 mapper.reset(ARBITRARY_TIME);
7593 }
7594};
7595
7596TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7597 addConfigurationProperty("touch.deviceType", "touchScreen");
7598 prepareDisplay(DISPLAY_ORIENTATION_0);
7599 prepareAxes(POSITION);
7600 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7601
7602 // Touch on center of normal display should work.
7603 const int32_t x = DISPLAY_WIDTH / 4;
7604 const int32_t y = DISPLAY_HEIGHT / 2;
7605 processPosition(mapper, toRawX(x), toRawY(y));
7606 processSync(mapper);
7607 NotifyMotionArgs args;
7608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7610 0.0f, 0.0f, 0.0f, 0.0f));
7611 // Reset.
7612 mapper.reset(ARBITRARY_TIME);
7613
7614 // Let physical display be different to device, and make surface and physical could be 1:1.
7615 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7616
7617 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7618 const int32_t yExpected = y;
7619 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7620}
7621
7622TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7623 addConfigurationProperty("touch.deviceType", "touchScreen");
7624 prepareDisplay(DISPLAY_ORIENTATION_0);
7625 prepareAxes(POSITION);
7626 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7627
7628 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7629 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7630
7631 const int32_t x = DISPLAY_WIDTH / 4;
7632 const int32_t y = DISPLAY_HEIGHT / 2;
7633
7634 // expect x/y = swap x/y then reverse y.
7635 const int32_t xExpected = y;
7636 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7637 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7638}
7639
7640TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7641 addConfigurationProperty("touch.deviceType", "touchScreen");
7642 prepareDisplay(DISPLAY_ORIENTATION_0);
7643 prepareAxes(POSITION);
7644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7645
7646 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7647 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7648
7649 const int32_t x = DISPLAY_WIDTH / 4;
7650 const int32_t y = DISPLAY_HEIGHT / 2;
7651
7652 // expect x/y = swap x/y then reverse x.
7653 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7654 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7655 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7656}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007657
7658TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7659 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7660 std::shared_ptr<FakePointerController> fakePointerController =
7661 std::make_shared<FakePointerController>();
7662 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7663 fakePointerController->setPosition(0, 0);
7664 fakePointerController->setButtonState(0);
7665
7666 // prepare device and capture
7667 prepareDisplay(DISPLAY_ORIENTATION_0);
7668 prepareAxes(POSITION | ID | SLOT);
7669 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7670 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7671 mFakePolicy->setPointerCapture(true);
7672 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7674
7675 // captured touchpad should be a touchpad source
7676 NotifyDeviceResetArgs resetArgs;
7677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7678 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7679
Chris Yef74dc422020-09-02 22:41:50 -07007680 InputDeviceInfo deviceInfo;
7681 mDevice->getDeviceInfo(&deviceInfo);
7682
7683 const InputDeviceInfo::MotionRange* relRangeX =
7684 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
7685 ASSERT_NE(relRangeX, nullptr);
7686 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
7687 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
7688 const InputDeviceInfo::MotionRange* relRangeY =
7689 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
7690 ASSERT_NE(relRangeY, nullptr);
7691 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
7692 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
7693
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007694 // run captured pointer tests - note that this is unscaled, so input listener events should be
7695 // identical to what the hardware sends (accounting for any
7696 // calibration).
7697 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007698 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007699 processId(mapper, 1);
7700 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7701 processKey(mapper, BTN_TOUCH, 1);
7702 processSync(mapper);
7703
7704 // expect coord[0] to contain initial location of touch 0
7705 NotifyMotionArgs args;
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7708 ASSERT_EQ(1U, args.pointerCount);
7709 ASSERT_EQ(0, args.pointerProperties[0].id);
7710 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7711 ASSERT_NO_FATAL_FAILURE(
7712 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7713
7714 // FINGER 1 DOWN
7715 processSlot(mapper, 1);
7716 processId(mapper, 2);
7717 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7718 processSync(mapper);
7719
7720 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007722 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7723 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007724 ASSERT_EQ(2U, args.pointerCount);
7725 ASSERT_EQ(0, args.pointerProperties[0].id);
7726 ASSERT_EQ(1, args.pointerProperties[1].id);
7727 ASSERT_NO_FATAL_FAILURE(
7728 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7729 ASSERT_NO_FATAL_FAILURE(
7730 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7731
7732 // FINGER 1 MOVE
7733 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7734 processSync(mapper);
7735
7736 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7737 // from move
7738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7740 ASSERT_NO_FATAL_FAILURE(
7741 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7742 ASSERT_NO_FATAL_FAILURE(
7743 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7744
7745 // FINGER 0 MOVE
7746 processSlot(mapper, 0);
7747 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7748 processSync(mapper);
7749
7750 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7753 ASSERT_NO_FATAL_FAILURE(
7754 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7755 ASSERT_NO_FATAL_FAILURE(
7756 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7757
7758 // BUTTON DOWN
7759 processKey(mapper, BTN_LEFT, 1);
7760 processSync(mapper);
7761
7762 // touchinputmapper design sends a move before button press
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7766 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7767
7768 // BUTTON UP
7769 processKey(mapper, BTN_LEFT, 0);
7770 processSync(mapper);
7771
7772 // touchinputmapper design sends a move after button release
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7774 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7777
7778 // FINGER 0 UP
7779 processId(mapper, -1);
7780 processSync(mapper);
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7782 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7783
7784 // FINGER 1 MOVE
7785 processSlot(mapper, 1);
7786 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7787 processSync(mapper);
7788
7789 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7792 ASSERT_EQ(1U, args.pointerCount);
7793 ASSERT_EQ(1, args.pointerProperties[0].id);
7794 ASSERT_NO_FATAL_FAILURE(
7795 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7796
7797 // FINGER 1 UP
7798 processId(mapper, -1);
7799 processKey(mapper, BTN_TOUCH, 0);
7800 processSync(mapper);
7801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7802 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7803
7804 // non captured touchpad should be a mouse source
7805 mFakePolicy->setPointerCapture(false);
7806 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7808 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7809}
7810
7811TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7812 std::shared_ptr<FakePointerController> fakePointerController =
7813 std::make_shared<FakePointerController>();
7814 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7815 fakePointerController->setPosition(0, 0);
7816 fakePointerController->setButtonState(0);
7817
7818 // prepare device and capture
7819 prepareDisplay(DISPLAY_ORIENTATION_0);
7820 prepareAxes(POSITION | ID | SLOT);
7821 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7822 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7823 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7824 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7825 // run uncaptured pointer tests - pushes out generic events
7826 // FINGER 0 DOWN
7827 processId(mapper, 3);
7828 processPosition(mapper, 100, 100);
7829 processKey(mapper, BTN_TOUCH, 1);
7830 processSync(mapper);
7831
7832 // start at (100,100), cursor should be at (0,0) * scale
7833 NotifyMotionArgs args;
7834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7836 ASSERT_NO_FATAL_FAILURE(
7837 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7838
7839 // FINGER 0 MOVE
7840 processPosition(mapper, 200, 200);
7841 processSync(mapper);
7842
7843 // compute scaling to help with touch position checking
7844 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7845 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7846 float scale =
7847 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7848
7849 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7851 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7853 0, 0, 0, 0, 0, 0, 0));
7854}
7855
7856TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7857 std::shared_ptr<FakePointerController> fakePointerController =
7858 std::make_shared<FakePointerController>();
7859
7860 prepareDisplay(DISPLAY_ORIENTATION_0);
7861 prepareAxes(POSITION | ID | SLOT);
7862 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7863 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7864 mFakePolicy->setPointerCapture(false);
7865 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7866
7867 // uncaptured touchpad should be a pointer device
7868 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7869
7870 // captured touchpad should be a touchpad device
7871 mFakePolicy->setPointerCapture(true);
7872 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7873 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7874}
7875
Michael Wrightd02c5b62014-02-10 15:10:22 -08007876} // namespace android